Sha256: c206c8eff832d5212678a924205750c8ff42c006621f19a05d14e016d5f53715

Contents?: true

Size: 1.91 KB

Versions: 27

Compression:

Stored size: 1.91 KB

Contents

# :stopdoc:
module HTree
  class Context
    DefaultNamespaces = {'xml'=>'http://www.w3.org/XML/1998/namespace'}
    DefaultNamespaces.default = ""
    DefaultNamespaces.freeze

    # The optional argument `namespaces' should be a hash or nil.
    # HTree::DefaultNamespaces is used if nil is specified.
    #
    # If it is a hash, its key should be nil or a string.
    # nil means default namespace.
    # The string means some prefix which must not be empty.
    #
    # The hash value should be a string.
    # The empty string "" means unbound namespace.
    def initialize(namespaces=nil)
      namespaces ||= DefaultNamespaces
      namespaces.each_pair {|k, v|
        check_namespace_prefix(k)
        check_namespace_uri(v)
      }
      namespaces = namespaces.dup.freeze unless namespaces.frozen?
      @namespaces = namespaces
    end
    attr_reader :namespaces

    # return a namespace URI corresponding to _prefix_.
    # It returns nil if _prefix_ is not defined.
    def namespace_uri(prefix)
      @namespaces[prefix]
    end

    # generate a new Context object which namespaces are substituted by
    # a hash _declared_namespaces_.
    def subst_namespaces(declared_namespaces)
      namespaces = @namespaces.dup
      declared_namespaces.each {|k, v|
        check_namespace_prefix(k)
        check_namespace_uri(v)
        namespaces[k] = v
      }
      if namespaces == @namespaces
        self
      else
        Context.new(namespaces)
      end
    end

    private
    def check_namespace_prefix(k)
      unless (String === k && !k.empty?) || k == nil
        raise ArgumentError, "invalid namespace prefix: #{k.inspect}"
      end
    end

    def check_namespace_uri(v)
      unless String === v
        raise ArgumentError, "invalid namespace URI: #{v.inspect}"
      end
    end
  end

  DefaultContext = Context.new
  HTMLContext = DefaultContext.subst_namespaces(nil=>"http://www.w3.org/1999/xhtml")
end
# :startdoc:

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
feedtools-0.2.10 lib/feed_tools/vendor/htree/context.rb
feedtools-0.2.11 lib/feed_tools/vendor/htree/context.rb
feedtools-0.2.12 lib/feed_tools/vendor/htree/context.rb
feedtools-0.2.0 lib/feed_tools/vendor/htree/context.rb
feedtools-0.2.1 lib/feed_tools/vendor/htree/context.rb
feedtools-0.2.13 lib/feed_tools/vendor/htree/context.rb
feedtools-0.2.16 lib/feed_tools/vendor/htree/context.rb
feedtools-0.2.14 lib/feed_tools/vendor/htree/context.rb
feedtools-0.2.15 lib/feed_tools/vendor/htree/context.rb
feedtools-0.2.17 lib/feed_tools/vendor/htree/context.rb
feedtools-0.2.18 lib/feed_tools/vendor/htree/context.rb
feedtools-0.2.19 lib/feed_tools/vendor/htree/context.rb
feedtools-0.2.2 lib/feed_tools/vendor/htree/context.rb
feedtools-0.2.20 lib/feed_tools/vendor/htree/context.rb
feedtools-0.2.21 lib/feed_tools/vendor/htree/context.rb
feedtools-0.2.23 lib/feed_tools/vendor/htree/context.rb
feedtools-0.2.22 lib/feed_tools/vendor/htree/context.rb
feedtools-0.2.24 lib/feed_tools/vendor/htree/context.rb
feedtools-0.2.25 lib/feed_tools/vendor/htree/context.rb
feedtools-0.2.26 lib/feed_tools/vendor/htree/context.rb