Class: HTree::Location

Inherits:
Object
  • Object
show all
Includes:
HTree
Defined in:
lib/htree/loc.rb,
lib/htree/modules.rb,
lib/htree/equality.rb,
lib/htree/extract_text.rb

Constant Summary

Constants included from HTree

DefaultContext, ElementContent, ElementExclusions, ElementInclusions, EmptyBindingObject, HTMLContext, NamedCharacters, NamedCharactersPattern, OmittedAttrName

Instance Attribute Summary collapse

Instance Method Summary collapse

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, #make_usual_equal_object, parse, parse_as, parse_pairs, parse_xml, scan, with_frozen_string_hash

Constructor Details

#initialize(parent, index, node) ⇒ Location

:nodoc:



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/htree/loc.rb', line 147

def initialize(parent, index, node) # :nodoc:
  if parent
    @parent = parent
    @index = index
    @node = parent.node.get_subnode(index)
    if !@node.equal?(node)
      raise ArgumentError, "unexpected node"
    end
  else
    @parent = nil
    @index = nil
    @node = node
  end
  if @node && self.class != @node.class::Loc
    raise ArgumentError, "invalid location class: #{self.class} should be #{node.class::Loc}"
  end
  @subloc = {}
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



165
166
167
# File 'lib/htree/loc.rb', line 165

def index
  @index
end

#nodeObject (readonly) Also known as: to_node

Returns the value of attribute node.



165
166
167
# File 'lib/htree/loc.rb', line 165

def node
  @node
end

#parentObject (readonly)

Returns the value of attribute parent.



165
166
167
# File 'lib/htree/loc.rb', line 165

def parent
  @parent
end

Instance Method Details

#extract_textObject



13
14
15
# File 'lib/htree/extract_text.rb', line 13

def extract_text
  to_node.extract_text
end

#index_list(node) ⇒ Object

:nodoc:

Raises:

  • (ArgumentError)


282
283
284
285
286
287
288
289
290
291
292
# File 'lib/htree/loc.rb', line 282

def index_list(node) # :nodoc:
  result = []
  loc = self
  while parent = loc.parent
    return result if loc.to_node.equal? node
    result << loc.index
    loc = parent
  end
  return result if loc.to_node.equal? node
  raise ArgumentError, "the location is not under the node: #{self.path}"
end

#loc_listObject

loc_list returns an array containing from location’s root to itself.

t = HTree('<a><b><c>')
l = t.make_loc.get_subnode(0, 0, 0)
pp l, l.loc_list
# =>
#<HTree::Location: doc()/a/b/c>
[#<HTree::Location: doc()>,
 #<HTree::Location: doc()/a>,
 #<HTree::Location: doc()/a/b>,
 #<HTree::Location: doc()/a/b/c>]


255
256
257
258
259
260
261
262
263
# File 'lib/htree/loc.rb', line 255

def loc_list
  loc = self
  result = [self]
  while loc = loc.parent
    result << loc
  end
  result.reverse!
  result
end

#make_exact_equal_objectObject Also known as: usual_equal_object



211
212
213
# File 'lib/htree/equality.rb', line 211

def make_exact_equal_object
  [@parent, @index, @node]
end

#make_locObject

return self.



169
170
171
# File 'lib/htree/loc.rb', line 169

def make_loc
  self
end

#pathObject

path returns the path of the location.

l = HTree.parse("<a><b>x</b><b/><a/>").make_loc
l = l.get_subnode(0, 0, 0)
p l.path # => "doc()/a/b[1]/text()"


270
271
272
273
274
275
276
277
278
279
280
# File 'lib/htree/loc.rb', line 270

def path
  result = ''
  loc_list.each {|loc|
    if parent = loc.parent
      result << '/' << parent.node.find_loc_step(loc.index)
    else
      result << loc.node.node_test_string
    end
  }
  result
end

#pretty_print(q) ⇒ Object

:stopdoc:



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/htree/loc.rb', line 295

def pretty_print(q)
  q.group(1, "#<#{self.class.name}", '>') {
    q.text ':'
    q.breakable
    loc_list.each {|loc|
      if parent = loc.parent
        q.text '/'
        q.group { q.breakable '' }
        q.text parent.node.find_loc_step(loc.index)
      else
        q.text loc.node.node_test_string
      end
    }
  }
end

#subst(pairs) ⇒ Object

subst substitutes several subtrees at once.

t = HTree('<r><x/><y/><z/></r>')
l = t.make_loc
l2 = l.subst({
  l.root.get_subnode('k') => 'v',
  l.root.get_subnode(-1) => HTree('<a/>'),
  l.find_element('y') => nil,
  l.find_element('z').get_subnode(0) => HTree('<b/>'),
})
pp l2, l2.to_node
# =>
#<HTree::Doc::Loc: doc()>
#<HTree::Doc
 {elem <r k="v"> {emptyelem <a>} {emptyelem <x>} {elem <z> {emptyelem <b>}}}>


239
240
241
# File 'lib/htree/loc.rb', line 239

def subst(pairs)
  subst_itself(@node.subst(pairs))
end

#subst_itself(node) ⇒ Object

subst_itself substitutes the node pointed by the location. It returns the location of substituted node.

t1 = HTree('<a><b><c><d>')
p t1
l1 = t1.make_loc.get_subnode(0, 0, 0, 0)
p l1
l2 = l1.subst_itself(HTree('<z/>'))
p l2
t2 = l2.top.to_node
p t2
# =>
#<HTree::Doc {elem <a> {elem <b> {elem <c> {emptyelem <d>}}}}>
#<HTree::Location: doc()/a/b/c/d>
#<HTree::Location: doc()/a/b/c/z>
#<HTree::Doc {elem <a> {elem <b> {elem <c> {emptyelem <z>}}}}>


206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/htree/loc.rb', line 206

def subst_itself(node)
  if @parent
    new_index = @index
    if !@node
      if Integer === @index
        if @index < 0
          new_index = 0
        elsif @parent.to_node.children.length < @index
          new_index = @parent.to_node.children.length
        end
      end
    end
    @parent.subst_itself(@parent.to_node.subst_subnode({@index=>node})).get_subnode(new_index)
  else
    node.make_loc
  end
end

#topObject

top returns the originator location.

t = HTree('<a><b><c><d>')
l = t.make_loc.get_subnode(0, 0, 0, 0)
p l, l.top
# =>
#<HTree::Location: doc()/a/b/c/d>
#<HTree::Location: doc()>


181
182
183
184
185
186
187
# File 'lib/htree/loc.rb', line 181

def top
  result = self
  while result.parent
    result = result.parent
  end
  result
end