2 require 'rexml/doctype'
4 require 'rexml/doctype'
5 require 'rexml/parseexception'
8 # Represents text nodes in an XML document
11 # The order in which the substitutions occur
12 SPECIALS = [ /&(?!#?[\w-]+;)/u, /</u, />/u, /"/u, /'/u, /\r/u ]
13 SUBSTITUTES = ['&', '<', '>', '"', ''', ' ']
14 # Characters which are substituted in written strings
15 SLAICEPS = [ '<', '>', '"', "'", '&' ]
16 SETUTITSBUS = [ /</u, />/u, /"/u, /'/u, /&/u ]
18 # If +raw+ is true, then REXML leaves the value alone
21 ILLEGAL = /(<|&(?!(#{Entity::NAME})|(#0*((?:\d+)|(?:x[a-fA-F0-9]+)));))/um
22 NUMERICENTITY = /�*((?:\d+)|(?:x[a-fA-F0-9]+));/
25 # +arg+ if a String, the content is set to the String. If a Text,
26 # the object is shallowly cloned.
28 # +respect_whitespace+ (boolean, false) if true, whitespace is
31 # +parent+ (nil) if this is a Parent object, the parent
32 # will be set to this.
34 # +raw+ (nil) This argument can be given three values.
35 # If true, then the value of used to construct this object is expected to
36 # contain no unescaped XML markup, and REXML will not change the text. If
37 # this value is false, the string may contain any characters, and REXML will
38 # escape any and all defined entities whose values are contained in the
39 # text. If this value is nil (the default), then the raw value of the
40 # parent will be used as the raw value for this node. If there is no raw
41 # value for the parent, and no value is supplied, the default is false.
42 # Use this field if you have entities defined for some text, and you don't
43 # want REXML to escape that text in output.
44 # Text.new( "<&", false, nil, false ) #-> "<&"
45 # Text.new( "<&", false, nil, false ) #-> "&lt;&amp;"
46 # Text.new( "<&", false, nil, true ) #-> Parse exception
47 # Text.new( "<&", false, nil, true ) #-> "<&"
48 # # Assume that the entity "s" is defined to be "sean"
49 # # and that the entity "r" is defined to be "russell"
50 # Text.new( "sean russell" ) #-> "&s; &r;"
51 # Text.new( "sean russell", false, nil, true ) #-> "sean russell"
53 # +entity_filter+ (nil) This can be an array of entities to match in the
54 # supplied text. This argument is only useful if +raw+ is set to false.
55 # Text.new( "sean russell", false, nil, false, ["s"] ) #-> "&s; russell"
56 # Text.new( "sean russell", false, nil, true, ["s"] ) #-> "sean russell"
57 # In the last example, the +entity_filter+ argument is ignored.
59 # +pattern+ INTERNAL USE ONLY
60 def initialize(arg, respect_whitespace=false, parent=nil, raw=nil,
61 entity_filter=nil, illegal=ILLEGAL )
72 @raw = raw unless raw.nil?
73 @entity_filter = entity_filter
74 @normalized = @unnormalized = nil
76 if arg.kind_of? String
78 @string.squeeze!(" \n\t") unless respect_whitespace
79 elsif arg.kind_of? Text
83 raise "Illegal argument of type #{arg.type} for Text constructor (#{arg})"
86 @string.gsub!( /\r\n?/, "\n" )
88 # check for illegal characters
91 raise "Illegal character '#{$1}' in raw string \"#{@string}\""
106 return Text.new(self)
110 # Appends text to this text node. The text is appended in the +raw+ mode
113 @string << to_append.gsub( /\r\n?/, "\n" )
117 # +other+ a String or a Text
118 # +returns+ the result of (to_s <=> arg.to_s)
120 to_s() <=> other.to_s
123 REFERENCE = /#{Entity::REFERENCE}/
124 # Returns the string value of this text node. This string is always
125 # escaped, meaning that it is a valid XML text node string, and all
126 # entities that can be escaped, have been inserted. This method respects
127 # the entity filter set in the constructor.
129 # # Assume that the entity "s" is defined to be "sean", and that the
130 # # entity "r" is defined to be "russell"
131 # t = Text.new( "< & sean russell", false, nil, false, ['s'] )
132 # t.to_s #-> "< & &s; russell"
133 # t = Text.new( "< & &s; russell", false, nil, false )
134 # t.to_s #-> "< & &s; russell"
135 # u = Text.new( "sean russell", false, nil, true )
136 # u.to_s #-> "sean russell"
138 return @string if @raw
139 return @normalized if @normalized
143 doc = @parent.document
144 doctype = doc.doctype if doc
147 @normalized = Text::normalize( @string, doctype, @entity_filter )
154 # Returns the string value of this text. This is the text without
155 # entities, as it might be used programmatically, or printed to the
156 # console. This ignores the 'raw' attribute setting, and any
159 # # Assume that the entity "s" is defined to be "sean", and that the
160 # # entity "r" is defined to be "russell"
161 # t = Text.new( "< & sean russell", false, nil, false, ['s'] )
162 # t.value #-> "< & sean russell"
163 # t = Text.new( "< & &s; russell", false, nil, false )
164 # t.value #-> "< & sean russell"
165 # u = Text.new( "sean russell", false, nil, true )
166 # u.value #-> "sean russell"
168 @unnormalized if @unnormalized
171 doc = @parent.document
172 doctype = doc.doctype if doc
174 @unnormalized = Text::unnormalize( @string, doctype )
177 # Sets the contents of this text node. This expects the text to be
178 # unnormalized. It returns self.
180 # e = Element.new( "a" )
181 # e.add_text( "foo" ) # <a>foo</a>
182 # e[0].value = "bar" # <a>bar</a>
183 # e[0].value = "<a>" # <a><a></a>
185 @string = val.gsub( /\r\n?/, "\n" )
191 def wrap(string, width, addnewline=false)
192 # Recursivly wrap string at width.
193 return string if string.length <= width
194 place = string.rindex(' ', width) # Position in string with last ' ' before cutoff
196 return "\n" + string[0,place] + "\n" + wrap(string[place+1..-1], width)
198 return string[0,place] + "\n" + wrap(string[place+1..-1], width)
202 def indent_text(string, level=1, style="\t", indentfirstline=true)
203 return string if level < 0
206 indent_string = style * level
207 new_line = (indent_string + line).sub(/[\s]+$/,'')
208 new_string << new_line
210 new_string.strip! unless indentfirstline
215 # See REXML::Formatters
217 def write( writer, indent=-1, transitive=false, ie_hack=false )
218 Kernel.warn("#{self.class.name}.write is deprecated. See REXML::Formatters")
219 formatter = if indent > -1
220 REXML::Formatters::Pretty.new( indent )
222 REXML::Formatters::Default.new
224 formatter.write( self, writer )
228 # This probably won't work properly
235 # Writes out text, substituting special characters beforehand.
236 # +out+ A String, IO, or any other object supporting <<( String )
237 # +input+ the text to substitute and the write out
239 # z=utf8.unpack("U*")
243 # ascOut.concat(r.chr)
245 # ascOut.concat(sprintf("&#x%x;", r))
249 def write_with_substitution out, input
251 # Doing it like this rather than in a loop improves the speed
252 copy.gsub!( SPECIALS[0], SUBSTITUTES[0] )
253 copy.gsub!( SPECIALS[1], SUBSTITUTES[1] )
254 copy.gsub!( SPECIALS[2], SUBSTITUTES[2] )
255 copy.gsub!( SPECIALS[3], SUBSTITUTES[3] )
256 copy.gsub!( SPECIALS[4], SUBSTITUTES[4] )
257 copy.gsub!( SPECIALS[5], SUBSTITUTES[5] )
261 # Reads text, substituting entities
262 def Text::read_with_substitution( input, illegal=nil )
266 raise ParseException.new( "malformed text: Illegal character #$& in \"#{copy}\"" )
269 copy.gsub!( /\r\n?/, "\n" )
271 copy.gsub!( SETUTITSBUS[0], SLAICEPS[0] )
272 copy.gsub!( SETUTITSBUS[1], SLAICEPS[1] )
273 copy.gsub!( SETUTITSBUS[2], SLAICEPS[2] )
274 copy.gsub!( SETUTITSBUS[3], SLAICEPS[3] )
275 copy.gsub!( SETUTITSBUS[4], SLAICEPS[4] )
276 copy.gsub!( /�*((?:\d+)|(?:x[a-f0-9]+));/ ) {|m|
279 m = "0#{m}" if m[0] == ?x
280 [Integer(m)].pack('U*')
286 EREFERENCE = /&(?!#{Entity::NAME};)/
287 # Escapes all possible entities
288 def Text::normalize( input, doctype=nil, entity_filter=nil )
290 # Doing it like this rather than in a loop improves the speed
291 #copy = copy.gsub( EREFERENCE, '&' )
292 copy = copy.gsub( "&", "&" )
294 # Replace all ampersands that aren't part of an entity
295 doctype.entities.each_value do |entity|
296 copy = copy.gsub( entity.value,
297 "&#{entity.name};" ) if entity.value and
298 not( entity_filter and entity_filter.include?(entity) )
301 # Replace all ampersands that aren't part of an entity
302 DocType::DEFAULT_ENTITIES.each_value do |entity|
303 copy = copy.gsub(entity.value, "&#{entity.name};" )
309 # Unescapes all possible entities
310 def Text::unnormalize( string, doctype=nil, filter=nil, illegal=nil )
312 rv.gsub!( /\r\n?/, "\n" )
313 matches = rv.scan( REFERENCE )
314 return rv if matches.size == 0
315 rv.gsub!( NUMERICENTITY ) {|m|
317 m = "0#{m}" if m[0] == ?x
318 [Integer(m)].pack('U*')
320 matches.collect!{|x|x[0]}.compact!
323 matches.each do |entity_reference|
324 unless filter and filter.include?(entity_reference)
325 entity_value = doctype.entity( entity_reference )
326 re = /&#{entity_reference};/
327 rv.gsub!( re, entity_value ) if entity_value
331 matches.each do |entity_reference|
332 unless filter and filter.include?(entity_reference)
333 entity_value = DocType::DEFAULT_ENTITIES[ entity_reference ]
334 re = /&#{entity_reference};/
335 rv.gsub!( re, entity_value.value ) if entity_value
339 rv.gsub!( /&/, '&' )