@@ -240,7 +240,9 @@ api.createTermDefinition = (activeCtx, localCtx, term, defined) => {
240
240
}
241
241
242
242
// convert short-hand value to object w/@id
243
+ let simpleTerm = false ;
243
244
if ( _isString ( value ) ) {
245
+ simpleTerm = true ;
244
246
value = { '@id' : value } ;
245
247
}
246
248
@@ -261,7 +263,7 @@ api.createTermDefinition = (activeCtx, localCtx, term, defined) => {
261
263
262
264
// JSON-LD 1.1 support
263
265
if ( api . processingMode ( activeCtx , 1.1 ) ) {
264
- validKeys . push ( '@context' ) ;
266
+ validKeys . push ( '@context' , '@prefix' ) ;
265
267
}
266
268
267
269
for ( let kw in value ) {
@@ -273,6 +275,11 @@ api.createTermDefinition = (activeCtx, localCtx, term, defined) => {
273
275
}
274
276
}
275
277
278
+ // always compute whether term has a colon as an optimization for
279
+ // _compactIri
280
+ const colon = term . indexOf ( ':' ) ;
281
+ mapping . _termHasColon = ( colon !== - 1 ) ;
282
+
276
283
if ( '@reverse' in value ) {
277
284
if ( '@id' in value ) {
278
285
throw new JsonLdError (
@@ -318,14 +325,13 @@ api.createTermDefinition = (activeCtx, localCtx, term, defined) => {
318
325
{ code : 'invalid IRI mapping' , context : localCtx } ) ;
319
326
}
320
327
mapping [ '@id' ] = id ;
328
+ // indicate if this term may be used as a compact IRI prefix
329
+ mapping . _prefix = ( ! mapping . _termHasColon &&
330
+ id . match ( / [: \/ \? # \[ \] @ ] $ / ) &&
331
+ ( simpleTerm || ! activeCtx . processingMode || activeCtx . processingMode == 'json-ld-1.0' ) ) ;
321
332
}
322
333
}
323
334
324
- // always compute whether term has a colon as an optimization for
325
- // _compactIri
326
- const colon = term . indexOf ( ':' ) ;
327
- mapping . _termHasColon = ( colon !== - 1 ) ;
328
-
329
335
if ( ! ( '@id' in mapping ) ) {
330
336
// see if the term has a prefix
331
337
if ( mapping . _termHasColon ) {
@@ -478,6 +484,24 @@ api.createTermDefinition = (activeCtx, localCtx, term, defined) => {
478
484
mapping [ '@language' ] = language ;
479
485
}
480
486
487
+ // term may be used as a prefix
488
+ if ( '@prefix' in value ) {
489
+ if ( mapping . _termHasColon ) {
490
+ throw new JsonLdError (
491
+ 'Invalid JSON-LD syntax; @context @prefix used on a compact IRI term' ,
492
+ 'jsonld.SyntaxError' ,
493
+ { code : 'invalid term definition' , context : localCtx } ) ;
494
+ }
495
+ if ( value [ '@prefix' ] === true || value [ '@prefix' ] === false ) {
496
+ mapping . _prefix = value [ '@prefix' ] === true
497
+ } else {
498
+ throw new JsonLdError (
499
+ 'Invalid JSON-LD syntax; @context value for @prefix must be boolean' ,
500
+ 'jsonld.SyntaxError' ,
501
+ { code : 'invalid @prefix value' , context : localCtx } ) ;
502
+ }
503
+ }
504
+
481
505
// disallow aliasing @context and @preserve
482
506
const id = mapping [ '@id' ] ;
483
507
if ( id === '@context' || id === '@preserve' ) {
@@ -861,6 +885,7 @@ api.isKeyword = v => {
861
885
case '@list' :
862
886
case '@none' :
863
887
case '@omitDefault' :
888
+ case '@prefix' :
864
889
case '@preserve' :
865
890
case '@requireAll' :
866
891
case '@reverse' :
0 commit comments