本文整理汇总了Golang中golang.org/x/net/html.Node.Attr方法的典型用法代码示例。如果您正苦于以下问题:Golang Node.Attr方法的具体用法?Golang Node.Attr怎么用?Golang Node.Attr使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类golang.org/x/net/html.Node
的用法示例。
在下文中一共展示了Node.Attr方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: toDiv
func (m *minificationHTML) toDiv(node *html.Node) (*html.Node, error) {
node.DataAtom = atom.Div
node.Data = "div"
node.Attr = nil
return m.parseChildren(node)
}
示例2: img2Link
func img2Link(img *html.Node) {
if img.Data == "img" {
img.Data = "a"
for i := 0; i < len(img.Attr); i++ {
if img.Attr[i].Key == "src" {
img.Attr[i].Key = "href"
}
}
double := closureTextNodeExists(img)
imgContent := ""
title := attrX(img.Attr, "title")
if double {
imgContent = fmt.Sprintf("[img] %v %v | ",
"[ctdr]", // content title double removed
urlBeautify(attrX(img.Attr, "href")))
} else {
imgContent = fmt.Sprintf("[img] %v %v | ",
title,
urlBeautify(attrX(img.Attr, "href")))
}
img.Attr = attrSet(img.Attr, "cfrom", "img")
nd := dom.Nd("text", imgContent)
img.AppendChild(nd)
}
}
示例3: RemoveAttributes
func RemoveAttributes(n *html.Node, keepAttrs []string) {
attrs := make([]html.Attribute, 0, len(n.Attr))
dataAttrs := make(map[string]html.Attribute)
originalAttrs := make(map[string]html.Attribute)
for _, a := range n.Attr {
if indexOfString(keepAttrs, a.Key) >= 0 {
attrs = append(attrs, a)
originalAttrs[a.Key] = a
} else if i := strings.Index(a.Key, "data-"); i == 0 {
a.Key = a.Key[5:]
if len(a.Key) > 0 && indexOfString(keepAttrs, a.Key) >= 0 {
dataAttrs[a.Key] = a
}
}
}
for k, v := range dataAttrs {
if _, found := originalAttrs[k]; !found {
attrs = append(attrs, v)
}
}
n.Attr = attrs
for c := n.FirstChild; c != nil; c = c.NextSibling {
RemoveAttributes(c, keepAttrs)
}
}
示例4: copyNode
func copyNode(to, from *html.Node) {
to.Attr = from.Attr
to.Data = from.Data
to.DataAtom = from.DataAtom
to.Namespace = from.Namespace
to.Type = from.Type
}
示例5: Attach
// Attache attaches these attributes to an html.Node.
func (g HTML) Attach(node *html.Node) {
attrs := []html.Attribute{}
if g.ContentEditable > 0 {
if g.ContentEditable == OTrue {
attrs = attr(attrs, "contenteditable", "true")
} else {
attrs = attr(attrs, "contenteditable", "false")
}
}
if g.Hidden > 0 {
if g.Hidden == OTrue {
attrs = attr(attrs, "hidden", "true")
} else {
attrs = attr(attrs, "hidden", "false")
}
}
if len(g.Data) > 0 {
for k, v := range g.Data {
attrs = attr(attrs, k, v)
}
}
if len(g.Class) > 0 {
v := strings.Join(g.Class, " ")
attrs = attr(attrs, "class", v)
}
s := []string{"AccessKey", "Id", "Dir", "Lang", "Style", "TabIndex", "Title", "Translate"}
attrs = append(attrs, structToAttrs(g, s...)...)
node.Attr = append(node.Attr, attrs...)
}
示例6: cleanseDom
// cleansDom performs brute reduction and simplification
//
func cleanseDom(n *html.Node, lvl int) {
n.Attr = removeAttr(n.Attr, unwantedAttrs)
// Children
for c := n.FirstChild; c != nil; c = c.NextSibling {
cleanseDom(c, lvl+1)
}
if directlyRemoveUnwanted {
removeUnwanted(n)
} else {
convertUnwanted(n)
}
// ---
convertExotic(n)
// one time text normalization
if n.Type == html.TextNode {
n.Data = stringspb.NormalizeInnerWhitespace(n.Data)
}
}
示例7: cleanNode
func cleanNode(c *Config, n *html.Node) *html.Node {
allowedAttr, ok1 := c.elem[n.DataAtom]
customAttr, ok2 := c.elemCustom[n.Data]
if ok1 || ok2 {
cleanChildren(c, n)
haveSrc := false
attrs := n.Attr
n.Attr = make([]html.Attribute, 0, len(attrs))
for _, attr := range attrs {
a := atom.Lookup([]byte(attr.Key))
re1, ok1 := allowedAttr[a]
re2, ok2 := customAttr[attr.Key]
_, ok3 := c.attr[a]
_, ok4 := c.attrCustom[attr.Key]
if attr.Namespace != "" || (!ok1 && !ok2 && !ok3 && !ok4) {
continue
}
if !cleanURL(c, a, &attr) {
continue
}
if re1 != nil && !re1.MatchString(attr.Val) {
continue
}
if re2 != nil && !re2.MatchString(attr.Val) {
continue
}
haveSrc = haveSrc || a == atom.Src
n.Attr = append(n.Attr, attr)
}
if n.DataAtom == atom.Img && !haveSrc {
// replace it with an empty text node
return &html.Node{Type: html.TextNode}
}
return n
}
return text(html.UnescapeString(Render(n)))
}
示例8: setAttr
func setAttr(key, val string, n *html.Node) {
attr := getAttr(key, n)
if attr != nil {
attr.Val = val
} else {
n.Attr = append(n.Attr, html.Attribute{Key: key, Val: val})
}
}
示例9: removeAttr
func removeAttr(key string, n *html.Node) {
for i := 0; i < len(n.Attr); i++ {
if n.Attr[i].Key == key {
n.Attr = append(n.Attr[:i], n.Attr[i+1:]...)
return
}
}
return
}
示例10: filterAttrs
func filterAttrs(n *html.Node, fn func(*html.Attribute) bool) {
var out = make([]html.Attribute, 0)
for _, a := range n.Attr {
if fn(&a) {
out = append(out, a)
}
}
n.Attr = out
}
示例11: clean
// clean normalises styles/colspan and removes any CleanTags specified, along with newlines;
// but also makes all the character handling (for example " " as utf-8) the same.
// It returns the estimated number of treeRunes that will be used.
// TODO more cleaning of the input HTML, as required.
func (c *Config) clean(n *html.Node) int {
size := 1
switch n.Type {
case html.ElementNode:
for ai := 0; ai < len(n.Attr); ai++ {
a := n.Attr[ai]
switch {
case strings.ToLower(a.Key) == "style":
if strings.TrimSpace(a.Val) == "" { // delete empty styles
n.Attr = delAttr(n.Attr, ai)
ai--
} else { // tidy non-empty styles
// TODO there could be more here to make sure the style entries are in the same order etc.
n.Attr[ai].Val = strings.Replace(a.Val, " ", "", -1)
if !strings.HasSuffix(n.Attr[ai].Val, ";") {
n.Attr[ai].Val += ";"
}
}
case n.DataAtom == atom.Td &&
strings.ToLower(a.Key) == "colspan" &&
strings.TrimSpace(a.Val) == "1":
n.Attr = delAttr(n.Attr, ai)
ai--
}
}
case html.TextNode:
n.Data = htm.UnescapeString(n.Data)
size += utf8.RuneCountInString(n.Data) - 1 // len(n.Data) would be faster, but use more memory
}
searchChildren:
for ch := n.FirstChild; ch != nil; ch = ch.NextSibling {
switch ch.Type {
case html.ElementNode:
for _, rr := range c.CleanTags {
if rr == ch.Data {
n.RemoveChild(ch)
goto searchChildren
}
}
}
size += c.clean(ch)
}
return size
}
示例12: RemoveSomeAttributes
func RemoveSomeAttributes(n *html.Node, someAttrs []string) {
attrMap := make(map[string]bool, len(someAttrs))
for _, attr := range someAttrs {
attrMap[attr] = true
}
for i := len(n.Attr) - 1; i >= 0; i-- {
a := n.Attr[i]
flag, _ := attrMap[a.Key]
if flag {
if i < len(n.Attr)-1 {
n.Attr = append(n.Attr[0:i], n.Attr[i+1:]...)
} else {
n.Attr = n.Attr[0:i]
}
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
RemoveSomeAttributes(n, someAttrs)
}
}
示例13: TestIsStylesheetNode
func TestIsStylesheetNode(t *testing.T) {
n := html.Node{Data: "link"}
if isStylesheetNode(&n) {
t.Errorf("Exepected node to not be a stylesheet node")
}
n.Attr = []html.Attribute{
html.Attribute{Key: "rel", Val: "stylesheet"},
}
if !isStylesheetNode(&n) {
t.Errorf("Exepected node to be a stylesheet node")
}
}
示例14: CloneNode
// CloneNode makes a copy of a Node with all descendants.
func CloneNode(n *exphtml.Node) *exphtml.Node {
clone := new(exphtml.Node)
clone.Type = n.Type
clone.DataAtom = n.DataAtom
clone.Data = n.Data
clone.Attr = make([]exphtml.Attribute, len(n.Attr))
copy(clone.Attr, n.Attr)
for c := n.FirstChild; c != nil; c = c.NextSibling {
nc := CloneNode(c)
clone.AppendChild(nc)
}
return clone
}
示例15: TestIsIcoNode
func TestIsIcoNode(t *testing.T) {
n := html.Node{Data: "link"}
if isIcoNode(&n) {
t.Errorf("Exepected node to not be a ico node")
}
n.Attr = []html.Attribute{
html.Attribute{Key: "rel", Val: "icon"},
}
if !isIcoNode(&n) {
t.Errorf("Exepected node to be a ico node")
}
}