github.com/elliott5/community@v0.14.1-0.20160709191136-823126fb026a/wordsmith/utility/html_test.go (about) 1 // Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved. 2 // 3 // This software (Documize Community Edition) is licensed under 4 // GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html 5 // 6 // You can operate outside the AGPL restrictions by purchasing 7 // Documize Enterprise Edition and obtaining a commercial license 8 // by contacting <sales@documize.com>. 9 // 10 // https://documize.com 11 12 package utility 13 14 import "testing" 15 16 func TestHTML(t *testing.T) { 17 type testConv struct { 18 htm, txt string 19 istest bool 20 } 21 convTest := []testConv{ 22 { 23 `<html><head><title>HTML TITLE</title></head><body><p>This <I>is</I>:</p><ul><li><a href="foo">Example</a><li><a href="/bar/baz">HTML text.</a><div class="documize-math">exclueded</div></ul></body></html>`, 24 "This is : Example HTML text. ", false, 25 }, 26 { 27 `<p>This is:</p><ul><li><documize type="field-start"></documize> <documize type="field-end"></documize><documize type="unknown"></documize><li><a href="/bar/baz">HTML text.</a></ul>`, 28 "This is: [ ] [ ] HTML text. ", true, 29 }, 30 } 31 for _, tst := range convTest { 32 var ch HTML 33 ch = HTML([]byte(tst.htm)) 34 //t.Logf("HTML: %s", ch) 35 txt, err := ch.Text(tst.istest) 36 if err != nil { 37 t.Log(err) 38 t.Fail() 39 } 40 expected := compressSpaces(tst.txt) 41 got := compressSpaces(string(txt)) 42 if expected != got { 43 t.Errorf("Conversion to text for `%s`, expected: `%s` got: `%s`\n", 44 ch, expected, got) 45 } //else { 46 // t.Logf("Text: %s", txt) 47 //} 48 } 49 } 50 51 func compressSpaces(s string) string { 52 ret := "" 53 inSpace := false 54 for _, r := range s { 55 switch r { 56 case ' ', '\t', '\n', '\u200b' /*zero width space*/ : 57 if !inSpace { 58 ret += " " 59 } 60 inSpace = true 61 default: 62 inSpace = false 63 ret += string(r) 64 } 65 } 66 return ret 67 } 68 69 func TestHTMLescape(t *testing.T) { 70 tianchao := "兲朝 test" 71 expected := "兲朝 test" 72 73 gotString := EscapeHTMLcomplexChars(tianchao) 74 if gotString != expected { 75 t.Errorf("EscapeHTMLcomplexChars error got `%s` expected `%s`\n", gotString, expected) 76 } 77 78 gotBytes := EscapeHTMLcomplexCharsByte([]byte(tianchao)) 79 if string(gotBytes) != expected { 80 t.Errorf("EscapeHTMLcomplexCharsByte error got `%s` expected `%s`\n", string(gotBytes), expected) 81 } 82 83 }