mirror of
https://github.com/miniflux/v2.git
synced 2025-06-27 16:36:00 +00:00
Refactor Sanitize
- Use `token.String()` instead of `html.EscapeString(token.Data)` - Refactor conditions to highlight their similitude, enabling further refactoring This refactoring brings forth at least one bug: `tagStack` is never emptied.
This commit is contained in:
parent
cc885bbabb
commit
58178d90cb
1 changed files with 16 additions and 10 deletions
|
@ -111,7 +111,7 @@ func Sanitize(baseURL, input string) string {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer.WriteString(html.EscapeString(token.Data))
|
buffer.WriteString(token.String())
|
||||||
case html.StartTagToken:
|
case html.StartTagToken:
|
||||||
parentTag = tagName
|
parentTag = tagName
|
||||||
|
|
||||||
|
@ -121,36 +121,42 @@ func Sanitize(baseURL, input string) string {
|
||||||
|
|
||||||
if isBlockedTag(tagName) || slices.ContainsFunc(token.Attr, func(attr html.Attribute) bool { return attr.Key == "hidden" }) {
|
if isBlockedTag(tagName) || slices.ContainsFunc(token.Attr, func(attr html.Attribute) bool { return attr.Key == "hidden" }) {
|
||||||
blockedStack = append(blockedStack, tagName)
|
blockedStack = append(blockedStack, tagName)
|
||||||
} else if len(blockedStack) == 0 && isValidTag(tagName) {
|
continue
|
||||||
attrNames, htmlAttributes := sanitizeAttributes(baseURL, tagName, token.Attr)
|
}
|
||||||
|
|
||||||
|
if len(blockedStack) == 0 && isValidTag(tagName) {
|
||||||
|
attrNames, htmlAttributes := sanitizeAttributes(baseURL, tagName, token.Attr)
|
||||||
if hasRequiredAttributes(tagName, attrNames) {
|
if hasRequiredAttributes(tagName, attrNames) {
|
||||||
if len(attrNames) > 0 {
|
if len(attrNames) > 0 {
|
||||||
buffer.WriteString("<" + tagName + " " + htmlAttributes + ">")
|
buffer.WriteString("<" + tagName + " " + htmlAttributes + ">")
|
||||||
} else {
|
} else {
|
||||||
buffer.WriteString("<" + tagName + ">")
|
buffer.WriteString(token.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
tagStack = append(tagStack, tagName)
|
tagStack = append(tagStack, tagName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case html.EndTagToken:
|
case html.EndTagToken:
|
||||||
if len(blockedStack) > 0 && blockedStack[len(blockedStack)-1] == tagName {
|
if len(blockedStack) == 0 {
|
||||||
blockedStack = blockedStack[:len(blockedStack)-1]
|
if isValidTag(tagName) && slices.Contains(tagStack, tagName) {
|
||||||
} else if len(blockedStack) == 0 && isValidTag(tagName) && slices.Contains(tagStack, tagName) {
|
buffer.WriteString(token.String())
|
||||||
buffer.WriteString("</" + tagName + ">")
|
}
|
||||||
|
} else {
|
||||||
|
if blockedStack[len(blockedStack)-1] == tagName {
|
||||||
|
blockedStack = blockedStack[:len(blockedStack)-1]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
case html.SelfClosingTagToken:
|
case html.SelfClosingTagToken:
|
||||||
if isPixelTracker(tagName, token.Attr) {
|
if isPixelTracker(tagName, token.Attr) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if isValidTag(tagName) && len(blockedStack) == 0 {
|
if len(blockedStack) == 0 && isValidTag(tagName) {
|
||||||
attrNames, htmlAttributes := sanitizeAttributes(baseURL, tagName, token.Attr)
|
attrNames, htmlAttributes := sanitizeAttributes(baseURL, tagName, token.Attr)
|
||||||
if hasRequiredAttributes(tagName, attrNames) {
|
if hasRequiredAttributes(tagName, attrNames) {
|
||||||
if len(attrNames) > 0 {
|
if len(attrNames) > 0 {
|
||||||
buffer.WriteString("<" + tagName + " " + htmlAttributes + "/>")
|
buffer.WriteString("<" + tagName + " " + htmlAttributes + "/>")
|
||||||
} else {
|
} else {
|
||||||
buffer.WriteString("<" + tagName + "/>")
|
buffer.WriteString(token.String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue