-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelement.go
109 lines (98 loc) · 4.43 KB
/
element.go
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
package goDom
type (
attributes map[string]string
classList []string
children []*Element
// DOM element with fields and element's DOM API.
Element struct {
TagName string // https://developer.mozilla.org/en-US/docs/Web/API/Element/tagName
TextContent string // https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent
Attributes attributes // https://developer.mozilla.org/en-US/docs/Web/API/Element/attributes
Children children // https://developer.mozilla.org/en-US/docs/Web/API/Element/children
ClassName string // https://developer.mozilla.org/en-US/docs/Web/API/Element/className
ClassList classList // https://developer.mozilla.org/en-US/docs/Web/API/Element/classList
Id string // https://developer.mozilla.org/en-US/docs/Web/API/Element/id
ParentElement *Element // https://developer.mozilla.org/en-US/docs/Web/API/Node/parentElement
NextElementSibling *Element // https://developer.mozilla.org/en-US/docs/Web/API/Element/nextElementSibling
PreviousElementSibling *Element // https://developer.mozilla.org/en-US/docs/Web/API/Element/previousElementSibling
domAPI
}
)
// Get HTML attribute.
// https://developer.mozilla.org/en-US/docs/Web/API/Element/getAttribute
//
// Examples:
//
// hrefAttr, err := yourElement.GetAttribute("href") // existed attribute value or err if not
func (e *Element) GetAttribute(attr string) (string, error) {
if val, ok := e.Attributes[attr]; ok {
return val, nil
}
return "", notFoundErr{Params: attr, Msg: "Attribute not found."}
}
// Element has HTML attribute.
// https://developer.mozilla.org/en-US/docs/Web/API/Element/hasAttribute
//
// Examples:
//
// hasHrefAttr := yourElement.HasAttribute("href") // true if attribute existed
func (e *Element) HasAttribute(attr string) bool {
_, err := e.GetAttribute(attr)
return err == nil
}
// Find element by query selector inside element. Exactly as yourElement.querySelector() in browser DOM.
// https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector
//
// Examples:
//
// element, err := yourElement.QuerySelector("div#lal .lol") // get element inside another element. err if not found
func (e *Element) QuerySelector(queryStr string) (*Element, error) {
return e.querySelector(queryStr, e)
}
// Find elements by query selector inside element. Exactly as yourElement.querySelectorAll() in browser DOM.
// https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll
//
// Examples:
//
// elements, err := yourElement.QuerySelector("#my_lol .lolipop") // get elements inside another element. err if not found
func (e *Element) QuerySelectorAll(queryStr string) ([]*Element, error) {
return e.querySelectorAll(queryStr, e)
}
// Find element by id inside element. Exactly as yourElement.getElementById() in browser DOM.
// https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById
//
// Examples:
//
// element, err := yourElement.GetElementById("piu") // get elements inside another element. err if not found
func (e *Element) GetElementById(id string) (*Element, error) {
return e.getElementById(id, e)
}
// Find elements by CSS class name inside element. Exactly as yourElement.getElementsByClassName() in browser DOM.
// https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName
//
// Examples:
//
// elements, err := yourElement.GetElementsByClassName(".lolipop") // get elements inside another element. err if not found
func (e *Element) GetElementsByClassName(class string) ([]*Element, error) {
return e.getElementsByClassName(class, e)
}
// Find elements by tag name inside element. Exactly as yourElement.getElementsByTagName() in browser DOM.
// https://developer.mozilla.org/en-US/docs/Web/API/Element/getElementsByTagName
//
// Examples:
//
// elements, err := yourElement.GetElementsByTagName("li") // get elements inside another element. err if not found
func (e *Element) GetElementsByTagName(tag string) ([]*Element, error) {
return e.getElementsByTagName(tag, e)
}
// If DOM element contains inside itself another DOM element.
// https://developer.mozilla.org/en-US/docs/Web/API/Node/contains
//
// Examples:
//
// parent, _ := document.QuerySelector(".pups")
// child, _ := document.GetElementById("wee")
// contains := parent.Contains(child) // true if parent contains inside itself child
func (e *Element) Contains(el *Element) bool {
return e.contains(e, el)
}