Skip to content

Commit

Permalink
Merge pull request #917 from gtm-nayan/master
Browse files Browse the repository at this point in the history
#916@patch: Handle comments with dash in text.
  • Loading branch information
capricorn86 committed May 13, 2023
2 parents 8960494 + 7242417 commit 84fc898
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
16 changes: 11 additions & 5 deletions packages/happy-dom/src/xml-parser/XMLParser.ts
Expand Up @@ -160,11 +160,17 @@ export default class XMLParser {
) {
// Comment.

currentNode.appendChild(
document.createComment(
Entities.decodeHTML((match[6] ? '?' : '') + (match[3] || match[4] || match[6]))
)
);
let comment: string;

if (match[3]) {
comment = match[3];
} else if (match[4]) {
comment = match[4].endsWith('--') ? match[4].slice(0, -2) : match[4];
} else {
comment = '?' + match[6];
}

currentNode.appendChild(document.createComment(Entities.decodeHTML(comment)));
} else if (match[5]) {
// Exclamation mark comment (usually <!DOCTYPE>).

Expand Down
8 changes: 8 additions & 0 deletions packages/happy-dom/test/xml-parser/XMLParser.test.ts
Expand Up @@ -9,6 +9,7 @@ import NamespaceURI from '../../src/config/NamespaceURI';
import DocumentType from '../../src/nodes/document-type/DocumentType';
import XMLSerializer from '../../src/xml-serializer/XMLSerializer';
import IHTMLTemplateElement from '../../src/nodes/html-template-element/IHTMLTemplateElement';
import NodeTypeEnum from '../../src/nodes/node/NodeTypeEnum';

const GET_EXPECTED_HTML = (html: string): string =>
html
Expand Down Expand Up @@ -506,6 +507,13 @@ describe('XMLParser', () => {
}
});

it('Parses comments with dash in them.', () => {
const root = XMLParser.parse(document, '<!-- comment with - in - it -->');
expect(root.childNodes.length).toBe(1);
expect(root.childNodes[0].nodeType).toBe(NodeTypeEnum.commentNode);
expect(root.childNodes[0].nodeValue).toBe(' comment with - in - it ');
});

it('Parses <template> elements, including its content.', () => {
const root = XMLParser.parse(document, '<div><template><tr><td></td></tr></template></div>');
expect(root.childNodes.length).toBe(1);
Expand Down

0 comments on commit 84fc898

Please sign in to comment.