-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathindex.js
More file actions
71 lines (61 loc) · 1.86 KB
/
index.js
File metadata and controls
71 lines (61 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
var _ = require('lodash'),
HtmlDiff = require('./HtmlDiff'),
modifyHtmlAccordingToOptions = require('./utils/modify'),
defaults = require('./defaults'),
handleMasks = require('./utils/mask');
/**
* Tokenizes the given HTML
* @param {String} html
* @returns {Array}
*/
HtmlDiff.prototype.tokenize = function (html) {
html = modifyHtmlAccordingToOptions(html, this.options);
return _.filter(html.split(/({{.+?}}(?!})|[{}\(\)\[\]#\*`=:;,.<>"'\/]|\s+)/));
};
/**
* @class HtmlDiffer
* @constructor
* @param {Object} [options]
* @param {String[]} [options.ignoreAttributes]
* @param {String[]} [options.compareAttributesAsJSON]
* @param {Boolean} [options.ignoreWhitespaces=true]
* @param {Boolean} [options.ignoreComments=true]
* @param {Boolean} [options.ignoreEndTags=false]
* @param {Boolean} [options.ignoreDuplicateAttributes=false]
* @param {Boolean} [options.ignoreEmptyAttributes=false]
*/
var HtmlDiffer = function (options) {
options = defaults(options);
this.options = options;
};
/**
* Returns the diff between two given chunks of HTML
* @class HtmlDiffer
* @method
* @param {String} html1
* @param {String} html2
* @returns {Diff}
*/
HtmlDiffer.prototype.diffHtml = function (html1, html2) {
var htmlDiffer = new HtmlDiff(this.options),
diff = htmlDiffer.diff(html1, html2);
return handleMasks(diff);
};
/**
* Compares two given chunks of HTML
* @class HtmlDiffer
* @method
* @param {String} html1
* @param {String} html2
* @returns {Boolean}
*/
HtmlDiffer.prototype.isEqual = function (html1, html2) {
var diff = this.diffHtml(html1, html2);
return (diff.length === 1 && !diff[0].added && !diff[0].removed);
};
var htmlDiffer = new HtmlDiffer();
module.exports = {
HtmlDiffer: HtmlDiffer,
diffHtml: htmlDiffer.diffHtml.bind(htmlDiffer),
isEqual: htmlDiffer.isEqual.bind(htmlDiffer)
};