Sha256: 4a7bd0d69ea2751cb1857913aebeec9f000a61682f130dcbb0c301a65f3bab14

Contents?: true

Size: 1.64 KB

Versions: 198

Compression:

Stored size: 1.64 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, options = XML::ParseOptions::DEFAULT_HTML, &block)
        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, nil, options, &block)
      end

      def initialize(document, tags = nil, ctx = nil, options = XML::ParseOptions::DEFAULT_HTML)
        return self unless tags

        options = Nokogiri::XML::ParseOptions.new(options) if Integer === options
        yield options if block_given?

        if ctx
          preexisting_errors = document.errors.dup
          node_set = ctx.parse("<div>#{tags}</div>", options)
          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, options)
          temp_doc.xpath(path).each { |child| child.parent = self }
          self.errors = temp_doc.errors
        end
        children
      end
    end
  end
end

Version data entries

198 entries across 196 versions & 5 rubygems

Version Path
nokogiri-1.13.1-x86_64-darwin lib/nokogiri/html4/document_fragment.rb
nokogiri-1.13.1-x86-mingw32 lib/nokogiri/html4/document_fragment.rb
nokogiri-1.13.1-x86-linux lib/nokogiri/html4/document_fragment.rb
nokogiri-1.13.1-x64-mingw32 lib/nokogiri/html4/document_fragment.rb
nokogiri-1.13.1-x64-mingw-ucrt lib/nokogiri/html4/document_fragment.rb
nokogiri-1.13.1-java lib/nokogiri/html4/document_fragment.rb
nokogiri-1.13.1-arm64-darwin lib/nokogiri/html4/document_fragment.rb
nokogiri-1.13.1-aarch64-linux lib/nokogiri/html4/document_fragment.rb
nokogiri-1.13.0 lib/nokogiri/html4/document_fragment.rb
nokogiri-1.13.0-x86_64-linux lib/nokogiri/html4/document_fragment.rb
nokogiri-1.13.0-x86_64-darwin lib/nokogiri/html4/document_fragment.rb
nokogiri-1.13.0-x86-mingw32 lib/nokogiri/html4/document_fragment.rb
nokogiri-1.13.0-x86-linux lib/nokogiri/html4/document_fragment.rb
nokogiri-1.13.0-x64-mingw32 lib/nokogiri/html4/document_fragment.rb
nokogiri-1.13.0-x64-mingw-ucrt lib/nokogiri/html4/document_fragment.rb
nokogiri-1.13.0-java lib/nokogiri/html4/document_fragment.rb
nokogiri-1.13.0-arm64-darwin lib/nokogiri/html4/document_fragment.rb
nokogiri-1.13.0-aarch64-linux lib/nokogiri/html4/document_fragment.rb