1
0
mirror of https://codeberg.org/nobody/LocalCDN.git synced 2025-01-22 15:31:03 +01:00

Remove UTF-8-BOM in HTML document (#1635)

This commit is contained in:
nobody 2024-08-01 18:44:04 +02:00
parent a3afc3b95d
commit 0f909daa2b
No known key found for this signature in database
GPG Key ID: 8F6DE3D614FCFD7A
2 changed files with 24 additions and 2 deletions

View File

@ -73,12 +73,26 @@ manipulateDOM._removeCrossOriginAndIntegrityAttr = function (details) {
// NOTE: should work if 'script' string is divided into two chunks // NOTE: should work if 'script' string is divided into two chunks
filter.ondata = (evt) => { filter.ondata = (evt) => {
let chunk, uint8View;
chunk = evt.data;
uint8View = new Uint8Array(chunk);
if (isFirstData) { if (isFirstData) {
if (!charset) { if (!charset) {
// content-type has no charset declared // content-type has no charset declared
let htmlHead, charsetMatch; let htmlHead, charsetMatch;
htmlHead = asciiDecoder.decode(evt.data, {'stream': false}); // // Check and remove UTF-8 BOM at the beginning of the Uint8Array
if (manipulateDOM._startWithUtf8Bom(uint8View)) {
// Remove the BOM by slicing off the first 3 bytes
chunk = chunk.slice(3);
// Update the Uint8Array view to reflect the modified chunk without BOM
uint8View = new Uint8Array(chunk);
}
htmlHead = asciiDecoder.decode(uint8View, {'stream': false});
// eslint-disable-next-line no-useless-escape // eslint-disable-next-line no-useless-escape
charsetMatch = htmlHead.match(/<meta\s+charset=["']?([^>"'\/]+)["'>\/]/i); charsetMatch = htmlHead.match(/<meta\s+charset=["']?([^>"'\/]+)["'>\/]/i);
if (charsetMatch === null) { if (charsetMatch === null) {
@ -99,7 +113,7 @@ manipulateDOM._removeCrossOriginAndIntegrityAttr = function (details) {
} }
isFirstData = false; isFirstData = false;
data.push(evt.data); data.push(chunk);
}; };
filter.onstop = () => { filter.onstop = () => {
@ -143,6 +157,10 @@ manipulateDOM._searchCharset = function (str, charset) {
return str; return str;
}; };
// Check if the given byte array starts with the UTF-8 BOM
manipulateDOM._startWithUtf8Bom = function (str) {
return str.byteLength >= 3 && str[0] === 0xEF && str[1] === 0xBB && str[2] === 0xBF;
};
/** /**
* Public Methods * Public Methods

View File

@ -30,6 +30,10 @@
<li>jquery-powertip v1.2.0 (<a href="https://codeberg.org/nobody/LocalCDN/issues/1634">#1634</a>)</li> <li>jquery-powertip v1.2.0 (<a href="https://codeberg.org/nobody/LocalCDN/issues/1634">#1634</a>)</li>
<li>mousetrap v1.6.5 (<a href="https://codeberg.org/nobody/LocalCDN/issues/1634">#1634</a>)</li> <li>mousetrap v1.6.5 (<a href="https://codeberg.org/nobody/LocalCDN/issues/1634">#1634</a>)</li>
</ul> </ul>
<p>Improved</p>
<ul>
<li>Remove UTF-8-BOM in HTML document (<a href="https://codeberg.org/nobody/LocalCDN/issues/1635">#1635</a>)</li>
</ul>
</div> </div>
<div id="generator-section"> <div id="generator-section">
<div class="topic-label"> <div class="topic-label">