Repository navigation
Parser: ignore whitespace before the XML declaration - #1005
Conversation
|
|
||
| // The XML declaration must be the very first thing in the document, but | ||
| // plenty of feeds are served with a stray newline or space in front of it. | ||
| $data = ltrim($data, "\x09\x0A\x0D\x20"); |
There was a problem hiding this comment.
I am a bit confused. The code above seems to suggest that arbitrary encoding is allowed, in which case this would corrupt UTF-16 documents. However, code bellow seems to assume ASCII-compatible encoding.
Either way, it is probably a better place to do this than
Lines 278 to 281 in 7a0550c
There was a problem hiding this comment.
In practice it's always ASCII-compatible by the time it gets here. SimplePie::init() converts the body to UTF-8 and calls parse($utf8_data, 'UTF-8'), and xml_parser_create_ns() throws on anything other than UTF-8, ISO-8859-1 or US-ASCII, so the UTF-16 and UTF-32 BOM branches above only matter to someone calling Parser::parse() directly with already-broken input.
Still, ltrim() would split \n\x00 in UTF-16LE, so 72942d0 switches to the same rule as File: strip the whitespace only when < follows. Both new tests still fail without the line.
I left the copy in File alone, since Locator and the content type sniffer read File::$body before it reaches the parser. It can go once File is deprecated.
There was a problem hiding this comment.
since
Locatorand the content type sniffer readFile::$bodybefore it reaches the parser
Hmm, I guess that is why it was in the File then. Do we need to add this to those two places as well? Or should we just decide to put it in Psr18Client instead?
There was a problem hiding this comment.
Neither, I think. With the strip in Parser, the copy in File is redundant:
- The sniffer skips leading whitespace on its own (
unknown()andfeed_or_html()both start with astrspn()over whitespace). Locatorreads the body throughloadHTML(), which doesn't care, and throughis_feed(), which goes to the sniffer.
I checked it on libxml2 2.13.9 with the strip in File removed, using a feed with \n\n before the declaration. Served as text/html, text/xml and application/rss+xml, found through <link rel="alternate">, and found through an <a href> in the body, it parses with this PR in every case and fails in every case with master's Parser. Since Parser gets the body from every client, Psr18Client doesn't need its own copy.
I can drop the copy in File in this PR, or leave it to go when File does.
There was a problem hiding this comment.
Thanks for the explanation, I think keeping it there is fine for now.
In particular: * File,Psr18Client: Remove support for local files simplepie/simplepie#994 * Parser: ignore whitespace before the XML declaration simplepie/simplepie#1005 * Require PHP 7.4 or newer simplepie/simplepie#1006, simplepie/simplepie#992 * Parser: treat https Atom 1.0 namespace as Atom simplepie/simplepie#1010
This PR makes the parser skip whitespace (space, tab, CR, LF) in front of the
<?xmldeclaration, including whitespace after a BOM.A leading newline is a common slip on feeds generated by PHP templates. Without this, expat/libxml reject the document with "XML or text declaration not at start of entity" ("Reserved XML Name") and the feed is reported as invalid XML. Leading whitespace is allowed for documents without a declaration, so dropping it can't change a valid document.
Tests: parsing a feed with
"\n \t\r\n"before the declaration, and with a BOM followed by a newline. Both fail on master.