Skip to content

Commit b878fd2

Browse files
committed
initial commit
0 parents  commit b878fd2

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

LICENSE

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Copyright (c) 2013, Thomas Peklak
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5+
6+
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7+
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#getQuerySelector
2+
3+
returns the query selector for a given dom node. The result can be directly fed to `document.querySelector`. It uses `body`, `#ids` and `:nth-child(i)` to compose the selector.
4+
5+
##Usage
6+
7+
```javascript
8+
var getQuerySelector = require("get-query-selector");
9+
10+
var aNodes = document.getElementByTagName("A");
11+
var aNode = aNodes[aNodes.length - 1];
12+
13+
var selector = getQuerySelector(aNode);
14+
```

index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"use strict";
2+
3+
function siblingPosition(node) {
4+
var i = 1;
5+
while(node = node.previousSibling) {
6+
if (node.nodeType == 1) i+= 1;
7+
}
8+
9+
return i;
10+
}
11+
12+
function getQuerySelector(node) {
13+
if(node.id) return "#" + node.id;
14+
if(node.nodeName == "BODY") return "body";
15+
16+
var position = siblingPosition(node);
17+
18+
return getQuerySelector(node.parentNode) + ">:nth-child("+ position +")";
19+
}
20+
21+
module.exports = getQuerySelector;

package.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "get-query-selector",
3+
"version": "0.0.1",
4+
"description": "get the query selector for a given element",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "Thomas Peklak <[email protected]>",
10+
"license": "BSD-2-Clause"
11+
}

0 commit comments

Comments
 (0)