Skip to content

Commit

Permalink
Support removing whole lines of text with regex in m3u-prune scriptlet
Browse files Browse the repository at this point in the history
Related issue:
- uBlockOrigin/uBlock-issues#2508

If the first argument is a regex with multine flag set, the
scriptlet will execute the regex against the whole text, and
remove matching text from the whole text.

If the matching text does not contains whole lines, the text
won't be removed, i.e. it is not allowed to remove only part
of a line.
  • Loading branch information
gorhill committed Mar 12, 2023
1 parent f80c84a commit b3821e6
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions assets/resources/scriptlets.js
Original file line number Diff line number Diff line change
Expand Up @@ -1668,7 +1668,6 @@
})();



/// xml-prune.js
(function() {
let selector = '{{1}}';
Expand Down Expand Up @@ -1739,7 +1738,6 @@
})();



/// m3u-prune.js
// https://en.wikipedia.org/wiki/M3U
(function() {
Expand All @@ -1753,7 +1751,12 @@
}
const regexFromArg = arg => {
if ( arg === '' ) { return /^/; }
if ( /^\/.*\/$/.test(arg) ) { return new RegExp(arg.slice(1, -1)); }
const match = /^\/(.+)\/([gms]*)$/.exec(arg);
if ( match !== null ) {
let flags = match[2] || '';
if ( flags.includes('m') ) { flags += 's'; }
return new RegExp(match[1], flags);
}
return new RegExp(
arg.replace(/[.+?^${}()|[\]\\]/g, '\\$&').replace(/\*+/g, '.*?')
);
Expand Down Expand Up @@ -1790,6 +1793,22 @@
};
const pruner = text => {
if ( (/^\s*#EXTM3U/.test(text)) === false ) { return text; }
if ( reM3u.multiline ) {
reM3u.lastIndex = 0;
for (;;) {
const match = reM3u.exec(text);
if ( match === null ) { break; }
const before = text.slice(0, match.index);
if ( before.length === 0 || /[\n\r]+\s*$/.test(before) ) {
const after = text.slice(match.index + match[0].length);
if ( after.length === 0 || /^\s*[\n\r]+/.test(after) ) {
text = before.trim() + '\n' + after.trim();
reM3u.lastIndex = before.length + 1;
}
}
if ( reM3u.global === false ) { break; }
}
}
const lines = text.split(/\n\r|\n|\r/);
for ( let i = 0; i < lines.length; i++ ) {
if ( lines[i] === undefined ) { continue; }
Expand Down Expand Up @@ -1841,7 +1860,6 @@
})();



/// href-sanitizer.js
(function() {
let selector = '{{1}}';
Expand Down

0 comments on commit b3821e6

Please sign in to comment.