本文整理汇总了Golang中golang.org/x/net/html.Node.RemoveChild方法的典型用法代码示例。如果您正苦于以下问题:Golang Node.RemoveChild方法的具体用法?Golang Node.RemoveChild怎么用?Golang Node.RemoveChild使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类golang.org/x/net/html.Node
的用法示例。
在下文中一共展示了Node.RemoveChild方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: CompactNode
func CompactNode(n *html.Node) {
var appendNodes []*html.Node
for c := n.FirstChild; c != nil; {
CompactNode(c)
if _mergeTextElements[c.Data] {
appendNodes = append(appendNodes, GetChildNodes(c)...)
log.Info("delete", c.Data)
c = RemoveNode(c)
} else if c.Type == html.ElementNode && c.FirstChild == nil && !_voidElements[c.Data] {
log.Info("delete", c.Data)
c = RemoveNode(c)
} else {
c = c.NextSibling
}
}
DetachNodes(appendNodes)
AppendChildNodes(n, appendNodes)
if n.FirstChild != nil && n.FirstChild.NextSibling == nil {
if n.FirstChild.Data == n.Data || (n.FirstChild.Data == "br" && (n.Data == "p" || n.Data == "div")) {
childNodes := GetChildNodes(n.FirstChild)
log.Info("delete", n.FirstChild.Data)
n.RemoveChild(n.FirstChild)
DetachNodes(childNodes)
AppendChildNodes(n, childNodes)
} else if n.FirstChild.Data == "img" && n.Data == "a" {
*n = *n.FirstChild
}
}
}
示例2: mergeNodes
func (u *parserUtils) mergeNodes(parent, prev, next *html.Node, addSeparator bool) *html.Node {
prevText := prev != nil && prev.Type == html.TextNode
nextText := next != nil && next.Type == html.TextNode
delim := ""
if addSeparator {
delim = " "
}
if prevText && nextText {
prev.Data = prev.Data + delim + next.Data
parent.RemoveChild(next)
return prev.NextSibling
}
if prevText {
prev.Data = prev.Data + delim
} else if nextText {
next.Data = delim + next.Data
} else if addSeparator {
newNode := &html.Node{
Type: html.TextNode,
Data: delim}
parent.InsertBefore(newNode, next)
}
return next
}
示例3: topDownV1
/*
div div
div p
p TO img
img p
p
Operates from the *middle* div.
Saves all children in inverted slice.
Removes each child and reattaches it one level higher.
Finally the intermediary, now childless div is removed.
\ /
\ /\ /
\_____/ \_____/
\ /
\_____/\_____/
\__________/ => Breaks are gone
\p1___p2___/ => Wrapping preserves breaks
*/
func topDownV1(n *html.Node, couple []string, parentType string) {
if noParent(n) {
return
}
p := n.Parent
parDiv := p.Type == html.ElementNode && p.Data == couple[0] // Parent is a div
iAmDiv := n.Type == html.ElementNode && n.Data == couple[1] // I am a div
noSiblings := n.PrevSibling == nil && n.NextSibling == nil
only1Child := n.FirstChild != nil && n.FirstChild == n.LastChild
svrlChildn := n.FirstChild != nil && n.FirstChild != n.LastChild
noChildren := n.FirstChild == nil
_, _ = noSiblings, noChildren
if parDiv && iAmDiv {
if only1Child || svrlChildn {
var children []*html.Node
for c := n.FirstChild; c != nil; c = c.NextSibling {
children = append([]*html.Node{c}, children...) // order inversion
}
insertionPoint := n.NextSibling
for _, c1 := range children {
n.RemoveChild(c1)
if c1.Type == html.TextNode || c1.Data == "a" {
// pf("wrapping %v\n", NodeTypeStr(c1.Type))
wrap := html.Node{Type: html.ElementNode, Data: "p",
Attr: []html.Attribute{html.Attribute{Key: "cfrm", Val: "div"}}}
wrap.FirstChild = c1
p.InsertBefore(&wrap, insertionPoint)
c1.Parent = &wrap
insertionPoint = &wrap
} else {
p.InsertBefore(c1, insertionPoint)
insertionPoint = c1
}
}
p.RemoveChild(n)
if p.Data != parentType {
p.Data = parentType
}
}
}
}
示例4: removeUnwanted
// We want to remove some children.
// A direct loop is impossible,
// since "NextSibling" is set to nil during Remove().
// Therefore:
// First assemble children separately.
// Then remove them.
func removeUnwanted(n *html.Node) {
cc := []*html.Node{}
for c := n.FirstChild; c != nil; c = c.NextSibling {
cc = append(cc, c)
}
for _, c := range cc {
if unwanteds[c.Data] {
n.RemoveChild(c)
}
}
}
示例5: removeUnwanted
func removeUnwanted(n *html.Node) {
cc := []*html.Node{}
for c := n.FirstChild; c != nil; c = c.NextSibling {
cc = append(cc, c)
}
for _, c := range cc {
if n.Type == html.ElementNode && n.Data == "script" || n.Type == html.CommentNode {
n.RemoveChild(c)
}
}
}
示例6: setNodeText
// Replace the given node's children with the given string.
func setNodeText(node *html.Node, s string) {
// remove all existing children
for node.FirstChild != nil {
node.RemoveChild(node.FirstChild)
}
// add the text
node.AppendChild(&html.Node{
Type: html.TextNode,
Data: s,
})
}
示例7: replaceNodeWithChildren
func replaceNodeWithChildren(n *html.Node) {
var next *html.Node
parent := n.Parent
for c := n.FirstChild; c != nil; c = next {
next = c.NextSibling
n.RemoveChild(c)
parent.InsertBefore(c, n)
}
parent.RemoveChild(n)
}
示例8: removeEmptyNodes
func removeEmptyNodes(n *html.Node, lvl int) {
// children
cc := []*html.Node{}
for c := n.FirstChild; c != nil; c = c.NextSibling {
cc = append(cc, c)
}
for _, c := range cc {
removeEmptyNodes(c, lvl+1)
}
// processing
// empty element nodes
if n.Type == html.ElementNode && n.Data == "img" {
src := attrX(n.Attr, "src")
if src == "" {
n.Parent.RemoveChild(n)
}
}
if n.Type == html.ElementNode && n.FirstChild == nil && n.Data == "a" {
href := attrX(n.Attr, "href")
if href == "#" || href == "" {
n.Parent.RemoveChild(n)
}
}
if n.Type == html.ElementNode && n.FirstChild == nil &&
(n.Data == "em" || n.Data == "strong") {
n.Parent.RemoveChild(n)
}
if n.Type == html.ElementNode && n.FirstChild == nil &&
(n.Data == "div" || n.Data == "span" || n.Data == "li" || n.Data == "p") {
n.Parent.RemoveChild(n)
}
// spans with less than 2 characters inside => flatten to text
only1Child := n.FirstChild != nil && n.FirstChild == n.LastChild
if n.Type == html.ElementNode &&
n.Data == "span" &&
only1Child &&
n.FirstChild.Type == html.TextNode &&
len(strings.TrimSpace(n.FirstChild.Data)) < 3 {
n.Type = html.TextNode
n.Data = n.FirstChild.Data
n.RemoveChild(n.FirstChild)
}
}
示例9: 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
}
示例10: sliceNode
// sliceNode returns the two halves of the HTML tree starting at node after
// splitting it at the given textual offset.
func sliceNode(offsets *nodeOffsets, node *html.Node, offset int) (*html.Node, *html.Node) {
origStart, origEnd := offsets.Bounds(node)
if origStart > offset || origEnd < offset {
log.Fatalf("sliceNode: offset %d out of node's span (%d → %d)", offset, origStart, origEnd)
}
n, m := copyNode(node), copyNode(node)
parent := node.Parent
if parent != nil {
parent.InsertBefore(n, node)
parent.InsertBefore(m, node)
parent.RemoveChild(node)
}
switch node.Type {
default:
log.Fatalf("Unhandled node kind: %d", node.Type)
case html.ElementNode:
child := node.FirstChild
for child != nil {
next := child.NextSibling
if _, end := offsets.Bounds(child); end <= offset {
node.RemoveChild(child)
n.AppendChild(child)
} else if start, _ := offsets.Bounds(child); start > offset {
node.RemoveChild(child)
m.AppendChild(child)
} else {
left, right := sliceNode(offsets, child, offset)
node.RemoveChild(left)
node.RemoveChild(right)
n.AppendChild(left)
m.AppendChild(right)
}
child = next
}
case html.TextNode:
mark := offset - origStart
n.Data = node.Data[:mark]
m.Data = node.Data[mark:]
}
if split := offsets.update(n, origStart); split != offset {
log.Fatalf("split %d ≠ %d", split, offset)
}
if newEnd := offsets.update(m, offset); newEnd != origEnd {
log.Fatalf("end %d ≠ %d", newEnd, origEnd)
}
return n, m
}
示例11: doMinify
// Minifies node and returns a minification Result.
func doMinify(node *html.Node, ctx *context) result {
prevWasWhitespace := false
var next *html.Node
rv := result{}
for child := node.FirstChild; child != nil; child = next {
next = child.NextSibling
script := getHTMLNodeAttr(child, "script", "src")
if rv.IndexHTMLBase == "" {
rv.IndexHTMLBase = getHTMLNodeAttr(child, "base", "href")
}
switch {
case strings.Contains(script, "libs/") && strings.HasSuffix(script, ".js"):
minFile := script[:len(script)-3] + ".min.js"
if _, err := os.Stat(filepath.Join(ctx.BaseDir, minFile)); err == nil {
replaceAttrValue(child, "src", minFile)
}
prevWasWhitespace = false
case strings.HasSuffix(script, ".js"):
if !ctx.FoundFirstAppScript {
ctx.FoundFirstAppScript = true
node.InsertBefore(makeAppMinJsNode(), child)
node.InsertBefore(makeNewLine(), child)
}
rv.AppScripts = append(rv.AppScripts, script)
node.RemoveChild(child)
case isWhitespaceText(child) && node.Type == html.ElementNode && node.Data == "head":
if !prevWasWhitespace {
node.InsertBefore(makeNewLine(), child)
}
node.RemoveChild(child)
prevWasWhitespace = true
default:
if isPluggableUIInjectionComment(child) {
rv.PluggableInjectionCount++
} else {
childResult := doMinify(child, ctx)
rv.merge(childResult)
}
prevWasWhitespace = false
}
}
return rv
}
示例12: cleanBody
// cleanBody removes unwanted HTML elements from the HTML body.
func (doc *Document) cleanBody(n *html.Node, level int) {
// removeNode returns true if a node should be removed from HTML document.
removeNode := func(c *html.Node, level int) bool {
return removeElements[c.DataAtom]
}
var curr *html.Node = n.FirstChild
var next *html.Node = nil
for ; curr != nil; curr = next {
// We have to remember the next sibling here because calling RemoveChild
// sets curr's NextSibling pointer to nil and we would quit the loop
// prematurely.
next = curr.NextSibling
if curr.Type == html.ElementNode {
if removeNode(curr, level) {
n.RemoveChild(curr)
} else {
doc.cleanBody(curr, level+1)
}
}
}
}
示例13: cleanChildren
func cleanChildren(c *Config, parent *html.Node) {
var children []*html.Node
for parent.FirstChild != nil {
child := parent.FirstChild
parent.RemoveChild(child)
children = append(children, filterNode(c, child))
}
if c.WrapText {
_, ok := c.wrap[parent.DataAtom]
if !ok && parent.DataAtom == 0 {
_, ok = c.wrapCustom[parent.Data]
}
if ok {
children = wrapText(children)
}
}
for _, child := range children {
parent.AppendChild(child)
}
}
示例14: deleteValuelessNodes
//return true if need to delete node, false another way
func deleteValuelessNodes(innode *html.Node) bool {
if innode.Type == html.CommentNode {
//fmt.Println("comment:" + innode.Data)
return true
}
if innode.Type == html.ElementNode {
//innode.Attr = []html.Attribute{}
if innode.Data == "script" || innode.Data == "meta" || innode.Data == "style" || innode.Data == "head" || innode.Data == "form" || innode.Data == "noscript" || innode.Data == "img" || innode.Data == "noindex" || innode.Data == "span" {
//fmt.Println("script: " + innode.Data)
return true
}
}
for node := innode.FirstChild; node != nil; {
if deleteValuelessNodes(node) {
tnode := node.NextSibling
innode.RemoveChild(node)
node = tnode
continue
}
node = node.NextSibling
}
return false
}
示例15: textifyNodeSubtree
func textifyNodeSubtree(n *html.Node) {
if n.Type == html.ElementNode {
nd := dom.Nd("text")
nd.Data = textifySubtreeBruteForce(n, 0)
nd.Data = stringspb.NormalizeInnerWhitespace(nd.Data)
cc := []*html.Node{}
for c := n.FirstChild; c != nil; c = c.NextSibling {
cc = append(cc, c)
}
for _, c := range cc {
n.RemoveChild(c)
}
n.AppendChild(nd)
nd2 := dom.Nd("br")
dom.InsertAfter(n, nd2)
}
}