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)
  @element = element
end

Instance Method Details

#==(other) ⇒ Object



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

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(@element.add_child tmp)
  if update
    @element.document.custom_namespace_prefixes_update
    @element.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(@element.add_next_sibling tmp)
  if update
    @element.document.custom_namespace_prefixes_update
    @element.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(@element.add_previous_sibling tmp)
  if update
    @element.document.custom_namespace_prefixes_update
    @element.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



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

def attributes; AttributeSet.new @element; end

#childrenObject



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

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

#children?Boolean

Returns:

  • (Boolean)


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

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 = @element
  doc.root.to_s
end

#element_only?Boolean

Returns:

  • (Boolean)


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

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

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?; !children?; end

#find(xpath) ⇒ Object



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

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

#mixed?Boolean

Returns:

  • (Boolean)


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

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

#namespaceObject



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

def namespace; namespace? ? Namespace.new(@element.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 = @element.document.custom_namespace_prefixes[n] || @element.document.user_custom_namespace_prefixes[n]
  unless tmp.nil?
    @element.namespace_scopes.each do |nss|
      @element.namespace = nss if nss.href == tmp
    end
  end
end

#namespace?Boolean

Returns:

  • (Boolean)


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

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

#namespacesObject



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

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

#parentObject



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

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

#parent?Boolean

Returns:

  • (Boolean)


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

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

#pathObject



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

def path; @element.path[-1] != ']' ? @element.path + '[1]' : @element.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



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

def qname; QName.new @element; 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 @element.replace(n.instance_variable_get(:@element).dup)
    when NodeSet; NodeSet.new @element.replace(n.instance_variable_get(:@nodeset).dup)
    else
      nil
  end
end

#textObject



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

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

#text=(t) ⇒ Object



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

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

#text_only?Boolean

Returns:

  • (Boolean)


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

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

#to_docObject



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

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

#to_fObject



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

def to_f; @element.content.to_f; end

#to_iObject



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

def to_i; @element.content.to_i; end

#to_sObject



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

def to_s; @element.content; end

#unique_idObject



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

def unique_id; @element.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)
  @element.do_xinclude_manual(basedir)
  @element.document.custom_namespace_prefixes_update
  @element.document.ns_update
  true
end