@@ -52,6 +52,9 @@ module.exports = api;
52
52
* @param insideList true if the element is a list, false if not.
53
53
* @param insideIndex true if the element is inside an index container,
54
54
* false if not.
55
+ * @param typeScopedContext an optional type-scoped active context for
56
+ * expanding values of nodes that were expressed according to
57
+ * a type-scoped context.
55
58
* @param expansionMap(info) a function that can be used to custom map
56
59
* unmappable values (or to throw an error when they are detected);
57
60
* if this function returns `undefined` then the default behavior
@@ -66,6 +69,7 @@ api.expand = ({
66
69
options = { } ,
67
70
insideList = false ,
68
71
insideIndex = false ,
72
+ typeScopedContext = null ,
69
73
expansionMap = ( ) => undefined
70
74
} ) => {
71
75
// nothing to expand
@@ -152,11 +156,16 @@ api.expand = ({
152
156
153
157
// recursively expand object:
154
158
155
- // First determine if any type-scoped context should be reverted; it should
156
- // only be reverted when the following are all true:
159
+ // first, expand the active property
160
+ const expandedActiveProperty = _expandIri (
161
+ activeCtx , activeProperty , { vocab : true } , options ) ;
162
+
163
+ // second, determine if any type-scoped context should be reverted; it
164
+ // should only be reverted when the following are all true:
157
165
// 1. `element` is not a value or subject reference
158
166
// 2. `insideIndex` is false
159
- const typeScopedContext = activeCtx . previousContext ? activeCtx : null ;
167
+ typeScopedContext = typeScopedContext ||
168
+ ( activeCtx . previousContext ? activeCtx : null ) ;
160
169
let keys = Object . keys ( element ) . sort ( ) ;
161
170
let mustRevert = ! insideIndex ;
162
171
if ( mustRevert && typeScopedContext && keys [ 0 ] !== '@context' ) {
@@ -165,7 +174,7 @@ api.expand = ({
165
174
const expandedProperty = _expandIri (
166
175
typeScopedContext , keys [ 0 ] , { vocab : true } , options ) ;
167
176
if ( expandedProperty === '@id' ) {
168
- // subject reference found, use type-scoped context to expand it
177
+ // subject reference found, do not revert
169
178
mustRevert = false ;
170
179
}
171
180
} else if ( keys . length <= 2 ) {
@@ -174,8 +183,9 @@ api.expand = ({
174
183
const expandedProperty = _expandIri (
175
184
typeScopedContext , key , { vocab : true } , options ) ;
176
185
if ( expandedProperty === '@value' ) {
177
- // value found, use type-scoped context to expand it
186
+ // value found, ensure type-scoped context is used to expand it
178
187
mustRevert = false ;
188
+ activeCtx = typeScopedContext ;
179
189
break ;
180
190
}
181
191
}
@@ -218,10 +228,6 @@ api.expand = ({
218
228
}
219
229
}
220
230
221
- // expand the active property
222
- const expandedActiveProperty = _expandIri (
223
- activeCtx , activeProperty , { vocab : true } , options ) ;
224
-
225
231
// process each key and value in element, ignoring @nest content
226
232
let rval = { } ;
227
233
_expandObject ( {
@@ -232,6 +238,7 @@ api.expand = ({
232
238
expandedParent : rval ,
233
239
options,
234
240
insideList,
241
+ typeScopedContext,
235
242
expansionMap} ) ;
236
243
237
244
// get property count on expanded output
0 commit comments