make code style more consistent

pull/3330/head
Richard Harris 5 years ago
parent 42717e893b
commit 6d962c5d95

@ -702,16 +702,20 @@ export default class Element extends Node {
return this.name === 'audio' || this.name === 'video'; return this.name === 'audio' || this.name === 'video';
} }
add_css_class(class_name = this.component.stylesheet.id) { add_css_class() {
const { id } = this.component.stylesheet;
const class_attribute = this.attributes.find(a => a.name === 'class'); const class_attribute = this.attributes.find(a => a.name === 'class');
if (class_attribute && !class_attribute.is_true) { if (class_attribute && !class_attribute.is_true) {
if (class_attribute.chunks.length === 1 && class_attribute.chunks[0].type === 'Text') { if (class_attribute.chunks.length === 1 && class_attribute.chunks[0].type === 'Text') {
(class_attribute.chunks[0] as Text).data += ` ${class_name}`; (class_attribute.chunks[0] as Text).data += ` ${id}`;
} else { } else {
(class_attribute.chunks as Node[]).push( (class_attribute.chunks as Node[]).push(
new Text(this.component, this, this.scope, { new Text(this.component, this, this.scope, {
type: 'Text', type: 'Text',
data: ` ${class_name}` data: ` ${id}`,
synthetic: true
}) })
); );
} }
@ -720,7 +724,7 @@ export default class Element extends Node {
new Attribute(this.component, this, this.scope, { new Attribute(this.component, this, this.scope, {
type: 'Attribute', type: 'Attribute',
name: 'class', name: 'class',
value: [{ type: 'Text', data: class_name }] value: [{ type: 'Text', data: id, synthetic: true }]
}) })
); );
} }

@ -6,9 +6,11 @@ import { INode } from './interfaces';
export default class Text extends Node { export default class Text extends Node {
type: 'Text'; type: 'Text';
data: string; data: string;
synthetic: boolean;
constructor(component: Component, parent: INode, scope: TemplateScope, info: any) { constructor(component: Component, parent: INode, scope: TemplateScope, info: any) {
super(component, parent, scope, info); super(component, parent, scope, info);
this.data = info.data; this.data = info.data;
this.synthetic = info.synthetic || false;
} }
} }

@ -85,13 +85,13 @@ export default class AttributeWrapper {
value = (this.node.chunks[0] as Expression).render(block); value = (this.node.chunks[0] as Expression).render(block);
} else { } else {
// '{foo} {bar}' — treat as string concatenation // '{foo} {bar}' — treat as string concatenation
const attrPrefix = this.node.chunks[0].type === 'Text' ? '' : `"" + `; const prefix = this.node.chunks[0].type === 'Text' ? '' : `"" + `;
const attrText = this.node.name === 'class' const text = this.node.name === 'class'
? this.get_class_name_text() ? this.get_class_name_text()
: this.get_attr_text(); : this.render_chunks().join(' + ');
value = `${attrPrefix}${attrText}`; value = `${prefix}${text}`;
} }
const is_select_value_attribute = const is_select_value_attribute =
@ -206,36 +206,27 @@ export default class AttributeWrapper {
} }
get_class_name_text() { get_class_name_text() {
const isStyled = this.node.chunks const scoped_css = this.node.chunks.some((chunk: Text) => chunk.synthetic);
.filter((chunk) => chunk.type === 'Text') const rendered = this.render_chunks();
.some((chunk: Text) => !chunk.start && !chunk.end);
const classNameStringArray = this.render_attr(); if (scoped_css && rendered.length === 2) {
// we have a situation like class={possiblyUndefined}
if (!isStyled || classNameStringArray.length !== 2) { rendered[0] = `@null_to_empty(${rendered[0]})`;
return classNameStringArray.join(' + ');
} }
const targetToken = 0; return rendered.join(' + ');
return classNameStringArray
.map((token, index) => index === targetToken ? `@class_name_resolver(${token})` : token)
.join(' + ');
}
get_attr_text() {
return this.render_attr().join(' + ');
} }
render_attr() { render_chunks() {
return this.node.chunks.map((chunk) => { return this.node.chunks.map((chunk) => {
if (chunk.type === 'Text') { if (chunk.type === 'Text') {
return stringify(chunk.data); return stringify(chunk.data);
} }
const renderedChunk = chunk.render(); const rendered = chunk.render();
return chunk.get_precedence() <= 13 return chunk.get_precedence() <= 13
? `(${renderedChunk})` ? `(${rendered})`
: renderedChunk; : rendered;
}); });
} }

@ -90,6 +90,6 @@ export function once(fn) {
}; };
} }
export function class_name_resolver(nextClassName) { export function null_to_empty(value) {
return nextClassName == undefined ? '' : nextClassName; return value == null ? '' : value;
} }

Loading…
Cancel
Save