当前位置: 首页>>代码示例>>Golang>>正文


Golang Node.Attr方法代码示例

本文整理汇总了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)
}
开发者ID:ReanGD,项目名称:go-web-search,代码行数:7,代码来源:minification_html.go

示例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)
	}

}
开发者ID:aarzilli,项目名称:tools,代码行数:32,代码来源:06_img2link.go

示例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)
	}
}
开发者ID:justintan,项目名称:gox,代码行数:27,代码来源:attr.go

示例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
}
开发者ID:documize,项目名称:html-diff,代码行数:7,代码来源:nodes.go

示例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...)
}
开发者ID:Masterminds,项目名称:engine,代码行数:34,代码来源:form.go

示例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)
	}

}
开发者ID:aarzilli,项目名称:tools,代码行数:27,代码来源:01_cleanse.go

示例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)))
}
开发者ID:BenLubar,项目名称:htmlcleaner,代码行数:47,代码来源:cleaner.go

示例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})
	}
}
开发者ID:chzyer,项目名称:pocket,代码行数:8,代码来源:utils.go

示例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
}
开发者ID:chzyer,项目名称:pocket,代码行数:9,代码来源:utils.go

示例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
}
开发者ID:bcampbell,项目名称:arts,代码行数:9,代码来源:tidy.go

示例11: clean

// clean normalises styles/colspan and removes any CleanTags specified, along with newlines;
// but also makes all the character handling (for example "&#160;" 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
}
开发者ID:documize,项目名称:html-diff,代码行数:48,代码来源:clean.go

示例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)
	}
}
开发者ID:justintan,项目名称:gox,代码行数:22,代码来源:attr.go

示例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")
	}
}
开发者ID:jordanpotter,项目名称:sitemapper,代码行数:13,代码来源:node_test.go

示例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
}
开发者ID:kristofer,项目名称:go-html-transform,代码行数:14,代码来源:node.go

示例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")
	}
}
开发者ID:jordanpotter,项目名称:sitemapper,代码行数:13,代码来源:node_test.go


注:本文中的golang.org/x/net/html.Node.Attr方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。