@@ -29,7 +29,8 @@ const {
2929} = require ( './context' ) ;
3030
3131const {
32- removeBase : _removeBase
32+ removeBase : _removeBase ,
33+ prependBase : _prependBase
3334} = require ( './url' ) ;
3435
3536const {
@@ -226,7 +227,8 @@ api.compact = async ({
226227 expandedIri => api . compactIri ( {
227228 activeCtx,
228229 iri : expandedIri ,
229- relativeTo : { vocab : false }
230+ relativeTo : { vocab : false } ,
231+ base : options . base
230232 } ) ) ;
231233 if ( compactedValue . length === 1 ) {
232234 compactedValue = compactedValue [ 0 ] ;
@@ -485,7 +487,8 @@ api.compact = async ({
485487 // index on @id or @index or alias of @none
486488 const key = ( container . includes ( '@id' ) ?
487489 expandedItem [ '@id' ] : expandedItem [ '@index' ] ) ||
488- api . compactIri ( { activeCtx, iri : '@none' , vocab : true } ) ;
490+ api . compactIri ( { activeCtx, iri : '@none' ,
491+ relativeTo : { vocab : true } } ) ;
489492 // add compactedItem to map, using value of `@id` or a new blank
490493 // node identifier
491494
@@ -570,7 +573,7 @@ api.compact = async ({
570573 const indexKey = _getContextValue (
571574 activeCtx , itemActiveProperty , '@index' ) || '@index' ;
572575 const containerKey = api . compactIri (
573- { activeCtx, iri : indexKey , vocab : true } ) ;
576+ { activeCtx, iri : indexKey , relativeTo : { vocab : true } } ) ;
574577 if ( indexKey === '@index' ) {
575578 key = expandedItem [ '@index' ] ;
576579 delete compactedItem [ containerKey ] ;
@@ -595,14 +598,15 @@ api.compact = async ({
595598 }
596599 }
597600 } else if ( container . includes ( '@id' ) ) {
598- const idKey = api . compactIri ( { activeCtx, iri : '@id' , vocab : true } ) ;
601+ const idKey = api . compactIri ( { activeCtx, iri : '@id' ,
602+ relativeTo : { vocab : true } } ) ;
599603 key = compactedItem [ idKey ] ;
600604 delete compactedItem [ idKey ] ;
601605 } else if ( container . includes ( '@type' ) ) {
602606 const typeKey = api . compactIri ( {
603607 activeCtx,
604608 iri : '@type' ,
605- vocab : true
609+ relativeTo : { vocab : true }
606610 } ) ;
607611 let types ;
608612 [ key , ...types ] = _asArray ( compactedItem [ typeKey ] || [ ] ) ;
@@ -634,7 +638,8 @@ api.compact = async ({
634638
635639 // if compacting this value which has no key, index on @none
636640 if ( ! key ) {
637- key = api . compactIri ( { activeCtx, iri : '@none' , vocab : true } ) ;
641+ key = api . compactIri ( { activeCtx, iri : '@none' ,
642+ relativeTo : { vocab : true } } ) ;
638643 }
639644 // add compact value to map object using key from expanded value
640645 // based on the container type
@@ -676,6 +681,7 @@ api.compact = async ({
676681 * @param relativeTo options for how to compact IRIs:
677682 * vocab: true to split after @vocab, false not to.
678683 * @param reverse true if a reverse property is being compacted, false if not.
684+ * @param base the absolute URL to use for compacting document-relative IRIs.
679685 *
680686 * @return the compacted term, prefix, keyword alias, or the original IRI.
681687 */
@@ -684,7 +690,8 @@ api.compactIri = ({
684690 iri,
685691 value = null ,
686692 relativeTo = { vocab : false } ,
687- reverse = false
693+ reverse = false ,
694+ base = null
688695} ) => {
689696 // can't compact null
690697 if ( iri === null ) {
@@ -933,7 +940,16 @@ api.compactIri = ({
933940
934941 // compact IRI relative to base
935942 if ( ! relativeTo . vocab ) {
936- return _removeBase ( activeCtx [ '@base' ] , iri ) ;
943+ if ( '@base' in activeCtx ) {
944+ if ( ! activeCtx [ '@base' ] ) {
945+ // The None case preserves rval as potentially relative
946+ return iri ;
947+ } else {
948+ return _removeBase ( _prependBase ( base , activeCtx [ '@base' ] ) , iri ) ;
949+ }
950+ } else {
951+ return _removeBase ( base , iri ) ;
952+ }
937953 }
938954
939955 // return IRI as is
@@ -1050,8 +1066,11 @@ api.compactValue = ({activeCtx, activeProperty, value, options}) => {
10501066 const expandedProperty = _expandIri ( activeCtx , activeProperty , { vocab : true } ,
10511067 options ) ;
10521068 const type = _getContextValue ( activeCtx , activeProperty , '@type' ) ;
1053- const compacted = api . compactIri (
1054- { activeCtx, iri : value [ '@id' ] , relativeTo : { vocab : type === '@vocab' } } ) ;
1069+ const compacted = api . compactIri ( {
1070+ activeCtx,
1071+ iri : value [ '@id' ] ,
1072+ relativeTo : { vocab : type === '@vocab' } ,
1073+ base : options . base } ) ;
10551074
10561075 // compact to scalar
10571076 if ( type === '@id' || type === '@vocab' || expandedProperty === '@graph' ) {
0 commit comments