1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-10-06 09:37:12 +02:00

CSS.escape: support "serialize a string" to avoid pointless escaping

This commit is contained in:
gorhill 2015-07-03 06:34:18 -04:00
parent 0487c23763
commit cc6a811934

View File

@ -52,6 +52,8 @@ if ( document.querySelector('iframe.dom-inspector.' + vAPI.sessionId) !== null )
// Modified to avoid installing as a global shim -- so the scriptlet can be
// flushed from memory once no longer in use.
// Added serializeAsString parameter.
/*! http://mths.be/cssescape v0.2.1 by @mathias | MIT license */
var cssEscape = (function(root) {
@ -67,7 +69,7 @@ var cssEscape = (function(root) {
InvalidCharacterError.prototype.name = 'InvalidCharacterError';
// http://dev.w3.org/csswg/cssom/#serialize-an-identifier
return function(value) {
return function(value, serializeAsString) {
var string = String(value);
var length = string.length;
var index = -1;
@ -90,16 +92,16 @@ var cssEscape = (function(root) {
if (
// If the character is in the range [\1-\1F] (U+0001 to U+001F) or is
// U+007F, […]
(codeUnit >= 0x0001 && codeUnit <= 0x001F) || codeUnit == 0x007F ||
(codeUnit >= 0x0001 && codeUnit <= 0x001F) || codeUnit === 0x007F ||
// If the character is the first character and is in the range [0-9]
// (U+0030 to U+0039), […]
(index === 0 && codeUnit >= 0x0030 && codeUnit <= 0x0039) ||
// If the character is the second character and is in the range [0-9]
// (U+0030 to U+0039) and the first character is a `-` (U+002D), […]
(
index == 1 &&
index === 1 &&
codeUnit >= 0x0030 && codeUnit <= 0x0039 &&
firstCodeUnit == 0x002D
firstCodeUnit === 0x002D
)
) {
// http://dev.w3.org/csswg/cssom/#escape-a-character-as-code-point
@ -113,8 +115,8 @@ var cssEscape = (function(root) {
// U+005A), or [a-z] (U+0061 to U+007A), […]
if (
codeUnit >= 0x0080 ||
codeUnit == 0x002D ||
codeUnit == 0x005F ||
codeUnit === 0x002D ||
codeUnit === 0x005F ||
codeUnit >= 0x0030 && codeUnit <= 0x0039 ||
codeUnit >= 0x0041 && codeUnit <= 0x005A ||
codeUnit >= 0x0061 && codeUnit <= 0x007A
@ -124,6 +126,16 @@ var cssEscape = (function(root) {
continue;
}
// If "serialize a string":
// If the character is '"' (U+0022) or "\" (U+005C), the escaped
// character. Otherwise, the character itself.
// http://dev.w3.org/csswg/cssom/#serialize-a-string
if ( serializeAsString && codeUnit !== 0x0022 && codeUnit !== 0x005C ) {
// the character itself
result += string.charAt(index);
continue;
}
// Otherwise, the escaped character.
// http://dev.w3.org/csswg/cssom/#escape-a-character
result += '\\' + string.charAt(index);
@ -239,7 +251,7 @@ var domLayout = (function() {
sw = '';
}
if ( str !== '' ) {
selector += '[' + attr + sw + '="' + cssEscape(str) + '"]';
selector += '[' + attr + sw + '="' + cssEscape(str, true) + '"]';
}
}
return selector;
@ -631,7 +643,7 @@ var cosmeticFilterFromNode = function(elem) {
if ( attr.v.length === 0 ) {
continue;
}
suffix.push('[', attr.k, '="', attr.v, '"]');
suffix.push('[', attr.k, '="', cssEscape(attr.v, true), '"]');
}
var selector = prefix + suffix.join('');