Skip to content

Instantly share code, notes, and snippets.

@think49
Created November 1, 2010 12:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save think49/658057 to your computer and use it in GitHub Desktop.
Save think49/658057 to your computer and use it in GitHub Desktop.
ExtXPathEvaluator.js : DOM L3 XPath の XPathEvaluator を拡張してノード操作を簡単にしたJavaScriptライブラリ
// ExtXPathEvaluator.js
function ExtXPathEvaluator () {
if (!(this instanceof ExtXPathEvaluator)) {
throw new Error(this + ' is not a object created by constructor');
}
return this;
}
(function () {
this.evaluate = function (domObject, expression /*, contextNode*/) {
var contextNode;
contextNode = arguments[2];
if (!contextNode) {
contextNode = domObject;
}
this.xPathResult = domObject.evaluate(expression, contextNode, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
return this;
};
this.replace = function (property, searchValue, replaceValue) {
var xPathResult, node, i;
xPathResult = this.xPathResult;
for (i = xPathResult.snapshotLength - 1; i > -1; i--) {
node = xPathResult.snapshotItem(i);
node[property] = node[property].replace(searchValue, replaceValue);
}
return this;
};
this.forEach = function (callbackfn /*, thisArg*/) {
var xPathResult, thisArg, k, len, foo;
if (typeof callbackfn !== 'function') {
throw new TypeError(callbackfn + ' is not a function');
}
xPathResult = this.xPathResult;
if (arguments.length < 2) {
for (k = 0, len = xPathResult.snapshotLength; k < len; k++){
callbackfn(xPathResult.snapshotItem(k));
}
} else {
thisArg = arguments[1];
for (k = 0, len = xPathResult.snapshotLength; k < len; k++){
callbackfn.call(thisArg, xPathResult.snapshotItem(k));
}
}
return this;
};
}).call(ExtXPathEvaluator.prototype);
@think49
Copy link
Author

think49 commented Nov 1, 2010

下記URLで解説しています。

ExtXPathEvaluator.js
http://vird2002.s8.xrea.com/javascript/ExtXPathEvaluator.html


このライブラリは「DOM L3 XPath」のメソッドを利用しています。
未対応ブラウザでも XPath を利用する出来るようにするためには「JavaScript-XPath」を併用してください。

JavaScript-XPath – CodeRepos::Share – Trac
http://coderepos.org/share/wiki/JavaScript-XPath


OKWave で回答しました。

javascriptで、<body>タグ内のnodeNameが"#te | OKWave
http://okwave.jp/qa/q6283012.html
google翻訳apiで、#TEXTを翻訳する。 | OKWave
http://okwave.jp/qa/q6284674.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment