Class: LibXML::XML::Node

Inherits:
Object show all
Defined in:
lib/rmtools/xml/finders.rb,
lib/rmtools/xml/node.rb

Constant Summary collapse

InputsMapper =

FORM

lambda {|i| [i['name'], i.name == 'select' ?
  (i.at('option[selected]') || i.at('option')) : 
  (i.value || i.checked)]
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(str, copy = true) ⇒ Object

JQUERY-LIKE MANIPULATION



19
20
21
22
# File 'lib/rmtools/xml/node.rb', line 19

def self.build(str, copy=true)
  nodes = Document.string("<html>#{str}</html>", :options=>97).root.to_a
  copy ? nodes.copies(true) : nodes
end

.from(str, copy = true) ⇒ Object



24
25
26
# File 'lib/rmtools/xml/node.rb', line 24

def self.from(str, copy=true) 
  build(str, copy)[0] 
end

Instance Method Details

#[](*item) ⇒ Object



11
12
13
14
15
16
# File 'lib/rmtools/xml/node.rb', line 11

def [](*item)
  if item[0].is String
    get item[0]
  else to_a[*item]
  end
end

#__find(xp = nil, ns = nil, ss = nil) ⇒ Object



35
36
37
38
# File 'lib/rmtools/xml/finders.rb', line 35

def __find(xp=nil, ns=nil, ss=nil)
  xp ||= ss.head
  context(ns).find(XPath.filter xp)
end

#after(obj) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/rmtools/xml/node.rb', line 37

def after(obj)
  case obj
    when Node then self.next = obj
    when Array then obj.reverse_each {|n| after n}
    else after Node.build obj.to_s
  end
  self
end

#append(obj) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/rmtools/xml/node.rb', line 28

def append(obj)
  case obj
    when Node then self << obj
    when Array then obj.each {|n| append n}
    else append Node.build obj.to_s
  end
  self
end

#at(xpath, nslist = DefaultNS) ⇒ Object



57
58
59
# File 'lib/rmtools/xml/finders.rb', line 57

def at(xpath, nslist=DefaultNS)
  find(xpath, nslist)[0]
end

#before(obj) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/rmtools/xml/node.rb', line 46

def before(obj)
  case obj
    when Node then self.prev = obj
    when Array then obj.each {|n| before n}
    else before Node.build obj.to_s
  end
  self
end

#closest(*args) ⇒ Object



136
# File 'lib/rmtools/xml/node.rb', line 136

def closest(*args) getNode :parent, *args end

#find(xpath, nslist = DefaultNS) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rmtools/xml/finders.rb', line 40

def find(xpath, nslist=DefaultNS)
  node = self
  ss = StringScanner.new xpath
  ss.each %r{\[-?\d+(\.\.\d+)?\]|\{[^\}]+\}}, 
    ?[ => lambda {|ss| 
      if node; node = FindByIndex[node, nslist, ss]
      else     return []     end  }, 
    ?{ => lambda {|ss| 
      if node; node = FindByProc[node, nslist, ss]
      else     return []     end  },
    nil => lambda {|str|
      node = node.is(Array) ?
        node.sum([]) {|n| n.__find(str, nslist).to_a} : node.__find(str, nslist) 
    }
  node ? (!ss.eos? || node.is(Array)) ? node : [node] :  []
end

#getObject



7
# File 'lib/rmtools/xml/node.rb', line 7

alias :get :[]

#html(str = nil) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/rmtools/xml/node.rb', line 59

def html(str=nil)
  if str
    self.content = ''
    append str
  else to_a.join
  end
end

#inputsObject



99
100
101
102
103
104
105
106
# File 'lib/rmtools/xml/node.rb', line 99

def inputs
  [Hash[find("input[name][type=hidden]").map(&InputsMapper)],
   Hash[
      ["input[name][type!=hidden]", "textarea[name]", "select[name]"].sum {|s|
        find(s).map &InputsMapper
      }
  ]]
end

#inputs_allObject



108
109
110
111
112
# File 'lib/rmtools/xml/node.rb', line 108

def inputs_all
  Hash[["input[name]", "textarea[name]", "select[name]"].sum {|s|
            find(s).map &InputsMapper
  }]
end

#klassObject



124
# File 'lib/rmtools/xml/node.rb', line 124

def klass() self['class'] end

#klass=(name) ⇒ Object



125
# File 'lib/rmtools/xml/node.rb', line 125

def klass=(name) self['class'] = name end

#metodObject



126
# File 'lib/rmtools/xml/node.rb', line 126

def metod() self['method'] end

#metod=(name) ⇒ Object



127
# File 'lib/rmtools/xml/node.rb', line 127

def metod=(name) self['method'] = name end

#next(*args) ⇒ Object



135
# File 'lib/rmtools/xml/node.rb', line 135

def next(*args) getNode :nextNode, *args end

#nextNodeObject



132
# File 'lib/rmtools/xml/node.rb', line 132

alias :nextNode :next

#prepend(obj) ⇒ Object



55
56
57
# File 'lib/rmtools/xml/node.rb', line 55

def prepend obj
  first ? first.before(obj) : append(obj)
end

#prev(*args) ⇒ Object



134
# File 'lib/rmtools/xml/node.rb', line 134

def prev(*args) getNode :prevNode, *args end

#prevNodeObject

RELATED FINDERS



131
# File 'lib/rmtools/xml/node.rb', line 131

alias :prevNode :prev

#text(str = nil) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/rmtools/xml/node.rb', line 67

def text(str=nil)
  if str
    self.content = str
    self
  else
    self.content
  end
end

#text_nodes(lvl = 0) ⇒ Object

FETCH CONTENT



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rmtools/xml/node.rb', line 78

def text_nodes(lvl=0) # 0 => nevermind
  nodes = []
  xp = "*"
  loop {
    ary = find(xp).to_a
    break if ary.empty?
    nodes.concat(ary.childrens.flatten.find_all {|e| 
      e.text? && e.text[/[a-zA-Zа-яА-Я]/]
    })
    xp << "/*"
    break if (lvl -= 1) == 0
  }
  nodes
end

#to_hashObject

ATTRIBUTES



116
117
118
# File 'lib/rmtools/xml/node.rb', line 116

def to_hash
  attributes.to_hash
end