Comparing version 1.0.0 to 1.1.0
{ | ||
"name": "diff-dom", | ||
"main": "diffDOM.js", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"homepage": "https://github.com/fiduswriter/diffDOM", | ||
@@ -6,0 +6,0 @@ "authors": [ |
100
diffDOM.js
@@ -66,3 +66,55 @@ (function () { | ||
var roughlyEqual = function roughlyEqual(e1, e2, preventRecursion) { | ||
var elementDescriptors = function(el) { | ||
var output = []; | ||
if (el.nodeType == 1) { | ||
output.push(el.tagName); | ||
if (typeof(el.className) == 'string') { | ||
output.push(el.tagName + '.' + el.className.replace(/ /g, '.')); | ||
} | ||
if (el.id) { | ||
output.push(el.tagName + '#' + el.id); | ||
} | ||
} | ||
return output; | ||
} | ||
var findUniqueDescriptors = function(li) { | ||
var uniqueDescriptors = {}; | ||
var duplicateDescriptors = {}; | ||
for (var i = 0; i < li.length; i++) { | ||
var node = li[i]; | ||
var descriptors = elementDescriptors(node); | ||
for (var j = 0; j < descriptors.length; j++) { | ||
var descriptor = descriptors[j]; | ||
var inUnique = descriptor in uniqueDescriptors; | ||
var inDupes = descriptor in duplicateDescriptors; | ||
if (!inUnique && !inDupes) { | ||
uniqueDescriptors[descriptor] = true; | ||
} else if (inUnique) { | ||
delete uniqueDescriptors[descriptor]; | ||
duplicateDescriptors[descriptor] = true; | ||
} | ||
} | ||
} | ||
return uniqueDescriptors; | ||
} | ||
var uniqueInBoth = function(l1, l2) { | ||
var l1Unique = findUniqueDescriptors(l1); | ||
var l2Unique = findUniqueDescriptors(l2); | ||
var inBoth = {}; | ||
var key; | ||
for (key in l1Unique) { | ||
if (l2Unique[key]) { | ||
inBoth[key] = true; | ||
} | ||
} | ||
return inBoth; | ||
} | ||
var roughlyEqual = function roughlyEqual(e1, e2, uniqueDescriptors, sameSiblings, preventRecursion) { | ||
if (!e1 || !e2) return false; | ||
@@ -78,4 +130,17 @@ if (e1.nodeType !== e2.nodeType) return false; | ||
if (e1.nodeName !== e2.nodeName) return false; | ||
if (e1.tagName === e2.tagName) { | ||
if (e1.tagName in uniqueDescriptors) return true | ||
if (e1.id && e1.id === e2.id) { | ||
var idDescriptor = e1.tagName + '#' + e1.id; | ||
if (idDescriptor in uniqueDescriptors) return true; | ||
} | ||
if (e1.className && e1.className === e2.className) { | ||
var classDescriptor = e1.tagName + '.' + e1.className.replace(/ /g, '.'); | ||
if (classDescriptor in uniqueDescriptors) return true; | ||
} | ||
if (sameSiblings) return true; | ||
} | ||
if (e1.childNodes.length !== e2.childNodes.length) return false; | ||
var thesame = true; | ||
var childUniqueDescriptors = uniqueInBoth(e1.childNodes, e2.childNodes); | ||
for (var i = e1.childNodes.length - 1; i >= 0; i--) { | ||
@@ -87,3 +152,3 @@ if (preventRecursion) { | ||
// was not set, we must explicitly force it to true for child iterations. | ||
thesame = thesame && roughlyEqual(e1.childNodes[i], e2.childNodes[i], true); | ||
thesame = thesame && roughlyEqual(e1.childNodes[i], e2.childNodes[i], childUniqueDescriptors, true, true); | ||
} | ||
@@ -213,10 +278,37 @@ } | ||
var matches = [], | ||
a, i, j; | ||
a, i, j, k, l; | ||
for (a = 0; a < len1 + 1; a++) { | ||
matches[a] = []; | ||
} | ||
var uniqueDescriptors = uniqueInBoth(c1, c2); | ||
// If all of the elements are the same tag, id and class, then we can | ||
// consider them roughly the same even if they have a different number of | ||
// children. This will reduce removing and re-adding similar elements. | ||
var subsetsSame = len1 == len2; | ||
if (subsetsSame) { | ||
for (k = 0; k < len1; k++) { | ||
var c1Desc = elementDescriptors(c1[k]); | ||
var c2Desc = elementDescriptors(c2[k]); | ||
if (c1Desc.length != c2Desc.length) { | ||
subsetsSame = false; | ||
break; | ||
} | ||
for (l = 0; l < c1Desc.length; l++) { | ||
if (c1Desc[l] != c2Desc[l]) { | ||
subsetsSame = false; | ||
break; | ||
} | ||
} | ||
if (!subsetsSame) { | ||
break; | ||
} | ||
} | ||
} | ||
// fill the matches with distance values | ||
for (i = 0; i < len1; i++) { | ||
for (j = 0; j < len2; j++) { | ||
if (!marked1[i] && !marked2[j] && roughlyEqual(c1[i], c2[j])) { | ||
if (!marked1[i] && !marked2[j] && roughlyEqual(c1[i], c2[j], uniqueDescriptors, subsetsSame)) { | ||
matches[i + 1][j + 1] = (matches[i][j] ? matches[i][j] + 1 : 1); | ||
@@ -223,0 +315,0 @@ if (matches[i + 1][j + 1] > lcsSize) { |
{ | ||
"name": "diff-dom", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "A diff for DOM elements, as client-side JavaScript code. Gets all modifications, insertions and removals between two DOM fragments.", | ||
@@ -5,0 +5,0 @@ "main": "diffDOM.js", |
@@ -26,3 +26,3 @@ # diffDOM - A JavaScript diffing algorithm for DOM elements | ||
``` | ||
var diffDOM = require("diffDOM"); | ||
var diffDOM = require("diff-dom"); | ||
``` | ||
@@ -29,0 +29,0 @@ |
Sorry, the diff of this file is not supported yet
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
0
0
89627
12
1076