From daf61a202cd1aa337ccaaa02cefdb725f6ba1555 Mon Sep 17 00:00:00 2001
From: Domenic Denicola
Let fallback encoding be null.
Let position be a pointer to a byte in the input byte stream, initially + pointing at the first byte.
Prescan for XML declarations: If position points to:
+ +Get an XML encoding. If this + does not return failure, set fallback encoding to the returned encoding, and then + continue with this algorithm.
+Let position be a pointer to a byte in the input byte stream, initially - pointing at the first byte.
+Abort the prescan a byte stream to determine its encoding algorithm, + returning UTF-16LE.
+ +Abort the prescan a byte stream to determine its encoding algorithm, + returning UTF-16BE.
+Prescanning for XML declarations, even in HTML documents, must be done for + compatibility with legacy content. See issue #1438.
When the prescan a byte stream to determine its encoding algorithm says to get an XML encoding, it means doing + this. If at any point during these steps the encodingPosition pointer created in the + first step below goes beyond the end of the byte stream obtained so far, abort the get an XML encoding algorithm and return + failure.
+ +Let encodingPosition be a distinct pointer to the same place in the input byte + stream as position.
Let xmlDeclarationEnd be a pointer to the next byte in the input byte + stream which is 0x3E (ASCII '>'). If there is no such byte, abort the get an XML encoding algorithm algorithm + and return failure
Set encodingPosition to the position of the first occurrence of the subsequence + of bytes 0x65, 0x6E, 0x63, 0x6F, 0x64, 0x69, 0x6E, 0x67 (ASCII 'encoding') at or after the + current encodingPosition. If there is no such sequence, abort the get an XML encoding algorithm algorithm + and return failure.
Advance encodingPosition past the 0x67 (ASCII 'g') byte.
While the byte at encodingPosition is less than or equal to 0x20 (i.e. it is + either an ASCII space or control character), advance encodingPosition to the next + byte.
If the byte at encodingPosition is not 0x3D (ASCII =), abort the get an XML encoding algorithm algorithm + and return failure.
Let quoteMark be the byte at encodingPosition.
Advance encodingPosition to the next byte.
Let encodingEndPosition be the position of the next occurence of + quoteMark at or after encodingPosition. If quoteMark does not + occur again, abort the get an XML encoding + algorithm algorithm and return failure.
Let potentialEncoding be the Unicode string whose code points are the same as + the values of the bytes between encodingPosition (inclusive) and + encodingEndPosition (exlusive).
Return the result of getting an encoding given + potentialEncoding.
For the sake of interoperability, user agents should not use a pre-scan algorithm that returns different results than the one described above. (But, if you do, please at least let us know, so that we can improve this algorithm and benefit everyone...)