Skip to content

Commit 5f0f0b8

Browse files
committed
Allow loading multiple profiled documents with the extractAllScripts option, but only a single fall back document, if none exist.
1 parent 74a05a9 commit 5f0f0b8

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

lib/json/ld/api.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,14 @@ def load_html(input, url:,
650650
MultiJson.load(content, options)
651651
elsif extractAllScripts
652652
res = []
653-
elements = input.xpath("//script[starts-with(@type, 'application/ld+json')]")
653+
elements = if profile
654+
es = input.xpath("//script[starts-with(@type, 'application/ld+json;profile=#{profile}')]")
655+
# If no profile script, just take a single script without profile
656+
es = [input.at_xpath("//script[starts-with(@type, 'application/ld+json')]")] if es.empty?
657+
es
658+
else
659+
input.xpath("//script[starts-with(@type, 'application/ld+json')]")
660+
end
654661
elements.each do |element|
655662
content = element.inner_html
656663
validate_input(content, url: url) if options[:validate]

0 commit comments

Comments
 (0)