Sha256: 503917b9b09c52d986af858b963fa595288e154e68e479d560029df89dca78b4

Contents?: true

Size: 1.48 KB

Versions: 136

Compression:

Stored size: 1.48 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 ||= 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

136 entries across 114 versions & 12 rubygems

Version Path
husc-0.1.1 vendor/bundle/gems/nokogiri-1.10.7/lib/nokogiri/html/document_fragment.rb
husc-0.1.0 vendor/bundle/gems/nokogiri-1.10.7/lib/nokogiri/html/document_fragment.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/nokogiri-1.10.7/lib/nokogiri/html/document_fragment.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/nokogiri-1.10.3/lib/nokogiri/html/document_fragment.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/nokogiri-1.10.5/lib/nokogiri/html/document_fragment.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/nokogiri-1.10.4/lib/nokogiri/html/document_fragment.rb
nokogiri-1.10.7 lib/nokogiri/html/document_fragment.rb
nokogiri-1.10.7-x86-mingw32 lib/nokogiri/html/document_fragment.rb
nokogiri-1.10.7-x64-mingw32 lib/nokogiri/html/document_fragment.rb
nokogiri-1.10.7-java lib/nokogiri/html/document_fragment.rb
nokogiri-1.10.6 lib/nokogiri/html/document_fragment.rb
nokogiri-1.10.6-x86-mingw32 lib/nokogiri/html/document_fragment.rb
nokogiri-1.10.6-x64-mingw32 lib/nokogiri/html/document_fragment.rb
nokogiri-1.10.6-java lib/nokogiri/html/document_fragment.rb
tdiary-5.1.0 vendor/bundle/gems/nokogiri-1.10.5/lib/nokogiri/html/document_fragment.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/nokogiri-1.10.5/lib/nokogiri/html/document_fragment.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/nokogiri-1.10.3/lib/nokogiri/html/document_fragment.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/nokogiri-1.10.4/lib/nokogiri/html/document_fragment.rb
chatops-rpc-0.0.2 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/nokogiri-1.10.4/lib/nokogiri/html/document_fragment.rb
chatops-rpc-0.0.1 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/nokogiri-1.10.4/lib/nokogiri/html/document_fragment.rb