===
for example
* this function compares them
*/
function render_equal_components(before, after, ops, class_name, components_search_rule) {
var i, j, r, attrs;
var op;
var amount;
var before_token, after_token;
var before_token_match, after_token_match;
for (i = 0; i < ops.length; i++) {
op = ops[i];
if (op.action === 'equal') {
amount = (op.end_in_before - op.start_in_before) + 1; // +1 since the array is 0 based
for (j = 0; j < amount; j++) {
before_token = before[op.start_in_before + j];
after_token = after[op.start_in_after + j];
if (before_token !== after_token) {
before_token_match = components_search_rule.exec(before_token);
after_token_match = components_search_rule.exec(after_token);
if (before_token_match && after_token_match) {
if (before_token_match[1].toLowerCase() === after_token_match[1].toLowerCase()) {
// compare content of these two same components
r = diff(before_token, after_token, class_name);
after[op.start_in_after + j] = r;
} else {
// components are different, wrap before by 'del' and after by 'ins'
attrs = class_name ? ' class="' + class_name + '"' : '';
r = '' + before_token + '' + '' + after_token + '';
after[op.start_in_after + j] = r;
}
}
}
}
}
}
}
/*
* Compares two pieces of HTML content and returns the combined content with differences
* wrapped in and tags.
*
* @param {string} before The HTML content before the changes.
* @param {string} after The HTML content after the changes.
* @param {string} class_name (Optional) The class attribute to include in and tags.
*
* @return {string} The combined HTML content with differences wrapped in and tags.
*/
function diff(before, after, class_name, components_search_rule) {
if (before === after) return before;
before = html_to_tokens(before, components_search_rule);
after = html_to_tokens(after, components_search_rule);
var ops = calculate_operations(before, after);
if (components_search_rule) {
render_equal_components(before, after, ops, class_name, components_search_rule);
}
return render_operations(before, after, ops, class_name);
}
diff.html_to_tokens = html_to_tokens;
diff.merge_html_by_class = merge_html_by_class;
diff.find_matching_blocks = find_matching_blocks;
find_matching_blocks.find_match = find_match;
find_matching_blocks.create_index = create_index;
find_matching_blocks.get_key_for_token = get_key_for_token;
diff.calculate_operations = calculate_operations;
diff.render_operations = render_operations;
if (typeof define === 'function') {
define([], function () {
return diff;
});
} else if (typeof module !== 'undefined' && module !== null) {
module.exports = diff;
} else {
this.htmldiff = diff;
}
}).call(this);