Skip to content
This repository was archived by the owner on May 7, 2025. It is now read-only.

Conversation

einarstubhaug
Copy link

Because IE returns null for text nodes

Related issue

#386

Additional information

Added function getParentElement which return node.parentElement if it is defined (chrome/firefox/safari/etc) or searches for the closest element in internet explorer 10 & 11

* @private
* Helper function because of #386, node.parentElement returns null for text nodes on IE
*
* @returns { node } parentElement
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note, rangy-core's implementation is:

    function parentElement(node) {
        var parent = node.parentNode;
        return (parent.nodeType == 1) ? parent : null;
    }

*
* @returns { node } parentElement
*/
function getParentElement(node) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to self: node.parentElement !== textRange.parentElement() (the latter is a function).

var node = $node[0];
var isTextNode = (node.nodeType === Node.TEXT_NODE);
var element = isTextNode ? node.parentElement : node;
var element = isTextNode ? getParentElement(node) : node;
Copy link
Member

@danielweck danielweck May 21, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are other uses of node.parentElement, notably plugins/highlights/manager.js getAnnotationMidpoints()
https://github.com/readium/readium-shared-js/blob/develop/plugins/highlights/manager.js#L304
I also note the use of jQuery's $node.parent() in many places in ReadiumJS, notably:

//figure out a better way to get the html parent from an element..
var $html = $element.parent();

https://github.com/readium/readium-shared-js/blob/develop/plugins/highlights/manager.js#L322

@jccr

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants