Sha256: aa1f77b21a343405c377d25d2826c452d986b1347c808148b79651b099475671

Contents?: true

Size: 1.57 KB

Versions: 8

Compression:

Stored size: 1.57 KB

Contents

# frozen_string_literal: true

require "nokogiri"
require "active_support"
require "active_support/core_ext/module/attribute_accessors"

require "rails/dom/testing/assertions"

module Rails
  module Dom
    module Testing
      mattr_accessor :default_html_version, default: :html4

      class << self
        def html5_support?
          defined?(Nokogiri::HTML5)
        end

        def html_document(html_version: nil)
          parser_classes = { html4: Nokogiri::HTML4::Document }
          parser_classes[:html5] = Nokogiri::HTML5::Document if html5_support?

          choose_html_parser(parser_classes, html_version: html_version)
        end

        def html_document_fragment(html_version: nil)
          parser_classes = { html4: Nokogiri::HTML4::DocumentFragment }
          parser_classes[:html5] = Nokogiri::HTML5::DocumentFragment if html5_support?

          choose_html_parser(parser_classes, html_version: html_version)
        end

        private
          def choose_html_parser(parser_classes, html_version: nil)
            html_version ||= Rails::Dom::Testing.default_html_version

            case html_version
            when :html4
              parser_classes[:html4]
            when :html5
              unless Rails::Dom::Testing.html5_support?
                raise NotImplementedError, "html5 parser is not supported on this platform"
              end
              parser_classes[:html5]
            else
              raise ArgumentError, "html_version must be :html4 or :html5, received #{html_version.inspect}"
            end
          end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 6 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rails-dom-testing-2.2.0/lib/rails/dom/testing.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rails-dom-testing-2.2.0/lib/rails/dom/testing.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rails-dom-testing-2.2.0/lib/rails/dom/testing.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rails-dom-testing-2.2.0/lib/rails/dom/testing.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rails-dom-testing-2.2.0/lib/rails/dom/testing.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rails-dom-testing-2.2.0/lib/rails/dom/testing.rb
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/rails-dom-testing-2.2.0/lib/rails/dom/testing.rb
rails-dom-testing-2.2.0 lib/rails/dom/testing.rb