@@ -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,15 @@ 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 || activeCtx . previousContext ;
160
168
let keys = Object . keys ( element ) . sort ( ) ;
161
169
let mustRevert = ! insideIndex ;
162
170
if ( mustRevert && typeScopedContext && keys [ 0 ] !== '@context' ) {
@@ -167,6 +175,7 @@ api.expand = ({
167
175
if ( expandedProperty === '@id' ) {
168
176
// subject reference found, use type-scoped context to expand it
169
177
mustRevert = false ;
178
+ activeCtx = typeScopedContext ;
170
179
}
171
180
} else if ( keys . length <= 2 ) {
172
181
// check if element is a value
@@ -176,6 +185,7 @@ api.expand = ({
176
185
if ( expandedProperty === '@value' ) {
177
186
// value found, use type-scoped context 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