1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-07 03:12:33 +01:00

fix #3399 (part 1)

This commit is contained in:
Raymond Hill 2018-01-04 18:26:52 -05:00
parent 21b52ec10b
commit 4812ac9b68
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2

View File

@ -582,6 +582,26 @@ var filterDocument = (function() {
var reContentTypeDocument = /^(?:text\/html|application\/xhtml+xml)/i, var reContentTypeDocument = /^(?:text\/html|application\/xhtml+xml)/i,
reContentTypeCharset = /charset=['"]?([^'" ]+)/i; reContentTypeCharset = /charset=['"]?([^'" ]+)/i;
var charsetFromContentType = function(contentType) {
var match = reContentTypeCharset.exec(contentType);
if ( match !== null ) {
return match[1].toLowerCase();
}
};
var charsetFromDoc = function(doc) {
var meta = doc.querySelector('meta[charset]');
if ( meta !== null ) {
return meta.getAttribute('charset').toLowerCase();
}
meta = doc.querySelector(
'meta[http-equiv="content-type" i][content]'
);
if ( meta !== null ) {
return charsetFromContentType(meta.getAttribute('content'));
}
};
// Purpose of following helper is to disconnect from watching the stream // Purpose of following helper is to disconnect from watching the stream
// if all the following conditions are fulfilled: // if all the following conditions are fulfilled:
// - Only need to inject scriptlets. // - Only need to inject scriptlets.
@ -600,7 +620,7 @@ var filterDocument = (function() {
if ( if (
filterer.scriptlets === undefined || filterer.scriptlets === undefined ||
filterer.selectors !== undefined || filterer.selectors !== undefined ||
filterer.charset !== undefined filterer.charset === undefined
) { ) {
return false; return false;
} }
@ -718,16 +738,19 @@ var filterDocument = (function() {
} }
// In case of unknown charset, assume utf-8. // In case of unknown charset, assume utf-8.
if ( filterer.charset !== textDecoderCharset ) { if (
filterer.charset === undefined && textDecoderCharset !== 'utf-8' ||
filterer.charset !== undefined && filterer.charset !== textDecoderCharset
) {
textDecoder = undefined; textDecoder = undefined;
} }
if ( textDecoder === undefined ) { if ( textDecoder === undefined ) {
try { try {
textDecoder = new TextDecoder(filterer.charset); textDecoder = new TextDecoder(filterer.charset);
textDecoderCharset = filterer.charset; textDecoderCharset = filterer.charset || 'utf-8';
} catch(ex) { } catch(ex) {
textDecoder = new TextDecoder(); textDecoder = new TextDecoder();
textDecoderCharset = undefined; textDecoderCharset = 'utf-8';
} }
} }
@ -736,6 +759,14 @@ var filterDocument = (function() {
'text/html' 'text/html'
); );
if ( filterer.charset === undefined ) {
filterer.charset = µb.textEncode.normalizeCharset(charsetFromDoc(doc));
if ( filterer.charset === undefined ) {
streamClose(filterer);
return;
}
}
var modified = false; var modified = false;
if ( filterer.selectors !== undefined ) { if ( filterer.selectors !== undefined ) {
if ( µb.htmlFilteringEngine.apply(doc, filterer) ) { if ( µb.htmlFilteringEngine.apply(doc, filterer) ) {
@ -763,9 +794,9 @@ var filterDocument = (function() {
doctypeStr + doctypeStr +
doc.documentElement.outerHTML doc.documentElement.outerHTML
); );
if ( textDecoderCharset !== undefined ) { if ( filterer.charset !== 'utf-8' ) {
encodedStream = µb.textEncode.encode( encodedStream = µb.textEncode.encode(
textDecoderCharset, filterer.charset,
encodedStream encodedStream
); );
} }
@ -809,13 +840,11 @@ var filterDocument = (function() {
contentType = headerValueFromName('content-type', headers); contentType = headerValueFromName('content-type', headers);
if ( contentType !== '' ) { if ( contentType !== '' ) {
if ( reContentTypeDocument.test(contentType) === false ) { return; } if ( reContentTypeDocument.test(contentType) === false ) { return; }
var match = reContentTypeCharset.exec(contentType); var charset = charsetFromContentType(contentType);
if ( match !== null ) { if ( charset !== undefined ) {
var charset = µb.textEncode.normalizeCharset(match[1]); charset = µb.textEncode.normalizeCharset(charset);
if ( charset === undefined ) { return; } if ( charset === undefined ) { return; }
if ( charset !== 'utf-8' ) { request.charset = charset;
request.charset = charset;
}
} }
} }
// https://bugzilla.mozilla.org/show_bug.cgi?id=1426789 // https://bugzilla.mozilla.org/show_bug.cgi?id=1426789