Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
GianlucaGuarini committed Mar 29, 2024
1 parent 45b44ab commit 84a03c5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
11 changes: 10 additions & 1 deletion src/generators/template/checks.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,23 @@ export function isTextNode(node) {
}

/**
* True if the node parsed is the root one
* True if the node parsed any of the root nodes (each, tag bindings create root nodes as well...)
* @param {RiotParser.Node} node - riot parser node
* @returns {boolean} true only for the root nodes
*/
export function isRootNode(node) {
return node.isRoot
}

/**
* True if the node parsed is the absolute root node (nested root nodes are not considered)
* @param {RiotParser.Node} node - riot parser node
* @returns {boolean} true only for the root nodes
*/
export function isAbsoluteRootNode(node) {
return node.isRoot && !node.isNestedRoot
}

/**
* True if the attribute parsed is of type spread one
* @param {RiotParser.Node} node - riot parser node
Expand Down
5 changes: 2 additions & 3 deletions src/generators/template/expressions/attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
simplePropertyNode,
} from '../../../utils/custom-ast-nodes.js'
import { builders } from '../../../utils/build-types.js'
import { isAbsoluteRootNode, isSpreadAttribute } from '../checks.js'
import { createAttributeEvaluationFunction } from '../utils.js'
/**
* Create a simple attribute expression
Expand Down Expand Up @@ -47,7 +48,7 @@ export default function createAttributeExpression(
// Custom nodes can't handle boolean attrs
// Riot.js will handle the bool attrs logic only on native html tags
(!parentNode[IS_CUSTOM_NODE] &&
!isRootNode(parentNode) &&
!isAbsoluteRootNode(parentNode) &&
!isSpread &&
!!sourceNode[IS_BOOLEAN_ATTRIBUTE]),
),
Expand All @@ -62,5 +63,3 @@ export default function createAttributeExpression(
),
])
}

import { isRootNode, isSpreadAttribute } from '../checks.js'
1 change: 1 addition & 0 deletions src/generators/template/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ export function createRootNode(node) {
export function createNestedRootNode(node) {
return {
...rootNodeFactory(node),
isNestedRoot: true,
attributes: cleanAttributes(node),
}
}
Expand Down
19 changes: 19 additions & 0 deletions test/generators/template.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,25 @@ describe('Generators - Template', () => {
expect(expression[BINDING_IS_BOOLEAN_ATTRIBUTE]).to.be.equal(true)
})

it('Known boolean attribute on a select node https://github.com/riot/riot/issues/3000', () => {
const source = `<my-tag>
<select name="customer">
<option each={ customer in state.customers } value="{ customer.id }" selected="{ state.current.id === customer.id }">
{ customer.name }
</option>
</select></my-tag>`
const { template } = parse(source)
const [, bindings] = builder(
createRootNode(template),
FAKE_SRC_FILE,
source,
)
const output = evaluateOutput(bindings[0])
const expression = output.template.bindingsData[0].expressions[2]

expect(expression[BINDING_IS_BOOLEAN_ATTRIBUTE]).to.be.equal(true)
})

it('Spread attribute expression', () => {
const source = '<li {...foo}></li>'
const { template } = parse(source)
Expand Down

0 comments on commit 84a03c5

Please sign in to comment.