Class: Nokogiri::XML::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/niceogiri/core_ext/nokogiri.rb

Overview

Nokogiri::Node extensions

Direct Known Subclasses

Niceogiri::XML::Node

Instance Method Summary collapse

Instance Method Details

#[]=(name, value) ⇒ Object

Override Nokogiri’s attribute setter to add the ability to kill an attribute by setting it to nil and to be able to lookup an attribute by symbol

Parameters:

  • name (#to_s)

    the name of the attribute

  • value (#to_s, nil)

    the new value or nil to remove it



17
18
19
20
21
22
23
24
25
# File 'lib/niceogiri/core_ext/nokogiri.rb', line 17

def []=(name, value)
  name = name.to_s
  if value.nil?
    remove_attribute name
  else
    value = value.is_a?(Array) ? value.join : value
    attr_set name, value.to_s
  end
end

#attr_setObject



11
# File 'lib/niceogiri/core_ext/nokogiri.rb', line 11

alias_method :attr_set, :[]=

#find_first(*paths) ⇒ Object

Return the first element at a specified xpath

See Also:



43
44
45
# File 'lib/niceogiri/core_ext/nokogiri.rb', line 43

def find_first(*paths)
  xpath(*paths).first
end

#nokogiri_xpathObject



27
# File 'lib/niceogiri/core_ext/nokogiri.rb', line 27

alias_method :nokogiri_xpath, :xpath

#xpath(*paths) ⇒ Object Also known as: find

Override Nokogiri’s #xpath method to add the ability to use symbols for lookup and namespace designation



30
31
32
33
34
35
36
37
38
# File 'lib/niceogiri/core_ext/nokogiri.rb', line 30

def xpath(*paths)
  paths[0] = paths[0].to_s

  if paths.size > 1 && (namespaces = paths.pop).is_a?(Hash)
    paths << namespaces.inject({}) { |h,v| h[v[0].to_s] = v[1]; h }
  end

  nokogiri_xpath *paths
end