Skip to content

Commit

Permalink
capricorn86#519@patch: Fixes XMLParser Bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mas0nShi committed Feb 23, 2023
1 parent e1fb3ee commit 1e95e0a
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
27 changes: 26 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,29 @@ export default class XMLParser {
if (parentTagName && PlainTextElements.includes(parentTagName)) {
parent.appendChild(document.createTextNode(text));
} else {
this.appendTextAndCommentNodes(document, parent, text);
let condCommMatch;
let condCommEndMatch;
let needRewrite = false;
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]) {
// Compatible with IE
while ((condCommEndMatch = condCommEndRegexp.exec(data))) {
if (condCommEndMatch[0]) {
needRewrite = true;
break;
} else {
throw new Error('conditional comments unclosed.');
}
}
if (needRewrite) {
markupRegexp.lastIndex = condCommEndRegexp.lastIndex;
continue; // Re parse.
}
} else {
this.appendTextAndCommentNodes(document, parent, text);
}
}
}

Expand Down
32 changes: 32 additions & 0 deletions packages/happy-dom/test/xml-parser/XMLParser.test.ts
Expand Up @@ -410,5 +410,37 @@ 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]-->'
];

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

0 comments on commit 1e95e0a

Please sign in to comment.