@@ -319,13 +319,18 @@ def save_example(examples:, element:, title:, example_number:, error:, warn:)
319
319
$stdout. write "F" . colorize ( :red )
320
320
next
321
321
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
+
322
327
script_content = doc . at_xpath ( xpath )
323
328
if script_content
324
329
# Remove (faked) XML comments and unescape sequences
325
330
content = script_content
326
331
. inner_html
327
- . sub ( /^\s *< !- -/ , '' )
328
- . sub ( /-- >\s *$/ , '' )
332
+ . sub ( /^\s *< !\s *- \s * -/ , '' )
333
+ . sub ( /-\s * - >\s *$/ , '' )
329
334
. gsub ( /</ , '<' )
330
335
end
331
336
@@ -438,7 +443,15 @@ def save_example(examples:, element:, title:, example_number:, error:, warn:)
438
443
# Set argument to referenced content to be parsed
439
444
args [ 0 ] = if examples [ ex [ :result_for ] ] [ :ext ] == 'html' && method == :expand
440
445
# 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
+
442
455
script_content = doc . at_xpath ( xpath )
443
456
unless script_content
444
457
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:)
447
460
end
448
461
StringIO . new ( script_content
449
462
. inner_html
450
- . sub ( /^\s *< !--/ , '' )
451
- . sub ( /-- >\s *$/ , '' )
452
463
. gsub ( /</ , '<' ) )
453
464
elsif examples [ ex [ :result_for ] ] [ :ext ] == 'html' && ex [ :target ]
454
465
# 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 *$/ , '' ) )
456
470
script_content = doc . at_xpath ( xpath )
457
471
unless script_content
458
472
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:)
461
475
end
462
476
StringIO . new ( script_content
463
477
. to_html
464
- . sub ( /^\s *< !--/ , '' )
465
- . sub ( /-- >\s *$/ , '' )
466
478
. gsub ( /</ , '<' ) )
467
479
else
468
480
StringIO . new ( examples [ ex [ :result_for ] ] [ :content ] )
0 commit comments