class HTree::Text

Attributes

normalized_rcdata[R]
rcdata[R]

Public Class Methods

concat(*args) click to toggle source

::concat returns a text which is concatenation of arguments.

An argument should be one of follows.

# File htree/text.rb, line 110
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(/&/, '&amp;')
    end
  }
  new_internal rcdata
end
new(arg) click to toggle source
# File htree/text.rb, line 19
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(/&/, '&amp;')
    arg = arg2.freeze if arg != arg2
    new_internal arg
  else
    raise TypeError, "cannot initialize Text with #{arg.inspect}"
  end
end
parse_cdata_content(raw_string) click to toggle source
# File htree/parse.rb, line 333
def Text.parse_cdata_content(raw_string)
  result = Text.new(raw_string)
  result.raw_string = raw_string
  result
end
parse_cdata_section(raw_string) click to toggle source
# File htree/parse.rb, line 339
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) click to toggle source
# File htree/parse.rb, line 309
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 '&'
      '&amp;'
    else 
      if NamedCharactersPattern =~ name
        "&#{name};"
      else
        "&amp;#{name}"
      end
    end
  }
  fixed = raw_string if fixed == raw_string
  result = Text.new_internal(fixed)
  result.raw_string = raw_string
  result
end

Public Instance Methods

empty?() click to toggle source
# File htree/text.rb, line 88
def empty?
  @normalized_rcdata.empty?
end
strip() click to toggle source
# File htree/text.rb, line 92
def strip
  rcdata = @normalized_rcdata.dup
  rcdata.sub!(/\A(?:\s|&nbsp;)+/, '')
  rcdata.sub!(/(?:\s|&nbsp;)+\z/, '')
  if rcdata == @normalized_rcdata
    self
  else
    rcdata.freeze
    Text.new_internal(rcdata, rcdata)
  end
end
to_s() click to toggle source

#to_s converts the text to a string.

  • character references are decoded as much as possible.

  • undecodable character reference are converted to `?' character.

# File htree/text.rb, line 77
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

Protected Instance Methods

initialize(rcdata, normalized_rcdata=internal_normalize(rcdata)) click to toggle source
# File htree/text.rb, line 32
def initialize(rcdata, normalized_rcdata=internal_normalize(rcdata)) # :notnew:
  init_raw_string
  @rcdata = rcdata && HTree.frozen_string(rcdata)
  @normalized_rcdata = @rcdata == normalized_rcdata ? @rcdata : normalized_rcdata
end