mirror of https://github.com/sveltejs/svelte
fix: avoid marking subtree as dynamic for inlined attributes (#14269)
* fix: avoid marking subtree as dynamic for inlined attributes * fix: i'm a silly goose 🪿 * chore: refactor `is_inlinable_expression` to accept the attribute * feat: inline dom expression too * fix: special case literals with `"` in it and fix standalone case * chore: simpler check first Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> * typo * add more stuff to snapshot test * simplify/speedup by doing the work once, during analysis * simplify * simplify - no reason these cases should prevent inlining * return template * name is incorrect * name is incorrect * fix escaping * no longer necessary * remove obsolete description * better concatenation * fix test * do the work at runtime * fix another thing * tidy * tidy up * simplify * simplify * fix * note to self * another * simplify * more accurate name * simplify * simplify * explain what is happening * tidy up * simplify * better inlining * update test * colocate some code * better inlining * use attribute metadata * Update packages/svelte/src/compiler/phases/2-analyze/visitors/Identifier.js Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> * Apply suggestions from code review --------- Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> Co-authored-by: Rich Harris <rich.harris@vercel.com>pull/14302/head
parent
6a7146bee7
commit
8a8e6f70e8
@ -0,0 +1,5 @@
|
||||
---
|
||||
'svelte': minor
|
||||
---
|
||||
|
||||
feat: better inlining of static attributes
|
@ -0,0 +1,28 @@
|
||||
import { escape_html } from '../../escaping.js';
|
||||
|
||||
/**
|
||||
* `<div translate={false}>` should be rendered as `<div translate="no">` and _not_
|
||||
* `<div translate="false">`, which is equivalent to `<div translate="yes">`. There
|
||||
* may be other odd cases that need to be added to this list in future
|
||||
* @type {Record<string, Map<any, string>>}
|
||||
*/
|
||||
const replacements = {
|
||||
translate: new Map([
|
||||
[true, 'yes'],
|
||||
[false, 'no']
|
||||
])
|
||||
};
|
||||
|
||||
/**
|
||||
* @template V
|
||||
* @param {string} name
|
||||
* @param {V} value
|
||||
* @param {boolean} [is_boolean]
|
||||
* @returns {string}
|
||||
*/
|
||||
export function attr(name, value, is_boolean = false) {
|
||||
if (value == null || (!value && is_boolean) || (value === '' && name === 'class')) return '';
|
||||
const normalized = (name in replacements && replacements[name].get(value)) || value;
|
||||
const assignment = is_boolean ? '' : `="${escape_html(normalized, true)}"`;
|
||||
return ` ${name}${assignment}`;
|
||||
}
|
@ -1 +1 @@
|
||||
<input READONLY={true} REQUIRED={false}>
|
||||
<input READONLY={!0} REQUIRED={!1}>
|
||||
|
Loading…
Reference in new issue