php-src/ext/standard/tests/strings/htmlentities21.phpt
Gustavo André dos Santos Lopes 91727cb844 - Completed rewrite of html.c. Except for determine_charset, almost nothing
remains.
- Fixed bug on determine_charset that was preventing correct detection in
  combination with internal mbstring encoding "none", "pass" or "auto".
- Added profiles for entity encode/decode for HTMl 4.01, XHTML 1.0, XML 1.0
  and HTML 5. Added the constants ENT_HTML401, ENT_XML1, ENT_XHTML and
  ENT_HTML5.
- htmlentities()/htmlspecialchars(), when told not to double encode, verify
  the correctness of the existenting entities more thoroughly.
  It is checked whether the numerical entity represents a valid unicode code
  point (number is between 0 and 0x10FFFF). If using the flag ENT_DISALLOWED,
  it is also checked whether that numerical entity is valid in selected
  document. In HTML 4.01, all the numerical entities that represent a Unicode
  code point (< U+10FFFFFF) are valid, but that's not the case with other
  document types. If the entity is not valid, & is encoded to &amp;.
  For named entities, the check is also more thorough. While before the only
  check would be to determine if the entity was constituted by alphanumeric
  characters, now it is checked whether that entity is necessarily defined for
  the target document type. Otherwise, & is encoded to &amp;.
- For html_entity_decode(), only valid numerical and named entities (as defined
  above for htmlentities()/htmlspecialchars() + !double_encode) are decoded.
  But there is in this case one additional check. Entities that represent
  non-SGML or otherwise invalid characters are not decoded. Note that, in
  HTML5, U+000D is a valid literal character, but the entity &#x0D is not
  valid and is therefore not decoded.
- The hash tables lazily created for decoding in html_entity_decode() that were
  added recently were substituted by static hash tables. Instead of 1 hash
  table per encoding, there's only one hash table per document type defined in
  terms of unicode code points. This means that for charsets other than UTF-8
  and ISO-8859-1, a conversion to unicode code points is necessary before
  decoding.
- On the encoding side, the ad hoc ranges of entities of the translation
  tables, which mapped (in general) non-unicode code points to HTML entities
  were replaced by three-stage tables for HTML 4 and HTML 5. This mapping
  tables are defined only in terms of unicode code points, so a conversion
  is necessary for charsets other than UTF-8 and ISO-8859-1. Even so, the
  multi-stage table is much faster than the previous method, by a factor
  of 5; the conversion to unicode is a small penalty because it's just a
  simple table lookup.
  XML 1.0/htmlspecialchars() uses a simple table instead of a three-stage
  table.
- Added the flag ENT_SUBSTITUTE, which makes htmlentities()/htmlspecialchars()
  replace the invalid multibyte sequences with U+FFFD (UTF-8) or &#FFFD;
  (other encodings).
- Added the flag ENT_DISALLOWED. Implements FR #52860. Characters that cannot
  appear literally are replaced by U+FFFD (UTF-8) or &#FFFD; (otherwise).
  An alternative implementation would be to encode those characters into
  numerical entities, but that would only work in HTML 4.01 due to limitations
  on the values of numerical entities in other document types. See also the
  effects on htmlentities()/htmlspecialchars() with !double_encode above.
2010-10-24 15:01:02 +00:00

199 lines
4.6 KiB
PHP

