Skip to content

Commit

Permalink
capricorn86#916@patch: Fixes issue in the XML parser where it failed …
Browse files Browse the repository at this point in the history
…to parse comments including dash characters (-).
  • Loading branch information
capricorn86 committed May 13, 2023
1 parent 63aa8c6 commit 7242417
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
18 changes: 12 additions & 6 deletions packages/happy-dom/src/xml-parser/XMLParser.ts
Expand Up @@ -24,7 +24,7 @@ import * as Entities from 'entities';
* Group 8: End of start tag (e.g. ">" in "<div>").
*/
const MARKUP_REGEXP =
/<([a-zA-Z0-9-]+)|<\/([a-zA-Z0-9-]+)>|<!--([\s\S]+)-->|<!--([^>]+)>|<!([^>]+)>|<\?([^>]+)>|(\/>)|(>)/gm;
/<([a-zA-Z0-9-]+)|<\/([a-zA-Z0-9-]+)>|<!--([^-]+)-->|<!--([^>]+)>|<!([^>]+)>|<\?([^>]+)>|(\/>)|(>)/gm;

/**
* Attribute RegExp.
Expand Down 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
9 changes: 5 additions & 4 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,11 +507,11 @@ describe('XMLParser', () => {
}
});

it('Parses comments with dash in them', () => {
const root = XMLParser.parse(document, '<!-- comment with - in it -->');
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(Node.COMMENT_NODE);
expect(root.childNodes[0].nodeValue).toBe(' comment with - in it ');
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.', () => {
Expand Down

0 comments on commit 7242417

Please sign in to comment.