-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetPathPlus.js
73 lines (63 loc) · 1.97 KB
/
getPathPlus.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
* jQuery getPathPlus
* v 0.1 - 02/21/2011
*
* Author: Koesmanto Bong - http://web.koesbong.com
* Licensed under MIT license - http://www.opensource.org/licenses/mit-license.php
*
* Purpose:
* This plugin will traverse up the DOM tree starting from
* the selected element until it finds the closest element with
* an ID and returns the selector in a string format.
*
* Example Usage:
* >> HTML
* <div id="main">
* <span class="inner">
* <p>text</p>
* </span>
* </div>
*
* >> $('p').getPathPlus();
* "#main span.inner p"
*
* >> $('p').getPathPlus(' > ');
* "#main > span.inner > p"
*/
(function($){
jQuery.fn.getPathPlus = function (separator) {
var path,
node = this;
if (this.length != 1) {
throw 'Requires one element.';
}
while (node.length) {
var realNode = node[0],
name = realNode.localName,
parent = node.parent(),
siblings = parent.children(name),
strippedNode;
if (!name) {
break;
}
name = name.toLowerCase();
separator = (!separator) ? ' ' : separator;
if (realNode.id) {
// As soon as an element with an ID is found, stop searching
return '#' + realNode.id + (path ? (separator + path) : '');
} else if (realNode.className) {
strippedNode = $.trim(realNode.className);
name += '.' + strippedNode.split(/\s+/).join('.');
}
/* In case there are more than one of the same element,
* specify using :eq(n) property.
*/
if (siblings.length > 1) {
name += ':eq(' + siblings.index(node) + ')';
}
path = name + (path ? (separator + path) : '');
node = parent;
}
return path;
};
})(jQuery);