@@ -405,6 +405,20 @@ class AstSerializer {
405
405
return str ;
406
406
}
407
407
408
+ // This will be the parent component in component definition files, and the host component in slotted content
409
+ getAncestorComponentForData ( options ) {
410
+ // slot content is content in the host component, not in the component definition.
411
+ // https://github.com/11ty/webc/issues/152
412
+ if ( options . isSlottedContent ) {
413
+ return this . componentManager . get ( options . hostComponentContextFilePath ) ;
414
+ }
415
+ return this . componentManager . get ( options . closestParentComponent ) ;
416
+ }
417
+
418
+ useGlobalDataAtTopLevel ( ancestorComponent ) {
419
+ return ancestorComponent ?. isTopLevelComponent ?? true ;
420
+ }
421
+
408
422
async renderStartTag ( node , tagName , component , renderingMode , options ) {
409
423
let content = "" ;
410
424
@@ -430,7 +444,9 @@ class AstSerializer {
430
444
}
431
445
}
432
446
433
- let nodeData = this . dataCascade . getData ( parentComponent . isTopLevelComponent , options . componentProps , options . hostComponentData , parentComponent ?. setupScript , options . injectedData ) ;
447
+ let ancestorComponent = this . getAncestorComponentForData ( options ) ;
448
+ let useGlobalData = this . useGlobalDataAtTopLevel ( ancestorComponent ) ;
449
+ let nodeData = this . dataCascade . getData ( useGlobalData , options . componentProps , options . hostComponentData , ancestorComponent ?. setupScript , options . injectedData ) ;
434
450
let evaluatedAttributes = await AttributeSerializer . evaluateAttributesArray ( attrs , nodeData , options . closestParentComponent ) ;
435
451
let finalAttributesObject = AttributeSerializer . mergeAttributes ( evaluatedAttributes ) ;
436
452
@@ -473,7 +489,7 @@ class AstSerializer {
473
489
return content ;
474
490
}
475
491
476
- async transformContent ( content , transformTypes , node , parentComponent , slots , options ) {
492
+ async transformContent ( content , transformTypes , node , slots , options ) {
477
493
if ( ! transformTypes ) {
478
494
transformTypes = [ ] ;
479
495
}
@@ -487,7 +503,9 @@ class AstSerializer {
487
503
slotsText . default = this . getPreparsedRawTextContent ( o . hostComponentNode , o ) ;
488
504
}
489
505
490
- let context = this . dataCascade . getData ( parentComponent . isTopLevelComponent , options . componentProps , options . currentTagAttributes , parentComponent ?. setupScript , options . injectedData , {
506
+ let ancestorComponent = this . getAncestorComponentForData ( options ) ;
507
+ let useGlobalData = this . useGlobalDataAtTopLevel ( ancestorComponent ) ;
508
+ let context = this . dataCascade . getData ( useGlobalData , options . componentProps , options . currentTagAttributes , ancestorComponent ?. setupScript , options . injectedData , {
491
509
// Ideally these would be under `webc.*`
492
510
filePath : this . filePath ,
493
511
slots : {
@@ -500,7 +518,7 @@ class AstSerializer {
500
518
content = await this . transforms [ type ] . call ( {
501
519
type,
502
520
...context
503
- } , content , parentComponent ) ;
521
+ } , content , ancestorComponent ) ;
504
522
}
505
523
506
524
return content ;
@@ -615,7 +633,7 @@ class AstSerializer {
615
633
return rawContent ;
616
634
}
617
635
618
- return this . transformContent ( rawContent , options . currentTransformTypes , node , this . componentManager . get ( options . closestParentComponent ) , slots , options ) ;
636
+ return this . transformContent ( rawContent , options . currentTransformTypes , node , slots , options ) ;
619
637
}
620
638
621
639
/**
@@ -754,8 +772,9 @@ class AstSerializer {
754
772
755
773
// Used for @html , @raw, @text, @attributes, webc:if, webc:elseif, webc:for
756
774
async evaluateAttribute ( name , attrContent , options ) {
757
- let parentComponent = this . componentManager . get ( options . closestParentComponent ) ;
758
- let data = this . dataCascade . getData ( parentComponent . isTopLevelComponent , options . componentProps , parentComponent ?. setupScript , options . injectedData ) ;
775
+ let ancestorComponent = this . getAncestorComponentForData ( options ) ;
776
+ let useGlobalData = this . useGlobalDataAtTopLevel ( ancestorComponent ) ;
777
+ let data = this . dataCascade . getData ( useGlobalData , options . componentProps , ancestorComponent ?. setupScript , options . injectedData ) ;
759
778
760
779
let { returns } = await ModuleScript . evaluateScriptInline ( attrContent , data , `Check the dynamic attribute: \`${ name } ="${ attrContent } "\`.` , options . closestParentComponent ) ;
761
780
return returns ;
@@ -1008,7 +1027,7 @@ class AstSerializer {
1008
1027
1009
1028
// Run transforms
1010
1029
if ( options . currentTransformTypes && options . currentTransformTypes . length > 0 ) {
1011
- c = await this . transformContent ( node . value , options . currentTransformTypes , node , this . componentManager . get ( options . closestParentComponent ) , slots , options ) ;
1030
+ c = await this . transformContent ( node . value , options . currentTransformTypes , node , slots , options ) ;
1012
1031
1013
1032
// only reprocess text nodes in a <* webc:is="template" webc:type>
1014
1033
if ( ! node . _webCProcessed && node . parentNode && AstQuery . getTagName ( node . parentNode ) === "template" ) {
@@ -1096,7 +1115,7 @@ class AstSerializer {
1096
1115
// TODO warning if top level page component using a style hash but has no root element (text only?)
1097
1116
1098
1117
// Start tag
1099
- let { content : startTagContent , attrs, evaluatedAttributes , nodeData } = await this . renderStartTag ( node , tagName , component , renderingMode , options ) ;
1118
+ let { content : startTagContent , attrs, nodeData } = await this . renderStartTag ( node , tagName , component , renderingMode , options ) ;
1100
1119
content += this . outputHtml ( startTagContent , streamEnabled ) ;
1101
1120
1102
1121
if ( component ) {
@@ -1192,7 +1211,7 @@ class AstSerializer {
1192
1211
if ( externalSource ) { // fetch file contents, note that child content of the node is ignored here
1193
1212
// We could check to make sure this isn’t already in the asset aggregation bucket *before* we read but that could result in out-of-date content
1194
1213
let fileContent = this . fileCache . read ( externalSource , options . closestParentComponent || this . filePath ) ;
1195
- childContent = await this . transformContent ( fileContent , options . currentTransformTypes , node , this . componentManager . get ( options . closestParentComponent ) , slots , options ) ;
1214
+ childContent = await this . transformContent ( fileContent , options . currentTransformTypes , node , slots , options ) ;
1196
1215
} else {
1197
1216
let { html } = await this . getChildContent ( node , slots , options , false ) ;
1198
1217
childContent = html ;
0 commit comments