class REXML::Text
Represents text nodes in an XML document
def <<( to_append )
+returns+ the text itself to enable method chain like
of this text node.
Appends text to this text node. The text is appended in the +raw+ mode
def <<( to_append ) @string << to_append.gsub( /\r\n?/, "\n" ) clear_cache self end
def <=>( other )
+other+ a String or a Text
def <=>( other ) to_s() <=> other.to_s end
def clear_cache
def clear_cache @normalized = nil @unnormalized = nil end
def clone
def clone return Text.new(self, true) end
def doctype
def doctype if @parent doc = @parent.document doc.doctype if doc end end
def empty?
def empty? @string.size==0 end
def indent_text(string, level=1, style="\t", indentfirstline=true)
def indent_text(string, level=1, style="\t", indentfirstline=true) return string if level < 0 new_string = '' string.each_line { |line| indent_string = style * level new_line = (indent_string + line).sub(/[\s]+$/,'') new_string << new_line } new_string.strip! unless indentfirstline return new_string end
def initialize(arg, respect_whitespace=false, parent=nil, raw=nil,
In the last example, the +entity_filter+ argument is ignored.
Text.new( "sean russell", false, nil, true, ["s"] ) #-> "sean russell"
Text.new( "sean russell", false, nil, false, ["s"] ) #-> "&s; russell"
supplied text. This argument is only useful if +raw+ is set to false.
+entity_filter+ (nil) This can be an array of entities to match in the
Text.new( "sean russell", false, nil, true ) #-> "sean russell"
Text.new( "sean russell" ) #-> "&s; &r;"
# and that the entity "r" is defined to be "russell"
# Assume that the entity "s" is defined to be "sean"
Text.new( "<&", false, nil, true ) #-> "<&"
Text.new( "<&", false, nil, true ) #-> Parse exception
Text.new( "<&", false, nil, false ) #-> "<&"
Text.new( "<&", false, nil, false ) #-> "<&"
want REXML to escape that text in output.
Use this field if you have entities defined for some text, and you don't
value for the parent, and no value is supplied, the default is false.
parent will be used as the raw value for this node. If there is no raw
text. If this value is nil (the default), then the raw value of the
escape any and all defined entities whose values are contained in the
this value is false, the string may contain any characters, and REXML will
contain no unescaped XML markup, and REXML will not change the text. If
If true, then the value of used to construct this object is expected to
+raw+ (nil) This argument can be given three values.
will be set to this.
+parent+ (nil) if this is a Parent object, the parent
respected
+respect_whitespace+ (boolean, false) if true, whitespace is
the object is shallowly cloned.
+arg+ if a String, the content is set to the String. If a Text,
Constructor
def initialize(arg, respect_whitespace=false, parent=nil, raw=nil, entity_filter=nil, illegal=NEEDS_A_SECOND_CHECK ) @raw = false @parent = nil @entity_filter = nil if parent super( parent ) @raw = parent.raw end if arg.kind_of? String @string = arg.dup elsif arg.kind_of? Text @string = arg.instance_variable_get(:@string).dup @raw = arg.raw @entity_filter = arg.instance_variable_get(:@entity_filter) else raise "Illegal argument of type #{arg.type} for Text constructor (#{arg})" end @string.squeeze!(" \n\t") unless respect_whitespace @string.gsub!(/\r\n?/, "\n") @raw = raw unless raw.nil? @entity_filter = entity_filter if entity_filter clear_cache Text.check(@string, illegal, doctype) if @raw end
def inspect
def inspect @string.inspect end
def node_type
def node_type :text end
def parent= parent
def parent= parent super(parent) Text.check(@string, NEEDS_A_SECOND_CHECK, doctype) if @raw and @parent end
def to_s
u = Text.new( "sean russell", false, nil, true )
t.to_s #-> "< & &s; russell"
t = Text.new( "< & &s; russell", false, nil, false )
t.to_s #-> "< & &s; russell"
t = Text.new( "< & sean russell", false, nil, false, ['s'] )
# entity "r" is defined to be "russell"
# Assume that the entity "s" is defined to be "sean", and that the
the entity filter set in the constructor.
entities that can be escaped, have been inserted. This method respects
escaped, meaning that it is a valid XML text node string, and all
Returns the string value of this text node. This string is always
def to_s return @string if @raw @normalized ||= Text::normalize( @string, doctype, @entity_filter ) end
def value
u = Text.new( "sean russell", false, nil, true )
t.value #-> "< & sean russell"
t = Text.new( "< & &s; russell", false, nil, false )
t.value #-> "< & sean russell"
t = Text.new( "< & sean russell", false, nil, false, ['s'] )
# entity "r" is defined to be "russell"
# Assume that the entity "s" is defined to be "sean", and that the
entity_filter.
console. This ignores the 'raw' attribute setting, and any
entities, as it might be used programmatically, or printed to the
Returns the string value of this text. This is the text without
def value @unnormalized ||= Text::unnormalize(@string, doctype, entity_expansion_text_limit: document&.entity_expansion_text_limit) end
def value=( val )
e[0].value = "bar" # bar
e.add_text( "foo" ) # foo
e = Element.new( "a" )
unnormalized. It returns self.
Sets the contents of this text node. This expects the text to be
def value=( val ) @string = val.gsub( /\r\n?/, "\n" ) clear_cache @raw = false end
def wrap(string, width, addnewline=false)
def wrap(string, width, addnewline=false) # Recursively wrap string at width. return string if string.length <= width place = string.rindex(' ', width) # Position in string with last ' ' before cutoff if addnewline then return "\n" + string[0,place] + "\n" + wrap(string[place+1..-1], width) else return string[0,place] + "\n" + wrap(string[place+1..-1], width) end end
def write( writer, indent=-1, transitive=false, ie_hack=false )
See REXML::Formatters
== DEPRECATED
def write( writer, indent=-1, transitive=false, ie_hack=false ) Kernel.warn("#{self.class.name}.write is deprecated. See REXML::Formatters", uplevel: 1) formatter = if indent > -1 REXML::Formatters::Pretty.new( indent ) else REXML::Formatters::Default.new end formatter.write( self, writer ) end
def write_with_substitution out, input
}
end
ascOut.concat(sprintf("%x;", r))
else
ascOut.concat(r.chr)
if r < 0x100
z.each{|r|
ascOut=""
z=utf8.unpack("U*")
+input+ the text to substitute and the write out
+out+ A String, IO, or any other object supporting <<( String )
Writes out text, substituting special characters beforehand.
def write_with_substitution out, input copy = input.clone # Doing it like this rather than in a loop improves the speed copy.gsub!( SPECIALS[0], SUBSTITUTES[0] ) copy.gsub!( SPECIALS[1], SUBSTITUTES[1] ) copy.gsub!( SPECIALS[2], SUBSTITUTES[2] ) copy.gsub!( SPECIALS[3], SUBSTITUTES[3] ) copy.gsub!( SPECIALS[4], SUBSTITUTES[4] ) copy.gsub!( SPECIALS[5], SUBSTITUTES[5] ) out << copy end
def xpath
FIXME
def xpath path = @parent.xpath path += "/text()" return path end