Class: HTree::Text
- Inherits:
-
Object
show all
- Includes:
- Leaf, Trav
- Defined in:
- lib/htree/output.rb,
lib/htree/loc.rb,
lib/htree/text.rb,
lib/htree/rexml.rb,
lib/htree/modules.rb,
lib/htree/modules.rb,
lib/htree/modules.rb,
lib/htree/equality.rb,
lib/htree/raw_string.rb,
lib/htree/raw_string.rb,
lib/htree/extract_text.rb
Overview
Defined Under Namespace
Modules: Trav
Classes: Loc
Constant Summary
collapse
- ChRef =
{
'>' => '>',
'<' => '<',
'"' => '"',
}
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 Trav
#traverse_text_internal
Methods included from Leaf::Trav
#traverse_all_element, #traverse_some_element, #traverse_text_internal
Methods included from Traverse
#bogusetag?, #comment?, #doc?, #doctype?, #elem?, #get_subnode, #procins?, #text?, #traverse_text, #xmldecl?
Methods included from Leaf
#init_raw_string, #pretty_print, #raw_string_internal
Methods included from Node
#display_html, #display_xml, #generate_xml_output_code, #make_loc, #raw_string, #subst, #subst_internal, #to_node, #to_rexml
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, parse_as, parse_pairs, parse_xml, scan, #usual_equal_object, with_frozen_string_hash
Constructor Details
#initialize(rcdata, normalized_rcdata = internal_normalize(rcdata)) ⇒ Text
29
30
31
32
33
|
# File 'lib/htree/text.rb', line 29
def initialize(rcdata, normalized_rcdata=internal_normalize(rcdata))
init_raw_string
@rcdata = rcdata && HTree.frozen_string(rcdata)
@normalized_rcdata = @rcdata == normalized_rcdata ? @rcdata : normalized_rcdata
end
|
Instance Attribute Details
#normalized_rcdata ⇒ Object
Returns the value of attribute normalized_rcdata.
34
35
36
|
# File 'lib/htree/text.rb', line 34
def normalized_rcdata
@normalized_rcdata
end
|
#rcdata ⇒ Object
Returns the value of attribute rcdata.
34
35
36
|
# File 'lib/htree/text.rb', line 34
def rcdata
@rcdata
end
|
Class Method Details
.concat(*args) ⇒ Object
HTree::Text.concat returns a text which is concatenation of arguments.
An argument should be one of follows.
- String
- HTree::Text
- HTree::Location which points HTree::Text
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/htree/text.rb', line 102
def Text.concat(*args)
rcdata = ''
args.each {|arg|
arg = arg.to_node if HTree::Location === arg
if Text === arg
rcdata << arg.rcdata
else
rcdata << arg.gsub(/&/, '&')
end
}
new_internal rcdata
end
|
.new(arg) ⇒ Object
16
17
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/htree/text.rb', line 16
def Text.new(arg)
arg = arg.to_node if HTree::Location === arg
if Text === arg
new_internal arg.rcdata, arg.normalized_rcdata
elsif String === arg
arg2 = arg.gsub(/&/, '&')
arg = arg2.freeze if arg != arg2
new_internal arg
else
raise TypeError, "cannot initialize Text with #{arg.inspect}"
end
end
|
.new_internal ⇒ Object
12
|
# File 'lib/htree/text.rb', line 12
alias new_internal new
|
.parse_cdata_content(raw_string) ⇒ Object
329
330
331
332
333
|
# File 'lib/htree/parse.rb', line 329
def Text.parse_cdata_content(raw_string)
result = Text.new(raw_string)
result.raw_string = raw_string
result
end
|
.parse_cdata_section(raw_string) ⇒ Object
335
336
337
338
339
340
341
342
343
344
345
|
# File 'lib/htree/parse.rb', line 335
def Text.parse_cdata_section(raw_string)
unless /\A#{Pat::CDATA_C}\z/o =~ raw_string
raise HTree::Error, "cannot recognize as CDATA section: #{raw_string.inspect}"
end
content = $1
result = Text.new(content)
result.raw_string = raw_string
result
end
|
.parse_pcdata(raw_string) ⇒ Object
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
# File 'lib/htree/parse.rb', line 305
def Text.parse_pcdata(raw_string)
fixed = raw_string.gsub(/&(?:(?:#[0-9]+|#x[0-9a-fA-F]+|([A-Za-z][A-Za-z0-9]*));?)?/o) {|s|
name = $1
case s
when /;\z/
s
when /\A&#/
"#{s};"
when '&'
'&'
else
if NamedCharactersPattern =~ name
"&#{name};"
else
"&#{name}"
end
end
}
fixed = raw_string if fixed == raw_string
result = Text.new_internal(fixed)
result.raw_string = raw_string
result
end
|
Instance Method Details
#eliminate_raw_string ⇒ Object
86
87
88
|
# File 'lib/htree/raw_string.rb', line 86
def eliminate_raw_string
Text.new_internal(@rcdata)
end
|
#empty? ⇒ Boolean
80
81
82
|
# File 'lib/htree/text.rb', line 80
def empty?
@normalized_rcdata.empty?
end
|
32
33
34
|
# File 'lib/htree/extract_text.rb', line 32
def
self
end
|
#make_exact_equal_object ⇒ Object
155
156
157
|
# File 'lib/htree/equality.rb', line 155
def make_exact_equal_object
[@raw_string, @rcdata]
end
|
#make_usual_equal_object ⇒ Object
159
160
161
|
# File 'lib/htree/equality.rb', line 159
def make_usual_equal_object
@normalized_rcdata
end
|
#node_test_string ⇒ Object
92
|
# File 'lib/htree/loc.rb', line 92
def node_test_string() 'text()' end
|
#output(out, context = nil) ⇒ Object
17
18
19
|
# File 'lib/htree/output.rb', line 17
def output(out, context=nil)
out.output_text @rcdata.gsub(/[<>]/) {|s| ChRef[s] }
end
|
#output_attvalue(out, context) ⇒ Object
25
26
27
28
29
|
# File 'lib/htree/output.rb', line 25
def output_attvalue(out, context)
out.output_string '"'
out.output_text to_attvalue_content
out.output_string '"'
end
|
#output_cdata(out) ⇒ Object
31
32
33
34
35
36
37
|
# File 'lib/htree/output.rb', line 31
def output_cdata(out)
str = self.to_s
if %r{</} =~ str
raise ArgumentError, "CDATA cannot contain '</': #{str.inspect}"
end
out.output_string(str)
end
|
#raw_string=(arg) ⇒ Object
52
53
54
55
56
57
58
|
# File 'lib/htree/raw_string.rb', line 52
def raw_string=(arg)
if arg == @rcdata then
@raw_string = @rcdata
else
super
end
end
|
#strip ⇒ Object
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/htree/text.rb', line 84
def strip
rcdata = @normalized_rcdata.dup
rcdata.sub!(/\A(?:\s| )+/, '')
rcdata.sub!(/(?:\s| )+\z/, '')
if rcdata == @normalized_rcdata
self
else
rcdata.freeze
Text.new_internal(rcdata, rcdata)
end
end
|
#to_attvalue_content ⇒ Object
21
22
23
|
# File 'lib/htree/output.rb', line 21
def to_attvalue_content
@rcdata.gsub(/[<>"]/) {|s| ChRef[s] }
end
|
#to_rexml_internal(parent, context) ⇒ Object
90
91
92
93
|
# File 'lib/htree/rexml.rb', line 90
def to_rexml_internal(parent, context)
rcdata = self.rcdata.gsub(/[<>]/) { Encoder::ChRef[$&] }
REXML::Text.new(rcdata, true, parent, true)
end
|
#to_s ⇒ Object
HTree::Text#to_s converts the text to a string.
- character references are decoded as much as possible.
- undecodable character reference are converted to `?' character.
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/htree/text.rb', line 69
def to_s
@normalized_rcdata.gsub(/&(?:#([0-9]+));/o) {|s|
u = $1.to_i
if 0 <= u && u <= 0x7f
[u].pack("C")
else
'?'
end
}
end
|