From d88ca50974d6ac60015ef4447f9924dd980bf8de Mon Sep 17 00:00:00 2001 From: Evgeniy Gavryushin Date: Thu, 24 Jul 2014 00:49:40 +0400 Subject: [PATCH 1/2] Modify 'ignoreWhitespace' option --- lib/index.js | 67 +++++++++++++++++++++++++++++----------------------- lib/utils.js | 2 +- 2 files changed, 39 insertions(+), 30 deletions(-) diff --git a/lib/index.js b/lib/index.js index 6d02858..7741927 100644 --- a/lib/index.js +++ b/lib/index.js @@ -11,13 +11,13 @@ var utils = require('./utils'), * @param {Object} options * @returns {AST} */ -function htmlToAST(HTMLDoc, options) { +function htmlToAST(HTMLDoc) { var parser, parserHandler; parserHandler = new htmlParser.DomHandler(function(err) { if (err) console.log(err); - }, options); + }); parser = new htmlParser.Parser(parserHandler); parser.parseComplete(HTMLDoc); @@ -26,45 +26,54 @@ function htmlToAST(HTMLDoc, options) { } /** - * Recursively sorts arttibutes' values of the AST leafs + * Recursively modifies the tree depending on the given options * @param {AST} tree * @param {Object} options * @returns {AST} */ function modifyASTTree(tree, options) { - _.each(tree, function(node) { - Object.keys(node).forEach(function(leaf) { - if (leaf === 'attribs') { - var attrs = utils.sortObj(node[leaf]); + _.forEach(tree, function(node) { + if (options.ignoreWhitespace && node.type === 'text') { - if (attrs.hasOwnProperty('class')) { - attrs['class'] = utils.sortCssClasses(attrs['class']); - } + node.data = node.data + .replace(/(\n|\r|\t|\v|\f)+/g, '') + .replace(/\s+/g, ' ') + .trim(); - _.each(options.compareHtmlAttrsAsJSON, function(attr) { - var attrValue, - isFunction = (attr === 'onclick' || attr === 'ondblclick'); // @FIXME: should be configurable + return; + } - if (attrs.hasOwnProperty(attr)) { + if (node.hasOwnProperty('attribs')) { + var attrs = utils.sortObj(node['attribs']); - attrValue = utils.parseAttr(attrs[attr].replace(/"/g, '"'), isFunction); - attrValue = utils.sortObj(attrValue); - attrValue = JSON.stringify(attrValue); + if (attrs.hasOwnProperty('class')) { + attrs['class'] = utils.sortCssClasses(attrs['class']); + } - attrs[attr] = (isFunction ? 'return ' : '') + attrValue.replace(/"/g, '"') - } - }); + _.forEach(options.compareHtmlAttrsAsJSON, function(attr) { + var attrValue, + isFunction = (attr === 'onclick' || attr === 'ondblclick'); // @FIXME: should be configurable - _.each(options.ignoreHtmlAttrs, function(attr) { - attrs.hasOwnProperty(attr) && (attrs[attr] = ''); - }); + if (attrs.hasOwnProperty(attr)) { - node[leaf] = attrs; - } - else if (leaf === 'children') { - modifyASTTree(node.children, options); - } - }); + attrValue = utils.parseAttr(attrs[attr].replace(/"/g, '"'), isFunction); + attrValue = utils.sortObj(attrValue); + attrValue = JSON.stringify(attrValue); + + attrs[attr] = (isFunction ? 'return ' : '') + attrValue.replace(/"/g, '"') + } + }); + + _.forEach(options.ignoreHtmlAttrs, function(attr) { + attrs.hasOwnProperty(attr) && (attrs[attr] = ''); + }); + + node['attribs'] = attrs; + } + + if (node.hasOwnProperty('children')) { + modifyASTTree(node.children, options); + } }); return tree; diff --git a/lib/utils.js b/lib/utils.js index 7b5f983..3270128 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -9,7 +9,7 @@ exports.sortObj = function sortObj(obj) { var keys = _.keys(obj).sort(), sortedObj = {}; - _.each(keys, function(key) { + _.forEach(keys, function(key) { var objValue = obj[key]; if(_.isPlainObject(objValue)) { From c955ce57c6fe3cd3b929bba8244dd843c458b14e Mon Sep 17 00:00:00 2001 From: Evgeniy Gavryushin Date: Thu, 24 Jul 2014 00:49:54 +0400 Subject: [PATCH 2/2] Update test --- test/diffHtml.js | 2 +- test/fixtures/10.html | 12 ++++++++++++ test/fixtures/7.html | 1 + test/fixtures/_10.html | 10 ++++++++++ test/fixtures/_7.html | 21 ++++++++++++++------- 5 files changed, 38 insertions(+), 8 deletions(-) create mode 100644 test/fixtures/10.html create mode 100644 test/fixtures/_10.html diff --git a/test/diffHtml.js b/test/diffHtml.js index abf9522..1ef5379 100644 --- a/test/diffHtml.js +++ b/test/diffHtml.js @@ -18,7 +18,7 @@ describe('\'diffHtml\'', function () { files = readFiles('3.html', '_3.html'), res = [ { - value: ' Test ', + value: 'Test', added: undefined, removed: undefined } ]; diff --git a/test/fixtures/10.html b/test/fixtures/10.html new file mode 100644 index 0000000..d41dc5a --- /dev/null +++ b/test/fixtures/10.html @@ -0,0 +1,12 @@ + + + + + + <!-- comments2 --> + + +Text + + + diff --git a/test/fixtures/7.html b/test/fixtures/7.html index 9e58a60..0f45715 100644 --- a/test/fixtures/7.html +++ b/test/fixtures/7.html @@ -1,5 +1,6 @@ +Text Text diff --git a/test/fixtures/_10.html b/test/fixtures/_10.html new file mode 100644 index 0000000..9e58a60 --- /dev/null +++ b/test/fixtures/_10.html @@ -0,0 +1,10 @@ + + + + + + + +Text + + diff --git a/test/fixtures/_7.html b/test/fixtures/_7.html index 4657c8b..ea381b9 100644 --- a/test/fixtures/_7.html +++ b/test/fixtures/_7.html @@ -1,14 +1,21 @@ - + + Text Text + + + - - - - + + -Text - + Text + + + + + +