Escape unescaped `"` in attribute values

Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/issues/1923
This commit is contained in:
Raymond Hill 2022-01-11 07:20:03 -05:00
parent 82ad7bd4fb
commit 6941ec7fb0
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
1 changed files with 7 additions and 3 deletions

View File

@ -416,6 +416,9 @@ const cosmeticFilterFromElement = function(elem) {
// Use attributes if still no selector found. // Use attributes if still no selector found.
// https://github.com/gorhill/uBlock/issues/1901 // https://github.com/gorhill/uBlock/issues/1901
// Trim attribute value, this may help in case of malformed HTML. // Trim attribute value, this may help in case of malformed HTML.
//
// https://github.com/uBlockOrigin/uBlock-issues/issues/1923
// Escape unescaped `"` in attribute values
if ( selector === '' ) { if ( selector === '' ) {
let attributes = [], attr; let attributes = [], attr;
switch ( tagName ) { switch ( tagName ) {
@ -457,13 +460,14 @@ const cosmeticFilterFromElement = function(elem) {
} }
while ( (attr = attributes.pop()) ) { while ( (attr = attributes.pop()) ) {
if ( attr.v.length === 0 ) { continue; } if ( attr.v.length === 0 ) { continue; }
const w = attr.v.replace(/([^\\])"/g, '$1\\"');
v = elem.getAttribute(attr.k); v = elem.getAttribute(attr.k);
if ( attr.v === v ) { if ( attr.v === v ) {
selector += `[${attr.k}="${attr.v}"]`; selector += `[${attr.k}="${w}"]`;
} else if ( v.startsWith(attr.v) ) { } else if ( v.startsWith(attr.v) ) {
selector += `[${attr.k}^="${attr.v}"]`; selector += `[${attr.k}^="${w}"]`;
} else { } else {
selector += `[${attr.k}*="${attr.v}"]`; selector += `[${attr.k}*="${w}"]`;
} }
} }
} }