Class: XML::Smart::Dom::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/xml/smart_domelement.rb

Instance Method Summary collapse

Constructor Details

#initialize(element) ⇒ Element

Returns a new instance of Element.



6
7
8
# File 'lib/xml/smart_domelement.rb', line 6

def initialize(element)
  @node = element
end

Instance Method Details

#==(other) ⇒ Object



216
217
218
219
220
# File 'lib/xml/smart_domelement.rb', line 216

def ==(other)
  return false unless other
  return false unless other.respond_to?(:unique_id)
  unique_id == other.unique_id
end

#===(cls) ⇒ Object



10
# File 'lib/xml/smart_domelement.rb', line 10

def ===(cls); self.is_a? cls; end

#add(*attrs) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/xml/smart_domelement.rb', line 101

def add(*attrs)
  tmp, update = add_helper(attrs)
  res = Dom::smart_helper(@node.add_child tmp)
  if update
    @node.document.custom_namespace_prefixes_update
    @node.document.ns_update
  end
  res
end

#add_after(*attrs) ⇒ Object



126
127
128
129
130
131
132
133
134
# File 'lib/xml/smart_domelement.rb', line 126

def add_after(*attrs)
  tmp, update = add_helper(attrs)
  res = Dom::smart_helper(@node.add_next_sibling tmp)
  if update
    @node.document.custom_namespace_prefixes_update
    @node.document.ns_update
  end
  res
end

#add_before(*attrs) ⇒ Object



117
118
119
120
121
122
123
124
125
# File 'lib/xml/smart_domelement.rb', line 117

def add_before(*attrs)
  tmp, update = add_helper(attrs)
  res = Dom::smart_helper(@node.add_previous_sibling tmp)
  if update
    @node.document.custom_namespace_prefixes_update
    @node.document.ns_update
  end
  res
end

#append(*attrs) ⇒ Object



110
111
112
# File 'lib/xml/smart_domelement.rb', line 110

def append(*attrs)
  add(*attrs)
end

#attributesObject



191
# File 'lib/xml/smart_domelement.rb', line 191

def attributes; AttributeSet.new @node; end

#childrenObject



196
# File 'lib/xml/smart_domelement.rb', line 196

def children; find('*|text()'); end

#children?Boolean

Returns:

  • (Boolean)


197
# File 'lib/xml/smart_domelement.rb', line 197

def children?; find('*|text()').length > 0 end

#dumpObject



136
137
138
139
140
# File 'lib/xml/smart_domelement.rb', line 136

def dump
  doc = Nokogiri::XML::Document.new
  doc.root = @node
  doc.root.to_s
end

#element_only?Boolean

Returns:

  • (Boolean)


210
211
212
# File 'lib/xml/smart_domelement.rb', line 210

def element_only?;
  @node.xpath_fast('*').length > 0 &&  @node.xpath_fast("string(text())") == '';
end

#empty?Boolean

Returns:

  • (Boolean)


203
# File 'lib/xml/smart_domelement.rb', line 203

def empty?; !children?; end

#find(xpath) ⇒ Object



12
13
14
# File 'lib/xml/smart_domelement.rb', line 12

def find(xpath)
  Dom::smart_helper(@node.xpath_fast(xpath))
end

#mixed?Boolean

Returns:

  • (Boolean)


204
205
206
# File 'lib/xml/smart_domelement.rb', line 204

def mixed?;
  @node.xpath_fast('*').length > 0 &&  @node.xpath_fast("string(text())") != '';
end

#namespaceObject



147
# File 'lib/xml/smart_domelement.rb', line 147

def namespace; namespace? ? Namespace.new(@node.namespace) : nil; end

#namespace=(n) ⇒ Object



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

def namespace=(n)
  n = case n
    when Namespace
      n.prefix
    when String
      n
    else
      return
  end
  tmp = @node.document.custom_namespace_prefixes[n] || @node.document.user_custom_namespace_prefixes[n]
  unless tmp.nil?
    @node.namespace_scopes.each do |nss|
      @node.namespace = nss if nss.href == tmp
    end
  end
end

#namespace?Boolean

Returns:

  • (Boolean)


146
# File 'lib/xml/smart_domelement.rb', line 146

def namespace?; !@node.namespace.nil?; end

#namespacesObject



164
# File 'lib/xml/smart_domelement.rb', line 164

def namespaces; NamespaceSet.new(self,@node); end

#parentObject



198
199
200
# File 'lib/xml/smart_domelement.rb', line 198

def parent
  Dom::smart_helper(@node.parent)
end

#parent?Boolean

Returns:

  • (Boolean)


201
# File 'lib/xml/smart_domelement.rb', line 201

def parent?; !@node.parent.nil?; end

#pathObject



214
# File 'lib/xml/smart_domelement.rb', line 214

def path; @node.path[-1] != ']' ? @node.path + '[1]' : @node.path; end

#prepend(*attrs) ⇒ Object



113
114
115
116
# File 'lib/xml/smart_domelement.rb', line 113

def prepend(*attrs)
  c = children
  c.empty? ? add(*attrs) : c.first.add_before(*attrs)
end

#qnameObject



190
# File 'lib/xml/smart_domelement.rb', line 190

def qname; QName.new @node; end

#replace_by(n) ⇒ Object



173
174
175
176
177
178
179
180
# File 'lib/xml/smart_domelement.rb', line 173

def replace_by(n)
  case n
    when Element; Element.new @node.replace(n.instance_variable_get(:@node))
    when NodeSet; NodeSet.new @node.replace(n.instance_variable_get(:@nodeset))
    else
      nil
  end
end

#replace_by_copy(n) ⇒ Object



181
182
183
184
185
186
187
188
# File 'lib/xml/smart_domelement.rb', line 181

def replace_by_copy(n)
  case n
    when Element; Element.new @node.replace(n.instance_variable_get(:@node).dup)
    when NodeSet; NodeSet.new @node.replace(n.instance_variable_get(:@nodeset).dup)
    else
      nil
  end
end

#textObject



193
# File 'lib/xml/smart_domelement.rb', line 193

def text; @node.xpath_fast("string(text())"); end

#text=(t) ⇒ Object



194
# File 'lib/xml/smart_domelement.rb', line 194

def text=(t); @node.content = t.to_s if t.respond_to? :to_s; end

#text_only?Boolean

Returns:

  • (Boolean)


207
208
209
# File 'lib/xml/smart_domelement.rb', line 207

def text_only?;
  @node.xpath_fast('*').length == 0 &&  @node.xpath_fast("string(text())") != '';
end

#to_docObject



223
224
225
226
227
# File 'lib/xml/smart_domelement.rb', line 223

def to_doc
  doc = Nokogiri::XML::Document.new
  doc.root = @node
  Dom.new(doc)
end

#to_fObject



144
# File 'lib/xml/smart_domelement.rb', line 144

def to_f; @node.content.to_f; end

#to_iObject



143
# File 'lib/xml/smart_domelement.rb', line 143

def to_i; @node.content.to_i; end

#to_sObject



142
# File 'lib/xml/smart_domelement.rb', line 142

def to_s; @node.content; end

#unique_idObject



221
# File 'lib/xml/smart_domelement.rb', line 221

def unique_id; @node.pointer_id; end

#xinclude!(basedir = nil) ⇒ Object



166
167
168
169
170
171
# File 'lib/xml/smart_domelement.rb', line 166

def xinclude!(basedir=nil)
  @node.do_xinclude_manual(basedir)
  @node.document.custom_namespace_prefixes_update
  @node.document.ns_update
  true
end