@@ -372,48 +372,64 @@ api.expand = async ({
372372 // drop certain top-level objects that do not occur in lists
373373 if ( _isObject ( rval ) &&
374374 ! options . keepFreeFloatingNodes && ! insideList &&
375- ( activeProperty === null || expandedActiveProperty === '@graph' ) ) {
375+ ( activeProperty === null ||
376+ expandedActiveProperty === '@graph' ||
377+ ( _getContextValue ( activeCtx , activeProperty , '@container' ) || [ ] )
378+ . includes ( '@graph' )
379+ ) ) {
376380 // drop empty object, top-level @value/@list, or object with only @id
377- if ( count === 0 || '@value' in rval || '@list' in rval ||
378- ( count === 1 && '@id' in rval ) ) {
379- // FIXME
380- if ( options . eventHandler ) {
381- // FIXME: one event or diff event for empty, @v/@l, {@id }?
382- let code ;
383- let message ;
384- if ( count === 0 ) {
385- code = 'empty object' ;
386- message = 'Dropping empty object.' ;
387- } else if ( '@value' in rval ) {
388- code = 'object with only @value' ;
389- message = 'Dropping object with only @value.' ;
390- } else if ( '@list' in rval ) {
391- code = 'object with only @list' ;
392- message = 'Dropping object with only @list.' ;
393- } else if ( count === 1 && '@id' in rval ) {
394- code = 'object with only @id' ;
395- message = 'Dropping object with only @id.' ;
396- }
397- _handleEvent ( {
398- event : {
399- type : [ 'JsonLdEvent' ] ,
400- code,
401- level : 'warning' ,
402- message,
403- details : {
404- value : rval
405- }
406- } ,
407- options
408- } ) ;
409- }
410- rval = null ;
411- }
381+ rval = _dropUnsafeObject ( { value : rval , count, options} ) ;
412382 }
413383
414384 return rval ;
415385} ;
416386
387+ /**
388+ * Drop empty object, top-level @value/@list, or object with only @id
389+ */
390+ function _dropUnsafeObject ( {
391+ value,
392+ count,
393+ options
394+ } ) {
395+ if ( count === 0 || '@value' in value || '@list' in value ||
396+ ( count === 1 && '@id' in value ) ) {
397+ // FIXME
398+ if ( options . eventHandler ) {
399+ // FIXME: one event or diff event for empty, @v/@l, {@id }?
400+ let code ;
401+ let message ;
402+ if ( count === 0 ) {
403+ code = 'empty object' ;
404+ message = 'Dropping empty object.' ;
405+ } else if ( '@value' in value ) {
406+ code = 'object with only @value' ;
407+ message = 'Dropping object with only @value.' ;
408+ } else if ( '@list' in value ) {
409+ code = 'object with only @list' ;
410+ message = 'Dropping object with only @list.' ;
411+ } else if ( count === 1 && '@id' in value ) {
412+ code = 'object with only @id' ;
413+ message = 'Dropping object with only @id.' ;
414+ }
415+ _handleEvent ( {
416+ event : {
417+ type : [ 'JsonLdEvent' ] ,
418+ code,
419+ level : 'warning' ,
420+ message,
421+ details : {
422+ value
423+ }
424+ } ,
425+ options
426+ } ) ;
427+ }
428+ return null ;
429+ }
430+ return value ;
431+ }
432+
417433/**
418434 * Expand each key and value of element adding to result
419435 *
@@ -933,8 +949,18 @@ async function _expandObject({
933949 if ( container . includes ( '@graph' ) &&
934950 ! container . some ( key => key === '@id' || key === '@index' ) ) {
935951 // ensure expanded values are arrays
936- expandedValue = _asArray ( expandedValue )
937- . map ( v => ( { '@graph' : _asArray ( v ) } ) ) ;
952+ // ensure an array
953+ expandedValue = _asArray ( expandedValue ) ;
954+ // check if needs to be dropped
955+ const count = Object . keys ( expandedValue [ 0 ] ) . length ;
956+ if ( ! options . isFrame && _dropUnsafeObject ( {
957+ value : expandedValue [ 0 ] , count, options
958+ } ) === null ) {
959+ // skip adding and continue
960+ continue ;
961+ }
962+ // convert to graph
963+ expandedValue = expandedValue . map ( v => ( { '@graph' : _asArray ( v ) } ) ) ;
938964 }
939965
940966 // FIXME: can this be merged with code above to simplify?
0 commit comments