Skip to content

Commit 1cfe73f

Browse files
committed
fix(core): escape comment markers which share characters
escapeCommentText matched each disallowed marker as a whole, so a marker that overlaps an already matched one was skipped by the scanner and left unescaped. Match the delimiter together with its context instead, which keeps the matched language identical for the existing cases.
1 parent f8c9e9d commit 1cfe73f

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

‎packages/core/src/util/dom.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@
77
*/
88

99
/**
10-
* Disallowed strings in the comment.
10+
* Matches the `<` or `>` of a disallowed string in the comment.
11+
*
12+
* The delimiter is matched together with the surrounding context rather than the disallowed string
13+
* as a whole, so that disallowed strings sharing characters (such as the `--` of `<!--` and `--!>`
14+
* in `<!--!>`) are all escaped. A pattern matching the whole string would consume the shared
15+
* characters with the first match and never see the second one.
1116
*
1217
* see: https://html.spec.whatwg.org/multipage/syntax.html#comments
1318
*/
14-
const COMMENT_DISALLOWED = /^>|^->|<!--|-->|--!>|<!-$/g;
19+
const COMMENT_DISALLOWED = /(^-?|--!?)>|<(?=!--|!-$)/g;
1520
/**
1621
* Delimiter in the disallowed strings which needs to be wrapped with zero with character.
1722
*/

‎packages/core/test/util/dom_spec.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,14 @@ describe('comment node text escaping', () => {
4545
expect(escapeCommentText('.->')).toEqual('.->');
4646
expect(escapeCommentText('<!-.')).toEqual('<!-.');
4747
});
48+
49+
it('should escape markers which share characters', () => {
50+
// The `--` belongs to both the `<!--` and the `--!>`/`-->` marker.
51+
expect(escapeCommentText('<!--!>')).toEqual('\u200b<\u200b!--!\u200b>\u200b');
52+
expect(escapeCommentText('<!--->')).toEqual('\u200b<\u200b!---\u200b>\u200b');
53+
expect(escapeCommentText('<!--!><img src="x">')).toEqual(
54+
'\u200b<\u200b!--!\u200b>\u200b<img src="x">',
55+
);
56+
});
4857
});
4958
});

0 commit comments

Comments
 (0)