Skip to content

Commit

Permalink
Merge pull request #760 from fcapolini/fix-spurious-comments-in-embed…
Browse files Browse the repository at this point in the history
…ded-scripts

#759@patch: Spurious HTML comments in embedded scripts.
  • Loading branch information
capricorn86 authored Feb 17, 2023
2 parents 1a41fc0 + 29d7670 commit 3631cef
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/happy-dom/src/config/PlainTextElements.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default ['style', 'script'];
12 changes: 11 additions & 1 deletion packages/happy-dom/src/xml-parser/XMLParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import INode from '../nodes/node/INode';
import IElement from '../nodes/element/IElement';
import HTMLLinkElement from '../nodes/html-link-element/HTMLLinkElement';
import IDocumentFragment from '../nodes/document-fragment/IDocumentFragment';
import PlainTextElements from '../config/PlainTextElements';

const MARKUP_REGEXP = /<(\/?)([a-z][-.0-9_a-z]*)\s*([^<>]*?)(\/?)>/gi;
const COMMENT_REGEXP = /<!--(.*?)-->|<([!?])([^>]*)>/gi;
Expand Down Expand Up @@ -38,6 +39,7 @@ export default class XMLParser {
const stack: Array<IElement | IDocumentFragment> = [root];
const markupRegexp = new RegExp(MARKUP_REGEXP, 'gi');
let parent: IDocumentFragment | IElement = root;
let parentTagName = null;
let parentUnnestableTagName = null;
let lastTextIndex = 0;
let match: RegExpExecArray;
Expand All @@ -51,7 +53,11 @@ export default class XMLParser {

if (parent && match.index !== lastTextIndex) {
const text = data.substring(lastTextIndex, match.index);
this.appendTextAndCommentNodes(document, parent, text);
if (parentTagName && PlainTextElements.includes(parentTagName)) {
parent.appendChild(document.createTextNode(text));
} else {
this.appendTextAndCommentNodes(document, parent, text);
}
}

if (isStartTag) {
Expand Down Expand Up @@ -84,6 +90,7 @@ export default class XMLParser {
}

parent = <Element>parent.appendChild(newElement);
parentTagName = tagName;
parentUnnestableTagName = this.getUnnestableTagName(parent);
stack.push(parent);
} else {
Expand All @@ -105,6 +112,9 @@ export default class XMLParser {
} else {
stack.pop();
parent = stack[stack.length - 1] || root;
parentTagName = (<IElement>parent).tagName
? (<IElement>parent).tagName.toLowerCase()
: null;
parentUnnestableTagName = this.getUnnestableTagName(parent);

lastTextIndex = markupRegexp.lastIndex;
Expand Down
32 changes: 31 additions & 1 deletion packages/happy-dom/test/dom-parser/DOMParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ describe('DOMParser', () => {
let window;

beforeEach(() => {
window = new Window();
window = new Window({
settings: {
disableJavaScriptFileLoading: true,
disableJavaScriptEvaluation: true,
disableCSSFileLoading: true,
enableFileSystemHttpRequests: false
}
});
domParser = new window.DOMParser();
});

Expand Down Expand Up @@ -55,5 +62,28 @@ describe('DOMParser', () => {
`.replace(/[\s]/gm, '')
);
});

it('Correctly parses JS script w/ `<!` in it', () => {
const newDocument = domParser.parseFromString(
`<html>
<body>
<script>
var test = {className:"meta",begin:/<![a-z]/,end:/>/,contains:[t,i,l,c]};
</script>
</body>
</html>`,
'text/html'
);
// Spurious comment `<!--[a-z]/,end:/-->` should be solved
expect(new XMLSerializer().serializeToString(newDocument).replace(/\s{1,}/, ' ')).toBe(
`<html>
<body>
<script>
var test = {className:"meta",begin:/<![a-z]/,end:/>/,contains:[t,i,l,c]};
</script>
</body>
</html>`.replace(/\s{1,}/, ' ')
);
});
});
});

0 comments on commit 3631cef

Please sign in to comment.