Skip to content

Commit

Permalink
Merge pull request #376 from Mas0nShi/task/373-Fixes-non-parsed-conte…
Browse files Browse the repository at this point in the history
…nt-tags-for-xmlparser

#373@patch: Fixes xmlparser error parse with tags which contain non-parsed content.
  • Loading branch information
capricorn86 committed Feb 22, 2022
2 parents 5d9ef5c + f499b39 commit 084023a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/happy-dom/src/config/ChildLessElements.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default ['style', 'script', 'template'];
2 changes: 2 additions & 0 deletions packages/happy-dom/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ import XMLSerializer from './xml-serializer/XMLSerializer';
import ElementTag from './config/ElementTag';
import SelfClosingElements from './config/SelfClosingElements';
import UnclosedElements from './config/UnclosedElements';
import ChildLessElements from './config/ChildLessElements';
import CSSStyleSheet from './css/CSSStyleSheet';
import Storage from './storage/Storage';
import URLSearchParams from './url-search-params/URLSearchParams';
Expand Down Expand Up @@ -211,6 +212,7 @@ export {
ElementTag,
SelfClosingElements,
UnclosedElements,
ChildLessElements,
CSSStyleSheet,
Storage,
URLSearchParams
Expand Down
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 @@ -3,6 +3,7 @@ import Element from '../nodes/element/Element';
import IDocument from '../nodes/document/IDocument';
import SelfClosingElements from '../config/SelfClosingElements';
import UnnestableElements from '../config/UnnestableElements';
import ChildLessElements from '../config/ChildLessElements';
import { decode } from 'he';
import NamespaceURI from '../config/NamespaceURI';
import HTMLScriptElement from '../nodes/html-script-element/HTMLScriptElement';
Expand Down Expand Up @@ -36,6 +37,7 @@ export default class XMLParser {
let parentUnnestableTagName = null;
let lastTextIndex = 0;
let match: RegExpExecArray;
let childLessIndex = 0;

while ((match = markupRegexp.exec(data))) {
const tagName = match[2].toLowerCase();
Expand Down Expand Up @@ -86,8 +88,16 @@ export default class XMLParser {
} else {
parent.appendChild(newElement);
}
lastTextIndex = childLessIndex = markupRegexp.lastIndex;

lastTextIndex = markupRegexp.lastIndex;
// Tags which contain non-parsed content
// For example: <script> JavaScript should not be parsed
if (ChildLessElements.includes(tagName)) {
while (markupRegexp.exec(data)[2].toLowerCase() != tagName) {
childLessIndex = markupRegexp.lastIndex;
}
markupRegexp.lastIndex = childLessIndex;
}
} else {
stack.pop();
parent = stack[stack.length - 1] || root;
Expand Down

0 comments on commit 084023a

Please sign in to comment.