github.com/aarzilli/tools@v0.0.0-20151123112009-0d27094f75e0/net/http/domclean2/funcs_attr.go (about) 1 package domclean2 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/pbberlin/tools/stringspb" 8 "golang.org/x/net/html" 9 ) 10 11 func attrsX(attributes []html.Attribute, keys []string) (s string) { 12 for _, a := range attributes { 13 for i := 0; i < len(keys); i++ { 14 if keys[i] == a.Key { 15 s += fmt.Sprintf("%v is %v\n", a.Key, a.Val) 16 } 17 } 18 } 19 return 20 } 21 22 func attrX(attributes []html.Attribute, key string) (s string) { 23 for _, a := range attributes { 24 if key == a.Key { 25 s = a.Val 26 break 27 } 28 } 29 return 30 } 31 32 func attrSet(attrs []html.Attribute, key, val string) []html.Attribute { 33 for i, a := range attrs { 34 if a.Key == key { 35 attrs[i].Val = val 36 return attrs 37 } 38 } 39 // attr does not exist => append it 40 attrs = append(attrs, html.Attribute{Key: key, Val: val}) 41 return attrs 42 } 43 44 func removeAttr(attributes []html.Attribute, removeKeys map[string]bool) []html.Attribute { 45 46 ret := []html.Attribute{} 47 var alt, title string 48 49 for _, a := range attributes { 50 a.Key = strings.TrimSpace(a.Key) 51 a.Val = strings.TrimSpace(a.Val) 52 a.Val = stringspb.NormalizeInnerWhitespace(a.Val) // having encountered title or alt values with newlines 53 if removeKeys[a.Key] || strings.HasPrefix(a.Key, "data") { 54 // 55 } else { 56 if a.Key == "alt" { 57 alt = a.Val 58 } 59 if a.Key == "title" { 60 title = a.Val 61 } 62 attrDistinct[a.Key]++ 63 ret = append(ret, a) 64 } 65 } 66 67 // normalize on title 68 if alt != "" && alt == title { 69 ret1 := []html.Attribute{} 70 for i := 0; i < len(ret); i++ { 71 if ret[i].Key != "alt" { 72 ret1 = append(ret1, ret[i]) 73 } 74 } 75 ret = ret1 76 } 77 78 // remove both 79 if alt == "" && alt == title { 80 ret1 := []html.Attribute{} 81 for i := 0; i < len(ret); i++ { 82 if ret[i].Key != "alt" && ret[i].Key != "title" { 83 ret1 = append(ret1, ret[i]) 84 } 85 } 86 ret = ret1 87 } 88 89 return ret 90 } 91 92 func Unused_addIdAttr(attributes []html.Attribute, id string) []html.Attribute { 93 hasId := false 94 for _, a := range attributes { 95 if a.Key == "id" { 96 hasId = true 97 break 98 } 99 } 100 if !hasId { 101 attributes = append(attributes, html.Attribute{"", "id", id}) 102 } 103 return attributes 104 }