Skip to content

Commit

Permalink
Merge pull request #782 from Mas0nShi/519-fixes-parse-conditional-com…
Browse files Browse the repository at this point in the history
…ments

#519@patch: Fixes XMLParser Bug.
  • Loading branch information
capricorn86 committed Feb 24, 2023
2 parents ca0d075 + bee830d commit 2cb3f15
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
21 changes: 20 additions & 1 deletion packages/happy-dom/src/xml-parser/XMLParser.ts
Expand Up @@ -13,6 +13,9 @@ import HTMLLinkElement from '../nodes/html-link-element/HTMLLinkElement';
import IDocumentFragment from '../nodes/document-fragment/IDocumentFragment';
import PlainTextElements from '../config/PlainTextElements';

const CONDITION_COMMENT_REGEXP =
/<!(--)?\[if (!|le|lt|lte|gt|gte|\(.*\)|&|\|| |IE|WindowsEdition|Contoso|true|false|\d+\.?(\d+)?|)*\]>/gi;
const CONDITION_COMMENT_END_REGEXP = /<!\[endif\](--)?>/gi;
const MARKUP_REGEXP = /<(\/?)([a-z][-.0-9_a-z]*)\s*([^<>]*?)(\/?)>/gi;
const COMMENT_REGEXP = /<!--(.*?)-->|<([!?])([^>]*)>/gi;
const DOCUMENT_TYPE_ATTRIBUTE_REGEXP = /"([^"]+)"/gm;
Expand Down Expand Up @@ -56,7 +59,23 @@ export default class XMLParser {
if (parentTagName && PlainTextElements.includes(parentTagName)) {
parent.appendChild(document.createTextNode(text));
} else {
this.appendTextAndCommentNodes(document, parent, text);
let condCommMatch;
let condCommEndMatch;
const condCommRegexp = new RegExp(CONDITION_COMMENT_REGEXP, 'gi');
const condCommEndRegexp = new RegExp(CONDITION_COMMENT_END_REGEXP, 'gi');
// @Refer: https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/?redirectedfrom=MSDN
if (
isStartTag &&
(condCommMatch = condCommRegexp.exec(text)) &&
condCommMatch[0] &&
(condCommEndMatch = condCommEndRegexp.exec(data.substring(markupRegexp.lastIndex))) &&
condCommEndMatch[0]
) {
markupRegexp.lastIndex += condCommEndRegexp.lastIndex;
continue;
} else {
this.appendTextAndCommentNodes(document, parent, text);
}
}
}

Expand Down
53 changes: 53 additions & 0 deletions packages/happy-dom/test/xml-parser/XMLParser.test.ts
Expand Up @@ -410,5 +410,58 @@ describe('XMLParser', () => {
const root4 = XMLParser.parse(window.document, <string>(<unknown>false));
expect(new XMLSerializer().serializeToString(root4)).toBe('false');
});

it('Parses conditional comments', () => {
const testHTML = [
'<!--[if IE 8]>\n' + '<p>Welcome to Internet Explorer 8.</p>\n' + '<![endif]-->',

'<!--[if gte IE 7]>\n' +
'<script>\n' +
' alert("Congratulations! You are running Internet Explorer 7 or a later version of Internet Explorer.");\n' +
'</script>\n' +
'<p>Thank you for closing the message box.</p>\n' +
'<![endif]-->',

'<!--[if IE 5]>\n' +
'<p>Welcome to any incremental version of Internet Explorer 5!</p>\n' +
'<![endif]-->',

'<!--[if IE 5.0000]>\n' + '<p>Welcome to Internet Explorer 5.0!</p>\n' + '<![endif]-->',

'<!--[if WindowsEdition 1]>\n' +
'<p>You are using Windows Ultimate Edition.</p>\n' +
'<![endif]-->',

'<!--[if lt Contoso 2]>\n' +
'<p>Your version of the Contoso control is out of date; Please update to the latest.</p>\n' +
'<![endif]-->',

'<!DOCTYPE html><html lang="en">\n' +
'<head>\n' +
' <meta charset="UTF-8">\n' +
' <title>Title</title>\n' +
'</head>\n' +
'<body>\n' +
'<!--[if lt IE 9]>\n' +
"<script>window.location = 'browser.htm';</script>\n" +
'<![endif]-->\n' +
'\n' +
'\n' +
'<script>\n' +
" const node = document.createElement('a');\n" +
" node.href = 'http://www.google.com';\n" +
" node.target = '_blank';\n" +
" node.innerHTML = 'google';\n" +
' window.document.body.appendChild(node);\n' +
'</script>\n' +
'</body>\n' +
'</html>\n'
];

for (const html of testHTML) {
const root = XMLParser.parse(window.document, html);
expect(new XMLSerializer().serializeToString(root)).toBe(html);
}
});
});
});

0 comments on commit 2cb3f15

Please sign in to comment.