Skip to content

Commit da1d4c8

Browse files
committed
Fix HTML libraries to deal with loading when both have been required by being explicit about which library to use.
1 parent 2e0a34f commit da1d4c8

File tree

5 files changed

+14
-12
lines changed

5 files changed

+14
-12
lines changed

lib/json/ld/api.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -703,14 +703,14 @@ def self.load_html(input, url:,
703703
require "json/ld/html/#{library}"
704704

705705
# Parse HTML using the appropriate library
706-
@implementation = case library
706+
implementation = case library
707707
when :nokogiri then Nokogiri
708708
when :rexml then REXML
709709
end
710-
self.extend(@implementation)
710+
self.extend(implementation)
711711

712712
input = begin
713-
initialize_html(input, **options)
713+
self.send("initialize_html_#{library}".to_sym, input, **options)
714714
rescue
715715
raise JSON::LD::JsonLdError::LoadingDocumentFailed, "Malformed HTML document: #{$!.message}"
716716
end

lib/json/ld/html/nokogiri.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def method_missing(method, *args)
129129
#
130130
# @param [Hash{Symbol => Object}] options
131131
# @return [NodeProxy] of root element
132-
def initialize_html(input, options = {})
132+
def initialize_html_nokogiri(input, options = {})
133133
require 'nokogiri' unless defined?(::Nokogiri)
134134
doc = case input
135135
when ::Nokogiri::HTML::Document, ::Nokogiri::XML::Document
@@ -146,6 +146,7 @@ def initialize_html(input, options = {})
146146

147147
NodeProxy.new(doc.root) if doc && doc.root
148148
end
149+
alias_method :initialize_html, :initialize_html_nokogiri
149150
end
150151
end
151152
end

lib/json/ld/html/rexml.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def method_missing(method, *args)
169169
#
170170
# @param [Hash{Symbol => Object}] options
171171
# @return [NodeProxy] of document root
172-
def initialize_html(input, options = {})
172+
def initialize_html_rexml(input, options = {})
173173
require 'rexml/document' unless defined?(::REXML)
174174
doc = case input
175175
when ::REXML::Document
@@ -181,6 +181,7 @@ def initialize_html(input, options = {})
181181

182182
NodeProxy.new(doc.root) if doc && doc.root
183183
end
184+
alias_method :initialize_html, :initialize_html_rexml
184185
end
185186
end
186187
end

spec/context_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ def containers
503503
"@type as object" => {"foo" => {"@type" => {}}},
504504
"@type as array" => {"foo" => {"@type" => []}},
505505
"@type as @list" => {"foo" => {"@type" => "@list"}},
506-
"@type as @none" => {"@version": 1.1, "foo" => {"@type" => "@none"}},
506+
"@type as @none" => {"@version" => 1.1, "foo" => {"@type" => "@none"}},
507507
"@type as @set" => {"foo" => {"@type" => "@set"}},
508508
"@container as object" => {"foo" => {"@container" => {}}},
509509
"@container as empty array" => {"foo" => {"@container" => []}},

spec/expand_spec.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3386,7 +3386,7 @@
33863386
%w(Nokogiri REXML).each do |impl|
33873387
next unless Module.constants.map(&:to_s).include?(impl)
33883388
context impl do
3389-
before(:all) {@library = impl.downcase.to_s.to_sym}
3389+
let(:library) {impl.downcase.to_s.to_sym}
33903390

33913391
{
33923392
"Expands embedded JSON-LD script element": {
@@ -3525,9 +3525,9 @@
35253525
]),
35263526
extractAllScripts: true
35273527
},
3528-
"Expands as empty with no script element": {
3528+
"Errors no script element": {
35293529
input: %(<html><head></head></html>),
3530-
output: %([])
3530+
exception: JSON::LD::JsonLdError::LoadingDocumentFailed
35313531
},
35323532
"Expands as empty with no script element and extractAllScripts": {
35333533
input: %(<html><head></head></html>),
@@ -3742,10 +3742,10 @@
37423742
},
37433743
}.each do |title, params|
37443744
it(title) do
3745-
skip "rexml" if params[:not] == @library
3746-
params[:input] = StringIO.new(params[:input])
3745+
skip "rexml" if params[:not] == library
3746+
params = params.merge(input: StringIO.new(params[:input]))
37473747
params[:input].send(:define_singleton_method, :content_type) {"text/html"}
3748-
run_expand params.merge(validate: true, library: @library)
3748+
run_expand params.merge(validate: true, library: library)
37493749
end
37503750
end
37513751
end

0 commit comments

Comments
 (0)