-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdom.go
380 lines (303 loc) · 6.92 KB
/
dom.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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package html2html
import (
"bytes"
"errors"
"strings"
)
type TokenType int
const (
TypeTagToken TokenType = 1 + iota
TypeDoctypeToken
TypeTextToken
TypeCommentToken
)
var ErrInvalidTokenType = errors.New("invalid token type")
type Token interface {
setParent(parent Tag)
Type() TokenType
Parent() Tag
BuildHTML(buf *bytes.Buffer)
Tag() Tag
TextToken() TextToken
}
type Tag interface {
Token
IsDocumentRoot() bool
Name() string
Tokens() []Token
SetTokens(tokens []Token)
AddChildTokens(childlen ...Token)
UnshiftChileToken(token Token)
ReplateChildToken(from Token, to Token)
GetElementsByTagName(tagName string) []Tag
FindAncestor(tagName string) Tag
Attrs() []*Attr
SetAttrs(attrs []*Attr)
AddAttr(key, value string)
GetAttr(attrKey string) *Attr
RemoveAttr(attrKey string)
HasAttr(attrKey string) bool
HasAttrValue(attrKey string, attrValue string) bool
HasAttrValueCaseInsensitive(attrKey string, attrValue string) bool
AddText(text string)
AddComment(text string)
}
type TextToken interface {
Token
Text() string
}
func CreateDocumentRoot() Tag {
return &tagImpl{documentRoot: true}
}
func CreateElement(tagName string) Tag {
return &tagImpl{name: tagName}
}
func CreateElementSelfClosing(tagName string) Tag {
return &tagImpl{name: tagName, selfClosing: true}
}
func CreateDoctypeToken(text string) Token {
return &textTokenImpl{tokenType: TypeDoctypeToken, text: text}
}
func CreateTextToken(text string) Token {
return &textTokenImpl{tokenType: TypeTextToken, text: text}
}
func CreateCommentToken(text string) Token {
return &textTokenImpl{tokenType: TypeCommentToken, text: text}
}
var _ Tag = &tagImpl{}
type tagImpl struct {
documentRoot bool
parent Tag
name string
selfClosing bool
tokens []Token
attrs []*Attr
}
func (tag *tagImpl) Type() TokenType {
return TypeTagToken
}
func (tag *tagImpl) Parent() Tag {
return tag.parent
}
func (tag *tagImpl) BuildHTML(buf *bytes.Buffer) {
// root node doesn't have name & attrs.
if tag.name != "" {
buf.WriteString("<")
buf.WriteString(tag.name)
for _, attr := range tag.attrs {
buf.WriteString(" ")
buf.WriteString(attr.Key)
if attr.Value != "" {
buf.WriteString("=\"")
buf.WriteString(attr.Value)
buf.WriteString("\"")
}
}
if tag.selfClosing {
buf.WriteString("/>")
} else {
buf.WriteString(">")
}
}
if tag.selfClosing {
return
}
if len(tag.tokens) == 0 {
for _, voidElement := range VoidElements {
if voidElement == tag.name {
return
}
}
}
for _, token := range tag.tokens {
token.BuildHTML(buf)
}
if tag.name != "" {
buf.WriteString("</")
buf.WriteString(tag.name)
buf.WriteString(">")
}
}
func (tag *tagImpl) setParent(parent Tag) {
current := tag.Parent()
for {
if current == nil {
break
}
if current == tag {
panic("recursive dom dependencies")
}
current = current.Parent()
}
tag.parent = parent
}
func (tag *tagImpl) Tag() Tag {
return tag
}
func (tag *tagImpl) TextToken() TextToken {
panic(ErrInvalidTokenType)
}
func (tag *tagImpl) IsDocumentRoot() bool {
return tag.documentRoot
}
func (tag *tagImpl) Name() string {
return tag.name
}
func (tag *tagImpl) Tokens() []Token {
return tag.tokens
}
func (tag *tagImpl) SetTokens(tokens []Token) {
for _, token := range tag.tokens {
token.setParent(nil)
}
tag.tokens = tokens
for _, token := range tag.tokens {
token.setParent(tag)
}
}
func (tag *tagImpl) AddChildTokens(childlen ...Token) {
for _, token := range childlen {
token.setParent(tag)
}
tag.tokens = append(tag.tokens, childlen...)
}
func (tag *tagImpl) UnshiftChileToken(token Token) {
token.setParent(tag)
newTokens := make([]Token, 0, len(tag.tokens)+1)
newTokens = append(newTokens, token)
newTokens = append(newTokens, tag.tokens...)
tag.tokens = newTokens
}
func (tag *tagImpl) ReplateChildToken(from Token, to Token) {
from.setParent(nil)
to.setParent(tag)
for idx, token := range tag.tokens {
if token == from {
tag.tokens[idx] = to
break
}
}
}
func (tag *tagImpl) Attrs() []*Attr {
return tag.attrs
}
func (tag *tagImpl) SetAttrs(attrs []*Attr) {
tag.attrs = attrs
}
func (tag *tagImpl) AddAttr(key, value string) {
tag.attrs = append(tag.attrs, &Attr{Key: key, Value: value})
}
func (tag *tagImpl) GetAttr(attrKey string) *Attr {
for _, attr := range tag.attrs {
if attr.Key == attrKey {
return attr
}
}
return nil
}
func (tag *tagImpl) RemoveAttr(attrKey string) {
newAttrs := make([]*Attr, 0, len(tag.attrs))
for _, attr := range tag.attrs {
if attr.Key == attrKey {
continue
}
newAttrs = append(newAttrs, attr)
}
tag.attrs = newAttrs
}
func (tag *tagImpl) HasAttr(attrKey string) bool {
return tag.GetAttr(attrKey) != nil
}
func (tag *tagImpl) HasAttrValue(attrKey string, attrValue string) bool {
attr := tag.GetAttr(attrKey)
if attr == nil {
return false
}
return attr.Value == attrValue
}
func (tag *tagImpl) HasAttrValueCaseInsensitive(attrKey string, attrValue string) bool {
attr := tag.GetAttr(attrKey)
if attr == nil {
return false
}
return strings.ToLower(attr.Value) == strings.ToLower(attrValue)
}
func (tag *tagImpl) AddText(text string) {
textToken := CreateTextToken(text)
tag.AddChildTokens(textToken)
}
func (tag *tagImpl) AddComment(text string) {
comment := CreateCommentToken(text)
tag.AddChildTokens(comment)
}
func (tag *tagImpl) GetElementsByTagName(tagName string) []Tag {
var result []Tag
tagName = strings.ToLower(tagName)
for _, token := range tag.tokens {
if token.Type() != TypeTagToken {
continue
}
child := token.Tag()
if strings.ToLower(child.Name()) == tagName {
result = append(result, child)
}
result = append(result, child.GetElementsByTagName(tagName)...)
}
return result
}
func (tag *tagImpl) FindAncestor(tagName string) Tag {
var target Tag = tag
for {
parent := target.Parent()
if parent == nil {
break
}
if parent.Name() == tagName {
return parent
}
target = parent
}
return nil
}
var _ TextToken = &textTokenImpl{}
type textTokenImpl struct {
parent Tag
tokenType TokenType
text string
}
func (textToken *textTokenImpl) setParent(parent Tag) {
textToken.parent = parent
}
func (textToken *textTokenImpl) Type() TokenType {
return textToken.tokenType
}
func (textToken *textTokenImpl) Parent() Tag {
return textToken.parent
}
func (textToken *textTokenImpl) BuildHTML(buf *bytes.Buffer) {
switch textToken.tokenType {
case TypeDoctypeToken:
buf.WriteString("<!DOCTYPE ")
buf.WriteString(textToken.text)
buf.WriteString(">")
case TypeTextToken:
buf.WriteString(textToken.text)
case TypeCommentToken:
buf.WriteString("<!--")
buf.WriteString(textToken.text)
buf.WriteString("-->")
}
}
func (textToken *textTokenImpl) Tag() Tag {
panic(ErrInvalidTokenType)
}
func (textToken *textTokenImpl) TextToken() TextToken {
return textToken
}
func (textToken *textTokenImpl) Text() string {
return textToken.text
}
type Attr struct {
Key string
Value string
}