Sha256: adb089b9babcc5d72ca6d7be0d742b7a7733c4d3d58f362fc379d705f17912fe

Contents?: true

Size: 1.19 KB

Versions: 10

Compression:

Stored size: 1.19 KB

Contents

module Nokogiri
  module HTML
    class DocumentFragment < Nokogiri::XML::DocumentFragment
      ####
      # 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

        children = if ctx
                     ctx.parse("<div>#{tags.strip}</div>").first.children
                   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

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

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
nokogiri-1.5.0.beta.2 lib/nokogiri/html/document_fragment.rb
nokogiri-1.5.0.beta.2-java lib/nokogiri/html/document_fragment.rb
nokogiri-1.4.3.1-java lib/nokogiri/html/document_fragment.rb
nokogiri-1.4.3.1-x86-mswin32 lib/nokogiri/html/document_fragment.rb
nokogiri-1.4.3.1-x86-mingw32 lib/nokogiri/html/document_fragment.rb
nokogiri-1.4.3.1 lib/nokogiri/html/document_fragment.rb
nokogiri-1.4.3-java lib/nokogiri/html/document_fragment.rb
nokogiri-1.4.3-x86-mswin32 lib/nokogiri/html/document_fragment.rb
nokogiri-1.4.3-x86-mingw32 lib/nokogiri/html/document_fragment.rb
nokogiri-1.4.3 lib/nokogiri/html/document_fragment.rb