htmltools_test.go (4038B)
1 package htmltools 2 3 import ( 4 "strings" 5 "testing" 6 7 "golang.org/x/net/html" 8 ) 9 10 func getdoc(source string) *html.Node { 11 doc, err := html.Parse(strings.NewReader(source)) 12 if err != nil || doc == nil || doc.FirstChild == nil { 13 panic(err) 14 } 15 return doc 16 } 17 18 func TestBody(t *testing.T) { 19 body, err := Body(getdoc(`<!DOCTYPE html><html> 20 <head><title>test</title></head> 21 <body>SUCCESS</body></html>`)) 22 if err != nil { 23 t.Fatalf("%v", err) 24 } 25 if body == nil { 26 t.Fatal("body is nil") 27 } 28 if body.Type != html.ElementNode { 29 t.Fatal("body is not an element") 30 } 31 if body.FirstChild == nil { 32 t.Fatal("body has no children") 33 } 34 if body.FirstChild.Data != "SUCCESS" { 35 t.Fatal("could not find body text") 36 } 37 38 body, err = Body(body) 39 if err != ErrNodeIsNotADocumentNode { 40 t.Fatal("no error on invalid node") 41 } 42 if body != nil { 43 t.Fatal("body not nil on invalid node") 44 } 45 } 46 47 func TestFindRecursive(t *testing.T) { 48 doc := getdoc(`<!DOCTYPE html> 49 <div><div><div><p></p></div><p></p></div><p></p></div><p></p>`) 50 ch := FindRecursive(doc, func(n *html.Node) bool { 51 return n.Data == "p" 52 }) 53 c := 0 54 for _ = range ch { 55 c++ 56 } 57 if c != 4 { 58 t.Fatalf("4 nodes expected, found %d", c) 59 } 60 61 doc = getdoc(`<!DOCTYPE html><html> 62 <div><div><div><p>Hello</p></div><p></p></div><p></p></div><p>World</p>`) 63 ch = FindRecursive(doc, MatchNodeTypeFunc(html.TextNode)) 64 c = 0 65 for _ = range ch { 66 c++ 67 } 68 if c != 2 { 69 t.Fatalf("2 nodes expected, found %d", c) 70 } 71 } 72 73 func TestAttr(t *testing.T) { 74 body, err := Body(getdoc(`<!DOCTYPE html> 75 <div id="1"></div> 76 <div id="1"></div> 77 TEST 78 <div ID="1"></div> 79 <div><div id="0"></div></div>`)) 80 if err != nil { 81 t.Fatalf("%v", err) 82 } 83 if body == nil { 84 t.Fatal("body is nil") 85 } 86 topNodes := Children(body) 87 ids, err := Attr([]string{"id"}, topNodes...) 88 if err != nil { 89 t.Fatalf("%v", err) 90 } 91 if len(ids) != 3 { 92 t.Fatalf("3 attributes expected, found %d", len(ids)) 93 } 94 for _, set := range ids { 95 if len(set) != 1 { 96 t.Fatalf("1 attribute in set expected, found %d", len(set)) 97 } 98 if set[0] != "1" { 99 t.Fatal("invalid attribute value") 100 } 101 } 102 103 body, err = Body(getdoc(`<!DOCTYPE html> 104 <div id="1" class="test"></div> 105 <div id="1" data-test="data"></div> 106 <div ID="1"></div>`)) 107 if err != nil { 108 t.Fatalf("%v", err) 109 } 110 if body == nil { 111 t.Fatal("body is nil") 112 } 113 topNodes = Children(body) 114 ids, err = Attr([]string{"id", "data-test", "class"}, topNodes...) 115 if err != nil { 116 t.Fatalf("%v", err) 117 } 118 if len(ids) != 3 { 119 t.Fatalf("3 attributes expected, found %d", len(ids)) 120 } 121 for _, set := range ids { 122 if len(set) != 3 { 123 t.Fatalf("3 attribute in set expected, found %d", len(set)) 124 } 125 if set[0] != "1" { 126 t.Fatal("invalid attribute value") 127 } 128 if set[1] != "" && set[1] != "data" { 129 t.Fatal("invalid attribute value") 130 } 131 if set[2] != "" && set[2] != "test" { 132 t.Fatal("invalid attribute value") 133 } 134 } 135 } 136 137 func TestIndentHeadings(t *testing.T) { 138 body, err := Body(getdoc(`<!DOCTYPE html> 139 <h1></h1>TEST<div></div><h2></h2><h6></h6>`)) 140 if err != nil { 141 t.Fatalf("%v", err) 142 } 143 if body == nil { 144 t.Fatal("body is nil") 145 } 146 topNodes := Children(body) 147 err = IndentHeadings(3, topNodes...) 148 if err != nil { 149 t.Fatalf("%v", err) 150 } 151 for _, n := range topNodes { 152 if n.Type != html.ElementNode { 153 continue 154 } 155 switch n.Data { 156 case "div", "h4", "h5", "h6": 157 default: 158 t.Fatalf("invalid node %s", n.Data) 159 } 160 } 161 162 err = IndentHeadings(-5, topNodes...) 163 if err != nil { 164 t.Fatalf("%v", err) 165 } 166 for _, n := range topNodes { 167 if n.Type != html.ElementNode { 168 continue 169 } 170 switch n.Data { 171 case "div", "h1", "h2": 172 default: 173 t.Fatalf("invalid node %s", n.Data) 174 } 175 } 176 } 177 178 func TestUnwrap(t *testing.T) { 179 body, err := Body(getdoc(`<!DOCTYPE html> 180 <div>TEST<!--TEST--></div>`)) 181 if err != nil { 182 t.Fatalf("%v", err) 183 } 184 if body == nil { 185 t.Fatal("body is nil") 186 } 187 err = Unwrap(body.FirstChild) 188 if err != nil { 189 t.Fatalf("%v", err) 190 } 191 topNodes := Children(body) 192 if len(topNodes) != 2 { 193 t.Fatalf("2 nodes expected, found %d", len(topNodes)) 194 } 195 }