Improve getNode() so that it doesn't find elements inside nodes that come from other MathML nodes (those with IDs). Resolves issue #1447.

This commit is contained in:
Davide P. Cervone 2016-04-29 19:02:15 -04:00
parent 288e4f476d
commit 7116cde328

View File

@ -336,34 +336,20 @@
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 children of a node for one with the given type
function (node,type) { // but don't step into child nodes that are from MathML elements
var NODES = []; // themselves (they will have IDs).
var nodes = node.getElementsByTagName("span"); //
var name = RegExp("\\b"+type+"\\b");
for (var i = 0, m = nodes.length; i < m; i++) {
if (name.test(nodes[i].className)) NODES.push = nodes[i];
}
return NODES;
}
),
getNode: function (node,type) { getNode: function (node,type) {
var nodes = this.getNodesByClass(node,type); var name = RegExp("\\b"+type+"\\b");
if (nodes.length === 1) return nodes[0]; for (var i = 0, m = node.childNodes.length; i < m; i++) {
var closest = nodes[0], N = this.getNodeDepth(node,closest); var child = node.childNodes[i];
for (var i = 1, m = nodes.length; i < m; i++) { if (name.test(child.className)) return child;
var n = this.getNodeDepth(node,nodes[i]); if (child.id == null) return this.getNode(child,type);
if (n < N) {closest = nodes[i]; N = n}
} }
return closest;
}, },
getNodeDepth: function (parent,node) {
var n = 0;
while (node && node !== parent) {node = node.parentNode; n++}
return n;
},
/********************************************/ /********************************************/