-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathattribute.go
More file actions
115 lines (97 loc) · 3.12 KB
/
Copy pathattribute.go
File metadata and controls
115 lines (97 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package helium
import "github.com/lestrrat-go/helium/enum"
type Enumeration []string
// Attribute represents an XML attribute (libxml2: xmlAttr).
type Attribute struct {
docnode
atype enum.AttributeType
defaultAttr bool
ns *Namespace
}
func newAttribute(name string, ns *Namespace) *Attribute {
attr := &Attribute{}
attr.etype = AttributeNode
attr.name = name
attr.ns = ns
return attr
}
// NextAttribute is a thin wrapper around NextSibling() so that the
// caller does not have to constantly type assert
func (n *Attribute) NextAttribute() *Attribute {
attr, _ := AsNode[*Attribute](n.NextSibling())
return attr
}
// AddChild adds cur as a child of this attribute node. For attributes
// the child is typically a text node holding the attribute value.
func (n *Attribute) AddChild(cur Node) error {
return addChild(n, cur)
}
// AppendText appends the given bytes to the attribute's text content.
func (n *Attribute) AppendText(b []byte) error {
return appendText(n, b)
}
// AddSibling inserts cur as the next sibling of this attribute,
// effectively appending another attribute to the owning element's
// attribute list.
func (n *Attribute) AddSibling(cur Node) error {
return addSibling(n, cur)
}
// Replace replaces this attribute node in its parent's attribute list
// with the given nodes.
func (n *Attribute) Replace(nodes ...Node) error {
return replaceNode(n, nodes...)
}
// SetTreeDoc recursively sets the owner document for this attribute
// and all of its children (e.g. its text-node value).
func (n *Attribute) SetTreeDoc(doc *Document) {
setTreeDoc(n, doc)
}
// AType returns the attribute type (e.g. enum.AttrID, enum.AttrCDATA).
func (n *Attribute) AType() enum.AttributeType {
return n.atype
}
// SetAType sets the attribute type.
func (n *Attribute) SetAType(v enum.AttributeType) {
n.atype = v
}
// SetDefault marks (or unmarks) this attribute as a default attribute,
// i.e. one supplied by the DTD rather than present in the source document.
func (n *Attribute) SetDefault(b bool) {
n.defaultAttr = b
}
// IsDefault reports whether this attribute was supplied by the DTD as a
// default value rather than being explicitly specified in the source document.
func (n *Attribute) IsDefault() bool {
return n.defaultAttr
}
// Value returns the attribute's text value as a string.
func (n Attribute) Value() string {
return string(n.Content())
}
// Name returns the qualified (prefixed) name of the attribute.
// If the attribute belongs to a namespace with a non-empty prefix,
// the result is "prefix:localname"; otherwise it is just the local name.
func (n Attribute) Name() string {
if n.ns != nil {
if p := n.ns.Prefix(); p != "" {
return p + ":" + n.docnode.Name()
}
}
return n.docnode.Name()
}
// Prefix returns the namespace prefix of the attribute, or an empty
// string if the attribute is not in a namespace.
func (n Attribute) Prefix() string {
if n.ns == nil {
return ""
}
return n.ns.Prefix()
}
// URI returns the namespace URI of the attribute, or an empty string
// if the attribute is not in a namespace.
func (n Attribute) URI() string {
if n.ns == nil {
return ""
}
return n.ns.URI()
}