Bug 148765

Summary: prepend should throw when prepending a Text node to Document
Product: WebKit Reporter: Ryosuke Niwa <rniwa>
Component: DOMAssignee: Nobody <webkit-unassigned>
Status: RESOLVED FIXED    
Severity: Normal CC: cdumez, webkit-bug-importer
Priority: P2 Keywords: InRadar
Version: WebKit Nightly Build   
Hardware: Unspecified   
OS: Unspecified   
URL: https://dom.spec.whatwg.org/#concept-node-ensure-pre-insertion-validity

Description Ryosuke Niwa 2015-09-03 18:08:57 PDT
document.createElement('div').prepend(document.createElement('span')) should throw HierarchyRequestError when ensureing pre-insertion validity.

This bug was found by the newly added test: LayoutTests/http/tests/w3c/dom/nodes/prepend-on-Document.html
Comment 1 Radar WebKit Bug Importer 2015-09-03 18:09:21 PDT
<rdar://problem/22570226>
Comment 2 Chris Dumez 2015-09-03 19:13:33 PDT
(In reply to comment #0)
> document.createElement('div').prepend(document.createElement('span')) should
> throw HierarchyRequestError when ensureing pre-insertion validity.
> 
> This bug was found by the newly added test:
> LayoutTests/http/tests/w3c/dom/nodes/prepend-on-Document.html

The case you're mentioning seems like it should work and I don't see anything in the spec [1] saying it shouldn't. Looking at http/tests/w3c/dom/nodes/prepend-on-Document.html, I see the following test case:
===
    test(function() {
        var parent = node.cloneNode();
        var x = document.createElement('x');
        parent.prepend(x);
        assert_array_equals(parent.childNodes, [x]);
    }, 'Document.prepend() with only one element as an argument, on a Document having no child.');
===

This seems to be the case you mention and the test does not expect an exception.

[1] https://dom.spec.whatwg.org/#concept-node-ensure-pre-insertion-validity
Comment 3 Chris Dumez 2015-09-03 19:15:00 PDT
Maybe you were referring to the following check in the test?
===
    test(function() {
        var parent = node.cloneNode();
        assert_throws('HierarchyRequestError', function() { parent.prepend('text'); });
        assert_array_equals(parent.childNodes, []);
    }, 'Document.prepend() with text as an argument, on a Document having no child.');
===

If so, the reason this is expected to throw is because the parent is a Document and the child is a Text Node (step 5 of https://dom.spec.whatwg.org/#concept-node-ensure-pre-insertion-validity).
Comment 4 Ryosuke Niwa 2016-07-15 17:18:20 PDT
Fixed in https://trac.webkit.org/changeset/189682.