Sha256: 65bab350ce5e836dabd50ef03f01dc2e325b5b05cc05a12a5c3e908f6a500237

Contents?: true

Size: 1.22 KB

Versions: 248

Compression:

Stored size: 1.22 KB

Contents

module Nokogiri
  module HTML
    class DocumentFragment < Nokogiri::XML::DocumentFragment
      attr_accessor :errors

      ####
      # Create a Nokogiri::XML::DocumentFragment from +tags+, using +encoding+
      def self.parse tags, encoding = nil
        doc = HTML::Document.new

        encoding ||= tags.respond_to?(:encoding) ? tags.encoding.name : 'UTF-8'
        doc.encoding = encoding

        new(doc, tags)
      end

      def initialize document, tags = nil, ctx = nil
        return self unless tags

        if ctx
          preexisting_errors = document.errors.dup
          node_set = ctx.parse("<div>#{tags}</div>")
          node_set.first.children.each { |child| child.parent = self } unless node_set.empty?
          self.errors = document.errors - preexisting_errors
        else
          # This is a horrible hack, but I don't care
          if tags.strip =~ /^<body/i
            path = "/html/body"
          else
            path = "/html/body/node()"
          end

          temp_doc = HTML::Document.parse "<html><body>#{tags}", nil, document.encoding
          temp_doc.xpath(path).each { |child| child.parent = self }
          self.errors = temp_doc.errors
        end
        children
      end
    end
  end
end

Version data entries

248 entries across 248 versions & 20 rubygems

Version Path
rubyjobbuilderdsl-0.0.2 vendor/bundle/ruby/2.1.0/gems/nokogiri-1.6.3.1-x64-mingw32/lib/nokogiri/html/document_fragment.rb
rubyjobbuilderdsl-0.0.1 vendor/bundle/ruby/2.1.0/gems/nokogiri-1.6.3.1-x64-mingw32/lib/nokogiri/html/document_fragment.rb
vagrant-cloudstack-1.2.0 vendor/bundle/gems/nokogiri-1.6.3.1-x86-mingw32/lib/nokogiri/html/document_fragment.rb
vagrant-cloudstack-1.1.0 vendor/bundle/gems/nokogiri-1.6.3.1/lib/nokogiri/html/document_fragment.rb
nokogiri-1.6.5 lib/nokogiri/html/document_fragment.rb
nokogiri-1.6.5-x86-mingw32 lib/nokogiri/html/document_fragment.rb
nokogiri-1.6.5-x64-mingw32 lib/nokogiri/html/document_fragment.rb
nokogiri-1.6.5-java lib/nokogiri/html/document_fragment.rb
nokogiri-1.6.4.1 lib/nokogiri/html/document_fragment.rb
nokogiri-1.6.4.1-x86-mingw32 lib/nokogiri/html/document_fragment.rb
nokogiri-1.6.4.1-x64-mingw32 lib/nokogiri/html/document_fragment.rb
nokogiri-1.6.4.1-java lib/nokogiri/html/document_fragment.rb
nokogiri-1.6.4 lib/nokogiri/html/document_fragment.rb
nokogiri-1.6.4-x86-mingw32 lib/nokogiri/html/document_fragment.rb
nokogiri-1.6.4-x64-mingw32 lib/nokogiri/html/document_fragment.rb
nokogiri-1.6.4-java lib/nokogiri/html/document_fragment.rb
nokogiri-1.6.3.1 lib/nokogiri/html/document_fragment.rb
nokogiri-1.6.3.1-x86-mingw32 lib/nokogiri/html/document_fragment.rb
nokogiri-1.6.3.1-x64-mingw32 lib/nokogiri/html/document_fragment.rb
nokogiri-1.6.3.1-java lib/nokogiri/html/document_fragment.rb