--TEST--
htmlentities() / htmlspecialchars() ENT_DISALLOWED charset variation
--FILE--
<?php
$tests = array(
0x00, //C0
0x01,
0x09,
0x0A,
0x0B,
0x0C,
0x0D,
0x0E,
0x1F,
0x20, //allowed always
0x7F, //DEL
0x80, //C1
0x9F,
0xA0, //allowed always
);
function test($flag, $charset) {
global $tests;
$i = -1;
error_reporting(-1 & ~E_STRICT);
foreach ($tests as $test) {
$test = chr($test);
$i++;
$a = htmlentities($test, $flag | ENT_DISALLOWED, $charset);
$b = htmlspecialchars($test, $flag | ENT_DISALLOWED, $charset);
if ($a == "" && $b == "") { echo sprintf("%05X", $tests[$i]), ": INVALID SEQUENCE\n"; continue; }
echo sprintf("%05X", $tests[$i]), ": ", bin2hex($a), " ", bin2hex($b), "\n";
}
error_reporting(-1);
}
echo "*** Testing HTML 4.01/Windows-1251 ***\n";
test(ENT_HTML401, "Windows-1251");
echo "\n*** Testing XHTML 1.0/Windows-1251 ***\n";
test(ENT_XHTML, "Windows-1251");
echo "\n*** Testing HTML 5/Windows-1251 ***\n";
test(ENT_HTML5, "Windows-1251");
echo "\n*** Testing XML 1.0/Windows-1251 ***\n";
test(ENT_XML1, "Windows-1251");
echo "\n*** Testing HTML 4.01/SJIS ***\n";
test(ENT_HTML401, "SJIS");
echo "\n*** Testing XHTML 1.0/SJIS ***\n";
test(ENT_XHTML, "SJIS");
echo "\n*** Testing HTML 5/SJIS ***\n";
test(ENT_HTML5, "SJIS");
echo "\n*** Testing XML 1.0/SJIS ***\n";
test(ENT_XML1, "SJIS");
?>
--EXPECT--
*** Testing HTML 4.01/Windows-1251 ***
00000: 262378464646443b 262378464646443b
00001: 262378464646443b 262378464646443b
00009: 09 09
0000A: 0a 0a
0000B: 262378464646443b 262378464646443b
0000C: 262378464646443b 262378464646443b
0000D: 0d 0d
0000E: 262378464646443b 262378464646443b
0001F: 262378464646443b 262378464646443b
00020: 20 20
0007F: 262378464646443b 7f
00080: 80 80
0009F: 9f 9f
000A0: 266e6273703b a0
*** Testing XHTML 1.0/Windows-1251 ***
00000: 262378464646443b 262378464646443b
00001: 262378464646443b 262378464646443b
00009: 09 09
0000A: 0a 0a
0000B: 262378464646443b 262378464646443b
0000C: 262378464646443b 262378464646443b
0000D: 0d 0d
0000E: 262378464646443b 262378464646443b
0001F: 262378464646443b 262378464646443b
00020: 20 20
0007F: 7f 7f
00080: 80 80
0009F: 9f 9f
000A0: 266e6273703b a0
*** Testing HTML 5/Windows-1251 ***
00000: 262378464646443b 262378464646443b
00001: 262378464646443b 262378464646443b
00009: 265461623b 09
0000A: 264e65774c696e653b 0a
0000B: 262378464646443b 262378464646443b
0000C: 0c 0c
0000D: 0d 0d
0000E: 262378464646443b 262378464646443b
0001F: 262378464646443b 262378464646443b
00020: 20 20
0007F: 262378464646443b 7f
00080: 26444a63793b 80
0009F: 26647a63793b 9f
000A0: 266e6273703b a0
*** Testing XML 1.0/Windows-1251 ***
00000: 262378464646443b 262378464646443b
00001: 262378464646443b 262378464646443b
00009: 09 09
0000A: 0a 0a
0000B: 262378464646443b 262378464646443b
0000C: 262378464646443b 262378464646443b
0000D: 0d 0d
0000E: 262378464646443b 262378464646443b
0001F: 262378464646443b 262378464646443b
00020: 20 20
0007F: 7f 7f
00080: 80 80
0009F: 9f 9f
000A0: a0 a0
*** Testing HTML 4.01/SJIS ***
00000: 262378464646443b 262378464646443b
00001: 262378464646443b 262378464646443b
00009: 09 09
0000A: 0a 0a
0000B: 262378464646443b 262378464646443b
0000C: 262378464646443b 262378464646443b
0000D: 0d 0d
0000E: 262378464646443b 262378464646443b
0001F: 262378464646443b 262378464646443b
00020: 20 20
0007F: 7f 7f
00080: INVALID SEQUENCE
0009F: INVALID SEQUENCE
000A0: INVALID SEQUENCE
*** Testing XHTML 1.0/SJIS ***
00000: 262378464646443b 262378464646443b
00001: 262378464646443b 262378464646443b
00009: 09 09
0000A: 0a 0a
0000B: 262378464646443b 262378464646443b
0000C: 262378464646443b 262378464646443b
0000D: 0d 0d
0000E: 262378464646443b 262378464646443b
0001F: 262378464646443b 262378464646443b
00020: 20 20
0007F: 7f 7f
00080: INVALID SEQUENCE
0009F: INVALID SEQUENCE
000A0: INVALID SEQUENCE
*** Testing HTML 5/SJIS ***
00000: 262378464646443b 262378464646443b
00001: 262378464646443b 262378464646443b
00009: 09 09
0000A: 0a 0a
0000B: 262378464646443b 262378464646443b
0000C: 0c 0c
0000D: 0d 0d
0000E: 262378464646443b 262378464646443b
0001F: 262378464646443b 262378464646443b
00020: 20 20
0007F: 7f 7f
00080: INVALID SEQUENCE
0009F: INVALID SEQUENCE
000A0: INVALID SEQUENCE
*** Testing XML 1.0/SJIS ***
00000: 262378464646443b 262378464646443b
00001: 262378464646443b 262378464646443b
00009: 09 09
0000A: 0a 0a
0000B: 262378464646443b 262378464646443b
0000C: 262378464646443b 262378464646443b
0000D: 0d 0d
0000E: 262378464646443b 262378464646443b
0001F: 262378464646443b 262378464646443b
00020: 20 20
0007F: 7f 7f
00080: INVALID SEQUENCE
0009F: INVALID SEQUENCE
000A0: INVALID SEQUENCE