Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: scriptlet argument parsing #4416

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 30 additions & 77 deletions packages/adblocker/src/filters/cosmetic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -716,93 +716,46 @@ export default class CosmeticFilter implements IFilter {

const parts: string[] = [];

let index = 0;
let lastComaIndex = -1;
let inDoubleQuotes = false;
let inSingleQuotes = false;
let inRegexp = false;
let objectNesting = 0;
let lastCharIsBackslash = false;
let inArgument = false;

for (; index < selector.length; index += 1) {
const char = selector[index];

if (lastCharIsBackslash === false) {
if (inDoubleQuotes === true) {
if (char === '"') {
inDoubleQuotes = false;
}
} else if (inSingleQuotes === true) {
if (char === "'") {
inSingleQuotes = false;
}
} else if (objectNesting !== 0) {
if (char === '{') {
objectNesting += 1;
} else if (char === '}') {
objectNesting -= 1;
} else if (char === '"') {
inDoubleQuotes = true;
} else if (char === "'") {
inSingleQuotes = true;
}
} else if (inRegexp === true) {
if (char === '/') {
inRegexp = false;
}
} else {
if (inArgument === false) {
if (char === ' ') {
// ignore
} else if (char === '"' && selector.indexOf('"', index + 1) > 0) {
inDoubleQuotes = true;
} else if (char === "'" && selector.indexOf("'", index + 1) > 0) {
inSingleQuotes = true;
} else if (char === '{' && selector.indexOf('}', index + 1) > 0) {
objectNesting += 1;
} else if (char === '/' && selector.indexOf('/', index + 1) > 0) {
inRegexp = true;
} else {
inArgument = true;
}
}
if (char === ',') {
parts.push(selector.slice(lastComaIndex + 1, index).trim());
lastComaIndex = index;
inArgument = false;
}
}
let pos = 0;
let start = 0;
let value = '';

for (; pos < selector.length; pos++) {
const code = selector.charCodeAt(pos);

if (code === 92 /* '\\' */ && selector.charCodeAt(pos + 1) === 44 /* ',' */) {
value += selector.slice(start, pos);
start = ++pos;
} else if (code === 44 /* ',' */) {
parts.push(value + selector.slice(start, pos));
start = pos + 1;
value = '';
}

lastCharIsBackslash = char === '\\';
}

parts.push(selector.slice(lastComaIndex + 1).trim());

if (parts.length === 0) {
return undefined;
if (start - pos !== 0) {
parts.push(value + selector.slice(start, pos));
}

const args = parts
.slice(1)
.map((part) => {
this.scriptletDetails = {
name: parts[0],
args: parts.slice(1).map((part) => {
part = part.trim();

if (
(part.startsWith(`'`) && part.endsWith(`'`)) ||
(part.startsWith(`"`) && part.endsWith(`"`))
(part.endsWith(`"`) && part.endsWith(`"`))
) {
return part.substring(1, part.length - 1);
return part.slice(1, -1);
}

if (part.startsWith('/') && part.endsWith('/')) {
return part.replace(REGEXP_UNICODE_COMMA, ',').replace(REGEXP_UNICODE_BACKSLASH, '\\');
}
return part;
})
.map((part) =>
part
.replace(REGEXP_UNICODE_COMMA, ',')
.replace(REGEXP_UNICODE_BACKSLASH, '\\')
.replace(REGEXP_ESCAPED_COMMA, ','),
);

this.scriptletDetails = { name: parts[0], args };
return part;
}),
};

return this.scriptletDetails;
}
Expand Down
Loading