Skip to content

Commit b3821e6

Browse files
committed
Support removing whole lines of text with regex in m3u-prune scriptlet
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.
1 parent f80c84a commit b3821e6

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

assets/resources/scriptlets.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,7 +1668,6 @@
16681668
})();
16691669

16701670

1671-
16721671
/// xml-prune.js
16731672
(function() {
16741673
let selector = '{{1}}';
@@ -1739,7 +1738,6 @@
17391738
})();
17401739

17411740

1742-
17431741
/// m3u-prune.js
17441742
// https://en.wikipedia.org/wiki/M3U
17451743
(function() {
@@ -1753,7 +1751,12 @@
17531751
}
17541752
const regexFromArg = arg => {
17551753
if ( arg === '' ) { return /^/; }
1756-
if ( /^\/.*\/$/.test(arg) ) { return new RegExp(arg.slice(1, -1)); }
1754+
const match = /^\/(.+)\/([gms]*)$/.exec(arg);
1755+
if ( match !== null ) {
1756+
let flags = match[2] || '';
1757+
if ( flags.includes('m') ) { flags += 's'; }
1758+
return new RegExp(match[1], flags);
1759+
}
17571760
return new RegExp(
17581761
arg.replace(/[.+?^${}()|[\]\\]/g, '\\$&').replace(/\*+/g, '.*?')
17591762
);
@@ -1790,6 +1793,22 @@
17901793
};
17911794
const pruner = text => {
17921795
if ( (/^\s*#EXTM3U/.test(text)) === false ) { return text; }
1796+
if ( reM3u.multiline ) {
1797+
reM3u.lastIndex = 0;
1798+
for (;;) {
1799+
const match = reM3u.exec(text);
1800+
if ( match === null ) { break; }
1801+
const before = text.slice(0, match.index);
1802+
if ( before.length === 0 || /[\n\r]+\s*$/.test(before) ) {
1803+
const after = text.slice(match.index + match[0].length);
1804+
if ( after.length === 0 || /^\s*[\n\r]+/.test(after) ) {
1805+
text = before.trim() + '\n' + after.trim();
1806+
reM3u.lastIndex = before.length + 1;
1807+
}
1808+
}
1809+
if ( reM3u.global === false ) { break; }
1810+
}
1811+
}
17931812
const lines = text.split(/\n\r|\n|\r/);
17941813
for ( let i = 0; i < lines.length; i++ ) {
17951814
if ( lines[i] === undefined ) { continue; }
@@ -1841,7 +1860,6 @@
18411860
})();
18421861

18431862

1844-
18451863
/// href-sanitizer.js
18461864
(function() {
18471865
let selector = '{{1}}';

0 commit comments

Comments
 (0)