mirror of
https://github.com/futurepress/epub.js.git
synced 2025-10-04 15:09:16 +02:00
103 lines
2.5 KiB
JavaScript
103 lines
2.5 KiB
JavaScript
// Generated by CoffeeScript 1.6.3
|
|
var findChild, getNodeName, getNodePosition, simpleXPathJQuery, simpleXPathPure;
|
|
|
|
simpleXPathJQuery = function(relativeRoot) {
|
|
var jq;
|
|
jq = this.map(function() {
|
|
var elem, idx, path, tagName;
|
|
path = '';
|
|
elem = this;
|
|
while ((elem != null ? elem.nodeType : void 0) === Node.ELEMENT_NODE && elem !== relativeRoot) {
|
|
tagName = elem.tagName.replace(":", "\\:");
|
|
idx = $(elem.parentNode).children(tagName).index(elem) + 1;
|
|
idx = "[" + idx + "]";
|
|
path = "/" + elem.tagName.toLowerCase() + idx + path;
|
|
elem = elem.parentNode;
|
|
}
|
|
return path;
|
|
});
|
|
return jq.get();
|
|
};
|
|
|
|
simpleXPathPure = function(relativeRoot) {
|
|
var getPathSegment, getPathTo, jq, rootNode;
|
|
getPathSegment = function(node) {
|
|
var name, pos;
|
|
name = getNodeName(node);
|
|
pos = getNodePosition(node);
|
|
return "" + name + "[" + pos + "]";
|
|
};
|
|
rootNode = relativeRoot;
|
|
getPathTo = function(node) {
|
|
var xpath;
|
|
xpath = '';
|
|
while (node !== rootNode) {
|
|
if (node == null) {
|
|
throw new Error("Called getPathTo on a node which was not a descendant of @rootNode. " + rootNode);
|
|
}
|
|
xpath = (getPathSegment(node)) + '/' + xpath;
|
|
node = node.parentNode;
|
|
}
|
|
xpath = '/' + xpath;
|
|
xpath = xpath.replace(/\/$/, '');
|
|
return xpath;
|
|
};
|
|
jq = this.map(function() {
|
|
var path;
|
|
path = getPathTo(this);
|
|
return path;
|
|
});
|
|
return jq.get();
|
|
};
|
|
|
|
findChild = function(node, type, index) {
|
|
var child, children, found, name, _i, _len;
|
|
if (!node.hasChildNodes()) {
|
|
throw new Error("XPath error: node has no children!");
|
|
}
|
|
children = node.childNodes;
|
|
found = 0;
|
|
for (_i = 0, _len = children.length; _i < _len; _i++) {
|
|
child = children[_i];
|
|
name = getNodeName(child);
|
|
if (name === type) {
|
|
found += 1;
|
|
if (found === index) {
|
|
return child;
|
|
}
|
|
}
|
|
}
|
|
throw new Error("XPath error: wanted child not found.");
|
|
};
|
|
|
|
getNodeName = function(node) {
|
|
var nodeName;
|
|
nodeName = node.nodeName.toLowerCase();
|
|
switch (nodeName) {
|
|
case "#text":
|
|
return "text()";
|
|
case "#comment":
|
|
return "comment()";
|
|
case "#cdata-section":
|
|
return "cdata-section()";
|
|
default:
|
|
return nodeName;
|
|
}
|
|
};
|
|
|
|
getNodePosition = function(node) {
|
|
var pos, tmp;
|
|
pos = 0;
|
|
tmp = node;
|
|
while (tmp) {
|
|
if (tmp.nodeName === node.nodeName) {
|
|
pos++;
|
|
}
|
|
tmp = tmp.previousSibling;
|
|
}
|
|
return pos;
|
|
};
|
|
|
|
/*
|
|
//@ sourceMappingURL=xpath.map
|
|
*/
|