Skip to content

Commit 75239b1

Browse files
committed
Update common files.
1 parent b169d8a commit 75239b1

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

common/common.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,6 @@ function unComment(doc, content) {
271271
return content
272272
.replace(/<!--/, '')
273273
.replace(/-->/, '')
274-
.replace(/< !--/g, '<!--')
275-
.replace(/-- >/g, '-->');
274+
.replace(/< !\s*-\s*-/g, '<!--')
275+
.replace(/-\s*- >/g, '-->');
276276
}

common/extract-examples.rb

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -319,13 +319,18 @@ def save_example(examples:, element:, title:, example_number:, error:, warn:)
319319
$stdout.write "F".colorize(:red)
320320
next
321321
end
322+
323+
# Get base from document, if present
324+
html_base = doc.at_xpath('/html/head/base/@href')
325+
ex[:base] = html_base.to_s if html_base
326+
322327
script_content = doc.at_xpath(xpath)
323328
if script_content
324329
# Remove (faked) XML comments and unescape sequences
325330
content = script_content
326331
.inner_html
327-
.sub(/^\s*< !--/, '')
328-
.sub(/-- >\s*$/, '')
332+
.sub(/^\s*< !\s*-\s*-/, '')
333+
.sub(/-\s*- >\s*$/, '')
329334
.gsub(/&lt;/, '<')
330335
end
331336

@@ -438,7 +443,15 @@ def save_example(examples:, element:, title:, example_number:, error:, warn:)
438443
# Set argument to referenced content to be parsed
439444
args[0] = if examples[ex[:result_for]][:ext] == 'html' && method == :expand
440445
# If we are expanding, and the reference is HTML, find the first script element.
441-
doc = Nokogiri::HTML.parse(examples[ex[:result_for]][:content])
446+
doc = Nokogiri::HTML.parse(
447+
examples[ex[:result_for]][:content]
448+
.sub(/^\s*< !\s*-\s*-/, '')
449+
.sub(/-\s*- >\s*$/, ''))
450+
451+
# Get base from document, if present
452+
html_base = doc.at_xpath('/html/head/base/@href')
453+
options[:base] = html_base.to_s if html_base
454+
442455
script_content = doc.at_xpath(xpath)
443456
unless script_content
444457
errors << "Example #{ex[:number]} at line #{ex[:line]} references example #{ex[:result_for].inspect} with no JSON-LD script element"
@@ -447,12 +460,13 @@ def save_example(examples:, element:, title:, example_number:, error:, warn:)
447460
end
448461
StringIO.new(script_content
449462
.inner_html
450-
.sub(/^\s*< !--/, '')
451-
.sub(/-- >\s*$/, '')
452463
.gsub(/&lt;/, '<'))
453464
elsif examples[ex[:result_for]][:ext] == 'html' && ex[:target]
454465
# Only use the targeted script
455-
doc = Nokogiri::HTML.parse(examples[ex[:result_for]][:content])
466+
doc = Nokogiri::HTML.parse(
467+
examples[ex[:result_for]][:content]
468+
.sub(/^\s*< !\s*-\s*-/, '')
469+
.sub(/-\s*- >\s*$/, ''))
456470
script_content = doc.at_xpath(xpath)
457471
unless script_content
458472
errors << "Example #{ex[:number]} at line #{ex[:line]} references example #{ex[:result_for].inspect} with no JSON-LD script element"
@@ -461,8 +475,6 @@ def save_example(examples:, element:, title:, example_number:, error:, warn:)
461475
end
462476
StringIO.new(script_content
463477
.to_html
464-
.sub(/^\s*< !--/, '')
465-
.sub(/-- >\s*$/, '')
466478
.gsub(/&lt;/, '<'))
467479
else
468480
StringIO.new(examples[ex[:result_for]][:content])

0 commit comments

Comments
 (0)