Source File
sanitize.go
Belonging Package
github.com/microcosm-cc/bluemonday
package bluemonday
import (
)
var (
dataAttribute = regexp.MustCompile("^data-.+")
dataAttributeXMLPrefix = regexp.MustCompile("^xml.+")
dataAttributeInvalidChars = regexp.MustCompile("[A-Z;]+")
)
return &
}
case html.CommentToken:
case html.StartTagToken:
= .Data
, := .elsAndAttrs[.Data]
if ! {
if , := .setOfElementsToSkipContent[.Data]; {
= true
++
}
if .addSpaces {
.WriteString(" ")
}
break
}
if len(.Attr) != 0 {
.Attr = .sanitizeAttrs(.Data, .Attr, )
}
if len(.Attr) == 0 {
if !.allowNoAttrs(.Data) {
= true
= append(, .Data)
if .addSpaces {
.WriteString(" ")
}
break
}
}
if ! {
.WriteString(.String())
}
case html.EndTagToken:
if == .Data {
= ""
}
if && [len()-1] == .Data {
= [:len()-1]
if len() == 0 {
= false
}
if .addSpaces {
.WriteString(" ")
}
break
}
if , := .elsAndAttrs[.Data]; ! {
if , := .setOfElementsToSkipContent[.Data]; {
--
if == 0 {
= false
}
}
if .addSpaces {
.WriteString(" ")
}
break
}
if ! {
.WriteString(.String())
}
case html.SelfClosingTagToken:
, := .elsAndAttrs[.Data]
if ! {
if .addSpaces {
.WriteString(" ")
}
break
}
if len(.Attr) != 0 {
.Attr = .sanitizeAttrs(.Data, .Attr, )
}
if len(.Attr) == 0 && !.allowNoAttrs(.Data) {
if .addSpaces {
.WriteString(" ")
}
break
}
if ! {
.WriteString(.String())
}
case html.TextToken:
if ! {
switch {
.WriteString(.Data)
.WriteString(.Data)
.WriteString(.String())
}
}
if isDataAttribute(.Key) {
= append(, )
continue
}
if , := .globalAttrs[.Key]; {
if .regexp != nil {
if .regexp.MatchString(.Val) {
= append(, )
}
} else {
= append(, )
}
}
}
return
if linkable() {
:= []html.Attribute{}
for , := range {
switch {
case "a", "area", "link":
if .Key == "href" {
if , := .validURL(.Val); {
.Val =
= append(, )
}
break
}
= append(, )
case "blockquote", "q":
if .Key == "cite" {
if , := .validURL(.Val); {
.Val =
= append(, )
}
break
}
= append(, )
case "img", "script":
if .Key == "src" {
if , := .validURL(.Val); {
.Val =
= append(, )
}
break
}
= append(, )
default:
= append(, )
}
}
=
}
if (.requireNoFollow ||
.requireNoFollowFullyQualifiedLinks ||
.addTargetBlankToFullyQualifiedLinks) &&
len() > 0 {
switch {
case "a", "area", "link":
var bool
var bool
for , := range {
if .Key == "href" {
= true
, := url.Parse(.Val)
if != nil {
continue
}
if .Host != "" {
= true
}
continue
}
}
if {
var (
bool
bool
)
:= (.requireNoFollow ||
&& .requireNoFollowFullyQualifiedLinks)
:= ( &&
.addTargetBlankToFullyQualifiedLinks)
:= []html.Attribute{}
for , := range {
var bool
if .Key == "rel" && {
if strings.Contains(.Val, "nofollow") {
= true
= append(, )
= true
} else {
.Val += " nofollow"
= true
= append(, )
= true
}
}
if == "a" && .Key == "target" {
if .Val == "_blank" {
= true
}
if && ! {
.Val = "_blank"
= true
= append(, )
= true
}
}
if ! {
= append(, )
}
}
if || {
=
}
if && ! {
:= html.Attribute{}
.Key = "rel"
.Val = "nofollow"
= append(, )
}
if == "a" && && ! {
:= html.Attribute{}
.Key = "target"
.Val = "_blank"
= true
= append(, )
}
, := url.Parse()
if != nil {
return "", false
}
if .Scheme != "" {
, := .allowURLSchemes[.Scheme]
if ! {
return "", false
}
if == nil || () == true {
return .String(), true
}
return "", false
}
if .allowRelativeURLs {
if .String() != "" {
return .String(), true
}
}
return "", false
}
return , true
}
func ( string) bool {
switch {
case "a", "area", "blockquote", "img", "link", "script":
return true
default:
return false
}
}
func ( string) bool {
if !dataAttribute.MatchString() {
return false
}
:= strings.Split(, "data-")
if len() == 1 {
return false
if dataAttributeXMLPrefix.MatchString([1]) {
return false
if dataAttributeInvalidChars.MatchString([1]) {
return false
}
return true
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |