From 84a03c50ff6e22d50681a5f1bc920b4f7b166379 Mon Sep 17 00:00:00 2001 From: gianlucaguarini Date: Fri, 29 Mar 2024 22:15:19 +0100 Subject: [PATCH] fixed: https://github.com/riot/riot/issues/3000 --- src/generators/template/checks.js | 11 ++++++++++- .../template/expressions/attribute.js | 5 ++--- src/generators/template/utils.js | 1 + test/generators/template.spec.js | 19 +++++++++++++++++++ 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/generators/template/checks.js b/src/generators/template/checks.js index 1bf3e5a..8978c5e 100644 --- a/src/generators/template/checks.js +++ b/src/generators/template/checks.js @@ -147,7 +147,7 @@ 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 */ @@ -155,6 +155,15 @@ 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 diff --git a/src/generators/template/expressions/attribute.js b/src/generators/template/expressions/attribute.js index d568bbb..412f7ea 100644 --- a/src/generators/template/expressions/attribute.js +++ b/src/generators/template/expressions/attribute.js @@ -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 @@ -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]), ), @@ -62,5 +63,3 @@ export default function createAttributeExpression( ), ]) } - -import { isRootNode, isSpreadAttribute } from '../checks.js' diff --git a/src/generators/template/utils.js b/src/generators/template/utils.js index bf81508..2f7f0fc 100644 --- a/src/generators/template/utils.js +++ b/src/generators/template/utils.js @@ -434,6 +434,7 @@ export function createRootNode(node) { export function createNestedRootNode(node) { return { ...rootNodeFactory(node), + isNestedRoot: true, attributes: cleanAttributes(node), } } diff --git a/test/generators/template.spec.js b/test/generators/template.spec.js index c4970d7..b0c74b0 100644 --- a/test/generators/template.spec.js +++ b/test/generators/template.spec.js @@ -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 = ` +` + 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 = '
  • ' const { template } = parse(source)