blib/lib/HTML/HTML5/Parser/Charset/UnicodeChecker.pm untested

filter: Statementstmt 0.0% 0 / 85 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 149 CC 9 SCAR 23.4
CC
9
Cov
0%
new_handle
20.0
onerror
20.0
read
6.0
CRAP 10.4
1 package HTML::HTML5::Parser::Charset::UnicodeChecker;
2 ## skip Test::Tabs
3 use strict;
4 use warnings;
5  
6 our $VERSION = '0.992';
7  
8 ## NOTE: For more information (including rationals of checks performed
9 ## in this module), see
10 ## <http://suika.fam.cx/gate/2005/sw/Unicode%E7%AC%A6%E5%8F%B7%E5%8C%96%E6%96%87%E5%AD%97%E5%88%97%E3%81%AE%E9%81%A9%E5%90%88%E6%80%A7>.
11  
12 ## NOTE: Unicode's definition for character string conformance is
13 ## very, very vague so that it is difficult to determine what error
14 ## level is appropriate for each error. The Unicode Standard abuses
15 ## conformance-creteria-like terms such as "deprecated", "discouraged",
16 ## "preferred", "better", "not encouraged", "should", and so on with no
17 ## clear explanation of their difference (if any) or relationship to
18 ## the conformance. In fact, that specification does not define the
19 ## conformance class for character strings.
20  
21 sub new_handle ($$;$) {
22 my $self = bless {
23 queue => [],
24 new_queue => [],
25 onerror => sub {},
26 #onerror_set
27 level => {
28 unicode_should => 'w',
29 unicode_deprecated => 'w', # = unicode_should
30 unicode_discouraged => 'w',
31 unicode_preferred => 'w',
32 ## NOTE: We do some "unification" of levels - for example,
33 ## "not encouraged" is unified with "discouraged" and
34 ## "better" is unified with "preferred".
35  
36 must => 'm',
37 warn => 'w',
38 },
39 }, $_[0];
40 $self->{handle} = $_[1]; # char stream
41 my $mode = $_[2] || 'default'; # or 'html5'
42 $self->{level_map} = {
43 ## Unicode errors
44 'unicode deprecated' => 'unicode_deprecated',
45 'nonchar' => $mode eq 'html5' ? 'must' : 'unicode_should',
46 ## NOTE: HTML5 parse error.
47 'unicode should' => 'unicode_should',
48 'unicode discouraged' => 'unicode_discouraged',
49 'unicode not preferred' => 'unicode_preferred',
50  
51 ## HTML5 errors (See "text" definition of the spec)
52 'control char' => $mode eq 'html5' ? 'must' : 'warn',
53 ## NOTE: HTML5 parse error.
54 'non unicode' => $mode eq 'html5' ? 'must' : 'warn',
55 ## NOTE: In HTML5, replaced by U+FFFD (not a parse error).
56 };
57 $self->{replace_non_unicode} = ($mode eq 'html5');
58 return $self;
59 } # new_handle
60  
61 my $etypes = {
62 0x0340 => 'unicode deprecated',
63 0x0341 => 'unicode deprecated',
64 0x17A3 => 'unicode deprecated',
65 0x17D3 => 'unicode deprecated',
66 0x206A => 'unicode deprecated',
67 0x206B => 'unicode deprecated',
68 0x206C => 'unicode deprecated',
69 0x206D => 'unicode deprecated',
70 0x206E => 'unicode deprecated',
71 0x206F => 'unicode deprecated',
72 0xE0001 => 'unicode deprecated',
73
74 0xFFFE => 'nonchar',
75 0xFFFF => 'nonchar',
76 0x1FFFE => 'nonchar',
77 0x1FFFF => 'nonchar',
78 0x2FFFE => 'nonchar',
79 0x2FFFF => 'nonchar',
80 0x3FFFE => 'nonchar',
81 0x3FFFF => 'nonchar',
82 0x4FFFE => 'nonchar',
83 0x4FFFF => 'nonchar',
84 0x5FFFE => 'nonchar',
85 0x5FFFF => 'nonchar',
86 0x6FFFE => 'nonchar',
87 0x6FFFF => 'nonchar',
88 0x7FFFE => 'nonchar',
89 0x7FFFF => 'nonchar',
90 0x8FFFE => 'nonchar',
91 0x8FFFF => 'nonchar',
92 0x9FFFE => 'nonchar',
93 0x9FFFF => 'nonchar',
94 0xAFFFE => 'nonchar',
95 0xAFFFF => 'nonchar',
96 0xBFFFE => 'nonchar',
97 0xBFFFF => 'nonchar',
98 0xCFFFE => 'nonchar',
99 0xCFFFF => 'nonchar',
100 0xDFFFE => 'nonchar',
101 0xDFFFF => 'nonchar',
102 0xEFFFE => 'nonchar',
103 0xEFFFF => 'nonchar',
104 0xFFFFE => 'nonchar',
105 0xFFFFF => 'nonchar',
106 0x10FFFE => 'nonchar',
107 0x10FFFF => 'nonchar',
108  
109 0x0344 => 'unicode should', # COMBINING GREEK DIALYTIKA TONOS
110 0x03D3 => 'unicode should', # GREEK UPSILON WITH ...
111 0x03D4 => 'unicode should', # GREEK UPSILON WITH ...
112 0x20A4 => 'unicode should', # LIRA SIGN
113
114 0x2126 => 'unicode should', # OHM SIGN # also, discouraged
115 0x212A => 'unicode should', # KELVIN SIGN
116 0x212B => 'unicode should', # ANGSTROM SIGN
117
118 ## Styled overlines/underlines in CJK Compatibility Forms
119 0xFE49 => 'unicode discouraged',
120 0xFE4A => 'unicode discouraged',
121 0xFE4B => 'unicode discouraged',
122 0xFE4C => 'unicode discouraged',
123 0xFE4D => 'unicode discouraged',
124 0xFE4E => 'unicode discouraged',
125 0xFE4F => 'unicode discouraged',
126
127 0x037E => 'unicode discouraged', # greek punctuations
128 0x0387 => 'unicode discouraged', # greek punctuations
129
130 #0x17A3 => 'unicode discouraged', # also, deprecated
131 0x17A4 => 'unicode discouraged',
132 0x17B4 => 'unicode discouraged',
133 0x17B5 => 'unicode discouraged',
134 0x17D8 => 'unicode discouraged',
135  
136 0x2121 => 'unicode discouraged', # tel
137 0x213B => 'unicode discouraged', # fax
138 #0x2120 => 'unicode discouraged', # SM (superscript)
139 #0x2122 => 'unicode discouraged', # TM (superscript)
140  
141 ## inline annotations
142 0xFFF9 => 'unicode discouraged',
143 0xFFFA => 'unicode discouraged',
144 0xFFFB => 'unicode discouraged',
145  
146 ## greek punctuations
147 0x055A => 'unicode not preferred',
148 0x0559 => 'unicode not preferred',
149
150 ## degree signs
151 0x2103 => 'unicode not preferred',
152 0x2109 => 'unicode not preferred',
153
154 ## strongly preferrs U+2060 WORD JOINTER
155 0xFEFE => 'unicode not preferred',
156 };
157  
158 $etypes->{$_} = 'unicode deprecated' for 0xE0020 .. 0xE007F;
159 $etypes->{$_} = 'nonchar' for 0xFDD0 .. 0xFDEF;
160 ## ISSUE: U+FDE0-U+FDEF are not excluded in HTML5.
161 $etypes->{$_} = 'unicode should' for 0xFA30 .. 0xFA6A, 0xFA70 .. 0xFAD9;
162 $etypes->{$_} = 'unicode should' for 0x2F800 .. 0x2FA1D, 0x239B .. 0x23B3;
163 $etypes->{$_} = 'unicode should'
164 for 0xFB50 .. 0xFBB1, 0xFBD3 .. 0xFD3D, 0xFD50 .. 0xFD8F,
165 0xFD92 .. 0xFDC7, 0xFDF0 .. 0xFDFB, 0xFE70 .. 0xFE74,
166 0xFE76 .. 0xFEFC;
167 ## NOTE: Arabic Presentation Forms-A/B blocks, w/o code points where
168 ## no character is assigned, noncharacter code points, and
169 ## U+FD3E and U+FD3F, which are explicitly allowed.
170 $etypes->{$_} = 'unicode discouraged' for 0x2153 .. 0x217F;
171 $etypes->{$_} = 'control char'
172 for 0x0001 .. 0x0008, 0x000B, 0x000E .. 0x001F, 0x007F .. 0x009F;
173 #0x0000
174 #$etypes->{$_} = 'control char' for 0xD800 .. 0xDFFF;
175  
176 my $check_char = sub ($$) {
177 my ($self, $char_code) = @_;
178  
179 ## NOTE: Negative $char_code is not supported.
180  
181 if ($char_code == 0x000D) {
182 $self->{line_diff}++;
183 $self->{column_diff} = 0;
184 $self->{set_column} = 1;
185 $self->{has_cr} = 1;
186 return;
187 } elsif ($char_code == 0x000A) {
188 if ($self->{has_cr}) {
189 delete $self->{has_cr};
190 } else {
191 $self->{line_diff}++;
192 $self->{column_diff} = 0;
193 $self->{set_column} = 1;
194 }
195 return;
196 } else {
197 $self->{column_diff}++;
198 delete $self->{has_cr};
199 }
200  
201 if ($char_code > 0x10FFFF) {
202 $self->{onerror}->(type => 'non unicode',
203 text => (sprintf 'U-%08X', $char_code),
204 layer => 'charset',
205 level => $self->{level}->{$self->{level_map}->{'non unicode'}},
206 line_diff => $self->{line_diff},
207 column_diff => $self->{column_diff},
208 ($self->{set_column} ? (column => 1) : ()));
209 if ($self->{replace_non_unicode}) {
210 return "\x{FFFD}";
211 } else {
212 return;
213 }
214 }
215
216 my $etype = $etypes->{$char_code};
217 if (defined $etype) {
218 $self->{onerror}->(type => $etype,
219 text => (sprintf 'U+%04X', $char_code),
220 layer => 'charset',
221 level => $self->{level}->{$self->{level_map}->{$etype}},
222 line_diff => $self->{line_diff},
223 column_diff => $self->{column_diff},
224 ($self->{set_column} ? (column => 1) : ()));
225 }
226  
227 ## TODO: "khanda ta" should be represented by U+09CE
228 ## rather than <U+09A4, U+09CD, U+200D>.
229  
230 ## TODO: IDS syntax
231  
232 ## TODO: langtag syntax
233  
234 return;
235 }; # $check_char
236  
237 sub read ($$$;$) {
238 my $self = shift;
239 my $offset = $_[2] || 0;
240 my $count = $self->{handle}->read (@_);
241 $self->{line_diff} = 0;
242 $self->{column_diff} = -1;
243 delete $self->{set_column};
244 delete $self->{has_cr};
245 for ($offset .. ($offset + $count - 1)) {
246 my $c = $check_char->($self, ord substr $_[0], $_, 1);
247 if (defined $c) {
248 substr ($_[0], $_, 1) = $c;
249 }
250 }
251 return $count;
252 } # read
253  
254 sub manakai_read_until ($$$;$) {
255 #my ($self, $scalar, $pattern, $offset) = @_;
256 my $self = shift;
257 my $offset = $_[2] || 0;
258 my $count = $self->{handle}->manakai_read_until (@_);
259 $self->{line_diff} = 0;
260 $self->{column_diff} = -1;
261 delete $self->{set_column};
262 delete $self->{has_cr};
263 for ($offset .. ($offset + $count - 1)) {
264 my $c = $check_char->($self, ord substr $_[0], $_, 1);
265 if (defined $c) {
266 substr ($_[0], $_, 1) = $c;
267 }
268 }
269 return $count;
270 } # manakai_read_until
271  
272 sub ungetc ($$) {
273 unshift @{$_[0]->{queue}}, chr int ($_[1] or 0);
274 } # ungetc
275  
276 sub close ($) {
277 shift->{handle}->close;
278 } # close
279  
280 sub charset ($) {
281 shift->{handle}->charset;
282 } # charset
283  
284 sub has_bom ($) {
285 shift->{handle}->has_bom;
286 } # has_bom
287  
288 sub input_encoding ($) {
289 shift->{handle}->input_encoding;
290 } # input_encoding
291  
292 sub onerror ($;$) {
293 if (@_ > 1) {
294 if (defined $_[1]) {
295 $_[0]->{handle}->onerror ($_[0]->{onerror} = $_[1]);
296 $_[0]->{onerror_set} = 1;
297 } else {
298 $_[0]->{handle}->onerror ($_[0]->{onerror} = sub {});
299 delete $_[0]->{onerror_set};
300 }
301 }
302  
303 return $_[0]->{onerror_set} ? $_[0]->{onerror} : undef;
304 } # onerror
305  
306 1;