0
Fork 0
mirror of https://github.com/fastmail/Squire.git synced 2025-01-03 13:16:31 -05:00

Make Element#getPath more robust.

Won't throw an error if called on a node that is not part of a document, but
will now instead return the path from whatever the root element is down to the
node.
This commit is contained in:
Neil Jenkins 2013-01-30 11:28:36 +11:00
parent b6473508a2
commit 775b72ec88

View file

@ -58,6 +58,7 @@ var isBlock = function ( el ) {
}; };
implement( window.Node ? [ Node ] : [ Text, Element, HTMLDocument ], { implement( window.Node ? [ Node ] : [ Text, Element, HTMLDocument ], {
isLeaf: $False,
isInline: $False, isInline: $False,
isBlock: $False, isBlock: $False,
isContainer: $False, isContainer: $False,
@ -105,7 +106,6 @@ implement( window.Node ? [ Node ] : [ Text, Element, HTMLDocument ], {
}); });
implement([ Text ], { implement([ Text ], {
isLeaf: $False,
isInline: $True, isInline: $True,
getLength: function () { getLength: function () {
return this.length; return this.length;
@ -141,23 +141,21 @@ implement([ Element ], {
return this.childNodes.length; return this.childNodes.length;
}, },
getPath: function () { getPath: function () {
var tag = this.nodeName; var parent = this.parentNode,
if ( tag === 'BODY' ) { path, id, className, classNames;
return tag; if ( !parent ) {
return '';
} }
var path = this.parentNode.getPath(), path = parent.getPath();
id = this.id, path += ( path ? '>' : '' ) + this.nodeName;
className = this.className.trim(); if ( id = this.id ) {
path += '>' + tag;
if ( id ) {
path += '#' + id; path += '#' + id;
} }
if ( className ) { if ( className = this.className.trim() ) {
className = className.split( /\s\s*/ ); classNames = className.split( /\s\s*/ );
className.sort(); classNames.sort();
path += '.'; path += '.';
path += className.join( '.' ); path += classNames.join( '.' );
} }
return path; return path;
}, },