|
|
|
@ -1,36 +1,38 @@
|
|
|
|
import { b, x } from 'code-red';
|
|
|
|
import { b, x } from 'code-red';
|
|
|
|
import Wrapper from './shared/Wrapper';
|
|
|
|
import Wrapper from './shared/Wrapper.js';
|
|
|
|
import Renderer from '../Renderer';
|
|
|
|
import { string_literal } from '../../utils/stringify.js';
|
|
|
|
import Block from '../Block';
|
|
|
|
import add_to_set from '../../utils/add_to_set.js';
|
|
|
|
import Title from '../../nodes/Title';
|
|
|
|
|
|
|
|
import { string_literal } from '../../utils/stringify';
|
|
|
|
|
|
|
|
import add_to_set from '../../utils/add_to_set';
|
|
|
|
|
|
|
|
import Text from '../../nodes/Text';
|
|
|
|
|
|
|
|
import { Identifier } from 'estree';
|
|
|
|
|
|
|
|
import MustacheTag from '../../nodes/MustacheTag';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @extends Wrapper */
|
|
|
|
export default class TitleWrapper extends Wrapper {
|
|
|
|
export default class TitleWrapper extends Wrapper {
|
|
|
|
node: Title;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
/** @type {import('../../nodes/Title.js').default} */
|
|
|
|
renderer: Renderer,
|
|
|
|
node;
|
|
|
|
block: Block,
|
|
|
|
|
|
|
|
parent: Wrapper,
|
|
|
|
/**
|
|
|
|
node: Title,
|
|
|
|
* @param {import('../Renderer.js').default} renderer
|
|
|
|
_strip_whitespace: boolean,
|
|
|
|
* @param {import('../Block.js').default} block
|
|
|
|
_next_sibling: Wrapper
|
|
|
|
* @param {import('./shared/Wrapper.js').default} parent
|
|
|
|
) {
|
|
|
|
* @param {import('../../nodes/Title.js').default} node
|
|
|
|
|
|
|
|
* @param {boolean} _strip_whitespace
|
|
|
|
|
|
|
|
* @param {import('./shared/Wrapper.js').default} _next_sibling
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
constructor(renderer, block, parent, node, _strip_whitespace, _next_sibling) {
|
|
|
|
super(renderer, block, parent, node);
|
|
|
|
super(renderer, block, parent, node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
render(block: Block, _parent_node: Identifier, _parent_nodes: Identifier) {
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @param {import('../Block.js').default} block
|
|
|
|
|
|
|
|
* @param {import('estree').Identifier} _parent_node
|
|
|
|
|
|
|
|
* @param {import('estree').Identifier} _parent_nodes
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
render(block, _parent_node, _parent_nodes) {
|
|
|
|
const is_dynamic = !!this.node.children.find((node) => node.type !== 'Text');
|
|
|
|
const is_dynamic = !!this.node.children.find((node) => node.type !== 'Text');
|
|
|
|
|
|
|
|
|
|
|
|
if (is_dynamic) {
|
|
|
|
if (is_dynamic) {
|
|
|
|
let value;
|
|
|
|
let value;
|
|
|
|
|
|
|
|
|
|
|
|
const all_dependencies: Set<string> = new Set();
|
|
|
|
/** @type {Set<string>} */
|
|
|
|
|
|
|
|
const all_dependencies = new Set();
|
|
|
|
// TODO some of this code is repeated in Tag.ts — would be good to
|
|
|
|
// TODO some of this code is repeated in Tag.ts — would be good to
|
|
|
|
// DRY it out if that's possible without introducing crazy indirection
|
|
|
|
// DRY it out if that's possible without introducing crazy indirection
|
|
|
|
if (this.node.children.length === 1) {
|
|
|
|
if (this.node.children.length === 1) {
|
|
|
|
@ -39,60 +41,53 @@ export default class TitleWrapper extends Wrapper {
|
|
|
|
const { expression } = this.node.children[0];
|
|
|
|
const { expression } = this.node.children[0];
|
|
|
|
value = expression.manipulate(block);
|
|
|
|
value = expression.manipulate(block);
|
|
|
|
add_to_set(all_dependencies, expression.dependencies);
|
|
|
|
add_to_set(all_dependencies, expression.dependencies);
|
|
|
|
} else {
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
// '{foo} {bar}' — treat as string concatenation
|
|
|
|
// '{foo} {bar}' — treat as string concatenation
|
|
|
|
value = this.node.children
|
|
|
|
value = this.node.children
|
|
|
|
.map((chunk) => {
|
|
|
|
.map((chunk) => {
|
|
|
|
if (chunk.type === 'Text') return string_literal(chunk.data);
|
|
|
|
if (chunk.type === 'Text')
|
|
|
|
|
|
|
|
return string_literal(chunk.data);
|
|
|
|
(chunk as MustacheTag).expression.dependencies.forEach((d) => {
|
|
|
|
( /** @type {import('../../nodes/MustacheTag.js').default} */(chunk)).expression.dependencies.forEach((d) => {
|
|
|
|
all_dependencies.add(d);
|
|
|
|
all_dependencies.add(d);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return ( /** @type {import('../../nodes/MustacheTag.js').default} */(chunk)).expression.manipulate(block);
|
|
|
|
return (chunk as MustacheTag).expression.manipulate(block);
|
|
|
|
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.reduce((lhs, rhs) => x`${lhs} + ${rhs}`);
|
|
|
|
.reduce((lhs, rhs) => x `${lhs} + ${rhs}`);
|
|
|
|
|
|
|
|
|
|
|
|
if (this.node.children[0].type !== 'Text') {
|
|
|
|
if (this.node.children[0].type !== 'Text') {
|
|
|
|
value = x`"" + ${value}`;
|
|
|
|
value = x `"" + ${value}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const last = this.node.should_cache && block.get_unique_name('title_value');
|
|
|
|
const last = this.node.should_cache && block.get_unique_name('title_value');
|
|
|
|
|
|
|
|
if (this.node.should_cache)
|
|
|
|
if (this.node.should_cache) block.add_variable(last);
|
|
|
|
block.add_variable(last);
|
|
|
|
|
|
|
|
const init = this.node.should_cache ? x `${last} = ${value}` : value;
|
|
|
|
const init = this.node.should_cache ? x`${last} = ${value}` : value;
|
|
|
|
block.chunks.init.push(b `@_document.title = ${init};`);
|
|
|
|
|
|
|
|
const updater = b `@_document.title = ${this.node.should_cache ? last : value};`;
|
|
|
|
block.chunks.init.push(b`@_document.title = ${init};`);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const updater = b`@_document.title = ${this.node.should_cache ? last : value};`;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (all_dependencies.size) {
|
|
|
|
if (all_dependencies.size) {
|
|
|
|
const dependencies = Array.from(all_dependencies);
|
|
|
|
const dependencies = Array.from(all_dependencies);
|
|
|
|
|
|
|
|
|
|
|
|
let condition = block.renderer.dirty(dependencies);
|
|
|
|
let condition = block.renderer.dirty(dependencies);
|
|
|
|
|
|
|
|
|
|
|
|
if (block.has_outros) {
|
|
|
|
if (block.has_outros) {
|
|
|
|
condition = x`!#current || ${condition}`;
|
|
|
|
condition = x `!#current || ${condition}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (this.node.should_cache) {
|
|
|
|
if (this.node.should_cache) {
|
|
|
|
condition = x`${condition} && (${last} !== (${last} = ${value}))`;
|
|
|
|
condition = x `${condition} && (${last} !== (${last} = ${value}))`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
block.chunks.update.push(b `
|
|
|
|
block.chunks.update.push(b`
|
|
|
|
|
|
|
|
if (${condition}) {
|
|
|
|
if (${condition}) {
|
|
|
|
${updater}
|
|
|
|
${updater}
|
|
|
|
}`);
|
|
|
|
}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
}
|
|
|
|
const value =
|
|
|
|
else {
|
|
|
|
this.node.children.length > 0
|
|
|
|
const value = this.node.children.length > 0
|
|
|
|
? string_literal((this.node.children[0] as Text).data)
|
|
|
|
? string_literal(( /** @type {import('../../nodes/Text.js').default} */(this.node.children[0])).data)
|
|
|
|
: x`""`;
|
|
|
|
: x `""`;
|
|
|
|
|
|
|
|
block.chunks.hydrate.push(b `@_document.title = ${value};`);
|
|
|
|
block.chunks.hydrate.push(b`@_document.title = ${value};`);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|