Skip to content

Commit

Permalink
fix scoping class and class: with spread attributes (sveltejs#3790)
Browse files Browse the repository at this point in the history
  • Loading branch information
Conduitry committed Oct 25, 2019
1 parent 33c8cd3 commit 73b5bd2
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 20 deletions.
6 changes: 6 additions & 0 deletions src/compiler/compile/nodes/Element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export default class Element extends Node {
animation?: Animation = null;
children: INode[];
namespace: string;
needs_manual_style_scoping: boolean;

constructor(component, parent, scope, info: any) {
super(component, parent, scope, info);
Expand Down Expand Up @@ -712,6 +713,11 @@ export default class Element extends Node {
}

add_css_class() {
if (this.attributes.some(attr => attr.is_spread)) {
this.needs_manual_style_scoping = true;
return;
}

const { id } = this.component.stylesheet;

const class_attribute = this.attributes.find(a => a.name === 'class');
Expand Down
9 changes: 9 additions & 0 deletions src/compiler/compile/render_dom/wrappers/Element/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ export default class ElementWrapper extends Wrapper {
this.add_animation(block);
this.add_actions(block);
this.add_classes(block);
this.add_manual_style_scoping(block);

if (nodes && this.renderer.options.hydratable) {
block.chunks.claim.push(
Expand Down Expand Up @@ -838,6 +839,14 @@ export default class ElementWrapper extends Wrapper {
}
});
}

add_manual_style_scoping(block) {
if (this.node.needs_manual_style_scoping) {
const updater = b`@toggle_class(${this.var}, "${this.node.component.stylesheet.id}", true);`;
block.chunks.hydrate.push(updater);
block.chunks.update.push(updater);
}
}
}

function to_html(wrappers: Array<ElementWrapper | TextWrapper>, block: Block, literal: any, state: any) {
Expand Down
36 changes: 17 additions & 19 deletions src/compiler/compile/render_ssr/handlers/Element.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { is_void } from '../../../utils/names';
import Class from '../../nodes/Class';
import { get_attribute_value, get_class_attribute_value } from './shared/get_attribute_value';
import { get_slot_scope } from './shared/get_slot_scope';
import Renderer, { RenderOptions } from '../Renderer';
Expand Down Expand Up @@ -69,15 +68,17 @@ export default function(node: Element, renderer: Renderer, options: RenderOption

renderer.add_string(`<${node.name}`);

const class_expression = node.classes.length > 0 && node.classes
.map((class_directive: Class) => {
const { expression, name } = class_directive;
const snippet = expression ? expression.node : x`#ctx.${name}`;
return x`${snippet} ? "${name}" : ""`;
})
.reduce((lhs, rhs) => x`${lhs} + ' ' + ${rhs}`);

let add_class_attribute = class_expression ? true : false;
const class_expression_list = node.classes.map(class_directive => {
const { expression, name } = class_directive;
const snippet = expression ? expression.node : x`#ctx.${name}`;
return x`${snippet} ? "${name}" : ""`;
});
if (node.needs_manual_style_scoping) {
class_expression_list.push(x`"${node.component.stylesheet.id}"`);
}
const class_expression =
class_expression_list.length > 0 &&
class_expression_list.reduce((lhs, rhs) => x`${lhs} + ' ' + ${rhs}`);

if (node.attributes.some(attr => attr.is_spread)) {
// TODO dry this out
Expand All @@ -98,17 +99,15 @@ export default function(node: Element, renderer: Renderer, options: RenderOption
) {
// a boolean attribute with one non-Text chunk
args.push(x`{ ${attribute.name}: ${(attribute.chunks[0] as Expression).node} || null }`);
} else if (name === 'class' && class_expression) {
// Add class expression
args.push(x`{ ${attribute.name}: [${get_class_attribute_value(attribute)}, ${class_expression}].join(' ').trim() }`);
} else {
args.push(x`{ ${attribute.name}: ${(name === 'class' ? get_class_attribute_value : get_attribute_value)(attribute)} }`);
args.push(x`{ ${attribute.name}: ${get_attribute_value(attribute)} }`);
}
}
});

renderer.add_expression(x`@spread([${args}])`);
renderer.add_expression(x`@spread([${args}], ${class_expression});`);
} else {
let add_class_attribute = !!class_expression;
node.attributes.forEach(attribute => {
const name = attribute.name.toLowerCase();
if (name === 'value' && node.name.toLowerCase() === 'textarea') {
Expand Down Expand Up @@ -137,6 +136,9 @@ export default function(node: Element, renderer: Renderer, options: RenderOption
renderer.add_string(`"`);
}
});
if (add_class_attribute) {
renderer.add_expression(x`@add_classes([${class_expression}].join(' ').trim())`);
}
}

node.bindings.forEach(binding => {
Expand All @@ -162,10 +164,6 @@ export default function(node: Element, renderer: Renderer, options: RenderOption
}
});

if (add_class_attribute) {
renderer.add_expression(x`@add_classes([${class_expression}].join(' ').trim())`);
}

renderer.add_string('>');

if (node_contents !== undefined) {
Expand Down
9 changes: 8 additions & 1 deletion src/runtime/internal/ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@ export const invalid_attribute_name_character = /[\s'">/=\u{FDD0}-\u{FDEF}\u{FFF
// https://html.spec.whatwg.org/multipage/syntax.html#attributes-2
// https://infra.spec.whatwg.org/#noncharacter

export function spread(args) {
export function spread(args, classes_to_add) {
const attributes = Object.assign({}, ...args);
if (classes_to_add) {
if (attributes.class == null) {
attributes.class = classes_to_add;
} else {
attributes.class += ' ' + classes_to_add;
}
}
let str = '';

Object.keys(attributes).forEach(name => {
Expand Down

0 comments on commit 73b5bd2

Please sign in to comment.