Class: HTree::STag
- Inherits:
-
Object
show all
- Includes:
- Tag
- Defined in:
- lib/htree/tag.rb,
lib/htree/output.rb,
lib/htree/inspect.rb,
lib/htree/modules.rb,
lib/htree/equality.rb,
lib/htree/raw_string.rb
Overview
Constant Summary
Constants included
from HTree
DefaultContext, ElementContent, ElementExclusions, ElementInclusions, EmptyBindingObject, HTMLContext, NamedCharacters, NamedCharactersPattern, OmittedAttrName
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Tag
#init_raw_string, #raw_string=, #raw_string_internal
Methods included from HTree
#==, build_node, #check_equality, compile_template, #exact_equal?, #exact_equal_object, expand_template, fix_element, fix_structure_list, frozen_string, #hash, parse_as, parse_pairs, parse_xml, scan, #usual_equal_object, with_frozen_string_hash
Constructor Details
#initialize(name, attributes = [], inherited_context = DefaultContext) ⇒ STag
Returns a new instance of STag.
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
|
# File 'lib/htree/tag.rb', line 12
def initialize(name, attributes=[], inherited_context=DefaultContext)
init_raw_string
attributes = attributes.map {|aname, val|
if !(Name === aname) && /\A(?:#{Pat::Name}?\{.*\})?#{Pat::Nmtoken}\z/o !~ aname
raise HTree::Error, "invalid attribute name: #{aname.inspect}"
end
if !(Name === aname) && /\Axmlns(?:\z|:)/ =~ aname
aname = Name.parse_attribute_name(aname, nil)
end
val = val.to_node if HTree::Location === val
val = Text.new(val) unless Text === val
[aname, val]
}
@inherited_context = inherited_context
@xmlns_decls = {}
if Name === name
@xmlns_decls[name.namespace_prefix] = name.namespace_uri
end
attributes.each {|aname, text|
next unless Name === aname
next if aname.xmlns?
if aname.namespace_prefix && aname.namespace_uri
if @xmlns_decls.include? aname.namespace_prefix
if @xmlns_decls[aname.namespace_prefix] != aname.namespace_uri
raise ArgumentError, "inconsistent namespace use: #{aname.namespace_prefix} is used as #{@xmlns_decls[aname.namespace_prefix]} and #{aname.namespace_uri}"
end
else
@xmlns_decls[aname.namespace_prefix] = aname.namespace_uri
end
end
}
attributes.each {|aname, text|
next unless Name === aname
next unless aname.xmlns?
next if @xmlns_decls.include? aname.local_name
if aname.local_name
@xmlns_decls[aname.local_name] = text.to_s
else
uri = text.to_s
@xmlns_decls[nil] = uri
end
}
@context = make_context(@inherited_context)
if Name === name
@name = name
else
@name = Name.parse_element_name(name, @context)
end
@attributes = attributes.map {|aname, text|
aname = Name.parse_attribute_name(aname, @context) unless Name === aname
if !aname.namespace_prefix && !aname.namespace_uri.empty?
raise HTree::Error, "global attribute without namespace prefix: #{aname.inspect}"
end
[aname, text]
}
@attributes.freeze
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
78
79
80
|
# File 'lib/htree/tag.rb', line 78
def attributes
@attributes
end
|
#context ⇒ Object
Returns the value of attribute context.
78
79
80
|
# File 'lib/htree/tag.rb', line 78
def context
@context
end
|
#inherited_context ⇒ Object
Returns the value of attribute inherited_context.
78
79
80
|
# File 'lib/htree/tag.rb', line 78
def inherited_context
@inherited_context
end
|
Class Method Details
.parse(raw_string, is_stag, is_xml, is_html, inherited_context = DefaultContext) ⇒ Object
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
# File 'lib/htree/parse.rb', line 236
def STag.parse(raw_string, is_stag, is_xml, is_html, inherited_context=DefaultContext)
attrs = []
if (is_stag ? /\A#{Pat::ValidStartTag_C}\z/o : /\A#{Pat::ValidEmptyTag_C}\z/o) =~ raw_string
qname = $1
$2.scan(Pat::ValidAttr_C) {
attrs << ($5 ? [nil, $5] : [$1, $2 || $3 || $4])
}
elsif (is_stag ? /\A#{Pat::InvalidStartTag_C}\z/o : /\A#{Pat::InvalidEmptyTag_C}\z/o) =~ raw_string
qname = $1
last_attr = $3
$2.scan(Pat::InvalidAttr1_C) {
attrs << ($5 ? [nil, $5] : [$1, $2 || $3 || $4])
}
if last_attr
/#{Pat::InvalidAttr1End_C}/o =~ last_attr
attrs << [$1, $2 || $3]
end
else
raise HTree::Error, "cannot recognize as start tag or empty tag: #{raw_string.inspect}"
end
qname = qname.downcase if !is_xml && is_html
attrs.map! {|aname, aval|
if aname
aname = (!is_xml && is_html) ? aname.downcase : aname
[aname, Text.parse_pcdata(aval)]
else
if val2name = OmittedAttrName[qname]
aval_downcase = aval.downcase
aname = val2name.fetch(aval_downcase, aval_downcase)
else
aname = aval
end
[aname, Text.new(aval)]
end
}
result = STag.new(qname, attrs, inherited_context)
result.raw_string = raw_string
result
end
|
Instance Method Details
#each_attribute ⇒ Object
95
96
97
98
99
100
101
|
# File 'lib/htree/tag.rb', line 95
def each_attribute
@attributes.each {|name, text|
next if name.xmlns?
yield name, text
}
nil
end
|
#each_namespace_attribute ⇒ Object
88
89
90
91
92
93
|
# File 'lib/htree/tag.rb', line 88
def each_namespace_attribute
@xmlns_decls.each {|name, uri|
yield name, uri
}
nil
end
|
#element_name ⇒ Object
80
81
82
|
# File 'lib/htree/tag.rb', line 80
def element_name
@name
end
|
#eliminate_raw_string ⇒ Object
92
93
94
|
# File 'lib/htree/raw_string.rb', line 92
def eliminate_raw_string
STag.new(@qualified_name, @attributes, @inherited_context)
end
|
#make_context(inherited_context) ⇒ Object
84
85
86
|
# File 'lib/htree/tag.rb', line 84
def make_context(inherited_context)
inherited_context.subst_namespaces(@xmlns_decls)
end
|
#make_exact_equal_object ⇒ Object
122
123
124
125
126
127
128
129
130
131
132
|
# File 'lib/htree/equality.rb', line 122
def make_exact_equal_object
[@raw_string,
@name,
@attributes.sort {|(n1,t1), (n2, t2)|
Util.cmp_with_nil(n1.namespace_prefix, n2.namespace_prefix).nonzero? ||
Util.cmp_with_nil(n1.namespace_uri, n2.namespace_uri).nonzero? ||
Util.cmp_with_nil(n1.local_name, n2.local_name)
},
@inherited_context
]
end
|
#make_usual_equal_object ⇒ Object
134
135
136
137
138
139
140
141
142
|
# File 'lib/htree/equality.rb', line 134
def make_usual_equal_object
[@name,
@attributes.find_all {|n,t| !n.xmlns? }.sort {|(n1,t1), (n2, t2)|
Util.cmp_with_nil(n1.namespace_prefix, n2.namespace_prefix).nonzero? ||
Util.cmp_with_nil(n1.namespace_uri, n2.namespace_uri).nonzero? ||
Util.cmp_with_nil(n1.local_name, n2.local_name)
}
]
end
|
#output_attributes(out, context) ⇒ Object
92
93
94
95
96
97
98
99
|
# File 'lib/htree/output.rb', line 92
def output_attributes(out, context)
@attributes.each {|aname, text|
next if aname.xmlns?
out.output_string ' '
aname.output_attribute(text, out, context)
}
@context.output_namespaces(out, context)
end
|
#output_emptytag(out, context) ⇒ Object
101
102
103
104
105
106
107
108
109
|
# File 'lib/htree/output.rb', line 101
def output_emptytag(out, context)
out.output_string '<'
@name.output(out, context)
children_context = output_attributes(out, context)
out.output_string "\n"
out.output_slash_if_xml
out.output_string ">"
children_context
end
|
#output_etag(out, context) ⇒ Object
119
120
121
122
123
|
# File 'lib/htree/output.rb', line 119
def output_etag(out, context)
out.output_string '</'
@name.output(out, context)
out.output_string "\n>"
end
|
#output_stag(out, context) ⇒ Object
111
112
113
114
115
116
117
|
# File 'lib/htree/output.rb', line 111
def output_stag(out, context)
out.output_string '<'
@name.output(out, context)
children_context = output_attributes(out, context)
out.output_string "\n>"
children_context
end
|
#pretty_print(q) ⇒ Object
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/htree/inspect.rb', line 72
def pretty_print(q)
q.group(1, '<', '>') {
q.text @name.inspect
@attributes.each {|n, t|
q.breakable
q.text "#{n.inspect}=\"#{t.to_attvalue_content}\""
}
}
end
|