if ('undefined' == typeof document.evaluate && typeof ActiveXObject != 'undefined')
document.evaluate = function (expression, contextNode, resolver, type, result) {
try {
var d = new ActiveXObject ('Msxml2.DOMDocument');
var s = contextNode.outerHTML;
// IE の inner/outerHTML は XML 整形式には程遠い
s = s.replace (/<([^\/][^>]+)>/g, function (m, c) {
return '<' + c.replace (/([^\s]+)=([^'"\s]+)/g, '$1="$2"') + '>'; // "'
} );
s = s.replace (/<(area|base|basefont|br|col|frame|hr|img|input|param)(\s*[^>]*)>/gi, '<$1$2 />');
d.async = false;
d.loadXML (s);
d.setProperty ('SelectionLanguage', 'XPath');
return d.documentElement.selectNodes (expression);
} catch (e) {
}
};var c, child = document.evaluate (
'//div[@class="foo"]',
document.documentElement,
null, // HTML only
XPathResult.ORDERED_NODE_ITERATOR_TYPE,
null
);
while (c = child.iterateNext ()) {
// ...
}var c, child = document.createTreeWalker (
document.documentElement,
NodeFilter.SHOW_ELEMENT,
function (n) {
if ('DIV' == n.tagName && 'foo' == n.className)
return NodeFilter.FILTER_ACCEPT;
else
return NodeFilter.FILTER_SKIP;
},
false
);
while (c = child.nextNode ()) {
// ...
}function getInnerHTML(node){
var str = '', x = 0, nodes = node.childNodes, X = nodes.length;
var _attributes = [];
while(x < X){
var _node = nodes[x];
switch (nodes[x].nodeType){
case 1: // エレメントノード
var tag = _node.nodeName.toLowerCase();
var _innerHTML = _node.parentNode.innerHTML;
var m = new RegExp("<"+tag+" +?([^>]*?)>", "i").exec(_innerHTML);
var attrs = m[1].replace(/^\s/, '').split(/\s/);
var i = 0, l = attrs.length;
str += ("<" + tag);
while(i < l){
var a = attrs[i].split(/=/);
if(a[0]!= "") str += ' '+a[0]+'="'+a[1].replace(/"/g, '')+'"';
i++;
}
if(_node.childNodes.length == 0 && /^(INPUT|HR|BR|IMG)$/.test(_node.nodeName)){
str += " />";
} else str += '>' + getInnerHTML(_node) + '</' + tag + '>';
break;
case 3: // テキストノード
if(/^[\r\n\t ]+$/.test(_node.nodeValue)) break;
str += _node.nodeValue;
break;
case 4: // CDATAセクション
str += "<![CDATA[" + _node.nodeValue + "]]>";
break;
case 5: // 実体参照ノード
str += "&" + _node.nodeName.toLowerCase() + ";"
break;
case 8: // コメントノード
str += "<!--" + _node.nodeValue + "-->"
break;
}
x++;
}
return str;
}<?xml version="1.0" encoding="Shift_JIS"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Page</title>
<script type="text/javascript">
<!--
function selectNodes(queryString)
{
function getHTMLNode(node)
{
switch(node.nodeType)
{
case 2:
var nodes_ele = doc.selectNodes("//*[@*[name()='"+node.nodeName+"']]");
var node_ele;
for(var i=0; i<nodes_ele.length; i++)
{
node_ele = nodes_ele[i];
if(node_ele.selectSingleNode("@*[name()='"+node.nodeName+"']") == node)
return getHTMLNode(node_ele).attributes[node.name];
}
break;
default:
var list_position = new Array();
var node_before;
var iPosition;
while(node != doc.documentElement)
{
iPosition = 0;
node_before = node.previousSibling;
while(node_before != null)
{
iPosition++;
node_before = node_before.previousSibling;
}
list_position.push(iPosition);
node = node.parentNode;
}
var node_html = document.documentElement;
while(list_position.length > 0)
node_html = node_html.childNodes[list_position.pop()];
return node_html;
}
return null;
}
var doc = new ActiveXObject("MSXML2.DOMDocument");
doc.setProperty("SelectionLanguage", "XPath");
doc.load(location.href);
var nodes = doc.selectNodes(queryString);
var node_html;
var list_return = new Array();
for(var i=0; i<nodes.length; i++)
{
node_html = getHTMLNode(nodes[i]);
if(node_html != null)
list_return.push(node_html);
}
return list_return;
};
function test()
{
var nodes;
nodes = selectNodes("//*[name()='div']/@*");
alert(nodes[0].nodeName);
nodes = selectNodes("//text()");
alert(nodes[1].nodeValue);
nodes = selectNodes("/*");
alert(nodes[0].outerHTML);
}
//-->
</script>
</head>
<body onload="test();">
<div id="d" class="a">test</div>
<span>ああああ</span>
</body>
</html>var parser = new XPathParser;
var expression = parser.parse ('//node()[name()="DIV" and @class="foo"]');
var context = new XPathContext;
context.expressionContextNode = document.documentElement;
var result = expression.evaluate (context);
alert (result.tree.node.innerHTML);
alert (result.tree.right.node.innerHTML); // もし結果が複数なら<?xml version="1.0" encoding="Shift_JIS"?>
<html>
<head>
<title>Untitled Page</title>
<script type="text/javascript">
<!--
function selectNodes(queryString, document)
{
function getHTMLNode(node)
{
switch(node.nodeType)
{
case 2:
var nodes_ele = doc.selectNodes("//*[@*[name()='"+node.nodeName+"']]");
var node_ele;
for(var i=0; i<nodes_ele.length; i++)
{
node_ele = nodes_ele[i];
if(node_ele.selectSingleNode("@*[name()='"+node.nodeName+"']") == node)
return getHTMLNode(node_ele).attributes[node.name];
}
break;
default:
var list_position = new Array();
var node_before;
var iPosition;
while(node != doc.documentElement)
{
iPosition = 0;
node_before = node.previousSibling;
while(node_before != null)
{
iPosition++;
node_before = node_before.previousSibling;
}
list_position.push(iPosition);
node = node.parentNode;
}
var node_html = document.documentElement;
var nodes_html;
while(list_position.length > 0)
{
iPosition = list_position.pop();
var nodes_html = node_html.childNodes;
for(var i=0; i<nodes_html.length; i++)
{
node_html = nodes_html[i];
if(node_html.nodeType != 3 || node_html.nodeValue.replace(/\s/g,"").length > 0)
iPosition -= 1;
if(iPosition < 0)
break;
}
}
return node_html;
}
return null;
}
if(document == null)
document = window.document;
var doc = new ActiveXObject("MSXML2.DOMDocument");
doc.setProperty("SelectionLanguage", "XPath");
doc.preserveWhiteSpace = false;
doc.load(document.parentWindow.location.href);
var nodes = doc.selectNodes(queryString);
var node_html;
var list_return = new Array();
for(var i=0; i<nodes.length; i++)
{
node_html = getHTMLNode(nodes[i]);
if(node_html != null)
list_return.push(node_html);
}
return list_return;
};
function test()
{
var nodes = selectNodes('//div[@class="foo"]', document);
for(var i=0; i<nodes.length; i++)
alert(nodes[i].innerHTML);
}
//-->
</script>
</head>
<body onload="test();">
<div class="foo">
<p>これこれ</p>
</div>
<div class="foo">
<p>あれあれ</p>
</div>
<div class="foo">
<p>そうそう、それそれ。</p>
</div>
</body>
</html>doc.load(document.parentWindow.location.href);
var r = new ActiveXObject ('Microsoft.XMLHTTP');
r.open ('get', d.location, false);
r.send(null);
if (r.responseText)
doc.loadXML (r.responseText.replace (/.../gi, ''));function getOwnerElement (attrNode, contextNode) {
try {
return attrNode.selectSingleNodes ('..');
} catch (e) {
var nodes = contextNode.getElementsByTagName ('*');
var i = 0;
while (nodes[i]) {
var node = nodes[i++];
var attrs = node.attributes;
var j = 0;
while (attrs[j])
if (attrs[j++] == attrNode)
return node;
}
return null;
}
}<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Untitled Page</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<script type="text/javascript">
<!--
function test()
{
alert('成功!');
}
test();
//-->
</script>
<script type="text/javascript">
if(document.write == null)
{
alert("変換開始");
var nodes_script = document.getElementsByTagName("script");
var nodes_script_child;
var list_script_code = new Array();
for(var i=0; i != nodes_script.length; i++)
{
nodes_script_child = nodes_script[i].childNodes;
for(var j=0; j != nodes_script_child.length; j++)
{
if(nodes_script_child[j].nodeType == 8)
list_script_code.push(nodes_script_child[j].nodeValue);
}
}
if(list_script_code.length != 0)
eval(list_script_code.join("\n"));
}
</script>
</head>
<body onload="test();">
<div>
<a href="javascript:test();">CALL_TESTFUNC</a>
</div>
</body>
</html>
<script type="text/javascript">
//<!CDATA[<!--
if ('undefined' == typeof document.evaluate && typeof ActiveXObject)
/* document.evaluate のエミュレーション実装 */
//-->]]>
</script>if ('undefined' == typeof document.evaluate)
(function () {
XPathResult = { };
XPathResult.ANY_TYPE = 0;
XPathResult.NUMBER_TYPE = 1;
XPathResult.STRING_TYPE = 2;
XPathResult.BOOLEAN_TYPE = 3;
XPathResult.UNORDERED_NODE_ITERATOR_TYPE = 4;
XPathResult.ORDERED_NODE_ITERATOR_TYPE = 5;
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE = 6;
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE = 7;
XPathResult.ANY_UNORDERED_NODE_TYPE = 8;
XPathResult.FIRST_ORDERED_NODE_TYPE = 9;
function IeXPathEvaluator (baseDocument) {
this.mirrorDocument = createMirrorDocument (baseDocument);
}
IeXPathEvaluator.prototype.evaluate = function (expression, contextNode, resolver, type, result) {
var origDoc = contextNode.ownerDocument.documentElement;
var mirrDoc = this.mirrorDocument.documentElement;
var mirrContext = getMirrorNode (contextNode, origDoc, mirrDoc);
var mirrNodes = mirrContext.selectNodes (expression);
var result = [ ];
var i = 0;
// 本来は type によって result の種別を変えるのだが省略
while (mirrNodes[i])
result.push (getMirrorNode (mirrNodes[i++], mirrContext, contextNode));
return result;
};
var xe = new IeXPathEvaluator (this.document);
document.evaluate = function () {
return xe.evaluate.apply (xe, arguments);
};
return;
//______________________________________________________________________
function createMirrorDocument () {
try {
var req = new ActiveXObject ('Microsoft.XMLHTTP');
req.open ('get', location, false);
req.send (null);
var src = req.responseText;
src = src.replace (/\u003C!DOCTYPE[^>]*>/i, '');
src = src.replace (/(\u003C(?:area|base|basefont|br|col|frame|hr|img|input|link|meta|param)[^>]*)>/gi, function (s1, s2) {
return s2 + (s2.charAt (s2.length - 1) != '/' ? ' /' : '') + '>';
} );
src = src.replace (/(\u003C)(script(?:[^\u003C]|\u003C+[^\u003C\/])*\u003C*)(\u003C\/script>)/gi,
function (s1, c1, c2, c3) {
c2 = c2.replace (new RegExp ('\u003C', 'g'), '<');
c2 = c2.replace (/&/g, '&');
return c1 + c2 + c3;
} );
var doc = new ActiveXObject ('Msxml2.DOMDocument');
doc.setProperty ('SelectionLanguage', 'XPath');
doc.preserveWhiteSpace = false;
doc.async = false;
doc.loadXML (src);
doc.documentElement.nodeName;
return doc;
} catch (e) {
throw Error ('cannot create an XML document.');
}
}
function getMirrorNode (node, contextNode, mirrorContextNode) {
if (node.nodeType != 2 /*Node.ELEMENT_NODE*/) {
var posData = getDocumentPosition (node, contextNode);
var currentNode = mirrorContextNode;
var i;
while (posData.length)
currentNode = currentNode.childNodes[ posData.pop () ];
return currentNode;
} else { // Node.ATTRIBUTE_NODE
// IE does not have Attr::ownerElement
var ownerElement = getOwnerElement (node, contextNode);
var posData = getDocumentPosition (ownerElement, contextNode);
var currentNode = mirrorContextNode;
var i;
while (posData.length)
currentNode = currentNode.childNodes[ posData.pop () ];
return currentNode.attributes.getNamedItem (node.name);
}
}
function getOwnerElement (attrNode, contextNode) {
try {
return attrNode.selectSingleNodes ('..');
} catch (e) {
var nodes = contextNode.getElementsByTagName ('*');
var i = 0;
while (nodes[i]) {
var node = nodes[i++];
var attrs = node.attributes;
var j = 0;
while (attrs[j])
if (attrs[j++] == attrNode)
return node;
}
return null;
}
}
function getDocumentPosition (node, contextNode) {
var result = [ ];
var i, dummy;
while (node && node != contextNode) {
i = 0, dummy = node;
while (dummy = dummy.previousSibling)
i++;
result.push (i);
node = node.parentNode;
}
return result;
}
} )();