Skip to content

Commit

Permalink
Make HTML content break out of foreign content
Browse files Browse the repository at this point in the history
Fixes html tags being nested inside svg and math tags. Uses the proposed
spec changes from whatwg/html#6399 and whatwg/html#6455
  • Loading branch information
TRowbotham committed Mar 25, 2021
1 parent f9c880d commit 84108af
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 26 deletions.
25 changes: 7 additions & 18 deletions src/Parser/HTML/InsertionMode/InForeignContentInsertionMode.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,36 +144,25 @@ public function processToken(TreeBuilderContext $context, Token $token): void
)
) {
// Parse error.
// If the parser was originally created for the HTML fragment
// parsing algorithm, then act as described in the "any other start
// tag" entry below. (fragment case)
if ($context->parser->isFragmentCase) {
$this->inForeignContentAnyOtherStartTag($context, $token);

return;
}

// Pop an element from the stack of open elements, and then keep
// popping more elements from the stack of open elements until the
// current node is a MathML text integration point, an HTML
// integration point, or an element in the HTML namespace.
// While the current node is not a MathML text integration point, an HTML integration point, or an
// element in the HTML namespace, pop elements from the stack of open elements.
while (!$context->parser->openElements->isEmpty()) {
$context->parser->openElements->pop();
$currentNode = $context->parser->openElements->bottom();

if (
$this->isMathMLTextIntegrationPoint($currentNode)
|| $this->isHTMLIntegrationPoint($currentNode, $context->elementTokenMap)
|| (
$currentNode instanceof Element
&& $currentNode->namespaceURI === Namespaces::HTML
)
|| $currentNode->namespaceURI === Namespaces::HTML
) {
break;
}

$context->parser->openElements->pop();
}

// Then, reprocess the token.
// Reprocess the token according to the rules given in the section corresponding to the current
// insertion mode in HTML content.
$context->insertionMode->processToken($context, $token);

return;
Expand Down
63 changes: 55 additions & 8 deletions tests/html5lib/test_data/tree-construction/foreign-fragment.dat
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#document-fragment
svg path
#document
| <svg nobr>
| <nobr>
| "X"

#data
Expand All @@ -17,7 +17,7 @@ svg path
#document-fragment
svg path
#document
| <svg font>
| <font>
| color=""
| "X"

Expand Down Expand Up @@ -390,7 +390,7 @@ math mtext
#document-fragment
math annotation-xml
#document
| <math div>
| <div>

#data
<figure></figure>
Expand All @@ -407,7 +407,7 @@ math annotation-xml
#document-fragment
math math
#document
| <math div>
| <div>

#data
<figure></figure>
Expand Down Expand Up @@ -461,12 +461,11 @@ svg desc
<div><h1>X</h1></div>
#errors
5: HTML start tag “div” in a foreign namespace context.
9: HTML start tag “h1” in a foreign namespace context.
#document-fragment
svg svg
#document
| <svg div>
| <svg h1>
| <div>
| <h1>
| "X"

#data
Expand All @@ -476,7 +475,7 @@ svg svg
#document-fragment
svg svg
#document
| <svg div>
| <div>

#data
<div></div>
Expand Down Expand Up @@ -557,3 +556,51 @@ svg desc
svg desc
#document
| "X"

#data
<svg><p>
#errors
8: HTML start tag “p” in a foreign namespace context.
#document-fragment
div
#document
| <svg svg>
| <p>

#data
<p>
#errors
3: HTML start tag “p” in a foreign namespace context.
#document-fragment
svg svg
#document
| <p>

#data
<body><foo>
#errors
6: HTML start tag “body” in a foreign namespace context.
#document-fragment
svg svg
#document
| <svg foo>

#data
<p><foo>
#errors
3: HTML start tag “p” in a foreign namespace context.
#document-fragment
svg svg
#document
| <p>
| <foo>

#data
<p></p><foo>
#errors
3: HTML start tag “p” in a foreign namespace context.
#document-fragment
svg svg
#document
| <p>
| <svg foo>

0 comments on commit 84108af

Please sign in to comment.