Bug 160640 - a.replaceChild(a, a) should throw a HierarchyRequestError
Summary: a.replaceChild(a, a) should throw a HierarchyRequestError
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: DOM (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Chris Dumez
URL:
Keywords: WebExposed
Depends on:
Blocks:
 
Reported: 2016-08-06 16:16 PDT by Chris Dumez
Modified: 2016-08-06 20:24 PDT (History)
8 users (show)

See Also:


Attachments
Patch (4.08 KB, patch)
2016-08-06 16:17 PDT, Chris Dumez
no flags Details | Formatted Diff | Diff
Patch (6.48 KB, patch)
2016-08-06 19:54 PDT, Chris Dumez
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Chris Dumez 2016-08-06 16:16:10 PDT
a.replaceChild(a, a) should throw a HierarchyRequestError, because 'a' is a host-including inclusive ancestor of 'a':
- https://dom.spec.whatwg.org/#concept-node-replace

However, the call is merely ignored in WebKit.
Comment 1 Chris Dumez 2016-08-06 16:17:31 PDT
Created attachment 285506 [details]
Patch
Comment 2 Darin Adler 2016-08-06 18:37:53 PDT
Comment on attachment 285506 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=285506&action=review

> Source/WebCore/dom/ContainerNode.cpp:407
> +    if (&oldChild == &newChild) // nothing to do
> +        return true;

Why move this past the NOT_FOUND_ERR check too and not just the validity check? Since this check is fast and it seems like a case we would like to optimize, I’d like this to remain as early in the function as practical.
Comment 3 Chris Dumez 2016-08-06 18:45:30 PDT
Comment on attachment 285506 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=285506&action=review

>> Source/WebCore/dom/ContainerNode.cpp:407
>> +        return true;
> 
> Why move this past the NOT_FOUND_ERR check too and not just the validity check? Since this check is fast and it seems like a case we would like to optimize, I’d like this to remain as early in the function as practical.

But the whole point is that we should throw in cases we are expected to throw. If you call b.replaceChild(a, a) and a is not a child of b, then we need to throw a NotFoundError. Note that this "optimization" does not seem to be in the specification so chances are we will eventually have to skip it in cases where it is observable.
Comment 4 Darin Adler 2016-08-06 19:09:01 PDT
Makes sense; I misread the check as somehow comparing the parents of oldChild and newChild. We should add a test for the NOT_FOUND case when the two are the same since it seems we did not have one. If there had been a test I would not have been confused.
Comment 5 Chris Dumez 2016-08-06 19:21:13 PDT
(In reply to comment #4)
> Makes sense; I misread the check as somehow comparing the parents of
> oldChild and newChild. We should add a test for the NOT_FOUND case when the
> two are the same since it seems we did not have one. If there had been a
> test I would not have been confused.

Good point, I'll add a test before landing.
Comment 6 Chris Dumez 2016-08-06 19:54:57 PDT
Created attachment 285517 [details]
Patch
Comment 7 WebKit Commit Bot 2016-08-06 20:24:23 PDT
Comment on attachment 285517 [details]
Patch

Clearing flags on attachment: 285517

Committed r204237: <http://trac.webkit.org/changeset/204237>
Comment 8 WebKit Commit Bot 2016-08-06 20:24:28 PDT
All reviewed patches have been landed.  Closing bug.