Sha256: eed95c1618d4718f2df70d539c70818315c1921ce02718acd8aace9fb56209e2

Contents?: true

Size: 1.39 KB

Versions: 61

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true
module Nokogiri
  module HTML4
    class DocumentFragment < Nokogiri::XML::DocumentFragment
      ####
      # Create a Nokogiri::XML::DocumentFragment from +tags+, using +encoding+
      def self.parse(tags, encoding = nil)
        doc = HTML4::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
          path = if /^\s*?<body/i.match?(tags)
            "/html/body"
          else
            "/html/body/node()"
          end

          temp_doc = HTML4::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

61 entries across 61 versions & 4 rubygems

Version Path
tdiary-5.2.0 vendor/bundle/ruby/3.0.0/gems/nokogiri-1.12.5-x86_64-linux/lib/nokogiri/html4/document_fragment.rb
date_n_time_picker_activeadmin-0.1.2 vendor/bundle/ruby/2.6.0/gems/nokogiri-1.12.5-x86_64-linux/lib/nokogiri/html4/document_fragment.rb
date_n_time_picker_activeadmin-0.1.1 vendor/bundle/ruby/2.6.0/gems/nokogiri-1.12.5-x86_64-linux/lib/nokogiri/html4/document_fragment.rb
nokogiri-1.12.5 lib/nokogiri/html4/document_fragment.rb
nokogiri-1.12.5-x86_64-linux lib/nokogiri/html4/document_fragment.rb
nokogiri-1.12.5-x86_64-darwin lib/nokogiri/html4/document_fragment.rb
nokogiri-1.12.5-x86-mingw32 lib/nokogiri/html4/document_fragment.rb
nokogiri-1.12.5-x86-linux lib/nokogiri/html4/document_fragment.rb
nokogiri-1.12.5-x64-mingw32 lib/nokogiri/html4/document_fragment.rb
nokogiri-1.12.5-java lib/nokogiri/html4/document_fragment.rb
nokogiri-1.12.5-arm64-darwin lib/nokogiri/html4/document_fragment.rb
nokogiri-1.12.4 lib/nokogiri/html4/document_fragment.rb
nokogiri-1.12.4-x86_64-linux lib/nokogiri/html4/document_fragment.rb
nokogiri-1.12.4-x86_64-darwin lib/nokogiri/html4/document_fragment.rb
nokogiri-1.12.4-x86-mingw32 lib/nokogiri/html4/document_fragment.rb
nokogiri-1.12.4-x86-linux lib/nokogiri/html4/document_fragment.rb
nokogiri-1.12.4-x64-mingw32 lib/nokogiri/html4/document_fragment.rb
nokogiri-1.12.4-java lib/nokogiri/html4/document_fragment.rb
nokogiri-1.12.4-arm64-darwin lib/nokogiri/html4/document_fragment.rb
tdiary-5.1.7 vendor/bundle/ruby/3.0.0/gems/nokogiri-1.12.3-x86_64-linux/lib/nokogiri/html4/document_fragment.rb