Merge pull request #12 from goojba/utf8bug
Use []rune in diff internals
diff --git a/diffmatchpatch/dmp.go b/diffmatchpatch/dmp.go
index 24e956d..dba0133 100644
--- a/diffmatchpatch/dmp.go
+++ b/diffmatchpatch/dmp.go
@@ -20,6 +20,7 @@
"bytes"
"errors"
"fmt"
+ "html"
"math"
"net/url"
"regexp"
@@ -1294,34 +1295,30 @@
return last_chars2 + (loc - last_chars1)
}
-/**
- * Convert a Diff list into a pretty HTML report.
- * @param diffs List of Diff objects.
- * @return HTML representation.
- */
-/*
- func (dmp *DiffMatchPatch) diffPrettyHtml(diffs []Diff) {
- StringBuilder html = new StringBuilder()
- foreach (Diff aDiff in diffs) {
- text := aDiff.text.Replace("&", "&").Replace("<", "<")
- .Replace(">", ">").Replace("\n", "¶<br>")
- switch (aDiff.Type) {
- case DiffInsert:
- html.Append("<ins style=\"background:#e6ffe6;\">").Append(text)
- .Append("</ins>")
- break
- case DiffDelete:
- html.Append("<del style=\"background:#ffe6e6;\">").Append(text)
- .Append("</del>")
- break
- case DiffEqual:
- html.Append("<span>").Append(text).Append("</span>")
- break
- }
- }
- return html.ToString()
- }
-*/
+// DiffPrettyHtml converts a []Diff into a pretty HTML report.
+// It is intended as an example from which to write one's own
+// display functions.
+func (dmp *DiffMatchPatch) DiffPrettyHtml(diffs []Diff) string {
+ var buff bytes.Buffer
+ for _, diff := range diffs {
+ text := strings.Replace(html.EscapeString(diff.Text), "\n", "¶<br>", -1)
+ switch diff.Type {
+ case DiffInsert:
+ buff.WriteString("<ins style=\"background:#e6ffe6;\">")
+ buff.WriteString(text)
+ buff.WriteString("</ins>")
+ case DiffDelete:
+ buff.WriteString("<del style=\"background:#ffe6e6;\">")
+ buff.WriteString(text)
+ buff.WriteString("</del>")
+ case DiffEqual:
+ buff.WriteString("<span>")
+ buff.WriteString(text)
+ buff.WriteString("</span>")
+ }
+ }
+ return buff.String()
+}
// Diff_text1 computes and returns the source text (all equalities and deletions).
func (dmp *DiffMatchPatch) DiffText1(diffs []Diff) string {
diff --git a/diffmatchpatch/dmp_test.go b/diffmatchpatch/dmp_test.go
index cf83344..a6af9ae 100644
--- a/diffmatchpatch/dmp_test.go
+++ b/diffmatchpatch/dmp_test.go
@@ -681,17 +681,16 @@
dmp.DiffEditCost = 4
}
-/*
func Test_diffPrettyHtml(t *testing.T) {
- dmp := New()
- // Pretty print.
- diffs := []Diff{
- Diff{DiffEqual, "a\n"},
- Diff{DiffDelete, "<B>b</B>"},
- Diff{DiffInsert, "c&d"}}
- assert.Equal(t, "<span>a¶<br></span><del style=\"background:#ffe6e6;\"><B>b</B></del><ins style=\"background:#e6ffe6;\">c&d</ins>",
- dmp.DiffPrettyHtml(diffs))
-}*/
+ dmp := New()
+ // Pretty print.
+ diffs := []Diff{
+ Diff{DiffEqual, "a\n"},
+ Diff{DiffDelete, "<B>b</B>"},
+ Diff{DiffInsert, "c&d"}}
+ assert.Equal(t, "<span>a¶<br></span><del style=\"background:#ffe6e6;\"><B>b</B></del><ins style=\"background:#e6ffe6;\">c&d</ins>",
+ dmp.DiffPrettyHtml(diffs))
+}
func Test_diffText(t *testing.T) {
dmp := New()