aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/PuerkitoBio/goquery/property_test.go
blob: 1095dcc8994ab1bf0f70078f6cffc8622a505d13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package goquery

import (
	"regexp"
	"strings"
	"testing"
)

func TestAttrExists(t *testing.T) {
	if val, ok := Doc().Find("a").Attr("href"); !ok {
		t.Error("Expected a value for the href attribute.")
	} else {
		t.Logf("Href of first anchor: %v.", val)
	}
}

func TestAttrOr(t *testing.T) {
	if val := Doc().Find("a").AttrOr("fake-attribute", "alternative"); val != "alternative" {
		t.Error("Expected an alternative value for 'fake-attribute' attribute.")
	} else {
		t.Logf("Value returned for not existing attribute: %v.", val)
	}
	if val := Doc().Find("zz").AttrOr("fake-attribute", "alternative"); val != "alternative" {
		t.Error("Expected an alternative value for 'fake-attribute' on an empty selection.")
	} else {
		t.Logf("Value returned for empty selection: %v.", val)
	}
}

func TestAttrNotExist(t *testing.T) {
	if val, ok := Doc().Find("div.row-fluid").Attr("href"); ok {
		t.Errorf("Expected no value for the href attribute, got %v.", val)
	}
}

func TestRemoveAttr(t *testing.T) {
	sel := Doc2Clone().Find("div")

	sel.RemoveAttr("id")

	_, ok := sel.Attr("id")
	if ok {
		t.Error("Expected there to be no id attributes set")
	}
}

func TestSetAttr(t *testing.T) {
	sel := Doc2Clone().Find("#main")

	sel.SetAttr("id", "not-main")

	val, ok := sel.Attr("id")
	if !ok {
		t.Error("Expected an id attribute on main")
	}

	if val != "not-main" {
		t.Errorf("Expected an attribute id to be not-main, got %s", val)
	}
}

func TestSetAttr2(t *testing.T) {
	sel := Doc2Clone().Find("#main")

	sel.SetAttr("foo", "bar")

	val, ok := sel.Attr("foo")
	if !ok {
		t.Error("Expected an 'foo' attribute on main")
	}

	if val != "bar" {
		t.Errorf("Expected an attribute 'foo' to be 'bar', got '%s'", val)
	}
}

func TestText(t *testing.T) {
	txt := Doc().Find("h1").Text()
	if strings.Trim(txt, " \n\r\t") != "Provok.in" {
		t.Errorf("Expected text to be Provok.in, found %s.", txt)
	}
}

func TestText2(t *testing.T) {
	txt := Doc().Find(".hero-unit .container-fluid .row-fluid:nth-child(1)").Text()
	if ok, e := regexp.MatchString(`^\s+Provok\.in\s+Prove your point.\s+$`, txt); !ok || e != nil {
		t.Errorf("Expected text to be Provok.in Prove your point., found %s.", txt)
		if e != nil {
			t.Logf("Error: %s.", e.Error())
		}
	}
}

func TestText3(t *testing.T) {
	txt := Doc().Find(".pvk-gutter").First().Text()
	// There's an   character in there...
	if ok, e := regexp.MatchString(`^[\s\x{00A0}]+$`, txt); !ok || e != nil {
		t.Errorf("Expected spaces, found <%v>.", txt)
		if e != nil {
			t.Logf("Error: %s.", e.Error())
		}
	}
}

func TestHtml(t *testing.T) {
	txt, e := Doc().Find("h1").Html()
	if e != nil {
		t.Errorf("Error: %s.", e)
	}

	if ok, e := regexp.MatchString(`^\s*<a href="/">Provok<span class="green">\.</span><span class="red">i</span>n</a>\s*$`, txt); !ok || e != nil {
		t.Errorf("Unexpected HTML content, found %s.", txt)
		if e != nil {
			t.Logf("Error: %s.", e.Error())
		}
	}
}

