Skip to content

Commit 4de7b8b

Browse files
author
Bill Long
committed
Treat nbsp as a tag
1 parent c0ec747 commit 4de7b8b

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

angular-rich-text-diff.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ var AngularRichTextDiff;
1717
});
1818
this.tagMap = {};
1919
this.mapLength = 0;
20+
// Go ahead and map nbsp;
21+
var unicodeCharacter = String.fromCharCode(this.unicodeRangeStart + this.mapLength);
22+
this.tagMap[' '] = unicodeCharacter;
23+
this.tagMap[unicodeCharacter] = ' ';
24+
this.mapLength++;
2025
this.dmp = new diff_match_patch();
2126
this.doDiff();
2227
}
@@ -33,6 +38,14 @@ var AngularRichTextDiff;
3338
this.$scope.diffOutput = this.$sce.trustAsHtml(diffOutput);
3439
};
3540
RichTextDiffController.prototype.insertTagsForOperation = function (diffableString, operation) {
41+
// Don't insert anything if these are all tags
42+
var n = -1;
43+
do {
44+
n++;
45+
} while (diffableString.charCodeAt(n) >= this.unicodeRangeStart);
46+
if (n + 1 >= diffableString.length) {
47+
return diffableString;
48+
}
3649
var openTag = '';
3750
var closeTag = '';
3851
if (operation === 1) {
@@ -71,6 +84,7 @@ var AngularRichTextDiff;
7184
return outputString;
7285
};
7386
RichTextDiffController.prototype.convertHtmlToDiffableString = function (htmlString) {
87+
htmlString = htmlString.replace(/ /g, this.tagMap[' ']);
7488
var diffableString = '';
7589
var offset = 0;
7690
while (offset < htmlString.length) {

angular-rich-text-diff.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,17 @@ module AngularRichTextDiff {
2525
dmp: diff_match_patch;
2626

2727
constructor(public $scope: IRichTextDiffScope, public $sce: ng.ISCEService) {
28-
$scope.$watch('left', () => { this.doDiff(); });
29-
$scope.$watch('right', () => { this.doDiff(); });
28+
$scope.$watch('left',() => { this.doDiff(); });
29+
$scope.$watch('right',() => { this.doDiff(); });
3030
this.tagMap = {};
3131
this.mapLength = 0;
32+
33+
// Go ahead and map nbsp;
34+
var unicodeCharacter = String.fromCharCode(this.unicodeRangeStart + this.mapLength);
35+
this.tagMap['&nbsp;'] = unicodeCharacter;
36+
this.tagMap[unicodeCharacter] = '&nbsp;';
37+
this.mapLength++;
38+
3239
this.dmp = new diff_match_patch();
3340
this.doDiff();
3441
}
@@ -48,6 +55,17 @@ module AngularRichTextDiff {
4855
}
4956

5057
insertTagsForOperation(diffableString: string, operation: number): string {
58+
59+
// Don't insert anything if these are all tags
60+
var n = -1;
61+
do {
62+
n++;
63+
}
64+
while (diffableString.charCodeAt(n) >= this.unicodeRangeStart);
65+
if (n + 1 >= diffableString.length) {
66+
return diffableString;
67+
}
68+
5169
var openTag = '';
5270
var closeTag = '';
5371
if (operation === 1) {
@@ -88,6 +106,7 @@ module AngularRichTextDiff {
88106
}
89107

90108
convertHtmlToDiffableString(htmlString: string): string {
109+
htmlString = htmlString.replace(/&nbsp;/g, this.tagMap['&nbsp;']);
91110
var diffableString = '';
92111

93112
var offset = 0;

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.2.0",
4+
"version": "0.2.1",
55
"authors": [
66
"Bill Long <bill@blackout.us>"
77
],

0 commit comments

Comments
 (0)