mirror of https://github.com/sveltejs/svelte
parent
f6f8f66c90
commit
40cf29b2b7
@ -0,0 +1,78 @@
|
||||
import { stringify } from '../../../utils/stringify';
|
||||
import getExpressionPrecedence from '../../../utils/getExpressionPrecedence';
|
||||
import Node from './Node';
|
||||
import Attribute from '../Attribute';
|
||||
import Block from '../../dom/Block';
|
||||
|
||||
export default function mungeAttribute(attribute: Node, block: Block): Attribute {
|
||||
if (attribute.value === true) {
|
||||
// attributes without values, e.g. <textarea readonly>
|
||||
return {
|
||||
name: attribute.name,
|
||||
value: true,
|
||||
dynamic: false
|
||||
};
|
||||
}
|
||||
|
||||
if (attribute.value.length === 0) {
|
||||
return {
|
||||
name: attribute.name,
|
||||
value: `''`,
|
||||
dynamic: false
|
||||
};
|
||||
}
|
||||
|
||||
if (attribute.value.length === 1) {
|
||||
const value = attribute.value[0];
|
||||
|
||||
if (value.type === 'Text') {
|
||||
// static attributes
|
||||
return {
|
||||
name: attribute.name,
|
||||
value: isNaN(value.data) ? stringify(value.data) : value.data,
|
||||
dynamic: false
|
||||
};
|
||||
}
|
||||
|
||||
// simple dynamic attributes
|
||||
block.contextualise(value.expression); // TODO remove
|
||||
const { dependencies, snippet } = value.metadata;
|
||||
|
||||
// TODO only update attributes that have changed
|
||||
return {
|
||||
name: attribute.name,
|
||||
value: snippet,
|
||||
dependencies,
|
||||
dynamic: true
|
||||
};
|
||||
}
|
||||
|
||||
// otherwise we're dealing with a complex dynamic attribute
|
||||
const allDependencies = new Set();
|
||||
|
||||
const value =
|
||||
(attribute.value[0].type === 'Text' ? '' : `"" + `) +
|
||||
attribute.value
|
||||
.map((chunk: Node) => {
|
||||
if (chunk.type === 'Text') {
|
||||
return stringify(chunk.data);
|
||||
} else {
|
||||
block.contextualise(chunk.expression); // TODO remove
|
||||
const { dependencies, snippet } = chunk.metadata;
|
||||
|
||||
dependencies.forEach((dependency: string) => {
|
||||
allDependencies.add(dependency);
|
||||
});
|
||||
|
||||
return getExpressionPrecedence(chunk.expression) <= 13 ? `(${snippet})` : snippet;
|
||||
}
|
||||
})
|
||||
.join(' + ');
|
||||
|
||||
return {
|
||||
name: attribute.name,
|
||||
value,
|
||||
dependencies: Array.from(allDependencies),
|
||||
dynamic: true
|
||||
};
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
export function getSpreadUpdate(levels, updates) {
|
||||
var update = {};
|
||||
|
||||
var to_null_out = {};
|
||||
var accounted_for = {};
|
||||
|
||||
var i = levels.length;
|
||||
while (i--) {
|
||||
var o = levels[i];
|
||||
var n = updates[i];
|
||||
|
||||
if (n) {
|
||||
for (var key in o) {
|
||||
if (!(key in n)) to_null_out[key] = 1;
|
||||
}
|
||||
|
||||
for (var key in n) {
|
||||
if (!accounted_for[key]) {
|
||||
update[key] = n[key];
|
||||
accounted_for[key] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
levels[i] = n;
|
||||
} else {
|
||||
for (var key in o) {
|
||||
accounted_for[key] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (var key in to_null_out) {
|
||||
if (!(key in update)) update[key] = undefined;
|
||||
}
|
||||
|
||||
return update;
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
import isValidIdentifier from './isValidIdentifier';
|
||||
import reservedNames from './reservedNames';
|
||||
|
||||
export default function quoteIfNecessary(name, legacy) {
|
||||
if (!isValidIdentifier(name) || (legacy && reservedNames.has(name))) return `"${name}"`;
|
||||
return name;
|
||||
}
|
Loading…
Reference in new issue