mirror of
https://github.com/russross/blackfriday.git
synced 2024-03-22 13:40:34 +08:00
Extract useful code to separate func
This commit is contained in:
parent
5405274d99
commit
e02c392dc6
43
html.go
43
html.go
|
@ -722,6 +722,29 @@ func isHtmlTag(tag []byte, tagname string) bool {
|
||||||
return found
|
return found
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Look for a character, but ignore it when it's in any kind of quotes, it
|
||||||
|
// might be JavaScript
|
||||||
|
func skipUntilCharIgnoreQuotes(html []byte, start int, char byte) int {
|
||||||
|
inSingleQuote := false
|
||||||
|
inDoubleQuote := false
|
||||||
|
inGraveQuote := false
|
||||||
|
i := start
|
||||||
|
for i < len(html) {
|
||||||
|
switch {
|
||||||
|
case html[i] == char && !inSingleQuote && !inDoubleQuote && !inGraveQuote:
|
||||||
|
return i
|
||||||
|
case html[i] == '\'':
|
||||||
|
inSingleQuote = !inSingleQuote
|
||||||
|
case html[i] == '"':
|
||||||
|
inDoubleQuote = !inDoubleQuote
|
||||||
|
case html[i] == '`':
|
||||||
|
inGraveQuote = !inGraveQuote
|
||||||
|
}
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
return start
|
||||||
|
}
|
||||||
|
|
||||||
func findHtmlTagPos(tag []byte, tagname string) (bool, int) {
|
func findHtmlTagPos(tag []byte, tagname string) (bool, int) {
|
||||||
i := 0
|
i := 0
|
||||||
if i < len(tag) && tag[0] != '<' {
|
if i < len(tag) && tag[0] != '<' {
|
||||||
|
@ -750,23 +773,9 @@ func findHtmlTagPos(tag []byte, tagname string) (bool, int) {
|
||||||
return false, -1
|
return false, -1
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now look for closing '>', but ignore it when it's in any kind of quotes,
|
rightAngle := skipUntilCharIgnoreQuotes(tag, i, '>')
|
||||||
// it might be JavaScript
|
if rightAngle > i {
|
||||||
inSingleQuote := false
|
return true, rightAngle
|
||||||
inDoubleQuote := false
|
|
||||||
inGraveQuote := false
|
|
||||||
for i < len(tag) {
|
|
||||||
switch {
|
|
||||||
case tag[i] == '>' && !inSingleQuote && !inDoubleQuote && !inGraveQuote:
|
|
||||||
return true, i
|
|
||||||
case tag[i] == '\'':
|
|
||||||
inSingleQuote = !inSingleQuote
|
|
||||||
case tag[i] == '"':
|
|
||||||
inDoubleQuote = !inDoubleQuote
|
|
||||||
case tag[i] == '`':
|
|
||||||
inGraveQuote = !inGraveQuote
|
|
||||||
}
|
|
||||||
i++
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false, -1
|
return false, -1
|
||||||
|
|
Loading…
Reference in New Issue
Block a user