Merge pull request #1459 from dpvc/issue1447

Improve getNode() so that is finds the correct node. #1447
This commit is contained in:
Davide P. Cervone 2016-06-08 06:33:11 -04:00
commit 58e2717d45

View File

@ -336,34 +336,29 @@
ucMatch: HTML.ucMatch, ucMatch: HTML.ucMatch,
setScript: HTML.setScript, setScript: HTML.setScript,
getNodesByClass: (document.getElementsByClassName ? //
function (node,type) {return node.getElementsByClassName(type)} : // Look through the direct children of a node for one with the given
function (node,type) { // type (but if the node has intervening containers for its children,
var NODES = []; // step into them; note that elements corresponding to MathML nodes
var nodes = node.getElementsByTagName("span"); // will have id's so we don't step into them).
var name = RegExp("\\b"+type+"\\b"); //
for (var i = 0, m = nodes.length; i < m; i++) { // This is used by munderover and msubsup to locate their child elements
if (name.test(nodes[i].className)) NODES.push = nodes[i]; // when they are part of an embellished operator that is being stretched.
} // We don't use querySelector because we want to find only the direct child
return NODES; // nodes, not nodes that might be nested deeper in the tree (see issue #1447).
} //
),
getNode: function (node,type) { getNode: function (node,type) {
var nodes = this.getNodesByClass(node,type); while (node && node.childNodes.length === 1 && node.firstChild.id == null)
if (nodes.length === 1) return nodes[0]; node = node.firstChild;
var closest = nodes[0], N = this.getNodeDepth(node,closest); if (node) {
for (var i = 1, m = nodes.length; i < m; i++) { var name = RegExp("\\b"+type+"\\b");
var n = this.getNodeDepth(node,nodes[i]); for (var i = 0, m = node.childNodes.length; i < m; i++) {
if (n < N) {closest = nodes[i]; N = n} var child = node.childNodes[i];
if (name.test(child.className)) return child;
} }
return closest; }
return null;
}, },
getNodeDepth: function (parent,node) {
var n = 0;
while (node && node !== parent) {node = node.parentNode; n++}
return n;
},
/********************************************/ /********************************************/