@@ -9,21 +9,13 @@ import (
99 "golang.org/x/net/html"
1010)
1111
12- func wrapAttributeValue (value string ) string {
13- if isDigit (value ) {
14- return value
15- }
16-
17- return `"` + strings .ReplaceAll (value , `"` , """ ) + `"`
18- }
19-
2012func encodeListAttributes (node * Node ) string {
2113 w := strings.Builder {}
2214 node .IterateAttributes (func (attribute , value string ) {
23- if strings .TrimSpace (attribute ) == "" {
15+ if strings .TrimSpace (value ) == "" {
2416 w .Write (fmt .Appendf (nil , " %s" , attribute ))
2517 } else {
26- w .Write (fmt .Appendf (nil , " %s=%s" , attribute , wrapAttributeValue (value )))
18+ w .Write (fmt .Appendf (nil , " %s=%s" , attribute , `"` + html . EscapeString (value )+ `"` ))
2719 }
2820
2921 })
@@ -32,43 +24,55 @@ func encodeListAttributes(node *Node) string {
3224
3325// Encode writes to w encoding of the node tree from rootNode.
3426func Encode (w io.Writer , rootNode * Node ) {
27+ if rootNode == nil {
28+ return
29+ }
30+
3531 type stackFrame struct {
36- node * Node
37- openedTag bool
32+ node * Node
33+ isClosingTag bool
3834 }
3935
4036 stack := linkedliststack .New ()
41- stack .Push (stackFrame {node : rootNode , openedTag : false })
37+ stack .Push (stackFrame {
38+ node : rootNode ,
39+ })
4240
4341 for stack .Size () > 0 {
44- t , _ := stack .Pop ()
45- top := t .(stackFrame )
46- current := top .node
42+ v , _ := stack .Pop ()
43+ currentStackFrame := v .(stackFrame )
4744
48- if current == nil {
45+ if currentStackFrame .isClosingTag {
46+ fmt .Fprintf (w , "</%s>" , currentStackFrame .node .GetTagName ())
4947 continue
48+ } else if currentStackFrame .node .IsTextNode () {
49+ fmt .Fprint (w , html .EscapeString (currentStackFrame .node .GetText ()))
50+ } else {
51+ fmt .Fprintf (w , "<%s%s>" , func () string {
52+ tagName := currentStackFrame .node .GetTagName ()
53+ tagNameUpperCased := strings .ToUpper (tagName )
54+ if tagNameUpperCased == DOCTYPEDTD {
55+ tagName = tagNameUpperCased
56+ }
57+ return tagName
58+ }(), encodeListAttributes (currentStackFrame .node ))
5059 }
5160
52- tagName := current .GetTagName ()
53- if tagName == "" {
54- w .Write ([]byte (html .EscapeString (current .GetText ())))
55- } else if IsVoidTag (tagName ) {
56- fmt .Fprintf (w , "<%s%s>" , tagName , encodeListAttributes (current ))
57- if current .GetNextNode () != nil {
58- stack .Push (stackFrame {node : current .GetNextNode (), openedTag : false })
59- }
60- } else if ! top .openedTag {
61- fmt .Fprintf (w , "<%s%s>" , tagName , encodeListAttributes (current ))
62- stack .Push (stackFrame {node : current , openedTag : true })
63-
64- if current .GetChildNode () != nil {
65- stack .Push (stackFrame {node : current .GetChildNode (), openedTag : false })
66- }
67- } else {
68- fmt .Fprintf (w , "</%s>" , tagName )
69- if current .GetNextNode () != nil {
70- stack .Push (stackFrame {node : current .GetNextNode (), openedTag : false })
71- }
61+ if currentStackFrame .node .GetNextNode () != nil {
62+ stack .Push (stackFrame {
63+ node : currentStackFrame .node .GetNextNode (),
64+ })
65+ }
66+ if ! IsVoidTag (currentStackFrame .node .GetTagName ()) && ! currentStackFrame .node .IsTextNode (){
67+ stack .Push (stackFrame {
68+ node : currentStackFrame .node ,
69+ isClosingTag : true ,
70+ })
71+ }
72+ if currentStackFrame .node .GetChildNode () != nil {
73+ stack .Push (stackFrame {
74+ node : currentStackFrame .node .GetChildNode (),
75+ })
7276 }
7377 }
7478}
0 commit comments