all repos

rss-tools @ a5ac52722b131734c74504b6e6f4d9900536cac7

get rss feed from sources that(i need and) dont provide one

rss-tools/vendor/golang.org/x/net/html/node.go (view raw)

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
we're vendoring now, 7 days ago
1
// Copyright 2011 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
4
5
package html
6
7
import (
8
	"golang.org/x/net/html/atom"
9
)
10
11
// A NodeType is the type of a Node.
12
type NodeType uint32
13
14
//go:generate stringer -type NodeType
15
const (
16
	ErrorNode NodeType = iota
17
	TextNode
18
	DocumentNode
19
	ElementNode
20
	CommentNode
21
	DoctypeNode
22
	// RawNode nodes are not returned by the parser, but can be part of the
23
	// Node tree passed to func Render to insert raw HTML (without escaping).
24
	// If so, this package makes no guarantee that the rendered HTML is secure
25
	// (from e.g. Cross Site Scripting attacks) or well-formed.
26
	RawNode
27
	scopeMarkerNode
28
)
29
30
// Section 12.2.4.3 says "The markers are inserted when entering applet,
31
// object, marquee, template, td, th, and caption elements, and are used
32
// to prevent formatting from "leaking" into applet, object, marquee,
33
// template, td, th, and caption elements".
34
var scopeMarker = Node{Type: scopeMarkerNode}
35
36
// A Node consists of a NodeType and some Data (tag name for element nodes,
37
// content for text) and are part of a tree of Nodes. Element nodes may also
38
// have a Namespace and contain a slice of Attributes. Data is unescaped, so
39
// that it looks like "a<b" rather than "a&lt;b". For element nodes, DataAtom
40
// is the atom for Data, or zero if Data is not a known tag name.
41
//
42
// Node trees may be navigated using the link fields (Parent,
43
// FirstChild, and so on) or a range loop over iterators such as
44
// [Node.Descendants].
45
//
46
// An empty Namespace implies a "http://www.w3.org/1999/xhtml" namespace.
47
// Similarly, "math" is short for "http://www.w3.org/1998/Math/MathML", and
48
// "svg" is short for "http://www.w3.org/2000/svg".
49
type Node struct {
50
	Parent, FirstChild, LastChild, PrevSibling, NextSibling *Node
51
52
	Type      NodeType
53
	DataAtom  atom.Atom
54
	Data      string
55
	Namespace string
56
	Attr      []Attribute
57
}
58
59
// InsertBefore inserts newChild as a child of n, immediately before oldChild
60
// in the sequence of n's children. oldChild may be nil, in which case newChild
61
// is appended to the end of n's children.
62
//
63
// It will panic if newChild already has a parent or siblings.
64
func (n *Node) InsertBefore(newChild, oldChild *Node) {
65
	if newChild.Parent != nil || newChild.PrevSibling != nil || newChild.NextSibling != nil {
66
		panic("html: InsertBefore called for an attached child Node")
67
	}
68
	var prev, next *Node
69
	if oldChild != nil {
70
		prev, next = oldChild.PrevSibling, oldChild
71
	} else {
72
		prev = n.LastChild
73
	}
74
	if prev != nil {
75
		prev.NextSibling = newChild
76
	} else {
77
		n.FirstChild = newChild
78
	}
79
	if next != nil {
80
		next.PrevSibling = newChild
81
	} else {
82
		n.LastChild = newChild
83
	}
84
	newChild.Parent = n
85
	newChild.PrevSibling = prev
86
	newChild.NextSibling = next
87
}
88
89
// AppendChild adds a node c as a child of n.
90
//
91
// It will panic if c already has a parent or siblings.
92
func (n *Node) AppendChild(c *Node) {
93
	if c.Parent != nil || c.PrevSibling != nil || c.NextSibling != nil {
94
		panic("html: AppendChild called for an attached child Node")
95
	}
96
	last := n.LastChild
97
	if last != nil {
98
		last.NextSibling = c
99
	} else {
100
		n.FirstChild = c
101
	}
102
	n.LastChild = c
103
	c.Parent = n
104
	c.PrevSibling = last
105
}
106
107
// RemoveChild removes a node c that is a child of n. Afterwards, c will have
108
// no parent and no siblings.
109
//
110
// It will panic if c's parent is not n.
111
func (n *Node) RemoveChild(c *Node) {
112
	if c.Parent != n {
113
		panic("html: RemoveChild called for a non-child Node")
114
	}
115
	if n.FirstChild == c {
116
		n.FirstChild = c.NextSibling
117
	}
118
	if c.NextSibling != nil {
119
		c.NextSibling.PrevSibling = c.PrevSibling
120
	}
121
	if n.LastChild == c {
122
		n.LastChild = c.PrevSibling
123
	}
124
	if c.PrevSibling != nil {
125
		c.PrevSibling.NextSibling = c.NextSibling
126
	}
127
	c.Parent = nil
128
	c.PrevSibling = nil
129
	c.NextSibling = nil
130
}
131
132
// reparentChildren reparents all of src's child nodes to dst.
133
func reparentChildren(dst, src *Node) {
134
	for {
135
		child := src.FirstChild
136
		if child == nil {
137
			break
138
		}
139
		src.RemoveChild(child)
140
		dst.AppendChild(child)
141
	}
142
}
143
144
// clone returns a new node with the same type, data and attributes.
145
// The clone has no parent, no siblings and no children.
146
func (n *Node) clone() *Node {
147
	m := &Node{
148
		Type:     n.Type,
149
		DataAtom: n.DataAtom,
150
		Data:     n.Data,
151
		Attr:     make([]Attribute, len(n.Attr)),
152
	}
153
	copy(m.Attr, n.Attr)
154
	return m
155
}
156
157
// nodeStack is a stack of nodes.
158
type nodeStack []*Node
159
160
// pop pops the stack. It will panic if s is empty.
161
func (s *nodeStack) pop() *Node {
162
	i := len(*s)
163
	n := (*s)[i-1]
164
	*s = (*s)[:i-1]
165
	return n
166
}
167
168
// top returns the most recently pushed node, or nil if s is empty.
169
func (s *nodeStack) top() *Node {
170
	if i := len(*s); i > 0 {
171
		return (*s)[i-1]
172
	}
173
	return nil
174
}
175
176
// index returns the index of the top-most occurrence of n in the stack, or -1
177
// if n is not present.
178
func (s *nodeStack) index(n *Node) int {
179
	for i := len(*s) - 1; i >= 0; i-- {
180
		if (*s)[i] == n {
181
			return i
182
		}
183
	}
184
	return -1
185
}
186
187
// contains returns whether a is within s.
188
func (s *nodeStack) contains(a atom.Atom) bool {
189
	for _, n := range *s {
190
		if n.DataAtom == a && n.Namespace == "" {
191
			return true
192
		}
193
	}
194
	return false
195
}
196
197
// insert inserts a node at the given index.
198
func (s *nodeStack) insert(i int, n *Node) {
199
	(*s) = append(*s, nil)
200
	copy((*s)[i+1:], (*s)[i:])
201
	(*s)[i] = n
202
}
203
204
// remove removes a node from the stack. It is a no-op if n is not present.
205
func (s *nodeStack) remove(n *Node) {
206
	i := s.index(n)
207
	if i == -1 {
208
		return
209
	}
210
	copy((*s)[i:], (*s)[i+1:])
211
	j := len(*s) - 1
212
	(*s)[j] = nil
213
	*s = (*s)[:j]
214
}
215
216
type insertionModeStack []insertionMode
217
218
func (s *insertionModeStack) pop() (im insertionMode) {
219
	i := len(*s)
220
	im = (*s)[i-1]
221
	*s = (*s)[:i-1]
222
	return im
223
}
224
225
func (s *insertionModeStack) top() insertionMode {
226
	if i := len(*s); i > 0 {
227
		return (*s)[i-1]
228
	}
229
	return nil
230
}