Skip to content
Snippets Groups Projects
Commit 89a274bc authored by svornost's avatar svornost
Browse files

Improved robustness of SVG attribute matching

parent e4c6d11f
No related branches found
No related tags found
No related merge requests found
...@@ -98,24 +98,31 @@ App.Art.SvgQueue = class { ...@@ -98,24 +98,31 @@ App.Art.SvgQueue = class {
* @param {NamedNodeMap} right * @param {NamedNodeMap} right
*/ */
function equalAttributes(left, right) { function equalAttributes(left, right) {
/** get all the attribute names from an attribute list and sort them /** get all the attribute names from an attribute list
* @param {NamedNodeMap} attrs * @param {NamedNodeMap} attrs
* @returns {string[]} * @returns {string[]}
*/ */
function sortedAttrNames(attrs) { function attrNames(attrs) {
let names = []; let names = [];
for (let index = 0; index < attrs.length; ++index) { for (let index = 0; index < attrs.length; ++index) {
names.push(attrs[index].nodeName); names.push(attrs[index].nodeName);
} }
names.sort();
return names; return names;
} }
if (typeof(left) !== typeof(right)) { if (!left && !right) {
return false; return true; // both are nullish, treat as equal
} else if (!left || !right) {
return false; // only one is nullish, not equal
} }
const allNames = _.union(sortedAttrNames(left), sortedAttrNames(right));
return allNames.every((attr) => left[attr].nodeValue === right[attr].nodeValue); const leftNames = attrNames(left), rightNames = attrNames(right);
const intersectionLength = _.intersection(leftNames, rightNames).length;
if (leftNames.length !== intersectionLength || rightNames.length !== intersectionLength) {
return false; // contain different attributes, not equal
}
// are all values equal?
return leftNames.every((attr) => left.getNamedItem(attr).nodeValue === right.getNamedItem(attr).nodeValue);
} }
let frag = document.createDocumentFragment(); let frag = document.createDocumentFragment();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment