0
Fork 0
mirror of https://github.com/fastmail/Squire.git synced 2024-12-22 07:13:08 -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 ], {
isLeaf: $False,
isInline: $False,
isBlock: $False,
isContainer: $False,
@ -105,7 +106,6 @@ implement( window.Node ? [ Node ] : [ Text, Element, HTMLDocument ], {
});
implement([ Text ], {
isLeaf: $False,
isInline: $True,
getLength: function () {
return this.length;
@ -141,23 +141,21 @@ implement([ Element ], {
return this.childNodes.length;
},
getPath: function () {
var tag = this.nodeName;
if ( tag === 'BODY' ) {
return tag;
var parent = this.parentNode,
path, id, className, classNames;
if ( !parent ) {
return '';
}
var path = this.parentNode.getPath(),
id = this.id,
className = this.className.trim();
path += '>' + tag;
if ( id ) {
path = parent.getPath();
path += ( path ? '>' : '' ) + this.nodeName;
if ( id = this.id ) {
path += '#' + id;
}
if ( className ) {
className = className.split( /\s\s*/ );
className.sort();
if ( className = this.className.trim() ) {
classNames = className.split( /\s\s*/ );
classNames.sort();
path += '.';
path += className.join( '.' );
path += classNames.join( '.' );
}
return path;
},