⚠ Archived content — this site is no longer maintained.   Current WebKit documentation is at docs.webkit.org.

Changeset 244012 in webkit


Ignore:
Timestamp:
Apr 8, 2019, 5:39:34 AM (7 years ago)
Author:
Carlos Garcia Campos
Message:

Merge r243233 - appendChild should throw when inserting an ancestor of a template into its content adopted to another document
https://bugs.webkit.org/show_bug.cgi?id=195984

Reviewed by Darin Adler.

LayoutTests/imported/w3c:

Rebaselined the test that is not fully passing.

  • web-platform-tests/html/semantics/scripting-1/the-template-element/template-element/template-content-hierarcy-expected.txt:

Source/WebCore:

The WPT test caught a bug that appendChild and other DOM insertion functions were incorrectly assuming that
any node that's in a HTML template element has the current document's template document as its owner.
The assumption is wrong when the template element's content DocumentFragment is adopted to another document.

Fixed the bug by always checking the ancestor host elements in checkAcceptChild. Also

Test: fast/dom/insert-template-parent-into-adopted-content.html

  • dom/ContainerNode.cpp:

(WebCore::isInTemplateContent): Deleted. This code is simply wrong.
(WebCore::containsConsideringHostElements): Deleted. Call sites are updated to use containsIncludingHostElements.
(WebCore::containsIncludingHostElements): Moved from Node.cpp and optimized this code a bit. It's more efficient
to get the parent node and check for ShadowRoot and DocumentFragment only when the parent is null than to check
for those two node types before getting the parent node.
(WebCore::checkAcceptChild): Merged two code paths to call containsIncludingHostElements. The early return for
a pseudo element is there only to prevent tree corruption in release build even in the presence of a major bug
so it shouldn't be an spec compliance issue.

  • dom/Node.cpp:

(WebCore::Node::containsIncludingHostElements const): Deleted.

  • dom/Node.h:

LayoutTests:

Added a regression test.

  • fast/dom/insert-template-parent-into-adopted-content-expected.txt: Added.
  • fast/dom/insert-template-parent-into-adopted-content.html: Added.
Location:
releases/WebKitGTK/webkit-2.24
Files:
2 added
7 edited

Legend:

