xref: /PHP-8.1/ext/xml/tests/bug26614.phpt (revision 8567bc10)
1--TEST--
2Bug #26614 (CDATA sections skipped on line count)
3--EXTENSIONS--
4xml
5--SKIPIF--
6<?php
7if (defined("LIBXML_VERSION")) die('skip expat test');
8?>
9--FILE--
10<?php
11/*
12this test works fine with Expat but fails with libxml
13which we now use as default
14
15further investigation has shown that not only line count
16is skippet on CDATA sections but that libxml does also
17show different column numbers and byte positions depending
18on context and in opposition to what one would expect to
19see and what good old Expat reported just fine ...
20*/
21
22$xmls = array();
23
24// Case 1: CDATA Sections
25$xmls["CDATA"] ='<?xml version="1.0" encoding="iso-8859-1" ?>
26<data>
27<![CDATA[
28multi
29line
30CDATA
31block
32]]>
33</data>';
34
35// Case 2: replace some characters so that we get comments instead
36$xmls["Comment"] ='<?xml version="1.0" encoding="iso-8859-1" ?>
37<data>
38<!-- ATA[
39multi
40line
41CDATA
42block
43-->
44</data>';
45
46// Case 3: replace even more characters so that only textual data is left
47$xmls["Text"] ='<?xml version="1.0" encoding="iso-8859-1" ?>
48<data>
49-!-- ATA[
50multi
51line
52CDATA
53block
54---
55</data>';
56
57function startElement($parser, $name, $attrs) {
58    printf("<$name> at line %d, col %d (byte %d)\n",
59               xml_get_current_line_number($parser),
60               xml_get_current_column_number($parser),
61               xml_get_current_byte_index($parser));
62}
63
64function endElement($parser, $name) {
65    printf("</$name> at line %d, col %d (byte %d)\n",
66               xml_get_current_line_number($parser),
67               xml_get_current_column_number($parser),
68               xml_get_current_byte_index($parser));
69}
70
71function characterData($parser, $data) {
72  // dummy
73}
74
75foreach ($xmls as $desc => $xml) {
76  echo "$desc\n";
77    $xml_parser = xml_parser_create();
78    xml_set_element_handler($xml_parser, "startElement", "endElement");
79    xml_set_character_data_handler($xml_parser, "characterData");
80    if (!xml_parse($xml_parser, $xml, true))
81        echo "Error: ".xml_error_string(xml_get_error_code($xml_parser))."\n";
82    xml_parser_free($xml_parser);
83}
84?>
85--EXPECT--
86CDATA
87<DATA> at line 2, col 0 (byte 45)
88</DATA> at line 9, col 0 (byte 90)
89Comment
90<DATA> at line 2, col 0 (byte 45)
91</DATA> at line 9, col 0 (byte 90)
92Text
93<DATA> at line 2, col 0 (byte 45)
94</DATA> at line 9, col 0 (byte 90)
95