-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpb_accessor.go
147 lines (126 loc) · 3.1 KB
/
pb_accessor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package amphtml
import (
"regexp"
"strconv"
"strings"
"github.com/favclip/ampassador/amppb"
"github.com/favclip/html2html"
)
type wrappedRules struct {
targetHTMLFormat amppb.TagSpec_HtmlFormat
rules *amppb.ValidatorRules
}
type attrSpecEx amppb.AttrSpec
func newWrappedRules(text string) (*wrappedRules, error) {
rules, err := amppb.ParseRules(text)
if err != nil {
return nil, err
}
w := &wrappedRules{
targetHTMLFormat: amppb.TagSpec_AMP,
rules: rules,
}
return w, nil
}
func (w *wrappedRules) countTagSpecs(tagName string) int {
tagName = strings.ToLower(tagName)
count := 0
for _, tagSpec := range w.rules.GetTags() {
if htmlFormats := tagSpec.GetHtmlFormat(); len(htmlFormats) != 0 {
found := false
for _, htmlFormat := range htmlFormats {
if htmlFormat == w.targetHTMLFormat {
found = true
break
}
}
if !found {
continue
}
}
if strings.ToLower(tagSpec.GetTagName()) == tagName {
count++
}
}
return count
}
func (w *wrappedRules) getAttrSpecs(tagSpec *amppb.TagSpec) []*amppb.AttrSpec {
var resultList []*amppb.AttrSpec
resultList = append(resultList, tagSpec.GetAttrs()...)
for _, attrListName := range tagSpec.GetAttrLists() {
resultList = append(resultList, w.findAttrList(attrListName).GetAttrs()...)
}
resultList = append(resultList, w.findAttrList("$GLOBAL_ATTRS").GetAttrs()...)
return resultList
}
func (w *wrappedRules) findAttrList(attrListName string) *amppb.AttrList {
for _, attrList := range w.rules.GetAttrLists() {
if attrList.GetName() == attrListName {
return attrList
}
}
return nil
}
func isAttrSpecMatch(attrSpec *amppb.AttrSpec, attr *html2html.Attr) bool {
if attr == nil {
return false
}
if attrSpec.GetName() != attr.Key {
return false
}
if attrSpec.Value != nil && attr.Value != attrSpec.GetValue() {
return false
}
if attrSpec.ValueCasei != nil && strings.ToLower(attr.Value) != attrSpec.GetValueCasei() {
return false
}
if attrSpec.ValueRegex != nil {
re, err := regexp.Compile(attrSpec.GetValueRegex())
if err != nil {
panic(err)
}
if !re.MatchString(attr.Value) {
return false
}
}
if attrSpec.ValueRegexCasei != nil {
re, err := regexp.Compile("(?i)" + attrSpec.GetValueRegexCasei())
if err != nil {
panic(err)
}
if !re.MatchString(attr.Value) {
return false
}
}
if attrSpec.BlacklistedValueRegex != nil {
re, err := regexp.Compile("(?i)" + attrSpec.GetBlacklistedValueRegex())
if err != nil {
panic(err)
}
if !re.MatchString(attr.Value) {
return false
}
}
if attrSpec.ValueProperties != nil {
valueMap := parsePropertiesValue(attr.Value)
for _, propSpec := range attrSpec.GetValueProperties().GetProperties() {
if v, ok := valueMap[propSpec.GetName()]; ok {
if propSpec.Value != nil && propSpec.GetValue() != v {
return false
}
if propSpec.ValueDouble != nil {
v, err := strconv.ParseFloat(attr.Value, 64)
if err != nil {
return false
}
if propSpec.GetValueDouble() != v {
return false
}
}
} else if propSpec.GetMandatory() {
return false
}
}
}
return true
}