-
Notifications
You must be signed in to change notification settings - Fork 0
/
domparser.go
1155 lines (1024 loc) · 31.1 KB
/
domparser.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* This is a relatively lightweight DOMParser that is safe to use in a web
* worker. This is far from a complete DOM implementation; however, it should
* contain the minimal set of functionality necessary for Readability.js.
*
* Aside from not implementing the full DOM API, there are other quirks to be
* aware of when using the JSDOMParser:
*
* 1) Properly formed HTML/XML must be used. This means you should be extra
* careful when using this parser on anything received directly from an
* XMLHttpRequest. Providing a serialized string from an XMLSerializer,
* however, should be safe (since the browser's XMLSerializer should
* generate valid HTML/XML). Therefore, if parsing a document from an XHR,
* the recommended approach is to do the XHR in the main thread, use
* XMLSerializer.serializeToString() on the responseXML, and pass the
* resulting string to the worker.
*
* 2) Live NodeLists are not supported. DOM methods and properties such as
* getElementsByTagName() and childNodes return standard arrays. If you
* want these lists to be updated when nodes are removed or added to the
* document, you must take care to manually update them yourself.
*/
package readability
import (
"fmt"
"log/slog"
"net/url"
"slices"
"strconv"
"strings"
"unicode/utf8"
"golang.org/x/net/html"
)
// XML only defines these and the numeric ones:
var entityReplacer = strings.NewReplacer(
"lt", `<`,
"gt", `>`,
"amp", `&`,
"quot", `"`,
"apos", `'`,
)
var reverseEntitySubsetReplacer = strings.NewReplacer(
`<`, "<",
`>`, ">",
`&`, "&",
)
var reverseEntityReplacer = strings.NewReplacer(
`<`, "<",
`>`, ">",
`&`, "&",
`"`, """,
`'`, "'",
)
func encodeTextContentHTML(text string) string {
return reverseEntitySubsetReplacer.Replace(text)
}
func encodeHTML(text string) string {
return reverseEntityReplacer.Replace(text)
}
func decodeHTML(s string) (string, error) {
s = entityReferencesRgx.ReplaceAllStringFunc(s, func(s string) string {
return string([]rune(entityReplacer.Replace(s))[1])
})
submatches := htmlCharCodesRgx.FindAllStringSubmatch(s, -1)
for _, submatch := range submatches {
if len(submatch) == 3 {
hex, dec := submatch[1], submatch[2]
if hex != "" {
codePoint, err := strconv.ParseInt(hex, 16, 64)
if err != nil {
return "", err
}
s = strings.ReplaceAll(s, submatch[0], string(rune(codePoint)))
} else if dec != "" {
codePoint, err := strconv.ParseInt(dec, 10, 64)
if err != nil {
return "", err
}
s = strings.ReplaceAll(s, submatch[0], string(rune(codePoint)))
}
}
}
return s, nil
}
// When a style is set in JS, map it to the corresponding CSS attribute
var styleMap = map[string]string{
"alignmentBaseline": "alignment-baseline",
"background": "background",
"backgroundAttachment": "background-attachment",
"backgroundClip": "background-clip",
"backgroundColor": "background-color",
"backgroundImage": "background-image",
"backgroundOrigin": "background-origin",
"backgroundPosition": "background-position",
"backgroundPositionX": "background-position-x",
"backgroundPositionY": "background-position-y",
"backgroundRepeat": "background-repeat",
"backgroundRepeatX": "background-repeat-x",
"backgroundRepeatY": "background-repeat-y",
"backgroundSize": "background-size",
"baselineShift": "baseline-shift",
"border": "border",
"borderBottom": "border-bottom",
"borderBottomColor": "border-bottom-color",
"borderBottomLeftRadius": "border-bottom-left-radius",
"borderBottomRightRadius": "border-bottom-right-radius",
"borderBottomStyle": "border-bottom-style",
"borderBottomWidth": "border-bottom-width",
"borderCollapse": "border-collapse",
"borderColor": "border-color",
"borderImage": "border-image",
"borderImageOutset": "border-image-outset",
"borderImageRepeat": "border-image-repeat",
"borderImageSlice": "border-image-slice",
"borderImageSource": "border-image-source",
"borderImageWidth": "border-image-width",
"borderLeft": "border-left",
"borderLeftColor": "border-left-color",
"borderLeftStyle": "border-left-style",
"borderLeftWidth": "border-left-width",
"borderRadius": "border-radius",
"borderRight": "border-right",
"borderRightColor": "border-right-color",
"borderRightStyle": "border-right-style",
"borderRightWidth": "border-right-width",
"borderSpacing": "border-spacing",
"borderStyle": "border-style",
"borderTop": "border-top",
"borderTopColor": "border-top-color",
"borderTopLeftRadius": "border-top-left-radius",
"borderTopRightRadius": "border-top-right-radius",
"borderTopStyle": "border-top-style",
"borderTopWidth": "border-top-width",
"borderWidth": "border-width",
"bottom": "bottom",
"boxShadow": "box-shadow",
"boxSizing": "box-sizing",
"captionSide": "caption-side",
"clear": "clear",
"clip": "clip",
"clipPath": "clip-path",
"clipRule": "clip-rule",
"color": "color",
"colorInterpolation": "color-interpolation",
"colorInterpolationFilters": "color-interpolation-filters",
"colorProfile": "color-profile",
"colorRendering": "color-rendering",
"content": "content",
"counterIncrement": "counter-increment",
"counterReset": "counter-reset",
"cursor": "cursor",
"direction": "direction",
"display": "display",
"dominantBaseline": "dominant-baseline",
"emptyCells": "empty-cells",
"enableBackground": "enable-background",
"fill": "fill",
"fillOpacity": "fill-opacity",
"fillRule": "fill-rule",
"filter": "filter",
"cssFloat": "float",
"floodColor": "flood-color",
"floodOpacity": "flood-opacity",
"font": "font",
"fontFamily": "font-family",
"fontSize": "font-size",
"fontStretch": "font-stretch",
"fontStyle": "font-style",
"fontVariant": "font-variant",
"fontWeight": "font-weight",
"glyphOrientationHorizontal": "glyph-orientation-horizontal",
"glyphOrientationVertical": "glyph-orientation-vertical",
"height": "height",
"imageRendering": "image-rendering",
"kerning": "kerning",
"left": "left",
"letterSpacing": "letter-spacing",
"lightingColor": "lighting-color",
"lineHeight": "line-height",
"listStyle": "list-style",
"listStyleImage": "list-style-image",
"listStylePosition": "list-style-position",
"listStyleType": "list-style-type",
"margin": "margin",
"marginBottom": "margin-bottom",
"marginLeft": "margin-left",
"marginRight": "margin-right",
"marginTop": "margin-top",
"marker": "marker",
"markerEnd": "marker-end",
"markerMid": "marker-mid",
"markerStart": "marker-start",
"mask": "mask",
"maxHeight": "max-height",
"maxWidth": "max-width",
"minHeight": "min-height",
"minWidth": "min-width",
"opacity": "opacity",
"orphans": "orphans",
"outline": "outline",
"outlineColor": "outline-color",
"outlineOffset": "outline-offset",
"outlineStyle": "outline-style",
"outlineWidth": "outline-width",
"overflow": "overflow",
"overflowX": "overflow-x",
"overflowY": "overflow-y",
"padding": "padding",
"paddingBottom": "padding-bottom",
"paddingLeft": "padding-left",
"paddingRight": "padding-right",
"paddingTop": "padding-top",
"page": "page",
"pageBreakAfter": "page-break-after",
"pageBreakBefore": "page-break-before",
"pageBreakInside": "page-break-inside",
"pointerEvents": "pointer-events",
"position": "position",
"quotes": "quotes",
"resize": "resize",
"right": "right",
"shapeRendering": "shape-rendering",
"size": "size",
"speak": "speak",
"src": "src",
"stopColor": "stop-color",
"stopOpacity": "stop-opacity",
"stroke": "stroke",
"strokeDasharray": "stroke-dasharray",
"strokeDashoffset": "stroke-dashoffset",
"strokeLinecap": "stroke-linecap",
"strokeLinejoin": "stroke-linejoin",
"strokeMiterlimit": "stroke-miterlimit",
"strokeOpacity": "stroke-opacity",
"strokeWidth": "stroke-width",
"tableLayout": "table-layout",
"textAlign": "text-align",
"textAnchor": "text-anchor",
"textDecoration": "text-decoration",
"textIndent": "text-indent",
"textLineThrough": "text-line-through",
"textLineThroughColor": "text-line-through-color",
"textLineThroughMode": "text-line-through-mode",
"textLineThroughStyle": "text-line-through-style",
"textLineThroughWidth": "text-line-through-width",
"textOverflow": "text-overflow",
"textOverline": "text-overline",
"textOverlineColor": "text-overline-color",
"textOverlineMode": "text-overline-mode",
"textOverlineStyle": "text-overline-style",
"textOverlineWidth": "text-overline-width",
"textRendering": "text-rendering",
"textShadow": "text-shadow",
"textTransform": "text-transform",
"textUnderline": "text-underline",
"textUnderlineColor": "text-underline-color",
"textUnderlineMode": "text-underline-mode",
"textUnderlineStyle": "text-underline-style",
"textUnderlineWidth": "text-underline-width",
"top": "top",
"unicodeBidi": "unicode-bidi",
"unicodeRange": "unicode-range",
"vectorEffect": "vector-effect",
"verticalAlign": "vertical-align",
"visibility": "visibility",
"whiteSpace": "white-space",
"widows": "widows",
"width": "width",
"wordBreak": "word-break",
"wordSpacing": "word-spacing",
"wordWrap": "word-wrap",
"writingMode": "writing-mode",
"zIndex": "z-index",
"zoom": "zoom",
}
// Elements that can be self-closing
var voidElems = map[string]bool{
"area": true,
"base": true,
"br": true,
"col": true,
"command": true,
"embed": true,
"hr": true,
"img": true,
"input": true,
"link": true,
"meta": true,
"param": true,
"source": true,
"wbr": true,
}
//var whitespaces = []rune{' ', '\t', '\n', '\r'}
// See https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType
const (
_ = iota
elementNode
attributeNode
textNode
cdataSectionNode
entityReferenceNode
entityNode
processingInstructionNode
commentNode
documentNode
documentTypeNode
documentFragmentNode
notationNode
)
func (n *Node) getElementsByTagName(tag string) []*Node {
tag = strings.ToUpper(tag)
var elems []*Node
var allTags = tag == "*"
var getElems func(from *Node)
getElems = func(from *Node) {
for i := 0; i < len(from.Children); i++ {
child := from.Children[i]
if allTags || child.TagName == tag {
elems = append(elems, child)
}
getElems(child)
}
}
getElems(n)
return elems
}
type Node struct {
NodeType uint
LocalName string
nodeName string
textContent string
innerHTML string
TagName string
Attributes []*attribute
style *style
// relations
ParentNode *Node
NextSibling *Node
PreviousSibling *Node
PreviousElementSibling *Node
NextElementSibling *Node
ChildNodes []*Node
Children []*Node
// element
matchingTag string
// document
DocumentURI string
baseURI string
title string
head *Node
Body *Node
DocumentElement *Node
ReadabilityNode *readabilityNode
ReadabilityDataTable *readabilityDataTable
}
type readabilityDataTable struct {
value bool
}
type readabilityNode struct {
ContentScore float64
}
func (n *Node) FirstChild() *Node {
if len(n.ChildNodes) == 0 {
return nil
}
return n.ChildNodes[0]
}
func (n *Node) FirstElementChild() *Node {
if len(n.Children) == 0 {
return nil
}
return n.Children[0]
}
func (n *Node) LastChild() *Node {
if len(n.ChildNodes) == 0 {
return nil
}
return n.ChildNodes[len(n.ChildNodes)-1]
}
/* func (n *node) lastElementChild() *node {
if len(n.ChildNodes) == 0 {
return nil
}
return n.Children[len(n.Children)-1]
} */
func (n *Node) AppendChild(child *Node) {
if child.ParentNode != nil {
if _, err := child.ParentNode.RemoveChild(child); err != nil {
slog.Error("cannot remove child", slog.String("err", err.Error()))
}
}
last := n.LastChild()
if last != nil {
last.NextSibling = child
}
child.PreviousSibling = last
if child.NodeType == elementNode {
if len(n.Children) != 0 {
child.PreviousElementSibling = n.Children[len(n.Children)-1]
}
n.Children = append(n.Children, child)
if child.PreviousElementSibling != nil {
child.PreviousElementSibling.NextElementSibling = child
}
}
n.ChildNodes = append(n.ChildNodes, child)
child.ParentNode = n
}
func (n *Node) RemoveChild(child *Node) (*Node, error) {
childNodes := n.ChildNodes
childIndex := indexOf(child, childNodes)
if childIndex == -1 {
return nil, fmt.Errorf("removeChild: node not found")
} else {
child.ParentNode = nil
prev := child.PreviousSibling
next := child.NextSibling
if prev != nil {
prev.NextSibling = next
}
if next != nil {
next.PreviousSibling = prev
}
if child.NodeType == elementNode {
prev = child.PreviousElementSibling
next = child.NextElementSibling
if prev != nil {
prev.NextElementSibling = next
}
if next != nil {
next.PreviousElementSibling = prev
}
n.Children = delete(indexOf(child, n.Children), n.Children)
}
child.PreviousSibling, child.NextSibling = nil, nil
child.PreviousElementSibling, child.NextElementSibling = nil, nil
n.ChildNodes = delete(childIndex, n.ChildNodes)
return child, nil
}
}
func (n *Node) ReplaceChild(newNode, oldNode *Node) *Node {
childNodes := n.ChildNodes
childIndex := indexOf(oldNode, childNodes)
if childIndex == -1 {
panic("removeChild: node not found")
} else {
// This will take care of updating the new node if it was somewhere else before:
if newNode.ParentNode != nil {
if _, err := newNode.ParentNode.RemoveChild(newNode); err != nil {
slog.Error("cannot remove child", slog.String("err", err.Error()))
}
}
childNodes[childIndex] = newNode
// update the new node's sibling properties, and its new siblings' sibling properties
newNode.NextSibling = oldNode.NextSibling
newNode.PreviousSibling = oldNode.PreviousSibling
if newNode.NextSibling != nil {
newNode.NextSibling.PreviousSibling = newNode
}
if newNode.PreviousSibling != nil {
newNode.PreviousSibling.NextSibling = newNode
}
newNode.ParentNode = n
// Now deal with elements before we clear out those values for the old node,
// because it can help us take shortcuts here:
if newNode.NodeType == elementNode {
if oldNode.NodeType == elementNode {
// Both were elements, which makes this easier, we just swap things out:
newNode.PreviousElementSibling = oldNode.PreviousElementSibling
newNode.NextElementSibling = oldNode.NextElementSibling
if newNode.PreviousElementSibling != nil {
newNode.PreviousElementSibling.NextElementSibling = newNode
}
if newNode.NextElementSibling != nil {
newNode.NextElementSibling.PreviousElementSibling = newNode
}
n.Children[indexOf(oldNode, n.Children)] = newNode
} else {
// Hard way:
newNode.PreviousElementSibling = func(childIndex int, childNodes []*Node) *Node {
for i := childIndex - 1; i >= 0; i-- {
if childNodes[i].NodeType == elementNode {
return childNodes[i]
}
}
return nil
}(childIndex, childNodes)
if newNode.PreviousElementSibling != nil {
newNode.NextElementSibling = newNode.PreviousElementSibling.NextElementSibling
} else {
newNode.NextElementSibling = func(childIndex int, childNodes []*Node) *Node {
for i := childIndex + 1; i < len(childNodes); i++ {
if childNodes[i].NodeType == elementNode {
return childNodes[i]
}
}
return nil
}(childIndex, childNodes)
}
if newNode.PreviousElementSibling != nil {
newNode.PreviousElementSibling.NextElementSibling = newNode
}
if newNode.NextElementSibling != nil {
newNode.NextElementSibling.PreviousElementSibling = newNode
}
if newNode.NextElementSibling != nil {
n.Children = insert(newNode, indexOf(newNode.NextElementSibling, n.Children), n.Children)
} else {
n.Children = append(n.Children, newNode)
}
}
} else if oldNode.NodeType == elementNode {
// new node is not an element node.
// if the old one was, update its element siblings:
if oldNode.PreviousElementSibling != nil {
oldNode.PreviousElementSibling.NextElementSibling = oldNode.NextElementSibling
}
if oldNode.NextElementSibling != nil {
oldNode.NextElementSibling.PreviousElementSibling = oldNode.PreviousElementSibling
}
n.Children = delete(indexOf(oldNode, n.Children), n.Children)
// If the old node wasn't an element, neither the new nor the old node was an element,
// and the children array and its members shouldn't need any updating.
}
oldNode.ParentNode = nil
oldNode.PreviousSibling = nil
oldNode.NextSibling = nil
if oldNode.NodeType == elementNode {
oldNode.PreviousElementSibling = nil
oldNode.NextElementSibling = nil
}
return oldNode
}
}
type attribute struct {
name, value string
}
func newAttribute(n, v string) *attribute {
return &attribute{
name: n,
value: v,
}
}
func (a *attribute) getName() string {
return a.name
}
func (a *attribute) getValue() string {
return a.value
}
func (a *attribute) setValue(newValue string) {
a.value = newValue
}
func (a *attribute) getEncodedValue() string {
return encodeHTML(a.value)
}
/*
func newComment() *node {
return &node{
nodeName: "#comment",
NodeType: commentNode,
}
} */
func newText() *Node {
return &Node{
nodeName: "#text",
NodeType: textNode,
innerHTML: "",
textContent: "",
}
}
func (t *Node) getTextContentFromTextNode() string {
if t.textContent == "" {
decoded, err := decodeHTML(t.GetInnerHTML())
if err != nil {
slog.Error("cannot decode inner html", "err", err)
return ""
}
t.textContent = decoded
}
return t.textContent
}
func (t *Node) getInnerHTMLFromTextNode() string {
if t.innerHTML == "" {
t.innerHTML = encodeTextContentHTML(t.GetTextContent())
}
return t.innerHTML
}
func (t *Node) setInnerHTMLFromTextNode(newHTML string) {
t.innerHTML = newHTML
t.textContent = ""
}
func (t *Node) setTextContentFromTextNode(newText string) {
t.textContent = newText
t.innerHTML = ""
}
func newDocument(url string) *Node {
return &Node{
DocumentURI: url,
nodeName: "#document",
NodeType: documentNode,
}
}
func (n *Node) GetElementById(id string) *Node {
var getElem func(from *Node) *Node
getElem = func(from *Node) *Node {
var length = len(from.Children)
if from.GetId() == id {
return from
}
for i := 0; i < length; i++ {
var el = getElem(from.Children[i])
if el != nil {
return el
}
}
return nil
}
return getElem(n)
}
func (d *Node) createElementNode(tag string) *Node {
return newElement(tag)
}
func (d *Node) createTextNode(text string) *Node {
node := newText()
node.SetTextContent(text)
return node
}
func (d *Node) getBaseURI() string {
if d.baseURI == "" {
d.baseURI = d.DocumentURI
baseElements := d.getElementsByTagName("base")
if len(baseElements) != 0 {
href := baseElements[0].GetAttribute("href")
if href != "" {
base, err := url.Parse(d.baseURI)
if err != nil {
// just fall back to documentURI
return d.DocumentURI
}
ref, err := url.Parse(href)
if err != nil {
// just fall back to documentURI
return d.DocumentURI
}
u := base.ResolveReference(ref)
d.baseURI = u.String()
}
}
}
return d.baseURI
}
func newElement(tag string) *Node {
n := &Node{
// We use this to find the closing tag.
matchingTag: tag,
NodeType: elementNode,
}
// We're explicitly a non-namespace aware parser, we just pretend it's all HTML.
var lastColonIndex = strings.LastIndex(tag, ":")
if lastColonIndex != -1 {
substrings := strings.Split(tag, ":")
tag = substrings[len(substrings)-1]
}
n.LocalName = strings.ToLower(tag)
n.TagName = strings.ToUpper(tag)
n.style = newStyle(n)
return n
}
func (n *Node) GetAttribute(name string) string {
var i = len(n.Attributes) - 1
for i >= 0 {
var attr = n.Attributes[i]
if attr.name == name {
return attr.value
}
i--
}
return ""
}
func (n *Node) GetAttributeByIndex(idx int) *attribute {
return n.Attributes[idx]
}
func (n *Node) GetAttributeLen() int {
return len(n.Attributes)
}
func (n *Node) SetAttribute(name, value string) {
for _, attr := range n.Attributes {
if attr.name == name {
attr.setValue(value)
return
}
}
n.Attributes = append(n.Attributes, newAttribute(name, value))
}
func (n *Node) RemoveAttribute(name string) {
for idx, attr := range n.Attributes {
if attr.name == name {
n.Attributes = delete(idx, n.Attributes)
break
}
}
}
func (n *Node) HasAttribute(name string) bool {
return slices.ContainsFunc[[]*attribute, *attribute](n.Attributes, func(a *attribute) bool {
return a.name == name
})
}
type style struct {
*Node
}
func newStyle(n *Node) *style {
return &style{
Node: n,
}
}
func (s *style) getStyle(jsName string) string {
var cssName = styleMap[jsName]
var attr = s.Node.GetAttribute("style")
if attr == "" {
return ""
}
var styles = strings.Split(attr, ";")
for i := 0; i < len(styles); i++ {
var style = strings.Split(styles[i], ":")
var name = strings.TrimSpace(style[0])
if name == cssName {
return strings.TrimSpace(style[1])
}
}
return ""
}
/*
func (s *style) setStyle(jsName, styleValue string) {
var cssName = styleMap[jsName]
var value = s.node.getAttribute("style")
var index = 0
for index >= 0 {
var next = indexOfFrom(value, ";", index)
var length = next - index - 1
var style string
if length > 0 {
style = substring(value, index, length)
} else {
style = substring(value, index, len(style))
}
substr := substring(style, 0, strings.IndexRune(style, ':'))
if strings.TrimSpace(substr) == cssName {
value = strings.TrimSpace(substring(value, 0, index))
if next >= 0 {
value += " " + strings.TrimSpace(substring(value, next, len((value))))
}
}
index = next
}
value += " " + cssName + ": " + styleValue + ";"
s.node.setAttribute("style", strings.TrimSpace(value))
}
*/
func (n *Node) GetClassName() string {
return n.GetAttribute("class")
}
func (n *Node) SetClassName(str string) {
n.SetAttribute("class", str)
}
func (n *Node) GetId() string {
return n.GetAttribute("id")
}
func (n *Node) SetId(str string) {
n.SetAttribute("id", str)
}
/* func (n *node) getHref() string {
return n.getAttribute("href")
}
func (n *node) setHref(str string) {
n.setAttribute("href", str)
} */
func (n *Node) GetSrc() string {
return n.GetAttribute("src")
}
/* func (n *node) setSrc(str string) {
n.setAttribute("src", str)
} */
func (n *Node) GetSrcset() string {
return n.GetAttribute("srcset")
}
/*
func (n *node) setSrcset(str string) {
n.setAttribute("srcset", str)
}
*/
func (n *Node) GetNodeName() string {
return n.TagName
}
func (n *Node) GetInnerHTML() string {
if n.NodeType == textNode {
return n.getInnerHTMLFromTextNode()
}
var getHTML func(from *Node, a []string) []string
getHTML = func(from *Node, a []string) []string {
for i := 0; i < len(from.ChildNodes); i++ {
var child = from.ChildNodes[i]
if child.LocalName != "" {
a = append(a, "<"+child.LocalName)
// serialize attribute list
for j := 0; j < len(child.Attributes); j++ {
var attr = child.Attributes[j]
// the attribute value will be HTML escaped.
var val = attr.getEncodedValue()
var quote = `"`
if strings.Contains(val, `"`) {
quote = "'"
}
a = append(a, " "+attr.name+"="+quote+val+quote)
}
if _, found := voidElems[child.LocalName]; found && len(child.ChildNodes) != 0 {
// if this is a self-closing element, end it here
a = append(a, "/>")
} else {
// otherwise, add its children
a = append(a, ">")
a = getHTML(child, a)
a = append(a, "</"+child.LocalName+">")
}
} else {
// This is a text node, so asking for innerHTML won't recurse.
a = append(a, child.GetInnerHTML())
}
}
return a
}
var arr []string
arr = getHTML(n, arr)
return strings.Join(arr, "")
}
func (n *Node) SetInnerHTML(html string) {
if n.NodeType == textNode {
n.setInnerHTMLFromTextNode(html)
} else if n.NodeType == elementNode {
var parser = newDOMParser()
var node = parser.parse(html, "")
for i := len(n.ChildNodes) - 1; i >= 0; i-- {
n.ChildNodes[i].ParentNode = nil
}
n.ChildNodes = node.ChildNodes
n.Children = node.Children
for i := len(n.ChildNodes) - 1; i >= 0; i-- {
n.ChildNodes[i].ParentNode = n
}
} else {
n.innerHTML = html
}
}
func (n *Node) SetTextContent(text string) {
if n.NodeType == textNode {
n.setTextContentFromTextNode(text)
return
} else if n.NodeType == elementNode {
// clear parentNodes for existing children
for i := len(n.ChildNodes) - 1; i >= 0; i-- {
n.ChildNodes[i].ParentNode = nil
}
var t = newText()
n.ChildNodes = []*Node{t}
n.Children = []*Node{}
t.textContent = text
t.ParentNode = n
} else {
n.textContent = text
}
}
func (n *Node) GetTextContent() string {
if n.NodeType == textNode {
return n.getTextContentFromTextNode()
} else if n.NodeType == elementNode {
var getText func(*Node, []string) []string
getText = func(from *Node, t []string) []string {
var nodes = from.ChildNodes
for i := 0; i < len(nodes); i++ {
var child = nodes[i]
if child.NodeType == textNode {
t = append(t, child.GetTextContent())
} else {
t = getText(child, t)
}
}
return t
}
text := make([]string, 0)
text = getText(n, text)