Sha256: 398bf00c58c66020f85d61aebbc0f9c4330b900fa344e5c55cf3c875529948be

Contents?: true

Size: 1.31 KB

Versions: 2

Compression:

Stored size: 1.31 KB

Contents

# @private
module Nokogiri
  module XML

    # Nokogiri::Node extensions
    class Node
      # Alias #name to #element_name so we can use #name in an XMPP Stanza context
      alias_method :element_name, :name
      alias_method :element_name=, :name=

      alias_method :attr_set, :[]=
      # 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
      #
      # @param [#to_s] name the name of the attribute
      # @param [#to_s, nil] value the new value or nil to remove it
      def []=(name, value)
        name = name.to_s
        value.nil? ? remove_attribute(name) : attr_set(name, value.to_s)
      end

      alias_method :nokogiri_xpath, :xpath
      # Override Nokogiri's #xpath method to add the ability to use symbols for lookup
      # and namespace designation
      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
      alias_method :find, :xpath

      # Return the first element at a specified xpath
      # @see #xpath
      def find_first(*paths)
        xpath(*paths).first
      end
    end

  end # XML
end # Nokogiri

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
niceogiri-0.0.4 lib/niceogiri/core_ext/nokogiri.rb
niceogiri-0.0.3 lib/niceogiri/core_ext/nokogiri.rb