OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [exp/] [html/] [node.go] - Blame information for rev 747

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// 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
// A NodeType is the type of a Node.
8
type NodeType int
9
 
10
const (
11
        ErrorNode NodeType = iota
12
        TextNode
13
        DocumentNode
14
        ElementNode
15
        CommentNode
16
        DoctypeNode
17
        scopeMarkerNode
18
)
19
 
20
// Section 12.2.3.3 says "scope markers are inserted when entering applet
21
// elements, buttons, object elements, marquees, table cells, and table
22
// captions, and are used to prevent formatting from 'leaking'".
23
var scopeMarker = Node{Type: scopeMarkerNode}
24
 
25
// A Node consists of a NodeType and some Data (tag name for element nodes,
26
// content for text) and are part of a tree of Nodes. Element nodes may also
27
// have a Namespace and contain a slice of Attributes. Data is unescaped, so
28
// that it looks like "a
29
//
30
// An empty Namespace implies a "http://www.w3.org/1999/xhtml" namespace.
31
// Similarly, "math" is short for "http://www.w3.org/1998/Math/MathML", and
32
// "svg" is short for "http://www.w3.org/2000/svg".
33
type Node struct {
34
        Parent    *Node
35
        Child     []*Node
36
        Type      NodeType
37
        Data      string
38
        Namespace string
39
        Attr      []Attribute
40
}
41
 
42
// Add adds a node as a child of n.
43
// It will panic if the child's parent is not nil.
44
func (n *Node) Add(child *Node) {
45
        if child.Parent != nil {
46
                panic("html: Node.Add called for a child Node that already has a parent")
47
        }
48
        child.Parent = n
49
        n.Child = append(n.Child, child)
50
}
51
 
52
// Remove removes a node as a child of n.
53
// It will panic if the child's parent is not n.
54
func (n *Node) Remove(child *Node) {
55
        if child.Parent == n {
56
                child.Parent = nil
57
                for i, m := range n.Child {
58
                        if m == child {
59
                                copy(n.Child[i:], n.Child[i+1:])
60
                                j := len(n.Child) - 1
61
                                n.Child[j] = nil
62
                                n.Child = n.Child[:j]
63
                                return
64
                        }
65
                }
66
        }
67
        panic("html: Node.Remove called for a non-child Node")
68
}
69
 
70
// reparentChildren reparents all of src's child nodes to dst.
71
func reparentChildren(dst, src *Node) {
72
        for _, n := range src.Child {
73
                if n.Parent != src {
74
                        panic("html: nodes have an inconsistent parent/child relationship")
75
                }
76
                n.Parent = dst
77
        }
78
        dst.Child = append(dst.Child, src.Child...)
79
        src.Child = nil
80
}
81
 
82
// clone returns a new node with the same type, data and attributes.
83
// The clone has no parent and no children.
84
func (n *Node) clone() *Node {
85
        m := &Node{
86
                Type: n.Type,
87
                Data: n.Data,
88
                Attr: make([]Attribute, len(n.Attr)),
89
        }
90
        copy(m.Attr, n.Attr)
91
        return m
92
}
93
 
94
// nodeStack is a stack of nodes.
95
type nodeStack []*Node
96
 
97
// pop pops the stack. It will panic if s is empty.
98
func (s *nodeStack) pop() *Node {
99
        i := len(*s)
100
        n := (*s)[i-1]
101
        *s = (*s)[:i-1]
102
        return n
103
}
104
 
105
// top returns the most recently pushed node, or nil if s is empty.
106
func (s *nodeStack) top() *Node {
107
        if i := len(*s); i > 0 {
108
                return (*s)[i-1]
109
        }
110
        return nil
111
}
112
 
113
// index returns the index of the top-most occurence of n in the stack, or -1
114
// if n is not present.
115
func (s *nodeStack) index(n *Node) int {
116
        for i := len(*s) - 1; i >= 0; i-- {
117
                if (*s)[i] == n {
118
                        return i
119
                }
120
        }
121
        return -1
122
}
123
 
124
// insert inserts a node at the given index.
125
func (s *nodeStack) insert(i int, n *Node) {
126
        (*s) = append(*s, nil)
127
        copy((*s)[i+1:], (*s)[i:])
128
        (*s)[i] = n
129
}
130
 
131
// remove removes a node from the stack. It is a no-op if n is not present.
132
func (s *nodeStack) remove(n *Node) {
133
        i := s.index(n)
134
        if i == -1 {
135
                return
136
        }
137
        copy((*s)[i:], (*s)[i+1:])
138
        j := len(*s) - 1
139
        (*s)[j] = nil
140
        *s = (*s)[:j]
141
}
142
 
143
// TODO(nigeltao): forTag no longer used. Should it be deleted?
144
 
145
// forTag returns the top-most element node with the given tag.
146
func (s *nodeStack) forTag(tag string) *Node {
147
        for i := len(*s) - 1; i >= 0; i-- {
148
                n := (*s)[i]
149
                if n.Type == ElementNode && n.Data == tag {
150
                        return n
151
                }
152
        }
153
        return nil
154
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.