diff options
Diffstat (limited to 'packages/astro/src')
-rw-r--r-- | packages/astro/src/ast.ts | 1 | ||||
-rw-r--r-- | packages/astro/src/compiler/codegen/index.ts | 16 | ||||
-rw-r--r-- | packages/astro/src/frontend/h.ts | 6 |
3 files changed, 17 insertions, 6 deletions
diff --git a/packages/astro/src/ast.ts b/packages/astro/src/ast.ts index f80084cb2..bee1d9b6f 100644 --- a/packages/astro/src/ast.ts +++ b/packages/astro/src/ast.ts @@ -11,6 +11,7 @@ export function getAttr(attributes: Attribute[], name: string): Attribute | unde /** Get TemplateNode attribute by value */ export function getAttrValue(attributes: Attribute[], name: string): string | undefined { + if (attributes.length === 0) return ''; const attr = getAttr(attributes, name); if (attr) { return attr.value[0]?.data; diff --git a/packages/astro/src/compiler/codegen/index.ts b/packages/astro/src/compiler/codegen/index.ts index 97687b4a9..9271ab50a 100644 --- a/packages/astro/src/compiler/codegen/index.ts +++ b/packages/astro/src/compiler/codegen/index.ts @@ -58,6 +58,10 @@ function getAttributes(attrs: Attribute[]): Record<string, string> { // note: attr.value shouldn’t be `undefined`, but a bad transform would cause a compile error here, so prevent that continue; } + if (attr.value.length === 0) { + result[attr.name] = '""'; + continue; + } if (attr.value.length > 1) { result[attr.name] = '(' + @@ -418,6 +422,8 @@ function dedent(str: string) { return !arr || !first ? str : str.replace(new RegExp(`^[ \\t]{0,${first}}`, 'gm'), ''); } +const FALSY_EXPRESSIONS = new Set(['false','null','undefined','void 0']); + /** Compile page markup */ async function compileHtml(enterNode: TemplateNode, state: CodegenState, compileOptions: CompileOptions): Promise<string> { return new Promise((resolve) => { @@ -475,10 +481,12 @@ async function compileHtml(enterNode: TemplateNode, state: CodegenState, compile } // TODO Do we need to compile this now, or should we compile the entire module at the end? let code = compileExpressionSafe(raw).trim().replace(/\;$/, ''); - if (state.markers.insideMarkdown) { - buffers[curr] += `{${code}}`; - } else { - buffers[curr] += `,(${code})`; + if (!FALSY_EXPRESSIONS.has(code)) { + if (state.markers.insideMarkdown) { + buffers[curr] += `{${code}}`; + } else { + buffers[curr] += `,(${code})`; + } } this.skip(); break; diff --git a/packages/astro/src/frontend/h.ts b/packages/astro/src/frontend/h.ts index ae54072a8..bcfb4833f 100644 --- a/packages/astro/src/frontend/h.ts +++ b/packages/astro/src/frontend/h.ts @@ -19,7 +19,9 @@ function* _h(tag: string, attrs: HProps, children: Array<HChild>) { yield `<${tag}`; if (attrs) { for (let [key, value] of Object.entries(attrs)) { - yield ` ${key}="${value}"`; + if (value === '') yield ` ${key}=""`; + else if (value == null) yield ''; + else yield ` ${key}="${value}"`; } } yield '>'; @@ -37,7 +39,7 @@ function* _h(tag: string, attrs: HProps, children: Array<HChild>) { yield child(); } else if (typeof child === 'string') { yield child; - } else if (!child) { + } else if (!child && child !== 0) { // do nothing, safe to ignore falsey values. } else { yield child; |