-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathtext_element.go
43 lines (37 loc) · 927 Bytes
/
text_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
package gohtml
import (
"regexp"
"strings"
)
// A textElement represents a text element of an HTML document.
type textElement struct {
text string
parent *tagElement
}
func (e *textElement) isInline() bool {
// Text nodes are always considered to be inline
return true
}
// write writes a text to the buffer.
func (e *textElement) write(bf *formattedBuffer, isPreviousNodeInline bool) bool {
text := unifyLineFeed(e.text)
if e.parent != nil && e.parent.isRaw {
bf.writeToken(text, formatterTokenType_Text)
return true
}
if !isPreviousNodeInline {
bf.writeLineFeed()
}
// Collapse leading and trailing spaces
text = regexp.MustCompile(`^\s+|\s+$`).ReplaceAllString(text, " ")
lines := strings.Split(text, "\n")
for l, line := range lines {
if l > 0 {
bf.writeLineFeed()
}
for _, word := range strings.Split(line, " ") {
bf.writeToken(word, formatterTokenType_Text)
}
}
return true
}