Skip to content

Commit d55f0d8

Browse files
committed
Makes atomic tags configurable via additional parameter of diff function. Adds 'head' and 'style' to default list of atomic tags.
1 parent 73ceb4a commit d55f0d8

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

js/htmldiff.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@
77
* @param {string} className (Optional) The class attribute to include in <ins> and <del> tags.
88
* @param {string} dataPrefix (Optional) The data prefix to use for data attributes. The
99
* operation index data attribute will be named `data-${dataPrefix-}operation-index`.
10+
* @param {string} atomicTags (Optional) List of tag names. The list has to be in the form
11+
* 'tag1|tag2|tag3|...' e. g. 'head|script|style|...'. An atomic tag is one whose child
12+
* nodes should not be compared - the entire tag should be treated as one token. This is
13+
* useful for tags where it does not make sense to insert <ins> and <del> tags.
14+
* If not used, the default list 'iframe|object|math|svg|script|head|style' will be used.
1015
*
1116
* @return {string} The combined HTML content with differences wrapped in <ins> and <del> tags.
1217
*/
13-
declare function diff(before: string, after: string, className?: string, dataPrefix?: string): string;
18+
declare function diff(before: string, after: string, className?: string | null, dataPrefix?: string | null, atomicTags?: string | null): string;
1419
export = diff;

js/htmldiff.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@
6464
return /--\>$/.test(word);
6565
}
6666

67+
/**
68+
* Regular expression to check atomic tags.
69+
* @see function diff.
70+
*/
71+
var atomicTagsRegExp;
72+
// Added head and style (for style tags inside the body)
73+
var defaultAtomicTagsRegExp = new RegExp('^<(iframe|object|math|svg|script|head|style)');
74+
6775
/**
6876
* Checks if the current word is the beginning of an atomic tag. An atomic tag is one whose
6977
* child nodes should not be compared - the entire tag should be treated as one token. This
@@ -75,7 +83,7 @@
7583
* null otherwise
7684
*/
7785
function isStartOfAtomicTag(word){
78-
var result = /^<(iframe|object|math|svg|script)/.exec(word);
86+
var result = atomicTagsRegExp.exec(word);
7987
return result && result[1];
8088
}
8189

@@ -897,12 +905,18 @@
897905
* @param {string} className (Optional) The class attribute to include in <ins> and <del> tags.
898906
* @param {string} dataPrefix (Optional) The data prefix to use for data attributes. The
899907
* operation index data attribute will be named `data-${dataPrefix-}operation-index`.
908+
* @param {string} atomicTags (Optional) List of atomic tag names. The list has to be in the
909+
* form 'tag1|tag2|tag3|...' e. g. 'head|script|style|...'. If not used, the default list
910+
* 'iframe|object|math|svg|script|head|style' will be used.
900911
*
901912
* @return {string} The combined HTML content with differences wrapped in <ins> and <del> tags.
902913
*/
903-
function diff(before, after, className, dataPrefix){
914+
function diff(before, after, className, dataPrefix, atomicTags){
904915
if (before === after) return before;
905916

917+
// Enable user provided atomic tag list.
918+
atomicTags ? (atomicTagsRegExp = new RegExp('^<(' + atomicTags + ')')) : (atomicTagsRegExp = defaultAtomicTagsRegExp);
919+
906920
before = htmlToTokens(before);
907921
after = htmlToTokens(after);
908922
var ops = calculateOperations(before, after);

0 commit comments

Comments
 (0)