Skip to content

Commit 2d50b19

Browse files
author
Bill Long
committed
Don't allow <ins> or <del> blocks to include other html tags
1 parent 95894c9 commit 2d50b19

3 files changed

Lines changed: 84 additions & 19 deletions

File tree

angular-rich-text-diff.js

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,52 @@ var AngularRichTextDiff;
2727
this.dmp.diff_cleanupSemantic(diffs);
2828
var diffOutput = '';
2929
for (var x = 0; x < diffs.length; x++) {
30-
var outputString = this.convertDiffableBackToHtml(diffs[x][1]);
31-
if (diffs[x][0] === 1) {
32-
// This is an add
33-
diffOutput += '<ins>' + outputString + '</ins>';
34-
} else if (diffs[x][0] === -1) {
35-
// This is a delete
36-
diffOutput += '<del>' + outputString + '</del>';
30+
diffs[x][1] = this.insertTagsForOperation(diffs[x][1], diffs[x][0]);
31+
diffOutput += this.convertDiffableBackToHtml(diffs[x][1]);
32+
}
33+
34+
this.$scope.diffOutput = this.$sce.trustAsHtml(diffOutput);
35+
};
36+
37+
RichTextDiffController.prototype.insertTagsForOperation = function (diffableString, operation) {
38+
var openTag = '';
39+
var closeTag = '';
40+
if (operation === 1) {
41+
openTag = '<ins>';
42+
closeTag = '</ins>';
43+
} else if (operation === -1) {
44+
openTag = '<del>';
45+
closeTag = '</del>';
46+
} else {
47+
return diffableString;
48+
}
49+
50+
var outputString = openTag;
51+
var isOpen = true;
52+
for (var x = 0; x < diffableString.length; x++) {
53+
if (diffableString.charCodeAt(x) < this.unicodeRangeStart) {
54+
// We just hit a regular character. If tag is not open, open it.
55+
if (!isOpen) {
56+
outputString += openTag;
57+
isOpen = true;
58+
}
59+
60+
outputString += diffableString[x];
3761
} else {
38-
diffOutput += outputString;
62+
// We just hit one of our mapped unicode characters. Close our tag.
63+
if (isOpen) {
64+
outputString += closeTag;
65+
isOpen = false;
66+
}
67+
68+
outputString += diffableString[x];
3969
}
4070
}
4171

42-
this.$scope.diffOutput = this.$sce.trustAsHtml(diffOutput);
72+
if (isOpen)
73+
outputString += closeTag;
74+
75+
return outputString;
4376
};
4477

4578
RichTextDiffController.prototype.convertHtmlToDiffableString = function (htmlString) {

angular-rich-text-diff.ts

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,51 @@ module AngularRichTextDiff {
3838
this.dmp.diff_cleanupSemantic(diffs);
3939
var diffOutput = '';
4040
for (var x = 0; x < diffs.length; x++) {
41-
var outputString = this.convertDiffableBackToHtml(diffs[x][1]);
42-
if (diffs[x][0] === 1) {
43-
// This is an add
44-
diffOutput += '<ins>' + outputString + '</ins>';
45-
} else if (diffs[x][0] === -1) {
46-
// This is a delete
47-
diffOutput += '<del>' + outputString + '</del>';
41+
diffs[x][1] = this.insertTagsForOperation(diffs[x][1], diffs[x][0]);
42+
diffOutput += this.convertDiffableBackToHtml(diffs[x][1]);
43+
}
44+
45+
this.$scope.diffOutput = this.$sce.trustAsHtml(diffOutput);
46+
}
47+
48+
insertTagsForOperation(diffableString: string, operation: number): string {
49+
var openTag = '';
50+
var closeTag = '';
51+
if (operation === 1) {
52+
openTag = '<ins>';
53+
closeTag = '</ins>';
54+
} else if (operation === -1) {
55+
openTag = '<del>';
56+
closeTag = '</del>';
57+
} else {
58+
return diffableString;
59+
}
60+
61+
var outputString = openTag;
62+
var isOpen = true;
63+
for (var x = 0; x < diffableString.length; x++) {
64+
if (diffableString.charCodeAt(x) < this.unicodeRangeStart) {
65+
// We just hit a regular character. If tag is not open, open it.
66+
if (!isOpen) {
67+
outputString += openTag;
68+
isOpen = true;
69+
}
70+
71+
outputString += diffableString[x];
4872
} else {
49-
diffOutput += outputString;
73+
// We just hit one of our mapped unicode characters. Close our tag.
74+
if (isOpen) {
75+
outputString += closeTag;
76+
isOpen = false;
77+
}
78+
79+
outputString += diffableString[x];
5080
}
5181
}
5282

53-
this.$scope.diffOutput = this.$sce.trustAsHtml(diffOutput);
83+
if (isOpen) outputString += closeTag;
84+
85+
return outputString;
5486
}
5587

5688
convertHtmlToDiffableString(htmlString: string): string {

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "angular-rich-text-diff",
33
"main": "angular-rich-text-diff.js",
4-
"version": "0.1.1",
4+
"version": "0.1.2",
55
"authors": [
66
"Bill Long <bill@blackout.us>"
77
],

0 commit comments

Comments
 (0)