File: element_base.go

package info (click to toggle)
golang-github-yosssi-ace 0.0.4%2Bgit20160102.51.71afeb7-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 468 kB
  • ctags: 249
  • sloc: makefile: 3; sh: 1
file content (76 lines) | stat: -rw-r--r-- 1,759 bytes parent folder | download
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
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
}

// 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 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,
	}
}