@@ -136,20 +136,20 @@ func (hcd *highlightCodeDiff) convertToPlaceholders(htmlCode string) string {
136
136
}
137
137
138
138
var tokenInMap string
139
- if strings .HasSuffix (token , "</" ) { // for closed tag
139
+ if strings .HasSuffix (token , "</" ) { // for closing tag
140
140
if len (tagStack ) == 0 {
141
- break // invalid diff result, no open tag but see close tag
141
+ break // invalid diff result, no opening tag but see closing tag
142
142
}
143
- // make sure the closed tag in map is related to the open tag, to make the diff algorithm can match the open/closed tags
144
- // the closed tag will be recorded in the map by key "</span><!-- <span the-open > -->" for "<span the-open >"
143
+ // make sure the closing tag in map is related to the open tag, to make the diff algorithm can match the opening/closing tags
144
+ // the closing tag will be recorded in the map by key "</span><!-- <span the-opening > -->" for "<span the-opening >"
145
145
tokenInMap = token + "<!-- " + tagStack [len (tagStack )- 1 ] + "-->"
146
146
tagStack = tagStack [:len (tagStack )- 1 ]
147
- } else if strings . HasPrefix ( token , "<" ) { // for open tag
147
+ } else if token [ 0 ] == '<' { // for opening tag
148
148
tokenInMap = token
149
149
tagStack = append (tagStack , token )
150
- } else if strings . HasPrefix ( token , "&" ) { // for html entity
150
+ } else if token [ 0 ] == '&' { // for html entity
151
151
tokenInMap = token
152
- }
152
+ } // else: impossible
153
153
154
154
// remember the placeholder and token in the map
155
155
placeholder , ok := hcd .tokenPlaceholderMap [tokenInMap ]
@@ -191,31 +191,31 @@ func (hcd *highlightCodeDiff) recoverOneDiff(diff *diffmatchpatch.Diff) {
191
191
continue
192
192
}
193
193
var tokenToRecover string
194
- if token [ 1 ] == '/' { // Closing tag
194
+ if strings . HasPrefix ( token , "</" ) { // for closing tag
195
195
// only get the tag itself, ignore the trailing comment (for how the comment is generated, see the code in `convert` function)
196
196
tokenToRecover = token [:strings .IndexByte (token , '>' )+ 1 ]
197
197
if len (tagStack ) == 0 {
198
- continue // if no open tag in stack yet, skip the closed tag
198
+ continue // if no opening tag in stack yet, skip the closing tag
199
199
}
200
200
tagStack = tagStack [:len (tagStack )- 1 ]
201
- } else if token [0 ] == '<' {
201
+ } else if token [0 ] == '<' { // for opening tag
202
202
tokenToRecover = token
203
203
tagStack = append (tagStack , token )
204
- } else { // html entity
204
+ } else if token [ 0 ] == '&' { // for html entity
205
205
tokenToRecover = token
206
- }
206
+ } // else: impossible
207
207
sb .WriteString (tokenToRecover )
208
208
}
209
209
210
210
if len (tagStack ) > 0 {
211
- // close all open tags
211
+ // close all opening tags
212
212
for i := len (tagStack ) - 1 ; i >= 0 ; i -- {
213
213
tagToClose := tagStack [i ]
214
- // get the closed tag "</span>" from "<span class=...>" or "<span>"
214
+ // get the closing tag "</span>" from "<span class=...>" or "<span>"
215
215
pos := strings .IndexAny (tagToClose , " >" )
216
216
if pos != - 1 {
217
217
sb .WriteString ("</" + tagToClose [1 :pos ] + ">" )
218
- } // else: impossible. every tag was pushed into the stack by the code above and is valid HTML open tag
218
+ } // else: impossible. every tag was pushed into the stack by the code above and is valid HTML opening tag
219
219
}
220
220
}
221
221
0 commit comments