Skip to content

Commit

Permalink
removing a bit of duplicateion of regex matching
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Smith committed Oct 14, 2019
1 parent 6d3f973 commit ae99c5d
Showing 1 changed file with 18 additions and 28 deletions.
46 changes: 18 additions & 28 deletions sanitize.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,20 +233,7 @@ func (p *Policy) sanitize(r io.Reader) *bytes.Buffer {

aps, ok := p.elsAndAttrs[token.Data]
if !ok {
// check if we have any regex that match the element
if aps == nil {
aps = make(map[string]attrPolicy, 0)
}
matched := false
for regex, attrs := range p.elsMatchingAndAttrs {
if regex.MatchString(token.Data) {
matched = true
// append matching attrs on as could have multiple depending on match
for k, v := range attrs {
aps[k] = v
}
}
}
aa, matched := p.matchRegex(token.Data)
if !matched {
if _, ok := p.setOfElementsToSkipContent[token.Data]; ok {
skipElementContent = true
Expand All @@ -257,7 +244,7 @@ func (p *Policy) sanitize(r io.Reader) *bytes.Buffer {
}
break
}

aps = aa
}
if len(token.Attr) != 0 {
token.Attr = p.sanitizeAttrs(token.Data, token.Attr, aps)
Expand Down Expand Up @@ -330,25 +317,14 @@ func (p *Policy) sanitize(r io.Reader) *bytes.Buffer {

aps, ok := p.elsAndAttrs[token.Data]
if !ok {
if aps == nil {
aps = make(map[string]attrPolicy, 0)
}
matched := false
for regex, attrs := range p.elsMatchingAndAttrs {
if regex.MatchString(token.Data) {
matched = true
// append matching attrs on as could have multiple depending on match
for k, v := range attrs {
aps[k] = v
}
}
}
aa, matched := p.matchRegex(token.Data)
if !matched {
if p.addSpaces && !matched {
buff.WriteString(" ")
}
break
}
aps = aa
}

if len(token.Attr) != 0 {
Expand Down Expand Up @@ -897,3 +873,17 @@ func removeUnicode(value string) string {
}
return substitutedValue
}

func (p *Policy) matchRegex(elementName string ) (map[string]attrPolicy, bool) {
aps := make(map[string]attrPolicy, 0)
matched := false
for regex, attrs := range p.elsMatchingAndAttrs {
if regex.MatchString(elementName) {
matched = true
for k, v := range attrs {
aps[k] = v
}
}
}
return aps, matched
}

0 comments on commit ae99c5d

Please sign in to comment.