Skip to content

Commit 84fc898

Browse files
authored
Merge pull request #917 from gtm-nayan/master
#916@patch: Handle comments with dash in text.
2 parents 8960494 + 7242417 commit 84fc898

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

packages/happy-dom/src/xml-parser/XMLParser.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,17 @@ export default class XMLParser {
160160
) {
161161
// Comment.
162162

163-
currentNode.appendChild(
164-
document.createComment(
165-
Entities.decodeHTML((match[6] ? '?' : '') + (match[3] || match[4] || match[6]))
166-
)
167-
);
163+
let comment: string;
164+
165+
if (match[3]) {
166+
comment = match[3];
167+
} else if (match[4]) {
168+
comment = match[4].endsWith('--') ? match[4].slice(0, -2) : match[4];
169+
} else {
170+
comment = '?' + match[6];
171+
}
172+
173+
currentNode.appendChild(document.createComment(Entities.decodeHTML(comment)));
168174
} else if (match[5]) {
169175
// Exclamation mark comment (usually <!DOCTYPE>).
170176

packages/happy-dom/test/xml-parser/XMLParser.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import NamespaceURI from '../../src/config/NamespaceURI';
99
import DocumentType from '../../src/nodes/document-type/DocumentType';
1010
import XMLSerializer from '../../src/xml-serializer/XMLSerializer';
1111
import IHTMLTemplateElement from '../../src/nodes/html-template-element/IHTMLTemplateElement';
12+
import NodeTypeEnum from '../../src/nodes/node/NodeTypeEnum';
1213

1314
const GET_EXPECTED_HTML = (html: string): string =>
1415
html
@@ -506,6 +507,13 @@ describe('XMLParser', () => {
506507
}
507508
});
508509

510+
it('Parses comments with dash in them.', () => {
511+
const root = XMLParser.parse(document, '<!-- comment with - in - it -->');
512+
expect(root.childNodes.length).toBe(1);
513+
expect(root.childNodes[0].nodeType).toBe(NodeTypeEnum.commentNode);
514+
expect(root.childNodes[0].nodeValue).toBe(' comment with - in - it ');
515+
});
516+
509517
it('Parses <template> elements, including its content.', () => {
510518
const root = XMLParser.parse(document, '<div><template><tr><td></td></tr></template></div>');
511519
expect(root.childNodes.length).toBe(1);

0 commit comments

Comments
 (0)