Skip to content

Commit

Permalink
Merge pull request #1358 from capricorn86/1288-add-support-for-commen…
Browse files Browse the repository at this point in the history
…t-shorthand

fix: [#1288] Adds support for parsing shorthand comments
  • Loading branch information
capricorn86 committed Mar 24, 2024
2 parents 06b556c + c71929b commit 0ee95b7
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 6 deletions.
10 changes: 5 additions & 5 deletions packages/happy-dom/src/xml-parser/XMLParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import HTMLElementConfigContentModelEnum from '../config/HTMLElementConfigConten
* Group 8: End of start tag (e.g. ">" in "<div>").
*/
const MARKUP_REGEXP =
/<([a-zA-Z0-9-]+)|<\/([a-zA-Z0-9-]+)\s*>|<!--([^-]+)-->|<!--([^>]+)>|<!([^>]+)>|<\?([^>]+)>|(\/>)|(>)/gm;
/<([a-zA-Z0-9-]+)|<\/([a-zA-Z0-9-]+)\s*>|<!--([^-]+)-->|<!--([^>]+)>|<!([^>]*)>|<\?([^>]+)>|(\/>)|(>)/gm;

/**
* Attribute RegExp.
Expand Down Expand Up @@ -95,7 +95,7 @@ export default class XMLParser {
case MarkupReadStateEnum.startOrEndTag:
if (
match.index !== lastIndex &&
(match[1] || match[2] || match[3] || match[4] || match[5] || match[6])
(match[1] || match[2] || match[3] || match[4] || match[5] !== undefined || match[6])
) {
// Plain text between tags.

Expand Down Expand Up @@ -181,9 +181,9 @@ export default class XMLParser {
}

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

} else if (match[5] !== undefined) {
// Exclamation mark comment.
// Document type node or comment.
const exclamationComment = Entities.decodeHTML(match[5]);
currentNode.appendChild(
this.getDocumentTypeNode(document, exclamationComment) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import CustomElement from '../CustomElement.js';
import CustomElementRegistry from '../../src/custom-element/CustomElementRegistry.js';
import Window from '../../src/window/Window.js';
import Document from '../../src/nodes/document/Document.js';
import Window from '../../src/window/Window.js';
import DOMException from '../../src/exception/DOMException.js';
import { beforeEach, describe, it, expect } from 'vitest';
import * as PropertySymbol from '../../src/PropertySymbol.js';
Expand Down
1 change: 1 addition & 0 deletions packages/happy-dom/test/xml-parser/XMLParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const GET_EXPECTED_HTML = (html: string): string =>
.replace('<?processing instruction?>', '<!--?processing instruction?-->')
.replace('<?processing-instruction>', '<!--?processing-instruction-->')
.replace('<!Exclamation mark comment>', '<!--Exclamation mark comment-->')
.replace('<!>', '<!---->')
.replace('<!DOCTYPE HTML', '<!DOCTYPE html')
.replace('<img />', '<img>');

Expand Down
1 change: 1 addition & 0 deletions packages/happy-dom/test/xml-parser/data/XMLParserHTML.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default `
<b>Bold</b>
<!-- Comment 2 !-->
<span>Span</span>
<!>
</div>
<article class="class1 class2" id="id">
<!-- Comment 1 !-->
Expand Down

0 comments on commit 0ee95b7

Please sign in to comment.