func TestNbsp(t *testing.T) {
	src := `<p>Some&nbsp;text</p>`
	d, err := NewDocumentFromReader(strings.NewReader(src))
	if err != nil {
		t.Fatal(err)
	}
	txt := d.Find("p").Text()
	ix := strings.Index(txt, "\u00a0")
	if ix != 4 {
		t.Errorf("Text: expected a non-breaking space at index 4, got %d", ix)
	}

	h, err := d.Find("p").Html()
	if err != nil {
		t.Fatal(err)
	}
	ix = strings.Index(h, "\u00a0")
	if ix != 4 {
		t.Errorf("Html: expected a non-breaking space at index 4, got %d", ix)
	}
}

func TestAddClass(t *testing.T) {
	sel := Doc2Clone().Find("#main")
	sel.AddClass("main main main")

	// Make sure that class was only added once
	if a, ok := sel.Attr("class"); !ok || a != "main" {
		t.Error("Expected #main to have class main")
	}
}

func TestAddClassSimilar(t *testing.T) {
	sel := Doc2Clone().Find("#nf5")
	sel.AddClass("odd")

	assertClass(t, sel, "odd")
	assertClass(t, sel, "odder")
	printSel(t, sel.Parent())
}

func TestAddEmptyClass(t *testing.T) {
	sel := Doc2Clone().Find("#main")
	sel.AddClass("")

	// Make sure that class was only added once
	if a, ok := sel.Attr("class"); ok {
		t.Errorf("Expected #main to not to have a class, have: %s", a)
	}
}

func TestAddClasses(t *testing.T) {
	sel := Doc2Clone().Find("#main")
	sel.AddClass("a b")

	// Make sure that class was only added once
	if !sel.HasClass("a") || !sel.HasClass("b") {
		t.Errorf("#main does not have classes")
	}
}

func TestHasClass(t *testing.T) {
	sel := Doc().Find("div")
	if !sel.HasClass("span12") {
		t.Error("Expected at least one div to have class span12.")
	}
}

func TestHasClassNone(t *testing.T) {
	sel := Doc().Find("h2")
	if sel.HasClass("toto") {
		t.Error("Expected h1 to have no class.")
	}
}

func TestHasClassNotFirst(t *testing.T) {
	sel := Doc().Find(".alert")
	if !sel.HasClass("alert-error") {
		t.Error("Expected .alert to also have class .alert-error.")
	}
}

func TestRemoveClass(t *testing.T) {
	sel := Doc2Clone().Find("#nf1")
	sel.RemoveClass("one row")

	if !sel.HasClass("even") || sel.HasClass("one") || sel.HasClass("row") {
		classes, _ := sel.Attr("class")
		t.Error("Expected #nf1 to have class even, has ", classes)
	}
}

func TestRemoveClassSimilar(t *testing.T) {
	sel := Doc2Clone().Find("#nf5, #nf6")
	assertLength(t, sel.Nodes, 2)

	sel.RemoveClass("odd")
	assertClass(t, sel.Eq(0), "odder")
	printSel(t, sel)
}

func TestRemoveAllClasses(t *testing.T) {
	sel := Doc2Clone().Find("#nf1")
	sel.RemoveClass()

	if a, ok := sel.Attr("class"); ok {
		t.Error("All classes were not removed, has ", a)
	}

	sel = Doc2Clone().Find("#main")
	sel.RemoveClass()
	if a, ok := sel.Attr("class"); ok {
		t.Error("All classes were not removed, has ", a)
	}
}

func TestToggleClass(t *testing.T) {
	sel := Doc2Clone().Find("#nf1")

	sel.ToggleClass("one")
	if sel.HasClass("one") {
		t.Error("Expected #nf1 to not have class one")
	}

	sel.ToggleClass("one")
	if !sel.HasClass("one") {
		t.Error("Expected #nf1 to have class one")
	}

	sel.ToggleClass("one even row")
	if a, ok := sel.Attr("class"); ok {
		t.Errorf("Expected #nf1 to have no classes, have %q", a)
	}
}