Bug 97361 - range.selectNode throws INVALID_NODE_TYPE_ERR unless node has Document/DocumentFragment ancestor
Summary: range.selectNode throws INVALID_NODE_TYPE_ERR unless node has Document/Docume...
Status: RESOLVED CONFIGURATION CHANGED
Alias: None
Product: WebKit
Classification: Unclassified
Component: DOM (show other bugs)
Version: 528+ (Nightly build)
Hardware: All Unspecified
: P2 Normal
Assignee: Nobody
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-09-21 14:24 PDT by Nathan Vander Wilt
Modified: 2023-12-29 03:26 PST (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Nathan Vander Wilt 2012-09-21 14:24:29 PDT
The following code fails unexpectedly in WebKit (works in FF, haven't tested IE):

    var range = document.createRange(),
        root = document.createElement('div');
    root.appendChild(document.createElement('span'));
    range.selectNode(root.firstChild);              // throws INVALID_NODE_TYPE_ERR

It is possible to `range.selectNodeContents(root)` or `range.setStart(root,0), range.setEnd(root,1)` but the `.selectNode` method specifically, errors when it should not. Reference: http://www.w3.org/TR/dom/#dom-range-selectnode — only case INVALID_NODE_TYPE_ERR should be thrown is if root.firstChild.parentNode is null...but that's unpossible!

Hooking the root element into a document, or even a document fragment via e.g. `document.createDocumentFragment().append(root)`, causes the bug to no longer manifest.
Comment 1 Ojan Vafai 2012-09-24 12:12:36 PDT
I think the simplifications that DOM4 makes to selectNode are mostly good. There's a bunch of things our code does differently though. Someone needs to evaluate what the WebKit code does now and make sure we're OK with the spec as is (e.g. is it OK for the parent of the refNode to be a DocumentFragment?).

At first glance, the spec looks good to me, but I haven't taken a thorough look.
Comment 2 Anne van Kesteren 2023-12-29 03:26:32 PST
This appears to have been fixed at some point.