Changeset 204150 in webkit


Ignore:
Timestamp:
Aug 4, 2016, 3:43:50 PM (9 years ago)
Author:
Chris Dumez
Message:

Move insertAdjacent*() API from HTMLElement to Element
https://bugs.webkit.org/show_bug.cgi?id=160567

Reviewed by Sam Weinig.

LayoutTests/imported/w3c:

Rebaseline several W3C tests now that more checks are passing.

  • web-platform-tests/dom/interfaces-expected.txt:
  • web-platform-tests/domparsing/insert_adjacent_html-expected.txt:

Source/WebCore:

Move insertAdjacent*() API from HTMLElement to Element:

Firefox and Chrome both have insertAdjacentHTML() on Element.
Chrome has insertAdjacentText() / insertAdjacentElement() on Element as
well. Firefox does not have this API at all.

Also align insertAdjacentHTML() with the specification so that we
now throw a NO_MODIFICATION_ALLOWED_ERR if called with "beforebegin" /
"afterend" on a parent-less element. This is as per:

Previously, WebKit would just silently ignore such calls. The new
behavior with consistent with both Firefox and Chrome.

No new tests, rebaselined existing tests.

  • dom/Element.cpp:

(WebCore::Element::insertAdjacent):
(WebCore::Element::insertAdjacentElement):
(WebCore::contextElementForInsertion):
(WebCore::Element::insertAdjacentHTML):
(WebCore::Element::insertAdjacentText):

  • dom/Element.h:
  • dom/Element.idl:
  • html/HTMLElement.cpp:

(WebCore::HTMLElement::insertAdjacent): Deleted.
(WebCore::HTMLElement::insertAdjacentElement): Deleted.
(WebCore::contextElementForInsertion): Deleted.
(WebCore::HTMLElement::insertAdjacentHTML): Deleted.
(WebCore::HTMLElement::insertAdjacentText): Deleted.

  • html/HTMLElement.h:
  • html/HTMLElement.idl:

LayoutTests:

Rebaseline several tests to reflect behavior change.

  • fast/dom/HTMLElement/insertAdjacentHTML-errors-expected.txt:
  • fast/dom/HTMLElement/script-tests/insertAdjacentHTML-errors.js:
  • fast/dynamic/insertAdjacentElement-expected.txt:
