diff --git a/packages/happy-dom/src/xml-parser/XMLParser.ts b/packages/happy-dom/src/xml-parser/XMLParser.ts index 98ed92be7..9cb8e787d 100755 --- a/packages/happy-dom/src/xml-parser/XMLParser.ts +++ b/packages/happy-dom/src/xml-parser/XMLParser.ts @@ -42,76 +42,80 @@ export default class XMLParser { let lastTextIndex = 0; let match: RegExpExecArray; - while ((match = markupRegexp.exec(data))) { - const tagName = match[2].toLowerCase(); - const isStartTag = !match[1]; + if (data !== null && data !== undefined) { + data = String(data); - if (parent && match.index !== lastTextIndex) { - const text = data.substring(lastTextIndex, match.index); - this.appendTextAndCommentNodes(document, parent, text); - } - - if (isStartTag) { - const namespaceURI = - tagName === 'svg' - ? NamespaceURI.svg - : (parent).namespaceURI || NamespaceURI.html; - const newElement = document.createElementNS(namespaceURI, tagName); - - // Scripts are not allowed to be executed when they are parsed using innerHTML, outerHTML, replaceWith() etc. - // However, they are allowed to be executed when document.write() is used. - // See: https://developer.mozilla.org/en-US/docs/Web/API/HTMLScriptElement - if (tagName === 'script') { - (newElement)._evaluateScript = evaluateScripts; - } + while ((match = markupRegexp.exec(data))) { + const tagName = match[2].toLowerCase(); + const isStartTag = !match[1]; - // An assumption that the same rule should be applied for the HTMLLinkElement is made here. - if (tagName === 'link') { - (newElement)._evaluateCSS = evaluateScripts; + if (parent && match.index !== lastTextIndex) { + const text = data.substring(lastTextIndex, match.index); + this.appendTextAndCommentNodes(document, parent, text); } - this.setAttributes(newElement, match[3]); + if (isStartTag) { + const namespaceURI = + tagName === 'svg' + ? NamespaceURI.svg + : (parent).namespaceURI || NamespaceURI.html; + const newElement = document.createElementNS(namespaceURI, tagName); + + // Scripts are not allowed to be executed when they are parsed using innerHTML, outerHTML, replaceWith() etc. + // However, they are allowed to be executed when document.write() is used. + // See: https://developer.mozilla.org/en-US/docs/Web/API/HTMLScriptElement + if (tagName === 'script') { + (newElement)._evaluateScript = evaluateScripts; + } - if (!match[4] && !VoidElements.includes(tagName)) { - // Some elements are not allowed to be nested (e.g. "" is not allowed.). - // Therefore we will auto-close the tag. - if (parentUnnestableTagName === tagName) { - stack.pop(); - parent = parent.parentNode || root; + // An assumption that the same rule should be applied for the HTMLLinkElement is made here. + if (tagName === 'link') { + (newElement)._evaluateCSS = evaluateScripts; } - parent = parent.appendChild(newElement); - parentUnnestableTagName = this.getUnnestableTagName(parent); - stack.push(parent); - } else { - parent.appendChild(newElement); - } - lastTextIndex = markupRegexp.lastIndex; - - // Tags which contain non-parsed content - // For example: