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

Tweak diff output #6

Merged
merged 2 commits 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
58 changes: 24 additions & 34 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ function stringifyStartTag(element) {

function stringifyEndTag(element) {
var elementName = element.nodeName.toLowerCase();
return '</' + elementName + '>';
if (isVoidElement(elementName) && element.childNodes.length === 0) {
return '';
} else {
return '</' + elementName + '>';
}
}

module.exports = {
Expand Down Expand Up @@ -207,9 +211,7 @@ module.exports = {
});
}
}
if (!isVoidElement(elementName) || element.childNodes.length > 0) {
output.code('</' + elementName + '>', 'html');
}
output.code(stringifyEndTag(element));
return output;
},
diffLimit: 512,
Expand All @@ -224,36 +226,24 @@ module.exports = {
return result;
}

if (actual.nodeName.toLowerCase() === expected.nodeName.toLowerCase() && equal(getAttributes(actual), getAttributes(expected))) {
var emptyElements = actual.childNodes.length === 0 && expected.childNodes.length === 0;
var conflictingElement = actual.nodeName.toLowerCase() !== expected.nodeName.toLowerCase() || !equal(getAttributes(actual), getAttributes(expected));

if (!conflictingElement) {
output.code(stringifyStartTag(actual), 'html');
} else {
output.append(diff(stringifyStartTag(actual), stringifyStartTag(expected)).diff).nl();
} else if (!emptyElements) {
output.append(diff(stringifyStartTag(actual), stringifyStartTag(expected)).diff);
}

if (actual.childNodes.length > 0 || expected.childNodes.length > 0) {
if (!emptyElements) {
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;
}

return (
a.nodeType === 1 && b.nodeType === 1 &&
a.nodeName === b.nodeName
);
});
output.nl().indentLines();

Expand Down Expand Up @@ -289,14 +279,14 @@ module.exports = {
output.outdentLines();
}

if (isVoidElement(actual.nodeName) && actual.childNodes.length === 0 && expected.childNodes.length === 0) {
return result;
}

if (actual.nodeName.toLowerCase() === expected.nodeName.toLowerCase()) {
output.code(stringifyEndTag(actual), 'html');
if (emptyElements && conflictingElement) {
output.append(diff(stringifyStartTag(actual) + stringifyEndTag(actual), stringifyStartTag(expected) + stringifyEndTag(expected)).diff);
} else {
output.append(diff(stringifyEndTag(actual), stringifyEndTag(expected)).diff);
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@
"unexpected": "^6.0.7"
},
"dependencies": {
"array-changes": "1.0.0"
"array-changes": "1.0.1"
}
}
40 changes: 37 additions & 3 deletions test/unexpected-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,8 @@ describe('unexpected-dom', function () {
'to equal',
'<div>\n' +
' <div id="foo"></div>\n' +
' -<div id="bar">\n' +
' +<div id="quux">\n' +
' </div>\n' +
' -<div id="bar"></div>\n' +
' +<div id="quux"></div>\n' +
'</div>');
});

Expand Down Expand Up @@ -319,5 +318,40 @@ describe('unexpected-dom', function () {
' <!--bar-->\n' +
'</div>');
});

it('should produce a nested diff', function () {
expect(
'<div>foo<span id="foo" class="bar"><span>foo</span></span><!--bar--></div>',
'diffed with',
'<div>foo<span><span>bar</span></span><!--bar--></div>',
'to equal',
'<div>\n' +
' foo\n' +
' -<span class="bar" id="foo">\n' +
' +<span>\n' +
' <span>\n' +
' -foo\n' +
' +bar\n' +
' </span>\n' +
' </span>\n' +
' <!--bar-->\n' +
'</div>');
});

it('should produce a nested diff', function () {
expect(
'<div>foo<span><i></i></span><!--bar--></div>',
'diffed with',
'<div>foo<span><span></span></span><!--bar--></div>',
'to equal',
'<div>\n' +
' foo\n' +
' <span>\n' +
' -<i></i>\n' +
' +<span></span>\n' +
' </span>\n' +
' <!--bar-->\n' +
'</div>');
});
});
});