Skip to content

Commit d406864

Browse files
Merge pull request #309 from adobe/issue-308-comments-cleanup
fix: improve comment cleanup regex
2 parents 7207ad2 + 0a16dc5 commit d406864

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

src/utils/DOMUtils.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ export default class DOMUtils {
139139
// eslint-disable-next-line no-param-reassign
140140
document.body.innerHTML = document.body.innerHTML
141141
// remove html comments
142-
.replace(/><!--(?!>)[\S\s]*?-->/gm, '>')
143-
.replace(/\n<!--(?!>)[\S\s]*?-->/gm, '\n');
142+
.replace(/(>\s*)<!--(?!>)[\S\s]*?-->/gm, '$1')
143+
.replace(/<!--(?!>)[\S\s]*?-->/gm, '');
144144
}
145145

146146
static removeSpans(document) {

test/utils/DOMUtils.spec.js

+2
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ describe('DOMUtils#removeCommments tests', () => {
182182
test('<p><!-- useless comment \n multiline --></p>', '<p></p>');
183183
test('<p><!-- useless comment \n multiline \n multiline --></p>', '<p></p>');
184184
test('<!-- useless comment --><p>The content stays</p><!-- another useless comment with \n line break -->', '<p>The content stays</p>');
185+
test('<p>The content stays<!-- useless comment inside an element --></p>', '<p>The content stays</p>');
186+
test('<p>The content and spaces stay</p> <!-- a useless comment preceded by spaces -->', '<p>The content and spaces stay</p> ');
185187
test('<p>This is a paragraph.</p>\n\x3C!--\n<p>Look at this cool image:</p>\n<img border="0" src="pic_trulli.jpg" alt="Trulli">\n-->\n<p>This is a paragraph too.</p>\x3C!-- same line -->\n\x3C!-- single line -->', '<p>This is a paragraph.</p>\n\n<p>This is a paragraph too.</p>\n');
186188
test('<div some-crazy-attribute="" <!--=""><!-- useless comment --></div>', '<div some-crazy-attribute="" <!--=""></div>');
187189
});

0 commit comments

Comments
 (0)