-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #109 from flavorjones/flavorjones-dom-testing-html…
…-version Allow user to choose the HTML parser used
- Loading branch information
Showing
10 changed files
with
446 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
encoding: UTF-8 | ||
charset: UTF-8 | ||
exclude: | ||
- "\\.gemspec\\z" | ||
- "gemfiles" | ||
- "Gemfile*" | ||
- "Rakefile" | ||
main_page: "README.md" | ||
markup: rdoc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# frozen_string_literal: true | ||
|
||
require "active_support" | ||
require "active_support/core_ext/module/attribute_accessors" | ||
|
||
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 | ||
|
||
require_relative "railtie" if defined?(Rails::Railtie) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
module Rails | ||
module Dom | ||
module Testing | ||
class Railtie < Rails::Railtie # :nodoc: | ||
config.after_initialize do |app| | ||
version = app.config.try(:dom_testing_default_html_version) # Rails 7.1+ | ||
Rails::Dom::Testing.default_html_version = version if version | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.