[英]JavaScript Recursive Node Equivalence Function Not Working
我正在Chrome开发工具中测试以下代码,即使两个节点不相等,它也会一直返回true。 示例节点如下。 只有Div1和Div4相等。 我非常有信心我会递归地接触所有节点,但是我认为我没有使用'if'正确设置变量,或者我可能需要在其他地方放置-有点迷失了,尝试了很多方法。
var htmlStrings = [
'<div id="one">Some<span>node <em>contents</em> for</span>comparison</div>',
'<div id="two">Some<span>node contents for</span>comparison</div>',
'<div id="one">Some<span>node <strong>contents</strong> for</span>comparison</div>',
'<div id="four">Some<span>node <em>contents</em> for</span>comparison</div>'
];
var div1 = document.createElement('div');
div1.innerHTML = htmlStrings[0];
document.body.appendChild(div1);
var div2 = document.createElement('div');
div2.innerHTML = htmlStrings[1];
document.body.appendChild(div2);
var div3 = document.createElement('div');
div3.innerHTML = htmlStrings[2];
document.body.appendChild(div3);
var div4 = document.createElement('div');
div4.innerHTML = htmlStrings[3];
document.body.appendChild(div4);
function nodeEquivalence(node1, node2) {
var passed = false;
if (node1.nodeType === node2.nodeType) {
if ((node1.tagName === node2.tagName || node1.nodeValue === node2.nodeValue)) {
passed = true;
}
}
node1 = node1.firstChild;
node2 = node2.firstChild;
while (node1 && node2) {
nodeEquivalence(node1, node2);
node1 = node1.nextSibling;
node2 = node2.nextSibling;
}
return passed;
}
console.log(nodeEquivalence(div1, div2));
console.log(nodeEquivalence(div1, div4));
这是一个适用于您的测试用例的实现。 这使用递归来测试子树。 它专门编码为处理元素,文档,文档片段,文本节点和注释节点。 可以添加对其他类型节点的支持。
function nodeEquivalence(node1, node2) {
var ch1, ch2, nType;
// OK to have both node1 and node2 not exist, but only if both are missing
if (!node1 || !node2) {
return node1 === node2;
}
// must be same nodeType
ntype = node1.nodeType;
if (ntype !== node2.nodeType) {
return false;
}
// if there is a tagName, must be same tagName
if (node1.tagName && node1.tagName !== node2.tagName) {
return false;
}
switch(ntype) {
// nodes that have children
case 1: // element node
case 9: // document node
case 11: // document fragment node
// check equivalence on each corresponding child
ch1 = node1.firstChild;
ch2 = node2.firstChild;
while (ch1 && ch2) {
if (!nodeEquivalence(ch1, ch2)) {
return false;
}
ch1 = ch1.nextSibling;
ch2 = ch2.nextSibling;
}
return nodeEquivalence(ch1, ch2);
// nodes whose content is nodeValue
case 3: // text node
case 8: // comment node
return node1.nodeValue === node2.nodeValue;
// other types of nodes
default:
throw new Error("unsupported type of node");
break;
}
}
并且,一个有效的演示: http : //jsfiddle.net/jfriend00/nruL6fdy/
您的代码逻辑存在许多问题:
if (node1.tagName === node2.tagName)
逻辑也会通过,因此您永远不会比较nodeValue
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.