Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/JS.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ protected function stripComments()
$this->stripMultilineComments();

// single-line comments
$this->registerPattern('/\/\/.*$/m', '');
$this->registerPattern('/(?<!:)\/\/.*$/m', '');
}

/**
Expand Down
29 changes: 29 additions & 0 deletions tests/JS/JSTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,35 @@ public static function dataProvider()
'/abc\/def\//.test("abc")',
'/abc\/def\//.test("abc")',
);

// template literal containing URL — `//` in `https://` must not be
// treated as line comment, or the URL path is stripped and the
// template literal becomes unclosed (real-world breakage: WP Dark
// Mode plugin v5.3.5 app.min.js, where the next template literal in
// file merges into the runaway one, producing SyntaxError on
// subsequent property declarations)
$tests[] = array(
'var url=`https://example.com/${id}/path`',
'var url=`https://example.com/${id}/path`',
);

// multiple URL-containing template literals in same script
$tests[] = array(
'var a=`https://a.example/${i}`;var b=`http://b.example/${j}`',
'var a=`https://a.example/${i}`;var b=`http://b.example/${j}`',
);

// legitimate `// line comment` after code on same line: still stripped
$tests[] = array(
"var a=1;// this comment must still be removed\nvar b=2",
'var a=1;var b=2',
);

// `// leading comment` at start of line: still stripped
$tests[] = array(
"// leading comment\nvar a=1",
'var a=1',
);
$tests[] = array(
'/abc\/def\//.test("abc\/def\/")',
'/abc\/def\//.test("abc\/def\/")',
Expand Down