Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/riot 3014 #176

Merged
merged 2 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/generators/template/bindings/slot.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import {
import {
createBindingAttributes,
createSelectorProperties,
createTemplateProperty,
getName,
getNodeAttributes,
} from '../utils.js'
import { builders } from '../../../utils/build-types.js'
import { findAttribute } from '../find.js'
import { simplePropertyNode } from '../../../utils/custom-ast-nodes.js'
import { createNestedBindings } from '../builder.js'

/**
* Transform a RiotParser.Node.Tag of type slot into a slot binding
Expand Down Expand Up @@ -61,6 +63,14 @@ export default function createSlotBinding(
),
),
simplePropertyNode(BINDING_NAME_KEY, builders.literal(slotName)),
createTemplateProperty(
createNestedBindings(
sourceNode,
sourceFile,
sourceCode,
selectorAttribute,
),
),
...createSelectorProperties(selectorAttribute),
])
}
5 changes: 4 additions & 1 deletion src/generators/template/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ function createTagWithBindings(sourceNode, sourceFile, sourceCode) {
]
case isSlotNode(cloneNode):
// slot tag
return [tagOpeningHTML, [slotBinding(cloneNode, bindingsSelector)]]
return [
tagOpeningHTML,
[slotBinding(cloneNode, bindingsSelector, sourceFile, sourceCode)],
]
default:
// this node has expressions bound to it
return [
Expand Down
8 changes: 4 additions & 4 deletions src/generators/template/checks.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,13 @@ export function hasExpressions(node) {
}

/**
* True if the node is a directive having its own template
* True if the node is a directive having its own template or it's a slot node
* @param {RiotParser.Node} node - riot parser node
* @returns {boolean} true only for the IF EACH and TAG bindings
* @returns {boolean} true only for the IF EACH and TAG bindings or it's a slot node
*/
export function hasItsOwnTemplate(node) {
return [findEachAttribute, findIfAttribute, isCustomNode].some((test) =>
test(node),
return [findEachAttribute, findIfAttribute, isCustomNode, isSlotNode].some(
(test) => test(node),
)
}

Expand Down
18 changes: 18 additions & 0 deletions test/generators/template.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import simpleBinding from '../../src/generators/template/bindings/simple.js'
import slotBinding from '../../src/generators/template/bindings/slot.js'
import tagBinding from '../../src/generators/template/bindings/tag.js'
import build from '../../src/generators/template/builder.js'

Check failure on line 37 in test/generators/template.spec.js

View workflow job for this annotation

GitHub Actions / build (18.x)

'build' is defined but never used

Check failure on line 37 in test/generators/template.spec.js

View workflow job for this annotation

GitHub Actions / build (20.x)

'build' is defined but never used

const FAKE_SRC_FILE = 'fake-file.js'
const renderExpr = compose(renderExpression, toScopedFunction, (expr) => ({
Expand Down Expand Up @@ -1292,6 +1293,23 @@
expect(output[BINDING_TYPE_KEY]).to.be.equal(bindingTypes.SLOT)
expect(output[NAME_ATTRIBUTE]).to.be.equal('default')
})

it('Slot fallback html', () => {
const source =
'<div><slot><ul><li each={item in items}>{item}</li></ul></slot></div>'
const { template } = parse(source)
const [, bindings] = builder(
createRootNode(template),
FAKE_SRC_FILE,
source,
)
const output = evaluateOutput(bindings[0])

// the fallback template is defined
expect(output[BINDING_TEMPLATE_KEY]).to.be.ok
// the each binding should be part of the fallback template
expect(bindings).to.have.length(1)
})
})

describe('Template builder', () => {
Expand Down
Loading