Location:
trunk
Files:
14 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r204148 r204150  
     12016-08-04  Chris Dumez  <cdumez@apple.com>
     2
     3        Move insertAdjacent*() API from HTMLElement to Element
     4        https://bugs.webkit.org/show_bug.cgi?id=160567
     5
     6        Reviewed by Sam Weinig.
     7
     8        Rebaseline several tests to reflect behavior change.
     9
     10        * fast/dom/HTMLElement/insertAdjacentHTML-errors-expected.txt:
     11        * fast/dom/HTMLElement/script-tests/insertAdjacentHTML-errors.js:
     12        * fast/dynamic/insertAdjacentElement-expected.txt:
     13
    1142016-08-04  Benjamin Poulain  <benjamin@webkit.org>
    215
  • trunk/LayoutTests/fast/dom/HTMLElement/insertAdjacentHTML-errors-expected.txt

    r203333 r204150  
    44
    55
    6 PASS div.insertAdjacentHTML('beforeBegin', 'text') is undefined.
    7 PASS div.insertAdjacentHTML('afterEnd', 'text') is undefined.
     6PASS div.insertAdjacentHTML('beforeBegin', 'text') threw exception NoModificationAllowedError (DOM Exception 7): The object can not be modified..
     7PASS div.insertAdjacentHTML('afterEnd', 'text') threw exception NoModificationAllowedError (DOM Exception 7): The object can not be modified..
    88PASS div.insertAdjacentHTML('FOO', 'text') threw exception SyntaxError (DOM Exception 12): The string did not match the expected pattern..
    99PASS document.documentElement.insertAdjacentHTML('afterEnd', 'text') threw exception NoModificationAllowedError (DOM Exception 7): The object can not be modified..
  • trunk/LayoutTests/fast/dom/HTMLElement/script-tests/insertAdjacentHTML-errors.js

    r203333 r204150  
    33var div = document.createElement("div");
    44
    5 shouldBeUndefined("div.insertAdjacentHTML('beforeBegin', 'text')");
    6 shouldBeUndefined("div.insertAdjacentHTML('afterEnd', 'text')");
     5shouldThrow("div.insertAdjacentHTML('beforeBegin', 'text')");
     6shouldThrow("div.insertAdjacentHTML('afterEnd', 'text')");
    77
    88shouldThrow("div.insertAdjacentHTML('FOO', 'text')", '"SyntaxError (DOM Exception 12): The string did not match the expected pattern."');
  • trunk/LayoutTests/fast/dynamic/insertAdjacentElement-expected.txt

    r204122 r204150  
    11Caught expected exception: SyntaxError (DOM Exception 12): The string did not match the expected pattern.
    2 Caught expected exception: TypeError: Argument 2 ('element') to HTMLElement.insertAdjacentElement must be an instance of Element
     2Caught expected exception: TypeError: Argument 2 ('element') to Element.insertAdjacentElement must be an instance of Element
    331 (black) 2 (green) 3 (green) 4 (black)
    44
  • trunk/LayoutTests/imported/w3c/ChangeLog

    r204126 r204150  
     12016-08-04  Chris Dumez  <cdumez@apple.com>
     2
     3        Move insertAdjacent*() API from HTMLElement to Element
     4        https://bugs.webkit.org/show_bug.cgi?id=160567
     5
     6        Reviewed by Sam Weinig.
     7
     8        Rebaseline several W3C tests now that more checks are passing.
     9
     10        * web-platform-tests/dom/interfaces-expected.txt:
     11        * web-platform-tests/domparsing/insert_adjacent_html-expected.txt:
     12
    1132016-08-04  Chris Dumez  <cdumez@apple.com>
    214
  • trunk/LayoutTests/imported/w3c/web-platform-tests/dom/interfaces-expected.txt

    r204126 r204150  
    765765PASS Element interface: operation getElementsByTagNameNS(DOMString,DOMString)
    766766PASS Element interface: operation getElementsByClassName(DOMString)
    767 FAIL Element interface: operation insertAdjacentElement(DOMString,Element) assert_own_property: interface prototype object missing non-static operation expected property "insertAdjacentElement" missing
    768 FAIL Element interface: operation insertAdjacentText(DOMString,DOMString) assert_own_property: interface prototype object missing non-static operation expected property "insertAdjacentText" missing
     767PASS Element interface: operation insertAdjacentElement(DOMString,Element)
     768PASS Element interface: operation insertAdjacentText(DOMString,DOMString)
    769769PASS Element interface: attribute children
    770770PASS Element interface: attribute firstElementChild
     
    836836PASS Element interface: element must inherit property "getElementsByClassName" with the proper type (31)
    837837PASS Element interface: calling getElementsByClassName(DOMString) on element with too few arguments must throw TypeError
    838 FAIL Element interface: element must inherit property "insertAdjacentElement" with the proper type (32) assert_inherits: property "insertAdjacentElement" not found in prototype chain
    839 FAIL Element interface: calling insertAdjacentElement(DOMString,Element) on element with too few arguments must throw TypeError assert_inherits: property "insertAdjacentElement" not found in prototype chain
    840 FAIL Element interface: element must inherit property "insertAdjacentText" with the proper type (33) assert_inherits: property "insertAdjacentText" not found in prototype chain
    841 FAIL Element interface: calling insertAdjacentText(DOMString,DOMString) on element with too few arguments must throw TypeError assert_inherits: property "insertAdjacentText" not found in prototype chain
     838PASS Element interface: element must inherit property "insertAdjacentElement" with the proper type (32)
     839PASS Element interface: calling insertAdjacentElement(DOMString,Element) on element with too few arguments must throw TypeError
     840PASS Element interface: element must inherit property "insertAdjacentText" with the proper type (33)
     841PASS Element interface: calling insertAdjacentText(DOMString,DOMString) on element with too few arguments must throw TypeError
    842842PASS Element interface: element must inherit property "children" with the proper type (34)
    843843PASS Element interface: element must inherit property "firstElementChild" with the proper type (35)
  • trunk/LayoutTests/imported/w3c/web-platform-tests/domparsing/insert_adjacent_html-expected.txt

    r203164 r204150  
    99PASS afterend content again, with next sibling
    1010PASS Should throw when inserting with invalid position string
    11 FAIL When the parent node is null, insertAdjacentHTML should throw for beforebegin and afterend (text) assert_throws: function "function () { element.insertAdjacentHTML("afterend", "") }" did not throw
    12 FAIL When the parent node is null, insertAdjacentHTML should throw for beforebegin and afterend (comments) assert_throws: function "function () { element.insertAdjacentHTML("afterend", "<!-..." did not throw
    13 FAIL When the parent node is null, insertAdjacentHTML should throw for beforebegin and afterend (elements) assert_throws: function "function () { element.insertAdjacentHTML("afterend", "<di..." did not throw
     11PASS When the parent node is null, insertAdjacentHTML should throw for beforebegin and afterend (text)
     12PASS When the parent node is null, insertAdjacentHTML should throw for beforebegin and afterend (comments)
     13PASS When the parent node is null, insertAdjacentHTML should throw for beforebegin and afterend (elements)
    1414PASS When the parent node is a document, insertAdjacentHTML should throw for beforebegin and afterend (text)
    1515PASS When the parent node is a document, insertAdjacentHTML should throw for beforebegin and afterend (comments)
  • trunk/Source/WebCore/ChangeLog

    r204149 r204150  
     12016-08-04  Chris Dumez  <cdumez@apple.com>
     2
     3        Move insertAdjacent*() API from HTMLElement to Element
     4        https://bugs.webkit.org/show_bug.cgi?id=160567
     5
     6        Reviewed by Sam Weinig.
     7
     8        Move insertAdjacent*() API from HTMLElement to Element:
     9        - https://w3c.github.io/DOM-Parsing/#extensions-to-the-element-interface
     10        - https://dom.spec.whatwg.org/#element
     11
     12        Firefox and Chrome both have insertAdjacentHTML() on Element.
     13        Chrome has insertAdjacentText() / insertAdjacentElement() on Element as
     14        well. Firefox does not have this API at all.
     15
     16        Also align insertAdjacentHTML() with the specification so that we
     17        now throw a NO_MODIFICATION_ALLOWED_ERR if called with "beforebegin" /
     18        "afterend" on a parent-less element. This is as per:
     19        - https://w3c.github.io/DOM-Parsing/#dom-element-insertadjacenthtml
     20
     21        Previously, WebKit would just silently ignore such calls. The new
     22        behavior with consistent with both Firefox and Chrome.
     23
     24        No new tests, rebaselined existing tests.
     25
     26        * dom/Element.cpp:
     27        (WebCore::Element::insertAdjacent):
     28        (WebCore::Element::insertAdjacentElement):
     29        (WebCore::contextElementForInsertion):
     30        (WebCore::Element::insertAdjacentHTML):
     31        (WebCore::Element::insertAdjacentText):
     32        * dom/Element.h:
     33        * dom/Element.idl:
     34        * html/HTMLElement.cpp:
     35        (WebCore::HTMLElement::insertAdjacent): Deleted.
     36        (WebCore::HTMLElement::insertAdjacentElement): Deleted.
     37        (WebCore::contextElementForInsertion): Deleted.
     38        (WebCore::HTMLElement::insertAdjacentHTML): Deleted.
     39        (WebCore::HTMLElement::insertAdjacentText): Deleted.
     40        * html/HTMLElement.h:
     41        * html/HTMLElement.idl:
     42
    1432016-08-04  Anders Carlsson  <andersca@apple.com>
    244
  • trunk/Source/WebCore/dom/Element.cpp

    r203852 r204150  
    34823482}
    34833483
     3484
     3485Node* Element::insertAdjacent(const String& where, Ref<Node>&& newChild, ExceptionCode& ec)
     3486{
     3487    // In Internet Explorer if the element has no parent and where is "beforeBegin" or "afterEnd",
     3488    // a document fragment is created and the elements appended in the correct order. This document
     3489    // fragment isn't returned anywhere.
     3490    //
     3491    // This is impossible for us to implement as the DOM tree does not allow for such structures,
     3492    // Opera also appears to disallow such usage.
     3493
     3494    if (equalLettersIgnoringASCIICase(where, "beforebegin")) {
     3495        ContainerNode* parent = this->parentNode();
     3496        return (parent && parent->insertBefore(newChild, this, ec)) ? newChild.ptr() : nullptr;
     3497    }
     3498
     3499    if (equalLettersIgnoringASCIICase(where, "afterbegin"))
     3500        return insertBefore(newChild, firstChild(), ec) ? newChild.ptr() : nullptr;
     3501
     3502    if (equalLettersIgnoringASCIICase(where, "beforeend"))
     3503        return appendChild(newChild, ec) ? newChild.ptr() : nullptr;
     3504
     3505    if (equalLettersIgnoringASCIICase(where, "afterend")) {
     3506        ContainerNode* parent = this->parentNode();
     3507        return (parent && parent->insertBefore(newChild, nextSibling(), ec)) ? newChild.ptr() : nullptr;
     3508    }
     3509
     3510    ec = SYNTAX_ERR;
     3511    return nullptr;
     3512}
     3513
     3514Element* Element::insertAdjacentElement(const String& where, Element& newChild, ExceptionCode& ec)
     3515{
     3516    Node* returnValue = insertAdjacent(where, newChild, ec);
     3517    ASSERT_WITH_SECURITY_IMPLICATION(!returnValue || is<Element>(*returnValue));
     3518    return downcast<Element>(returnValue);
     3519}
     3520
     3521// Step 1 of https://w3c.github.io/DOM-Parsing/#dom-element-insertadjacenthtml.
     3522static Element* contextElementForInsertion(const String& where, Element* element, ExceptionCode& ec)
     3523{
     3524    if (equalLettersIgnoringASCIICase(where, "beforebegin") || equalLettersIgnoringASCIICase(where, "afterend")) {
     3525        auto* parent = element->parentElement();
     3526        if (!parent) {
     3527            ec = NO_MODIFICATION_ALLOWED_ERR;
     3528            return nullptr;
     3529        }
     3530        return parent;
     3531    }
     3532    if (equalLettersIgnoringASCIICase(where, "afterbegin") || equalLettersIgnoringASCIICase(where, "beforeend"))
     3533        return element;
     3534    ec =  SYNTAX_ERR;
     3535    return nullptr;
     3536}
     3537
     3538void Element::insertAdjacentHTML(const String& where, const String& markup, ExceptionCode& ec)
     3539{
     3540    Element* contextElement = contextElementForInsertion(where, this, ec);
     3541    if (!contextElement)
     3542        return;
     3543    RefPtr<DocumentFragment> fragment = createFragmentForInnerOuterHTML(*contextElement, markup, AllowScriptingContent, ec);
     3544    if (!fragment)
     3545        return;
     3546    insertAdjacent(where, fragment.releaseNonNull(), ec);
     3547}
     3548
     3549void Element::insertAdjacentText(const String& where, const String& text, ExceptionCode& ec)
     3550{
     3551    insertAdjacent(where, document().createTextNode(text), ec);
     3552}
     3553
    34843554} // namespace WebCore
  • trunk/Source/WebCore/dom/Element.h

    r203852 r204150  
    296296    virtual Element* focusDelegate();
    297297
     298    Element* insertAdjacentElement(const String& where, Element& newChild, ExceptionCode&);
     299    void insertAdjacentHTML(const String& where, const String& html, ExceptionCode&);
     300    void insertAdjacentText(const String& where, const String& text, ExceptionCode&);
     301
    298302    const RenderStyle* computedStyle(PseudoId = NOPSEUDO) override;
    299303
     
    591595    void updateLabel(TreeScope&, const AtomicString& oldForAttributeValue, const AtomicString& newForAttributeValue);
    592596
     597    Node* insertAdjacent(const String& where, Ref<Node>&& newChild, ExceptionCode&);
     598
    593599    void scrollByUnits(int units, ScrollGranularity);
    594600
  • trunk/Source/WebCore/dom/Element.idl

    r204131 r204150  
    144144#endif
    145145
     146    [RaisesException] Element insertAdjacentElement(DOMString where, Element element);
     147    [RaisesException] void insertAdjacentHTML(DOMString where, DOMString html);
     148    [RaisesException] void insertAdjacentText(DOMString where, DOMString text);
     149
    146150    [Conditional=POINTER_LOCK] void requestPointerLock();
    147151
  • trunk/Source/WebCore/html/HTMLElement.cpp

    r204122 r204150  
    631631}
    632632
    633 Node* HTMLElement::insertAdjacent(const String& where, Ref<Node>&& newChild, ExceptionCode& ec)
    634 {
    635     // In Internet Explorer if the element has no parent and where is "beforeBegin" or "afterEnd",
    636     // a document fragment is created and the elements appended in the correct order. This document
    637     // fragment isn't returned anywhere.
    638     //
    639     // This is impossible for us to implement as the DOM tree does not allow for such structures,
    640     // Opera also appears to disallow such usage.
    641 
    642     if (equalLettersIgnoringASCIICase(where, "beforebegin")) {
    643         ContainerNode* parent = this->parentNode();
    644         return (parent && parent->insertBefore(newChild, this, ec)) ? newChild.ptr() : nullptr;
    645     }
    646 
    647     if (equalLettersIgnoringASCIICase(where, "afterbegin"))
    648         return insertBefore(newChild, firstChild(), ec) ? newChild.ptr() : nullptr;
    649 
    650     if (equalLettersIgnoringASCIICase(where, "beforeend"))
    651         return appendChild(newChild, ec) ? newChild.ptr() : nullptr;
    652 
    653     if (equalLettersIgnoringASCIICase(where, "afterend")) {
    654         ContainerNode* parent = this->parentNode();
    655         return (parent && parent->insertBefore(newChild, nextSibling(), ec)) ? newChild.ptr() : nullptr;
    656     }
    657    
    658     ec = SYNTAX_ERR;
    659     return nullptr;
    660 }
    661 
    662 Element* HTMLElement::insertAdjacentElement(const String& where, Element& newChild, ExceptionCode& ec)
    663 {
    664     Node* returnValue = insertAdjacent(where, newChild, ec);
    665     ASSERT_WITH_SECURITY_IMPLICATION(!returnValue || is<Element>(*returnValue));
    666     return downcast<Element>(returnValue);
    667 }
    668 
    669 // Step 3 of http://www.whatwg.org/specs/web-apps/current-work/multipage/apis-in-html-documents.html#insertadjacenthtml()
    670 static Element* contextElementForInsertion(const String& where, Element* element, ExceptionCode& ec)
    671 {
    672     if (equalLettersIgnoringASCIICase(where, "beforebegin") || equalLettersIgnoringASCIICase(where, "afterend")) {
    673         ContainerNode* parent = element->parentNode();
    674         if (parent && !is<Element>(*parent)) {
    675             ec = NO_MODIFICATION_ALLOWED_ERR;
    676             return nullptr;
    677         }
    678         ASSERT_WITH_SECURITY_IMPLICATION(!parent || is<Element>(*parent));
    679         return downcast<Element>(parent);
    680     }
    681     if (equalLettersIgnoringASCIICase(where, "afterbegin") || equalLettersIgnoringASCIICase(where, "beforeend"))
    682         return element;
    683     ec =  SYNTAX_ERR;
    684     return nullptr;
    685 }
    686 
    687 void HTMLElement::insertAdjacentHTML(const String& where, const String& markup, ExceptionCode& ec)
    688 {
    689     Element* contextElement = contextElementForInsertion(where, this, ec);
    690     if (!contextElement)
    691         return;
    692     RefPtr<DocumentFragment> fragment = createFragmentForInnerOuterHTML(*contextElement, markup, AllowScriptingContent, ec);
    693     if (!fragment)
    694         return;
    695     insertAdjacent(where, fragment.releaseNonNull(), ec);
    696 }
    697 
    698 void HTMLElement::insertAdjacentText(const String& where, const String& text, ExceptionCode& ec)
    699 {
    700     insertAdjacent(where, document().createTextNode(text), ec);
    701 }
    702 
    703633void HTMLElement::applyAlignmentAttributeToStyle(const AtomicString& alignment, MutableStyleProperties& style)
    704634{
  • trunk/Source/WebCore/html/HTMLElement.h

    r200448 r204150  
    4949    void setInnerText(const String&, ExceptionCode&);
    5050    void setOuterText(const String&, ExceptionCode&);
    51 
    52     Element* insertAdjacentElement(const String& where, Element& newChild, ExceptionCode&);
    53     void insertAdjacentHTML(const String& where, const String& html, ExceptionCode&);
    54     void insertAdjacentText(const String& where, const String& text, ExceptionCode&);
    5551
    5652    virtual bool hasCustomFocusLogic() const;
     
    131127    virtual HTMLFormElement* virtualForm() const;
    132128
    133     Node* insertAdjacent(const String& where, Ref<Node>&& newChild, ExceptionCode&);
    134129    Ref<DocumentFragment> textToFragment(const String&, ExceptionCode&);
    135130
  • trunk/Source/WebCore/html/HTMLElement.idl

    r203803 r204150  
    4545    [TreatNullAs=EmptyString, SetterRaisesException] attribute DOMString outerText;
    4646
    47     [RaisesException] Element insertAdjacentElement(DOMString where, Element element);
    48 
    49     [RaisesException] void insertAdjacentHTML(DOMString where, DOMString html);
    50     [RaisesException] void insertAdjacentText(DOMString where, DOMString text);
    51 
    5247    [SetterRaisesException] attribute DOMString contentEditable;
    5348    readonly attribute boolean isContentEditable;
Note: See TracChangeset for help on using the changeset viewer.