github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/zhttp/query_test.go (about) 1 package zhttp_test 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/sohaha/zlsgo" 8 "github.com/sohaha/zlsgo/zhttp" 9 ) 10 11 const html = ` 12 <html> 13 <head> 14 <title>Test</title> 15 </head> 16 <body> 17 ok 18 <div id="Red" class="content red">is red box</div> 19 <hr id="HR" />ha 20 <br> 21 <div class="content">is box</div> 22 <div class="content test blue"> 23 24 blue box </div> 25 <div class="content test blue blue2" data-name="TheSmurfs">blue2 box</div> 26 <div class="multiple boxs"> 27 <div id="One" class="content">content:<span>666</span></div> 28 <div id="Tow" name="saiya" class="content tow">div->div.tow<i>M</i></div> 29 <div id="Three">Three</div> 30 <div id="Four">Four</div> 31 <span id="six">666</div> 32 </div> 33 yes 34 </body> 35 </html> 36 ` 37 38 func TestHTMLParse(tt *testing.T) { 39 t := zlsgo.NewTest(tt) 40 41 h, err := zhttp.HTMLParse([]byte("<div></div>")) 42 tt.Log(h, err) 43 44 h, err = zhttp.HTMLParse([]byte("div")) 45 tt.Log(h, err) 46 47 h, err = zhttp.HTMLParse([]byte("<!- ok -->")) 48 tt.Log(h, err) 49 50 h, err = zhttp.HTMLParse([]byte("")) 51 tt.Log(h.Attr("name"), err) 52 53 h, err = zhttp.HTMLParse([]byte("<html><div class='box'>The is HTML</div><div class='red'>Red</div></html>")) 54 t.EqualNil(err) 55 tt.Log(h.Select("div").Text(), h.Select("div").Attr("class"), 56 h.Select("div", map[string]string{"class": "red"}).HTML()) 57 58 h, err = zhttp.HTMLParse([]byte(html)) 59 if err != nil { 60 tt.Fatal(err) 61 } 62 63 tt.Log(len(h.Select("body").FullText(true))) 64 65 t.EqualExit("okhayes", strings.Replace(strings.Replace(h.Select("body").Text(), "\n", "", -1), "\t", "", -1)) 66 t.EqualExit("blue box", h.Find(".blue").Text(true)) 67 68 // does not exist 69 el := h.Select("div", map[string]string{"id": "does not exist"}) 70 tt.Logf("id Not: %s|%s|%s\n", el.Attr("name"), el.Text(), el.HTML()) 71 72 // Tow 73 el = h.Select("div", map[string]string{"id": "Tow"}) 74 tt.Logf("id Tow: %s|%s|%s\n", el.Attr("name"), el.Text(), el.HTML()) 75 t.EqualTrue(el.Exist()) 76 t.EqualExit("Tow", el.Attr("id")) 77 78 // Tow 79 el = h.Select("div", map[string]string{"Id": "Tow"}) 80 tt.Logf("Id Tow: %s|%s|%s\n", el.Attr("name"), el.Text(), el.HTML()) 81 82 // Blue 83 el = h.Select("div", map[string]string{"class": "blue"}) 84 tt.Logf("class Blue: %s|%s|%s\n", el.Attr("data-name"), el.Text(true), 85 el.HTML(true)) 86 87 // Blue2 88 el = h.Select("div", map[string]string{"class": " blue blue2 "}) 89 tt.Logf("class Blue2: %s|%s|%s\n", el.Attr("data-name"), el.Text(true), 90 el.HTML(true)) 91 92 } 93 94 func TestSelectAll(tt *testing.T) { 95 t := zlsgo.NewTest(tt) 96 h, _ := zhttp.HTMLParse([]byte(html)) 97 t.EqualExit(9, len(h.SelectAll("div"))) 98 t.EqualExit(0, len(h.SelectAll("vue"))) 99 } 100 101 func TestSelectParent(tt *testing.T) { 102 t := zlsgo.NewTest(tt) 103 h, _ := zhttp.HTMLParse([]byte(html)) 104 105 span := h.Select("span") 106 tt.Log(span.Exist()) 107 108 multiple := span.SelectParent("div", map[string]string{"class": "multiple"}) 109 tt.Log(multiple.HTML()) 110 tt.Log(multiple) 111 t.EqualTrue(multiple.Exist()) 112 113 parent := span.SelectParent("vue") 114 tt.Log(parent.HTML()) 115 t.EqualTrue(!parent.Exist()) 116 } 117 118 func TestChild(tt *testing.T) { 119 t := zlsgo.NewTest(tt) 120 h, _ := zhttp.HTMLParse([]byte(html)) 121 122 tt.Log(h.Select("no-body").Child()) 123 child := h.Select("body").Child() 124 for i, v := range child { 125 switch i { 126 case 1: 127 t.EqualExit("hr", v.Name()) 128 case 2: 129 t.EqualExit("br", v.Name()) 130 case 5: 131 t.EqualExit("div", v.Name()) 132 tt.Log(v.Text()) 133 t.EqualExit("blue2 box", v.Text()) 134 } 135 } 136 137 h.Select("body").SelectAllChild("").ForEach(func(index int, el zhttp.QueryHTML) bool { 138 t.Equal(child[index], el) 139 return true 140 }) 141 142 h.Select("body").ForEachChild(func(i int, v zhttp.QueryHTML) bool { 143 switch i { 144 case 1: 145 t.EqualExit("hr", v.Name()) 146 case 2: 147 t.EqualExit("br", v.Name()) 148 case 5: 149 t.EqualExit("div", v.Name()) 150 tt.Log(v.Text()) 151 t.EqualExit("blue2 box", v.Text()) 152 } 153 return true 154 }) 155 multiple := h.Select("body").Select("div", map[string]string{"class": "multiple"}) 156 157 t.Equal("", multiple.NthChild(0).Attr("id")) 158 t.Equal("One", multiple.NthChild(1).Attr("id")) 159 t.Equal("Tow", multiple.NthChild(2).Attr("id")) 160 t.Equal("Three", multiple.NthChild(3).Attr("id")) 161 t.Equal("", multiple.NthChild(3).Attr("no-id")) 162 163 span := multiple.SelectChild("span") 164 tt.Log(span.Exist(), span.HTML()) 165 166 span = multiple.Select("div", map[string]string{"class": "content"}).SelectChild("span") 167 t.EqualTrue(span.Exist()) 168 tt.Log(span.HTML()) 169 170 d := h.Select("div", map[string]string{ 171 "class": "multiple", 172 }) 173 tt.Log(d) 174 d.SelectAllChild("div").ForEach(func(index int, el zhttp.QueryHTML) bool { 175 t.Log(el) 176 return true 177 }) 178 } 179 180 func TestFind(tt *testing.T) { 181 t := zlsgo.NewTest(tt) 182 h, _ := zhttp.HTMLParse([]byte(html)) 183 184 t.Equal(h.Find("div#Three").SelectBrother("div"), h.Find("div#Three ~ div")) 185 t.EqualTrue(h.Find("div#Tow.tow").Exist()) 186 t.EqualTrue(h.Find("div.multiple.boxs .tow>i").Exist()) 187 t.EqualTrue(!h.Find(".multiple.boxs > i").Exist()) 188 189 t.Log(h.Select("div", map[string]string{"class": "multiple boxs"}).Select("", map[string]string{"class": "tow"}).SelectChild("i").Exist()) 190 t.Log(h.Select("div", map[string]string{"class": "multiple boxs"}).Select("", map[string]string{"class": "tow"}).SelectChild("i").HTML()) 191 } 192 193 func TestBrother(tt *testing.T) { 194 t := zlsgo.NewTest(tt) 195 t.Log("oik") 196 h, _ := zhttp.HTMLParse([]byte(html)) 197 d := h.Find("#Tow") 198 parent := d.SelectParent("") 199 tt.Log(parent.HTML()) 200 201 b := d.SelectBrother("div") 202 t.EqualTrue(b.Exist()) 203 t.Equal("Three", b.Attr("id")) 204 tt.Log(b.HTML(true)) 205 206 b = b.SelectBrother("div") 207 t.EqualTrue(b.Exist()) 208 t.Equal("Four", b.Attr("id")) 209 tt.Log(b.HTML(true)) 210 211 tt.Log(b) 212 b = b.SelectBrother("div") 213 tt.Log(b) 214 t.EqualTrue(!b.Exist()) 215 216 b = h.Find("#One").SelectBrother("") 217 tt.Log(b.HTML(true)) 218 t.EqualTrue(b.Exist()) 219 }