@@ -7,14 +7,12 @@ import (
7
7
)
8
8
9
9
// A formatState holds the current state of open tag, class, or style formats.
10
- type formatState struct {
11
- open []* Format // the list of currently open attribute tags
12
- }
10
+ type formatState []* Format // the list of currently open attribute tags
13
11
14
12
// hasSet says if the given format is already opened.
15
13
func (fs * formatState ) hasSet (fm * Format ) bool {
16
- for i := range fs . open {
17
- if fs . open [i ].Place == fm .Place && fs . open [i ].Val == fm .Val {
14
+ for i := range * fs {
15
+ if ( * fs ) [i ].Place == fm .Place && ( * fs ) [i ].Val == fm .Val {
18
16
return true
19
17
}
20
18
}
@@ -25,19 +23,19 @@ func (fs *formatState) hasSet(fm *Format) bool {
25
23
// in the opposite order in which they were opened.
26
24
func (fs * formatState ) closePrevious (buf * bytes.Buffer , o * Op , doingBlock bool ) {
27
25
28
- closedTemp := & formatState {}
26
+ closedTemp := make ( formatState , 0 , 1 )
29
27
30
- for i := len (fs . open ) - 1 ; i >= 0 ; i -- { // Start with the last format opened.
28
+ for i := len (* fs ) - 1 ; i >= 0 ; i -- { // Start with the last format opened.
31
29
32
- f := fs . open [i ]
30
+ f := ( * fs ) [i ]
33
31
34
32
// If this format is not set on the current Op, close it.
35
- if (! f .wrap && ! f .fm .HasFormat (o )) || (f .wrap && f .fm .(FormatWrapper ).Close (fs . open , o , doingBlock )) {
33
+ if (! f .wrap && ! f .fm .HasFormat (o )) || (f .wrap && f .fm .(FormatWrapper ).Close (* fs , o , doingBlock )) {
36
34
37
35
// If we need to close a tag after which there are tags that should stay open, close the following tags for now.
38
- if i < len (fs . open )- 1 {
39
- for ij := len (fs . open ) - 1 ; ij > i ; ij -- {
40
- closedTemp .add (fs . open [ij ])
36
+ if i < len (* fs )- 1 {
37
+ for ij := len (* fs ) - 1 ; ij > i ; ij -- {
38
+ closedTemp .add (( * fs ) [ij ])
41
39
fs .pop (buf )
42
40
}
43
41
}
@@ -50,29 +48,29 @@ func (fs *formatState) closePrevious(buf *bytes.Buffer, o *Op, doingBlock bool)
50
48
51
49
// Re-open the temporarily closed formats.
52
50
closedTemp .writeFormats (buf )
53
- fs . open = append (fs . open , closedTemp . open ... ) // Copy after the sorting.
51
+ * fs = append (* fs , closedTemp ... ) // Copy after the sorting.
54
52
55
53
}
56
54
57
55
// pop removes the last format from the state of currently open formats.
58
56
func (fs * formatState ) pop (buf * bytes.Buffer ) {
59
- indx := len (fs . open ) - 1
60
- if fs . open [indx ].wrap {
61
- buf .WriteString (fs . open [indx ].wrapPost )
62
- } else if fs . open [indx ].Place == Tag {
63
- closeTag (buf , fs . open [indx ].Val )
57
+ indx := len (* fs ) - 1
58
+ if ( * fs ) [indx ].wrap {
59
+ buf .WriteString (( * fs ) [indx ].wrapPost )
60
+ } else if ( * fs ) [indx ].Place == Tag {
61
+ closeTag (buf , ( * fs ) [indx ].Val )
64
62
} else {
65
63
closeTag (buf , "span" )
66
64
}
67
- fs . open = fs . open [:indx ]
65
+ * fs = ( * fs ) [:indx ]
68
66
}
69
67
70
68
// add adds a format that the string that will be written to buf right after this will have.
71
69
// Before calling add, check if the Format is already opened up earlier.
72
70
// Do not use add to write block-level styles (those are written by o.writeBlock after being merged).
73
71
func (fs * formatState ) add (f * Format ) {
74
72
if f .Place < 3 { // Check if the Place is valid.
75
- fs . open = append (fs . open , f )
73
+ * fs = append (* fs , f )
76
74
}
77
75
}
78
76
@@ -82,24 +80,24 @@ func (fs *formatState) writeFormats(buf *bytes.Buffer) {
82
80
83
81
sort .Sort (fs ) // Ensure that the serialization is consistent even if attribute ordering in a map changes.
84
82
85
- for i := range fs . open {
83
+ for i := range * fs {
86
84
87
- if fs . open [i ].wrap {
88
- buf .WriteString (fs . open [i ].Val ) // The complete opening or closing wrap is given.
85
+ if ( * fs ) [i ].wrap {
86
+ buf .WriteString (( * fs ) [i ].Val ) // The complete opening or closing wrap is given.
89
87
continue
90
88
}
91
89
92
90
buf .WriteByte ('<' )
93
91
94
- switch fs . open [i ].Place {
92
+ switch ( * fs ) [i ].Place {
95
93
case Tag :
96
- buf .WriteString (fs . open [i ].Val )
94
+ buf .WriteString (( * fs ) [i ].Val )
97
95
case Class :
98
96
buf .WriteString ("span class=" )
99
- buf .WriteString (strconv .Quote (fs . open [i ].Val ))
97
+ buf .WriteString (strconv .Quote (( * fs ) [i ].Val ))
100
98
case Style :
101
99
buf .WriteString ("span style=" )
102
- buf .WriteString (strconv .Quote (fs . open [i ].Val ))
100
+ buf .WriteString (strconv .Quote (( * fs ) [i ].Val ))
103
101
}
104
102
105
103
buf .WriteByte ('>' )
@@ -110,29 +108,27 @@ func (fs *formatState) writeFormats(buf *bytes.Buffer) {
110
108
111
109
// Implement the sort.Interface interface.
112
110
113
- func (fs * formatState ) Len () int {
114
- return len (fs .open )
115
- }
111
+ func (fs * formatState ) Len () int { return len (* fs ) }
116
112
117
113
func (fs * formatState ) Less (i , j int ) bool {
118
114
119
115
// Formats that implement the FormatWrapper interface are written first.
120
- if _ , ok := fs . open [i ].fm .(FormatWrapper ); ok {
116
+ if _ , ok := ( * fs ) [i ].fm .(FormatWrapper ); ok {
121
117
return true
122
- } else if _ , ok := fs . open [j ].fm .(FormatWrapper ); ok {
118
+ } else if _ , ok := ( * fs ) [j ].fm .(FormatWrapper ); ok {
123
119
return false
124
120
}
125
121
126
122
// Tags are written first, then classes, and then style attributes.
127
- if fs . open [i ].Place != fs . open [j ].Place {
128
- return fs . open [i ].Place < fs . open [j ].Place
123
+ if ( * fs ) [i ].Place != ( * fs ) [j ].Place {
124
+ return ( * fs ) [i ].Place < ( * fs ) [j ].Place
129
125
}
130
126
131
127
// Simply check values.
132
- return fs . open [i ].Val < fs . open [j ].Val
128
+ return ( * fs ) [i ].Val < ( * fs ) [j ].Val
133
129
134
130
}
135
131
136
132
func (fs * formatState ) Swap (i , j int ) {
137
- fs . open [i ], fs . open [j ] = fs . open [j ], fs . open [i ]
133
+ ( * fs ) [i ], ( * fs ) [j ] = ( * fs ) [j ], ( * fs ) [i ]
138
134
}
0 commit comments