Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/plugins/format/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { has_modifier } from "@/utilities/utils";

function plugin({ directive, evaluateLater, mutateDom }) {
directive("format", (el, { modifiers }, { effect }) => {
const placeholder_regex = /{{(?<expr>.+?)}}/g;
const is_once = has_modifier(modifiers, "once");
const has_format_attr = el => el.hasAttribute("x-format");

Expand Down Expand Up @@ -55,7 +54,7 @@ function plugin({ directive, evaluateLater, mutateDom }) {
}

function process_text_node(node) {
const tokens = node.textContent.split(placeholder_regex);
const tokens = node.textContent.split(/{{(?<expr>.+?)}}/g);

if (tokens.length > 1) {
const fragment = new DocumentFragment();
Expand All @@ -80,10 +79,19 @@ function plugin({ directive, evaluateLater, mutateDom }) {

function process_attributes(node) {
for (let attr of node.attributes) {
const matches = [...attr.value.matchAll(placeholder_regex)];
const matches = [...attr.value.matchAll(/{{(?<expr>.+?)}}/g)];
if (matches.length) {
const getters = new Map(
matches.map(m => [
m.groups.expr,
create_getter(
evaluateLater,
node,
m.groups.expr)]));

const template = attr.value;
update(() => attr.value = template.replace(placeholder_regex, (_, expr) => create_getter(evaluateLater, node, expr)()));

update(() => attr.value = template.replace(/{{(?<expr>.+?)}}/g, (_, expr) => getters.get(expr)()));
}
}
}
Expand Down
Loading