@@ -152,7 +152,37 @@ api.expand = ({
152
152
153
153
// recursively expand object:
154
154
155
- if ( ! insideIndex ) {
155
+ // First determine if any type-scoped context should be reverted; it should
156
+ // only be reverted when the following are all true:
157
+ // 1. `element` is not a value or subject reference
158
+ // 2. `insideIndex` is false
159
+ const typeScopedContext = activeCtx . previousContext ? activeCtx : null ;
160
+ let keys = Object . keys ( element ) . sort ( ) ;
161
+ let mustRevert = ! insideIndex ;
162
+ if ( mustRevert && typeScopedContext && keys [ 0 ] !== '@context' ) {
163
+ // check if element is a subject reference
164
+ if ( keys . length === 1 ) {
165
+ const expandedProperty = _expandIri (
166
+ typeScopedContext , keys [ 0 ] , { vocab : true } , options ) ;
167
+ if ( expandedProperty === '@id' ) {
168
+ // subject reference found, use type-scoped context to expand it
169
+ mustRevert = false ;
170
+ }
171
+ } else if ( keys . length <= 2 ) {
172
+ // check if element is a value
173
+ for ( const key of keys ) {
174
+ const expandedProperty = _expandIri (
175
+ typeScopedContext , key , { vocab : true } , options ) ;
176
+ if ( expandedProperty === '@value' ) {
177
+ // value found, use type-scoped context to expand it
178
+ mustRevert = false ;
179
+ break ;
180
+ }
181
+ }
182
+ }
183
+ }
184
+
185
+ if ( mustRevert ) {
156
186
// revert type scoped context
157
187
activeCtx = activeCtx . revertTypeScopedContext ( ) ;
158
188
}
@@ -163,8 +193,7 @@ api.expand = ({
163
193
{ activeCtx, localCtx : element [ '@context' ] , options} ) ;
164
194
}
165
195
166
- // look for scoped context on @type
167
- let keys = Object . keys ( element ) . sort ( ) ;
196
+ // look for scoped contexts on `@type`
168
197
for ( const key of keys ) {
169
198
const expandedProperty = _expandIri ( activeCtx , key , { vocab : true } , options ) ;
170
199
if ( expandedProperty === '@type' ) {
0 commit comments