Class: Hpricot::Text

Inherits:
BaseEle show all
Includes:
Leaf, Trav
Defined in:
lib/hpricot/tag.rb,
lib/hpricot/text.rb,
lib/hpricot/inspect.rb,
lib/hpricot/modules.rb,
lib/hpricot/modules.rb

Direct Known Subclasses

CData

Defined Under Namespace

Modules: Trav

Constant Summary

Constants included from Hpricot

ElementContent, ElementExclusions, ElementInclusions, NamedCharacters, NamedCharactersPattern, OmittedAttrName

Instance Attribute Summary collapse

Attributes inherited from BaseEle

#parent, #raw_string

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

#after, #at, #before, #bogusetag?, #children_of_type, #clean_path, #comment?, #css_path, #doc?, #doctype?, #elem?, filter, #get_subnode, #inner_html, #inner_html=, #next_node, #node_position, #nodes_at, #position, #previous_node, #procins?, #search, #swap, #text?, #to_html, #to_original_html, #traverse_element, #traverse_text, #xmldecl?, #xpath

Methods included from Hpricot

XML, build_node, make, parse, scan

Methods inherited from BaseEle

alterable, #altered!, #html_quote, #if_output

Constructor Details

#initialize(rcdata, normalized_rcdata = internal_normalize(rcdata)) ⇒ Text

:notnew:



29
30
31
# File 'lib/hpricot/text.rb', line 29

def initialize(text)
  @content = text
end

Instance Attribute Details

#normalized_rcdataObject (readonly)

Returns the value of attribute normalized_rcdata.



34
35
36
# File 'lib/hpricot/text.rb', line 34

def normalized_rcdata
  @normalized_rcdata
end

#rcdataObject (readonly)

Returns the value of attribute rcdata.



34
35
36
# File 'lib/hpricot/text.rb', line 34

def rcdata
  @rcdata
end

Class Method Details

.concat(*args) ⇒ Object

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

An argument should be one of follows.

  • String

  • Hpricot::Text

  • Hpricot::Location which points Hpricot::Text



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/hpricot/text.rb', line 102

def Text.concat(*args)
  rcdata = ''
  args.each {|arg|
    arg = arg.to_node if Hpricot::Location === arg
    if Text === arg
      rcdata << arg.rcdata
    else
      rcdata << arg.gsub(/&/, '&amp;')
    end
  }
  new_internal rcdata
end

.new(arg) ⇒ Object

:startdoc:



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/hpricot/text.rb', line 16

def Text.new(arg)
  arg = arg.to_node if Hpricot::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

.new_internalObject



12
# File 'lib/hpricot/text.rb', line 12

alias new_internal new

.parse_cdata_content(raw_string) ⇒ Object



211
212
213
214
# File 'lib/hpricot/parse.rb', line 211

def Text.parse_cdata_content(raw_string)
  result = CData.new(raw_string)
  result
end

.parse_cdata_section(content) ⇒ Object



216
217
218
219
# File 'lib/hpricot/parse.rb', line 216

def Text.parse_cdata_section(content)
  result = CData.new(content)
  result
end

.parse_pcdata(raw_string) ⇒ Object



206
207
208
209
# File 'lib/hpricot/parse.rb', line 206

def Text.parse_pcdata(raw_string)
  result = Text.new(raw_string)
  result
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/hpricot/text.rb', line 80

def empty?
  @normalized_rcdata.empty?
end

#output(out, opts = {}) ⇒ Object



137
138
139
140
141
142
# File 'lib/hpricot/tag.rb', line 137

def output(out, opts = {})
  out <<
    if_output(opts) do
      @content
    end
end

#pathnameObject



134
# File 'lib/hpricot/tag.rb', line 134

def pathname; "text()" end

#pretty_print(q) ⇒ Object



84
85
86
# File 'lib/hpricot/inspect.rb', line 84

def pretty_print(q)
  q.text @content.dump
end

#stripObject



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/hpricot/text.rb', line 84

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_sObject

Hpricot::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/hpricot/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