-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathelement_base.go
87 lines (76 loc) · 1.98 KB
/
element_base.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
package ace
import "bytes"
// elementBase holds common fields for the elements.
type elementBase struct {
ln *line
rslt *result
src *source
parent element
children []element
opts *Options
lastChild bool
}
// AppendChild appends the child element to the element.
func (e *elementBase) AppendChild(child element) {
e.children = append(e.children, child)
}
// ContainPlainText returns false.
// This method should be overrided by a struct which contains
// the element base struct.
func (e *elementBase) ContainPlainText() bool {
return false
}
// Base returns the element base.
func (e *elementBase) Base() *elementBase {
return e
}
// CanHaveChildren returns true.
// This method should be overrided by a struct which contains
// the element base struct.
func (e *elementBase) CanHaveChildren() bool {
return true
}
func (e *elementBase) IsBlockElement() bool {
return false
}
func (e *elementBase) IsControlElement() bool {
return false
}
// InsertBr returns false.
// This method should be overrided by a struct which contains
// the element base struct.
func (e *elementBase) InsertBr() bool {
return false
}
// SetLastChild set the value to the last child field.
func (e *elementBase) SetLastChild(lastChild bool) {
e.lastChild = lastChild
}
// writeChildren writes the children's HTML.
func (e *elementBase) writeChildren(bf *bytes.Buffer) (int64, error) {
l := len(e.children)
for index, child := range e.children {
if index == l-1 {
child.SetLastChild(true)
}
if e.opts.formatter != nil {
if i, err := e.opts.formatter.OpeningElement(bf, child); err != nil {
return int64(i), err
}
}
if i, err := child.WriteTo(bf); err != nil {
return int64(i), err
}
}
return 0, nil
}
// newElementBase creates and returns an element base.
func newElementBase(ln *line, rslt *result, src *source, parent element, opts *Options) elementBase {
return elementBase{
ln: ln,
rslt: rslt,
src: src,
parent: parent,
opts: opts,
}
}