@@ -90,13 +90,12 @@ def initialize(input, context, options = {}, &block)
9090 @options = {
9191 compactArrays : true ,
9292 rename_bnodes : true ,
93- documentLoader : self . class . method ( :documentLoader ) ,
94- compactToRelative : true
93+ documentLoader : self . class . method ( :documentLoader )
9594 } . merge ( options )
9695 @namer = options [ :unique_bnodes ] ? BlankNodeUniqer . new : ( @options [ :rename_bnodes ] ? BlankNodeNamer . new ( "b" ) : BlankNodeMapper . new )
9796
9897 # For context via Link header
99- context_ref = nil
98+ remote_base , context_ref = nil , nil
10099
101100 @value = case input
102101 when Array , Hash then input . dup
@@ -116,8 +115,9 @@ def initialize(input, context, options = {}, &block)
116115 when String
117116 remote_doc = @options [ :documentLoader ] . call ( input , @options )
118117
119- @options = { base : remote_doc . documentUrl } . merge ( @options )
118+ remote_base = remote_doc . documentUrl
120119 context_ref = remote_doc . contextUrl
120+ @options = { base : remote_doc . documentUrl } . merge ( @options ) unless @options [ :no_default_base ]
121121
122122 case remote_doc . document
123123 when String
@@ -136,13 +136,6 @@ def initialize(input, context, options = {}, &block)
136136 @options [ :processingMode ] ||= @context . processingMode || "json-ld-1.0"
137137 @options [ :validate ] ||= %w( json-ld-1.0 json-ld-1.1 ) . include? ( @options [ :processingMode ] )
138138
139- # If, after processing, the context does not have a _base IRI_, and the _compactToRelative_ option is set to true or processingMode is json-ld-1.0, set _base IRI_ in the active context to either the _base_ option from the API, if set, or the IRI of the currently being processed document.
140- if !@context . base && @options [ :base ]
141- doc_base = RDF ::URI ( @options [ :base ] ) . dup
142- doc_base . canonicalize! if options [ :canonicalize ]
143- @context . base = doc_base
144- end
145-
146139 if block_given?
147140 case block . arity
148141 when 0 , -1 then instance_eval ( &block )
@@ -222,16 +215,17 @@ def self.expand(input, options = {}, &block)
222215 # @see http://json-ld.org/spec/latest/json-ld-api/#compaction-algorithm
223216 def self . compact ( input , context , options = { } )
224217 result = nil
218+ options = { compactToRelative : true } . merge ( options )
225219
226220 # 1) Perform the Expansion Algorithm on the JSON-LD input.
227221 # This removes any existing context to allow the given context to be cleanly applied.
228222 expanded_input = options [ :expanded ] ? input : API . expand ( input , options ) do |result , base_iri |
229- options [ :base ] ||= base_iri
223+ options [ :base ] ||= base_iri if options [ :compactToRelative ]
230224 result
231225 end
232226
233227 #require 'byebug'; byebug
234- API . new ( expanded_input , context , options ) do
228+ API . new ( expanded_input , context , options . merge ( no_default_base : true ) ) do
235229 log_debug ( ".compact" ) { "expanded input: #{ expanded_input . to_json ( JSON_STATE ) rescue 'malformed json' } " }
236230 result = compact ( value )
237231
@@ -267,15 +261,16 @@ def self.compact(input, context, options = {})
267261 # @see http://json-ld.org/spec/latest/json-ld-api/#framing-algorithm
268262 def self . flatten ( input , context , options = { } )
269263 flattened = [ ]
264+ options = { compactToRelative : true } . merge ( options )
270265
271266 # Expand input to simplify processing
272267 expanded_input = options [ :expanded ] ? input : API . expand ( input , options ) do |result , base_iri |
273- options [ :base ] ||= base_iri
268+ options [ :base ] ||= base_iri if options [ :compactToRelative ]
274269 result
275270 end
276271
277272 # Initialize input using
278- API . new ( expanded_input , context , options ) do
273+ API . new ( expanded_input , context , options . merge ( no_default_base : true ) ) do
279274 log_debug ( ".flatten" ) { "expanded input: #{ value . to_json ( JSON_STATE ) rescue 'malformed json' } " }
280275
281276 # Initialize node map to a JSON object consisting of a single member whose key is @default and whose value is an empty JSON object.
@@ -340,6 +335,7 @@ def self.frame(input, frame, options = {})
340335 options = {
341336 base : ( input if input . is_a? ( String ) ) ,
342337 compactArrays : true ,
338+ compactToRelative : true ,
343339 embed : '@last' ,
344340 explicit : false ,
345341 requireAll : true ,
@@ -369,15 +365,15 @@ def self.frame(input, frame, options = {})
369365
370366 # Expand input to simplify processing
371367 expanded_input = options [ :expanded ] ? input : API . expand ( input , options ) do |result , base_iri |
372- options [ :base ] ||= base_iri
368+ options [ :base ] ||= base_iri if options [ :compactToRelative ]
373369 result
374370 end
375371
376372 # Expand frame to simplify processing
377373 expanded_frame = API . expand ( frame , options . merge ( processingMode : "json-ld-1.1-expand-frame" ) )
378374
379375 # Initialize input using frame as context
380- API . new ( expanded_input , nil , options ) do
376+ API . new ( expanded_input , nil , options . merge ( no_default_base : true ) ) do
381377 log_debug ( ".frame" ) { "expanded frame: #{ expanded_frame . to_json ( JSON_STATE ) rescue 'malformed json' } " }
382378
383379 # Get framing nodes from expanded input, replacing Blank Node identifiers as necessary
0 commit comments