Unmodified
Added
Removed
  • releases/WebKitGTK/webkit-2.24/LayoutTests/ChangeLog

    r244009 r244012  
     12019-03-19  Ryosuke Niwa  <rniwa@webkit.org>
     2
     3        appendChild should throw when inserting an ancestor of a template into its content adopted to another document
     4        https://bugs.webkit.org/show_bug.cgi?id=195984
     5
     6        Reviewed by Darin Adler.
     7
     8        Added a regression test.
     9
     10        * fast/dom/insert-template-parent-into-adopted-content-expected.txt: Added.
     11        * fast/dom/insert-template-parent-into-adopted-content.html: Added.
     12
    1132019-03-19  Ryosuke Niwa  <rniwa@webkit.org>
    214
  • releases/WebKitGTK/webkit-2.24/LayoutTests/imported/w3c/ChangeLog

    r243588 r244012  
     12019-03-19  Ryosuke Niwa  <rniwa@webkit.org>
     2
     3        appendChild should throw when inserting an ancestor of a template into its content adopted to another document
     4        https://bugs.webkit.org/show_bug.cgi?id=195984
     5
     6        Reviewed by Darin Adler.
     7
     8        Rebaselined the test that is not fully passing.
     9
     10        * web-platform-tests/html/semantics/scripting-1/the-template-element/template-element/template-content-hierarcy-expected.txt:
     11
    1122019-03-22  Alicia Boya García  <aboya@igalia.com>
    213
  • releases/WebKitGTK/webkit-2.24/LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-template-element/template-element/template-content-hierarcy-expected.txt

    r224156 r244012  
    11
    22PASS Template content should throw when its ancestor is being appended.
    3 FAIL Template content should throw exception when its ancestor in a different document but connected via host is being append. assert_throws: Template content should throw if any of ancestor is being appended. function "() => {
    4     tmpl.content.appendChild(parent);
    5   }" did not throw
     3PASS Template content should throw exception when its ancestor in a different document but connected via host is being append.
    64
  • releases/WebKitGTK/webkit-2.24/Source/WebCore/ChangeLog

    r244008 r244012  
     12019-03-19  Ryosuke Niwa  <rniwa@webkit.org>
     2
     3        appendChild should throw when inserting an ancestor of a template into its content adopted to another document
     4        https://bugs.webkit.org/show_bug.cgi?id=195984
     5
     6        Reviewed by Darin Adler.
     7
     8        The WPT test caught a bug that appendChild and other DOM insertion functions were incorrectly assuming that
     9        any node that's in a HTML template element has the current document's template document as its owner.
     10        The assumption is wrong when the template element's content DocumentFragment is adopted to another document.
     11
     12        Fixed the bug by always checking the ancestor host elements in checkAcceptChild. Also
     13
     14        Test: fast/dom/insert-template-parent-into-adopted-content.html
     15
     16        * dom/ContainerNode.cpp:
     17        (WebCore::isInTemplateContent): Deleted. This code is simply wrong.
     18        (WebCore::containsConsideringHostElements): Deleted. Call sites are updated to use containsIncludingHostElements.
     19        (WebCore::containsIncludingHostElements): Moved from Node.cpp and optimized this code a bit. It's more efficient
     20        to get the parent node and check for ShadowRoot and DocumentFragment only when the parent is null than to check
     21        for those two node types before getting the parent node.
     22        (WebCore::checkAcceptChild): Merged two code paths to call containsIncludingHostElements. The early return for
     23        a pseudo element is there only to prevent tree corruption in release build even in the presence of a major bug
     24        so it shouldn't be an spec compliance issue.
     25        * dom/Node.cpp:
     26        (WebCore::Node::containsIncludingHostElements const): Deleted.
     27        * dom/Node.h:
     28
    1292019-03-19  Ryosuke Niwa  <rniwa@webkit.org>
    230
  • releases/WebKitGTK/webkit-2.24/Source/WebCore/dom/ContainerNode.cpp

    r244008 r244012  
    293293}
    294294
    295 static inline bool isInTemplateContent(const Node* node)
    296 {
    297     Document& document = node->document();
    298     return &document == document.templateDocument();
    299 }
    300 
    301 static inline bool containsConsideringHostElements(const Node& newChild, const Node& newParent)
    302 {
    303     return (newParent.isInShadowTree() || isInTemplateContent(&newParent))
    304         ? newChild.containsIncludingHostElements(&newParent)
    305         : newChild.contains(&newParent);
     295static bool containsIncludingHostElements(const Node& possibleAncestor, const Node& node)
     296{
     297    const Node* currentNode = &node;
     298    do {
     299        if (currentNode == &possibleAncestor)
     300            return true;
     301        const ContainerNode* parent = currentNode->parentNode();
     302        if (!parent) {
     303            if (is<ShadowRoot>(currentNode))
     304                parent = downcast<ShadowRoot>(currentNode)->host();
     305            else if (is<DocumentFragment>(*currentNode) && downcast<DocumentFragment>(*currentNode).isTemplateContent())
     306                parent = static_cast<const TemplateContentDocumentFragment*>(currentNode)->host();
     307        }
     308        currentNode = parent;
     309    } while (currentNode);
     310
     311    return false;
    306312}
    307313
    308314static inline ExceptionOr<void> checkAcceptChild(ContainerNode& newParent, Node& newChild, const Node* refChild, Document::AcceptChildOperation operation)
    309315{
     316    if (containsIncludingHostElements(newChild, newParent))
     317        return Exception { HierarchyRequestError };
     318
    310319    // Use common case fast path if possible.
    311320    if ((newChild.isElementNode() || newChild.isTextNode()) && newParent.isElementNode()) {
    312321        ASSERT(!newParent.isDocumentTypeNode());
    313322        ASSERT(isChildTypeAllowed(newParent, newChild));
    314         if (containsConsideringHostElements(newChild, newParent))
    315             return Exception { HierarchyRequestError };
    316323        if (operation == Document::AcceptChildOperation::InsertOrAdd && refChild && refChild->parentNode() != &newParent)
    317324            return Exception { NotFoundError };
     
    322329    ASSERT(!newChild.isPseudoElement());
    323330    if (newChild.isPseudoElement())
    324         return Exception { HierarchyRequestError };
    325 
    326     if (containsConsideringHostElements(newChild, newParent))
    327331        return Exception { HierarchyRequestError };
    328332
     
    343347    ASSERT(!newParent.isDocumentTypeNode());
    344348    ASSERT(isChildTypeAllowed(newParent, newChild));
    345     if (containsConsideringHostElements(newChild, newParent))
     349    if (containsIncludingHostElements(newChild, newParent))
    346350        return Exception { HierarchyRequestError };
    347351    return { };
  • releases/WebKitGTK/webkit-2.24/Source/WebCore/dom/Node.cpp

    r244006 r244012  
    10311031}
    10321032
    1033 bool Node::containsIncludingHostElements(const Node* node) const
    1034 {
    1035     while (node) {
    1036         if (node == this)
    1037             return true;
    1038         if (is<DocumentFragment>(*node) && downcast<DocumentFragment>(*node).isTemplateContent())
    1039             node = static_cast<const TemplateContentDocumentFragment*>(node)->host();
    1040         else
    1041             node = node->parentOrShadowHostNode();
    1042     }
    1043     return false;
    1044 }
    1045 
    10461033Node* Node::pseudoAwarePreviousSibling() const
    10471034{
  • releases/WebKitGTK/webkit-2.24/Source/WebCore/dom/Node.h

    r242438 r244012  
    393393    WEBCORE_EXPORT bool contains(const Node*) const;
    394394    bool containsIncludingShadowDOM(const Node*) const;
    395     bool containsIncludingHostElements(const Node*) const;
    396395
    397396    // Number of DOM 16-bit units contained in node. Note that rendered text length can be different - e.g. because of
Note: See TracChangeset for help on using the changeset viewer.