Sha256: 65e6dfc01d81375b959481b9854e6568c17a4059c8752a8e7aafb4c53f5a691e

Contents?: true

Size: 1.51 KB

Versions: 48

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true
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 ||= if tags.respond_to?(:encoding)
                       encoding = tags.encoding
                       if encoding == ::Encoding::ASCII_8BIT
                         'UTF-8'
                       else
                         encoding.name
                       end
                     else
                       'UTF-8'
                     end

        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

48 entries across 46 versions & 3 rubygems

Version Path
nokogiri-1.11.0.rc2-x64-mingw32 lib/nokogiri/html/document_fragment.rb
nokogiri-1.11.0.rc2-java lib/nokogiri/html/document_fragment.rb
nokogiri-1.11.0.rc1-x86_64-linux lib/nokogiri/html/document_fragment.rb
nokogiri-1.11.0.rc1-x86-linux lib/nokogiri/html/document_fragment.rb
nokogiri-1.11.0.rc1 lib/nokogiri/html/document_fragment.rb
nokogiri-1.11.0.rc1-x86-mingw32 lib/nokogiri/html/document_fragment.rb
nokogiri-1.11.0.rc1-x64-mingw32 lib/nokogiri/html/document_fragment.rb
nokogiri-1.11.0.rc1-java lib/nokogiri/html/document_fragment.rb