Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spike: Improve element inspection and implement rudimentary diffing #5

Merged
merged 1 commit into from
Mar 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
221 changes: 205 additions & 16 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
function getAttributes(elm) {
var attrs = elm.attributes;
var arrayChanges = require('array-changes');

function getAttributes(element) {
var attrs = element.attributes;
var result = {};

for (var i = 0; i < attrs.length; i += 1) {
Expand All @@ -9,8 +11,8 @@ function getAttributes(elm) {
return result;
}

function getCanonicalAttributes(elm) {
var attrs = getAttributes(elm);
function getCanonicalAttributes(element) {
var attrs = getAttributes(element);
var result = {};

Object.keys(attrs).sort().forEach(function (key) {
Expand Down Expand Up @@ -44,11 +46,10 @@ function isBooleanAttribute(attrName, attrValue) {
}
}


function stringifyElement(elm) {
var elementName = elm.nodeName.toLowerCase();
function stringifyStartTag(element) {
var elementName = element.nodeName.toLowerCase();
var str = '<' + elementName;
var attrs = getCanonicalAttributes(elm);
var attrs = getCanonicalAttributes(element);

Object.keys(attrs).forEach(function (key) {
if (isBooleanAttribute(key)) {
Expand All @@ -59,11 +60,22 @@ function stringifyElement(elm) {
});

str += '>';
return str;
}

function stringifyEndTag(element) {
var elementName = element.nodeName.toLowerCase();
return '</' + elementName + '>';
}

function stringifyElement(element) {
var elementName = element.nodeName.toLowerCase();
var str = stringifyStartTag(element);
if (!isVoidElement(elementName)) {
if (elm.children.length > 0) {
if (element.children.length > 0) {
str += '...';
}
str += '</' + elementName + '>';
str += stringifyEndTag(element);
}
return str;
}
Expand Down Expand Up @@ -97,6 +109,53 @@ module.exports = {
},
inspect: function (element, depth, output) {
return output.code('<!--' + element.nodeValue + '-->', 'html');
},
diff: function (actual, expected, output, diff, inspect, equal) {
var d = diff('<!--' + actual.nodeValue + '-->', '<!--' + expected.nodeValue + '-->');
d.inline = true;
return d;
}
});

expect.addType({
name: 'DOMTextNode',
base: 'DOMNode',
identify: function (obj) {
return obj && obj.nodeType === 3;
},
equal: function (a, b) {
return a.nodeValue.trim() === b.nodeValue.trim();
},
inspect: function (element, depth, output) {
return output.code(element.nodeValue.trim().replace(/</g, '&lt;'), 'html');
},
diff: function (actual, expected, output, diff, inspect, equal) {
var d = diff(actual.nodeValue, expected.nodeValue);
d.inline = true;
return d;
}
});

expect.addType({
name: 'DOMNodeList',
base: 'array-like',
prefix: function (output) {
return output.text('NodeList[');
},
suffix: function (output) {
return output.text(']');
},
delimiter: function (output) {
return output.text('delimiter');
},
identify: function (obj) {
return (
obj &&
typeof obj.length === 'number' &&
typeof obj.toString === 'function' &&
typeof obj.item === 'function' &&
obj.toString().indexOf('NodeList') !== -1
);
}
});

Expand All @@ -114,14 +173,144 @@ module.exports = {
identify: function (obj) {
return obj && obj.nodeType === 1 && obj.nodeName && obj.attributes && obj.outerHTML;
},
equal: function (a, b) {
return stringifyElement(a) === stringifyElement(b);
equal: function (a, b, equal) {
return a.nodeName.toLowerCase() === b.nodeName.toLowerCase() && equal(getAttributes(a), getAttributes(b)) && equal(a.childNodes, b.childNodes);
},
inspect: function (element, depth, output) {
return output.code(stringifyElement(element), 'html');
inspect: function (element, depth, output, inspect) {
var elementName = element.nodeName.toLowerCase();
var startTag = '<' + elementName;
var attrs = getCanonicalAttributes(element);

Object.keys(attrs).forEach(function (key) {
if (isBooleanAttribute(key)) {
startTag += ' ' + key;
} else {
startTag += ' ' + key + '="' + attrs[key].replace(/&/g, '&amp;').replace(/"/g, '&quot;') + '"';
}
});

var inspectedChildren = [];
for (var i = 0 ; i < element.childNodes.length ; i += 1) {
inspectedChildren.push(inspect(element.childNodes[i]));
}

var width = 0;
var multipleLines = inspectedChildren.some(function (o) {
var size = o.size();
width += size.width;
return width > 50 || o.height > 1;
});

startTag += '>';
output.code(startTag, 'html');
if (element.childNodes.length > 0) {

if (multipleLines) {
output.nl().indentLines();

inspectedChildren.forEach(function (inspectedChild, index) {
output.i().block(inspectedChild).nl();
});

output.outdentLines();
} else {
inspectedChildren.forEach(function (inspectedChild, index) {
output.append(inspectedChild);
});
}
}
if (!isVoidElement(elementName) || element.childNodes.length > 0) {
output.code('</' + elementName + '>', 'html');
}
return output;
},
diff: function (actual, expected, output, diff, inspect) {
return diff(stringifyElement(actual), stringifyElement(expected));
diffLimit: 512,
diff: function (actual, expected, output, diff, inspect, equal) {
var result = {
diff: output,
inline: true
};

if (Math.max(actual.length, expected.length) > this.diffLimit) {
result.diff.jsComment('Diff suppressed due to size > ' + this.diffLimit);
return result;
}

if (actual.nodeName.toLowerCase() === expected.nodeName.toLowerCase() && equal(getAttributes(actual), getAttributes(expected))) {
output.code(stringifyStartTag(actual), 'html');
} else {
output.append(diff(stringifyStartTag(actual), stringifyStartTag(expected)).diff).nl();
}

if (actual.childNodes.length > 0 || expected.childNodes.length > 0) {
var changes = arrayChanges(Array.prototype.slice.call(actual.childNodes), Array.prototype.slice.call(expected.childNodes), equal, function (a, b) {
// Figure out whether a and b are "struturally similar" so they can be diffed inline.
// TODO: Consider similarity of the child nodes
if (a.nodeType === 1 && b.nodeType === 1 && a.nodeName === b.nodeName) {
var aAttributes = getAttributes(a);
var bAttributes = getAttributes(b);
var aAttributeNames = Object.keys(aAttributes);
var bAttributeNames = Object.keys(bAttributes);
if (aAttributeNames.length === 0 && bAttributeNames.length === 0) {
return true;
}
var numberOfSimilarAttributes = 0;
var requiredSimilarAttributes = Math.round(Math.max(aAttributeNames.length, bAttributeNames.length) / 2);
return aAttributeNames.concat(bAttributeNames).some(function (attributeName) {
if (attributeName in aAttributes && attributeName in bAttributes) {
numberOfSimilarAttributes += 1;
}
return numberOfSimilarAttributes >= requiredSimilarAttributes;
});
} else {
return false;
}

});
output.nl().indentLines();

changes.forEach(function (diffItem, index) {
output.i().block(function () {
var type = diffItem.type;
var last = !!diffItem.last;

if (type === 'insert') {
this.annotationBlock(function () {
this.error('missing ').block(inspect(diffItem.value));
});
} else if (type === 'remove') {
this.block(inspect(diffItem.value).sp().error('// should be removed'));
} else if (type === 'equal') {
this.block(inspect(diffItem.value));
} else {
var valueDiff = diff(diffItem.value, diffItem.expected);
if (valueDiff && valueDiff.inline) {
this.block(valueDiff.diff);
} else if (valueDiff) {
this.block(inspect(diffItem.value).sp()).annotationBlock(function () {
this.shouldEqualError(diffItem.expected, inspect).nl().append(valueDiff.diff);
});
} else {
this.block(inspect(diffItem.value).sp()).annotationBlock(function () {
this.shouldEqualError(diffItem.expected, inspect);
});
}
}
}).nl();
});
output.outdentLines();
}

if (isVoidElement(actual.nodeName) && actual.childNodes.length === 0 && expected.childNodes.length === 0) {
} else {
if (actual.nodeName.toLowerCase() === expected.nodeName.toLowerCase()) {
output.code(stringifyEndTag(actual), 'html');
} else {
output.append(diff(stringifyEndTag(actual), stringifyEndTag(expected)).diff);
}
}

return result;
}
});

Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,8 @@
"mocha": "^2.1.0",
"mocha-lcov-reporter": "0.0.2",
"unexpected": "^6.0.7"
},
"dependencies": {
"array-changes": "1.0.0"
}
}
Loading