I see now. In the single-root path, the normalize_prop_entries function collects all static style values and merges them into an array. Then the v-show directive adds a style entry with ((_ctx.ok) ? null : { display: "none" }).
But wait - in the single-root path, the v-show entry is DYNAMIC (the third param is false in component_prop_entry("style", ..., false)). And the static style from an attribute is also non-dynamic (false). So normalize_prop_entries would collect them both into the style_values array and merge them.
Actually, looking more carefully at normalize_prop_entries:
if !entry.dynamic && entry.key == "style" {
style_values.push(entry.value);
}
So it only collects STATIC (non-dynamic) style entries. The v-show entry has dynamic: false (the third param), so it would be collected too.
Actually wait, let me re-read:
entries.push(component_prop_entry(
"style",
&cstr!("(({exp}) ? null : {{ display: \"none\" }})"),
false, // dynamic = false
));
The dynamic flag in VNodePropEntry determines whether the key is dynamic (for dynamic keys like :[key]). It doesn't determine whether the VALUE is dynamic. So the v-show style entry has dynamic: false but the VALUE is dynamic.
And normalize_prop_entries only collects entries where !entry.dynamic && entry.key == "style". Since v-show entry has dynamic: false, it would be collected.
OK so in the single-root path:
- Static style attr → entry:
{ key: "style", value: "\"color:red\"", dynamic: false }
- v-show directive → entry:
{ key: "style", value: "((_ctx.ok) ? null : { display: \"none\" })", dynamic: false }
normalize_prop_entries collects both into style_values and merges: ["\"color:red\"", "((_ctx.ok) ? null : { display: \"none\" })"]
- Final entry:
{ key: "style", value: "[\"color:red\", ((_ctx.ok) ? null : { display: \"none\" })]", dynamic: false }
- This becomes part of the props object:
{ style: ["color:red", ((_ctx.ok) ? null : { display: "none" })] }
- When rendered via
_ssrRenderStyle, this works correctly!
Now, for the non-root (plain) element path, the fix should be similar. The challenge is that we're emitting immediately rather than collecting.
But actually, thinking about it more carefully, the simplest fix for process_v_show_on_element is:
- Check if the element has a
:style binding or static style attr
- If yes, emit a proper merged style binding using
_ssrRenderStyle
- If no, emit standalone style (current behavior)
For case 2, the static style may or may not have been emitted yet depending on prop ordering. But the key insight is: we're emitting to the same output stream. So if we emit a complete style="..." attribute, and the static style was already emitted, we'd get duplicates.
Hmm, let me think about this differently. What if we handle it this way:
For v-show on a plain element with existing style:
- If element has
:style binding → the static style is skipped (by process_element_attrs), so we emit a merged :style
- If element has static style but no
:style → the static style was already emitted. We need to emit a merged style that REPLACES it.
But we can't replace what's already been emitted. So the second case is problematic.
Wait, let me look at this from the perspective of what Vue's compiler actually does. In Vue's SSR:
ssrTransformShow rewrites v-show into a style transformation
- The v-show directive is removed from the template
- Instead, the expression is injected into the style attribute
Let me look at Vue's actual code for inspiration...
Actually, I think the simplest approach is:
- In
process_element_attrs, before iterating props, check if v-show is present
- If v-show is present AND there's a static style (no
:style), skip emitting the static style
- In
process_v_show_on_element, emit a merged style binding using _ssrRenderStyle
- If v-show is present AND there's a
:style binding, merge v-show into the :style expression
Let me implement this approach:
fn process_element_attrs(&mut self, el: &ElementNode) {
let has_dynamic_class = self.has_dynamic_bind(el, "class");
let has_dynamic_style = self.has_dynamic_bind(el, "style");
let has_v_show = self.has_v_show(el); // New helper
for prop in &el.props {
match prop {
PropNode::Attribute(attr) => {
if (attr.name == "class" && has_dynamic_class)
|| (attr.name == "style" && has_dynamic_style)
{
continue;
}
// Skip static style if v-show is present (v-show will emit merged style)
if attr.name == "style" && has_v_show && !has_dynamic_style {
continue;
}
// ... emit static attr
}
PropNode::Directive(dir) => {
self.process_directive_on_element(el, dir);
}
}
}
}
And then in process_v_show_on_element:
fn process_v_show_on_element(
&mut self,
el: &ElementNode,
dir: &vize_atelier_core::ast::DirectiveNode,
) {
let exp = match &dir.exp {
Some(exp) => self.expression_to_string(exp),
None => return,
};
let has_static_style = self.get_element_attr_value(el, "style").is_some();
let has_dynamic_style = self.has_dynamic_bind(el, "style");
if has_static_style || has_dynamic_style {
// Merge v-show into existing style binding
self.use_ssr_helper(RuntimeHelper::SsrRenderStyle);
self.push_string_part_static(" style=\"");
if has_dynamic_style {
let dyn_style_exp = self.get_dynamic_bind_exp(el, "style").unwrap_or_else(|| "null".to_compact_string());
self.push_string_part_dynamic(&cstr!(
"_ssrRenderStyle([{dyn_style_exp}, ({exp}) ? null : {{ display: \"none\" }}])"
));
} else if has_static_style {
let static_style = self.get_element_attr_value(el, "style").unwrap_or_default();
let quoted = quoted_js_string(&static_style);
self.push_string_part_dynamic(&cstr!(
"_ssrRenderStyle([{quoted}, ({exp}) ? null : {{ display: \"none\" }}])"
));
}
self.push_string_part_static("\"");
} else {
// No existing style - emit standalone style attribute (original behavior)
self.push_string_part_dynamic(&cstr!(
"(({exp}) ? \"\" : \" style=\\\"display: none;\\\"\")"
));
}
}
And I need a new helper:
fn has_v_show(&self, el: &ElementNode) -> bool {
use vize_atelier_core::ast::PropNode;
el.props.iter().any(|prop| {
let PropNode::Directive(dir) = prop else {
return false;
};
dir.name == "show"
})
}
Wait, but there's a complication. The get_element_attr_value and has_dynamic_bind functions might not work correctly because they need to be called at the right time. Let me trace through more carefully...
For <span style="color:red" v-show="ok">x</span>:
process_element_attrs starts
has_v_show = true
has_dynamic_style = false
- Iterate props:
style="color:red" attr → has_static_style && !has_dynamic_style is true → skip (continue)
v-show="ok" directive → process_v_show_on_element
- In
process_v_show_on_element:
has_static_style = true (there's still a style attr in el.props)
has_dynamic_style = false
- Emit:
style="${_ssrRenderStyle(["color:red", ((_ctx.ok) ? null : { display: "none" }})])}"
For <span v-show="ok" :style="{ color }">x</span>:
process_element_attrs starts
has_v_show = true
has_dynamic_style = true
- Iterate props:
v-show="ok" directive → process_v_show_on_element
- In
process_v_show_on_element:
has_static_style = false
has_dynamic_style = true
- Emit:
style="${_ssrRenderStyle([{ color: _ctx.color }, ((_ctx.ok) ? null : { display: "none" }})])}"
- Continue iterating props:
:style="{ color }" directive → process_v_bind_on_element
- In
process_v_bind_on_element, for style case:
has_static_style = false (no static style attr)
- But it checks
self.get_element_attr_value(_el, "style") which would return None
- So it would emit
style="${_ssrRenderStyle({ color: _ctx.color })}"
Hmm, that's a problem. If we merge v-show into the :style in step 5, then in step 6 we'd emit the :style again as a separate attribute.
The issue is that process_v_bind_on_element also emits a style attribute when processing :style, regardless of whether v-show was already handled.
I think the fix needs to be more comprehensive:
process_v_show_on_element needs to handle the merge when there's a :style binding
- But
process_v_bind_on_element ALSO needs to skip emitting the :style when v-show is present (since v-show already merged into it)
OR alternatively:
- Handle v-show FIRST, before any other processing
- Then skip the style emission in other directives
Actually, looking at the iteration order again:
- For
<span v-show="ok" :style="{ color }">x</span>, props are [v-show, :style]
- v-show is processed first, then :style
So if v-show processes first and merges into the :style, the :style will be processed second and emit a separate style attribute. We need to prevent that.
Hmm, this is getting complex. Let me think about a simpler approach.
What if process_v_show_on_element checks whether a style attribute will be (or has been) emitted by other props, and if so, it emits a merged style instead? And then we also need to prevent the duplicate emission.
Actually, I think the cleanest approach is:
- In
process_element_attrs, detect if v-show is present
- If v-show is present:
a. Skip static style attr (if no
:style)
b. In process_v_show_on_element, emit the merged style
c. In process_v_bind_on_element, when processing :style, check if v-show is present and if so, skip (since v-show already merged into it)
Wait, but process_v_bind_on_element already checks for existing static style to merge with it. If there's a static style, it merges. If not, it just emits style="${_ssrRenderStyle({exp})}".
So for <span v-show="ok" :style="{ color }">:
- v-show is processed first, emits merged style
- :style is processed, but there's no static style, so it emits
style="${_ssrRenderStyle({ color: _ctx.color })}" separately
That's still wrong. We need to prevent the :style from emitting its own style.
Alternative approach: what if in process_v_show_on_element, we DON'T merge into the :style, but instead we handle the merge in process_v_bind_on_element?
Let me think about this differently. What if the fix is in process_v_bind_on_element for the "style" case?
Current code:
Some("style") => {
self.use_ssr_helper(RuntimeHelper::SsrRenderStyle);
self.push_string_part_static(" style=\"");
let style_exp =
if let Some(static_style) = self.get_element_attr_value(_el, "style") {
let quoted = quoted_js_string(&static_style);
cstr!("_ssrRenderStyle([{quoted}, {exp}])")
} else {
cstr!("_ssrRenderStyle({exp})")
};
self.push_string_part_dynamic(&style_exp);
self.push_string_part_static("\"");
}
What if we add v-show handling here? Like:
Some("style") => {
self.use_ssr_helper(RuntimeHelper::SsrRenderStyle);
self.push_string_part_static(" style=\"");
let has_v_show = self.has_v_show(el);
let v_show_exp = if has_v_show {
self.get_v_show_exp(el).map(|e| self.expression_to_string(&e))
} else {
None
};
let style_exp = if let Some(static_style) = self.get_element_attr_value(el, "style") {
let quoted = quoted_js_string(&static_style);
if let Some(v_exp) = v_show_exp {
cstr!("_ssrRenderStyle([{quoted}, {exp}, ({v_exp}) ? null : {{ display: \"none\" }}])")
} else {
cstr!("_ssrRenderStyle([{quoted}, {exp}])")
}
} else {
if let Some(v_exp) = v_show_exp {
cstr!("_ssrRenderStyle([{exp}, ({v_exp}) ? null : {{ display: \"none\" }}])")
} else {
cstr!("_ssrRenderStyle({exp})")
}
};
self.push_string_part_dynamic(&style_exp);
self.push_string_part_static("\"");
}
And in process_v_show_on_element, if v-show is on an element with a style binding (static or dynamic), we do nothing (the merging is handled by process_v_bind_on_element). If there's no style binding, we emit standalone style.
But this creates a coupling: process_v_show_on_element needs to know about styles, and process_v_bind_on_element needs to know about v-show.
Hmm, this is getting complicated. Let me think about a cleaner approach.
Actually, I think the simplest approach is:
-
In process_v_show_on_element, ALWAYS emit a proper style binding:
- If element has static style → merge into it:
style="${_ssrRenderStyle(["static", conditional])}"
- If element has
:style → merge into it: style="${_ssrRenderStyle([dynamic, conditional])}"
- If element has neither → emit standalone style (original behavior)
-
In process_element_attrs, skip static style when v-show is present and there's no :style binding (since v-show will emit the merged style)
-
In process_v_bind_on_element, when processing :style, don't emit if v-show is present and we've already handled the style (since v-show merged into it). OR, emit nothing and let v-show handle it.
Actually wait, option 3 doesn't work because process_v_show_on_element might be called BEFORE or AFTER process_v_bind_on_element depending on prop ordering.
Let me look at this from yet another angle. What if we use a flag to track whether v-show has already emitted a style?
// In the context
v_show_style_emitted: bool
// In process_v_show_on_element
if has_existing_style {
self.use_ssr_helper(RuntimeHelper::SsrRenderStyle);
self.push_string_part_static(" style=\"");
// emit merged style
self.push_string_part_static("\"");
self.v_show_style_emitted = true;
} else {
// emit standalone style
}
// In process_v_bind_on_element for "style"
if self.v_show_style_emitted {
// Skip - v-show already handled it
return;
}
// ... emit :style
But this approach has problems:
- It requires state in the context
- The ordering of prop processing determines which one "wins"
- If :style is processed before v-show, v-show will emit a separate style
Actually, I think the key insight is that we need to handle v-show merging at the point where the style attribute is emitted. Since both process_v_show_on_element and process_v_bind_on_element can emit style, we need to coordinate.
Let me try a different approach: modify process_v_show_on_element to NOT emit when there's a :style binding (because process_v_bind_on_element will handle it), but DO emit when there's a static style (because the static style won't automatically merge with v-show).
But then for the static style case, we'd emit the static style AND the merged v-show style... unless we skip the static style first.
OK I think the correct approach is:
-
In process_element_attrs, when iterating props:
- Detect if v-show is present
- If v-show is present AND there's a static style attr (no
:style), SKIP the static style attr
- If v-show is present AND there's a
:style binding, SKIP processing :style in v_bind (or skip v-show handling)
-
In process_v_show_on_element, when called:
- If there's a static style (and it was skipped): emit merged style with static + conditional
- If there's a
:style binding: emit merged style with :style + conditional
- If neither: emit standalone style
But we can't skip :style in v_bind without some coordination...
Let me try yet another approach. What if we move the v-show handling INTO the style emission logic? That is:
process_v_show_on_element becomes: check if there's a style attr or :style binding; if yes, set a flag; if no, emit standalone style
- When
process_v_bind_on_element emits :style, it checks the flag and merges v-show into it
- When a static style is emitted (in
process_element_attrs), it checks if v-show will be merged...
This is getting too complicated. Let me look at what Vue's actual compiler does.
Actually, I think the simplest clean approach is:
-
process_v_show_on_element always handles the merging:
- Check for existing style (static or
:style)
- If found, emit a merged style using
_ssrRenderStyle([...])
- If not found, emit standalone style (original behavior)
-
Skip the static style attr in process_element_attrs when v-show is present and there's no :style binding
-
Skip the :style directive in process_v_bind_on_element when v-show is present
For step 3, we need to detect v-show in process_v_bind_on_element. Let me add a helper function.
Actually, let me reconsider. Looking at the original code flow again:
For <span style="color:red" v-show="ok">:
process_element_attrs iterates props: [style attr, v-show dir]
- Style attr is emitted:
style="color:red"
- v-show dir calls
process_v_show_on_element: emits style="display: none;" conditional
→ Result: <span style="color:red" style="display: none;"> - BUG!
For <span v-show="ok" :style="{ color }">:
process_element_attrs iterates props: [v-show dir, :style dir]
- v-show dir calls
process_v_show_on_element: emits style="display: none;" conditional
- :style dir calls
process_v_bind_on_element: emits style="${_ssrRenderStyle({ color })}"
→ Result: <span style="display: none;" style="${_ssrRenderStyle({ color })}"> - BUG!
The fix for case 1: skip static style when v-show is present, and in v-show handler, emit merged style.
The fix for case 2: in v-show handler, don't emit standalone; instead, the :style handler should merge v-show into it.
For case 2, the challenge is that v-show is processed BEFORE :style. So when :style is processed, it doesn't know that v-show was already handled.
Solution: add a flag to the context. When v-show processes and merges into a style, it sets a flag. Then when :style is processed, it checks the flag and skips emitting its own style.
OR: handle the merge in a different way. What if process_v_show_on_element sets a v-show expression in the context, and process_v_bind_on_element reads it?
Let me implement this approach:
// In SsrCodegenContext
pub v_show_conditional: Option<String>,
// In process_v_show_on_element
fn process_v_show_on_element(
&mut self,
el: &ElementNode,
dir: &vize_atelier_core::ast::DirectiveNode,
) {
let exp = match &dir.exp {
Some(exp) => self.expression_to_string(exp),
None => return,
};
let has_static_style = self.get_element_attr_value(el, "style").is_some();
let has_dynamic_style = self.has_dynamic_bind(el, "style");
if has_static_style || has_dynamic_style {
// Store the v-show conditional for merging into style
self.v_show_conditional = Some(cstr!("(({exp}) ? null : {{ display: \"none\" }})"));
return; // Don't emit yet - let the style handler merge it
} else {
// No existing style - emit standalone style attribute (original behavior)
self.push_string_part_dynamic(&cstr!(
"(({exp}) ? \"\" : \" style=\\\"display: none;\\\"\")"
));
}
}
And in process_element_attrs:
// Skip static style when v-show is present and we'll merge into it
if attr.name == "style" && self.v_show_conditional.is_some() {
continue;
}
And in process_v_bind_on_element for style:
Some("style") => {
self.use_ssr_helper(RuntimeHelper::SsrRenderStyle);
self.push_string_part_static(" style=\"");
let v_show_cond = self.v_show_condinal.take();
let style_exp = if let Some(static_style) = self.get_element_attr_value(_el, "style") {
let quoted = quoted_js_string(&static_style);
if let Some(cond) = v_show_cond {
cstr!("_ssrRenderStyle([{quoted}, {exp}, {cond}])")
} else {
cstr!("_ssrRenderStyle([{quoted}, {exp}])")
}
} else {
if let Some(cond) = v_show_cond {
cstr!("_ssrRenderStyle([{exp}, {cond}])")
} else {
cstr!("_ssrRenderStyle({exp})")
}
};
self.push_string_part_dynamic(&style_exp);
self.push_string_part_static("\"");
}
This approach uses a context flag to coordinate between v-show and style handlers. It's clean but requires adding state to the context.
Wait, but there's an issue with the has_v_show logic I was thinking about. Let me simplify:
process_v_show_on_element checks for existing style (static or :style)
- If found, store the v-show conditional and return (don't emit anything)
- If not found, emit standalone style (original behavior)
- When a static style is encountered, skip it if
v_show_conditional is set
- When
:style is encountered, merge the conditional into the style expression
This should work! Let me trace through the cases:
Case 1: <span style="color:red" v-show="ok">
Props: [style, v-show]
- style attr:
v_show_conditional is None → emit style="color:red"
- v-show:
has_static_style is true → store conditional, return
But the static style was already emitted! This approach doesn't work for this case.
OK so I need to pre-check for v-show in process_element_attrs. Let me refactor:
fn process_element_attrs(&mut self, el: &ElementNode) {
let has_dynamic_class = self.has_dynamic_bind(el, "class");
let has_dynamic_style = self.has_dynamic_bind(el, "style");
let has_v_show = self.get_v_show_exp(el).is_some();
for prop in &el.props {
match prop {
PropNode::Attribute(attr) => {
// Skip static style if v-show is present (v-show will handle merging)
if attr.name == "style" && has_v_show && !has_dynamic_style {
continue;
}
if (attr.name == "class" && has_dynamic_class)
|| (attr.name == "style" && has_dynamic_style)
{
continue;
}
// Emit static attr
}
PropNode::Directive(dir) => {
self.process_directive_on_element(el, dir);
}
}
}
}
And in process_v_show_on_element:
fn process_v_show_on_element(
&mut self,
el: &ElementNode,
dir: &vize_atelier_core::ast::DirectiveNode,
) {
let exp = match &dir.exp {
Some(exp) => self.expression_to_string(exp),
None => return,
};
let has_static_style = self.get_element_attr_value(el, "style").is_some();
let has_dynamic_style = self.has_dynamic_bind(el, "style");
if has_static_style || has_dynamic_style {
// Store v-show conditional for merging
self.v_show_conditional = Some(cstr!("(({exp}) ? null : {{ display: \"none\" }})"));
return;
} else {
// No existing style - emit standalone style attribute (original behavior)
self.push_string_part_dynamic(&cstr!(
"(({exp}) ? \"\" : \" style=\\\"display: none;\\\"\")"
));
}
}
And in process_v_bind_on_element for style:
Some("style") => {
self.use_ssr_helper(RuntimeHelper::SsrRenderStyle);
self.push_string_part_static(" style=\"");
let v_show_cond = self.v_show_conditional.take();
let style_exp = if let Some(static_style) = self.get_element_attr_value(_el, "style") {
let quoted = quoted_js_string(&static_style);
if let Some(cond) = v_show_cond {
cstr!("_ssrRenderStyle([{quoted}, {exp}, {cond}])")
} else {
cstr!("_ssrRenderStyle([{quoted}, {exp}])")
}
} else {
if let Some(cond) = v_show_cond {
cstr!("_ssrRenderStyle([{exp}, {cond}])")
} else {
cstr!("_ssrRenderStyle({exp})")
}
};
self.push_string_part_dynamic(&style_exp);
self.push_string_part_static("\"");
}
Let me trace through:
Case 1: <span style="color:red" v-show="ok">
Props: [style, v-show]
has_v_show = true, has_dynamic_style = false
- style attr:
attr.name == "style" && has_v_show && !has_dynamic_style → skip
- v-show:
has_static_style = true → store conditional ((_ctx.ok) ? null : { display: "none" }), return
Result: <span style="${_ssrRenderStyle([...])}">... wait, where's the merged style emitted?
Oh, I see the problem. When v-show stores the conditional and returns, nobody picks it up. The static style was skipped, and no other directive handles it.
So I need to either:
A) Have process_v_show_on_element emit the merged style directly
B) Have something else pick up the stored conditional
For option A, the problem is that process_v_show_on_element can't emit the merged style directly because it doesn't know whether there's a :style binding that will also be processed.
For <span v-show="ok" :style="{ color }">, if v-show emits the merged style, then :style would also emit a separate style.
I think the cleanest approach is:
- When there's a
:style binding: v-show stores the conditional, and process_v_bind_on_element merges it
- When there's only static style: v-show emits the merged style directly (since there's no :style to merge with)
- When there's neither: v-show emits standalone style
This avoids the need for a context flag.
fn process_v_show_on_element(
&mut self,
el: &ElementNode,
dir: &vize_atelier_core::ast::DirectiveNode,
) {
let exp = match &dir.exp {
Some(exp) => self.expression_to_string(exp),
None => return,
};
let has_static_style = self.get_element_attr_value(el, "style").is_some();
let has_dynamic_style = self.has_dynamic_bind(el, "style");
if has_dynamic_style {
// There's a :style binding. We need to merge v-show into it.
// But :style will be processed later (or earlier) by process_v_bind_on_element.
// Store the conditional for later.
self.v_show_conditional = Some(cstr!("(({exp}) ? null : {{ display: \"none\" }})"));
return;
} else if has_static_style {
// Only static style, no :style binding.
// Skip the static style in process_element_attrs, and emit merged style here.
self.use_ssr_helper(RuntimeHelper::SsrRenderStyle);
self.push_string_part_static(" style=\"");
let quoted = quoted_js_string(&self.get_element_attr_value(el, "style").unwrap_or_default());
self.push_string_part_dynamic(&cstr!(
"_ssrRenderStyle([{quoted}, ({exp}) ? null : {{ display: \"none\" }}])"
));
self.push_string_part_static("\"");
} else {
// No existing style - emit standalone style attribute (original behavior)
self.push_string_part_dynamic(&cstr!(
"(({exp}) ? \"\" : \" style=\\\"display: none;\\\"\")"
));
}
}
And in process_element_attrs:
// Skip static style if v-show is present and there's no :style (v-show handles it)
let has_v_show = self.get_v_show_exp(el).is_some();
if attr.name == "style" && has_v_show && !has_dynamic_style {
continue;
}
And in process_v_bind_on_element for style:
Some("style") => {
self.use_ssr_helper(RuntimeHelper::SsrRenderStyle);
self.push_string_part_static(" style=\"");
let v_show_cond = self.v_show_conditional.take();
let style_exp = if let Some(static_style) = self.get_element_attr_value(_el, "style") {
let quoted = quoted_js_string(&static_style);
if let Some(cond) = v_show_cond {
cstr!("_ssrRenderStyle([{quoted}, {exp}, {cond}])")
} else {
cstr!("_ssrRenderStyle([{quoted}, {exp}])")
}
} else {
if let Some(cond) = v_show_cond {
cstr!("_ssrRenderStyle([{exp}, {cond}])")
} else {
cstr!("_ssrRenderStyle({exp})")
}
};
self.push_string_part_dynamic(&style_exp);
self.push_string_part_static("\"");
}
Let me trace through:
Case 1: <span style="color:red" v-show="ok">
Props: [style, v-show]
has_v_show = true, has_dynamic_style = false
- style attr:
attr.name == "style" && has_v_show && !has_dynamic_style → skip
- v-show:
has_static_style = true, has_dynamic_style = false → emit merged style: _ssrRenderStyle(["color:red", ((_ctx.ok) ? null : { display: "none" }])"
Result: <span style="${_ssrRenderStyle(["color:red", ((_ctx.ok) ? null : { display: "none" }])}">x</span> ✓
Case 2: <span v-show="ok" :style="{ color }">
Props: [v-show, :style]
has_v_show = true, has_dynamic_style = false
- v-show:
has_static_style = false, has_dynamic_style = false → Wait, has_dynamic_style checks for :style binding...
Actually, has_dynamic_bind(el, "style") returns true if there's a :style binding. So has_dynamic_style should be true.
Let me re-check:
fn has_dynamic_bind(&self, el: &ElementNode, name: &str) -> bool {
el.props.iter().any(|prop| {
let PropNode::Directive(dir) = prop else {
return false;
};
if dir.name != "bind" {
return false;
}
matches!(&dir.arg, Some(ExpressionNode::Simple(arg)) if arg.is_static && arg.content == name)
})
}
This checks if any prop is a v-bind:style directive. For <span v-show="ok" :style="{ color }">, there IS a :style binding, so has_dynamic_style = true. ✓
So:
- v-show:
has_static_style = false, has_dynamic_style = true → store conditional, return
- :style:
v_show_conditional.take() returns the conditional → emit _ssrRenderStyle([{ color: _ctx.color }, ((_ctx.ok) ? null : { display: "none" }])"
Result: <span style="${_ssrRenderStyle([{ color: _ctx.color }, ((_ctx.ok) ? null : { display: "none" }])}">x</span> ✓
Case 3: <span v-show="ok">
Props: [v-show]
has_v_show = true, has_dynamic_style = false
- v-show:
has_static_style = false, has_dynamic_style = false → emit standalone style
Result: <span${((_ctx.ok) ? "" : " style=\"display: none;\"")}>x</span> ✓
Case 4: <span v-show="ok" :style="{ color }" style="color: red;">
Props: [v-show, :style, style]
has_v_show = true, has_dynamic_style = true
- v-show:
has_static_style = true, has_dynamic_style = true → store conditional, return
- :style:
v_show_conditional.take() returns the conditional → emit _ssrRenderStyle([{ color: _ctx.color }, cond])
- style:
has_v_show && !has_dynamic_style → false (has_dynamic_style is true) → doesn't skip → emits style="color: red;"
Wait, the static style wouldn't be skipped because has_dynamic_style is true. But it SHOULD be skipped because :style will handle it...
Actually, looking at the original process_element_attrs:
if (attr.name == "class" && has_dynamic_class)
|| (attr.name == "style" && has_dynamic_style)
{
continue;
}
This already skips static style when there's a :style binding. So the static style would already be skipped.
So the flow for Case 4:
- v-show:
has_static_style = true, has_dynamic_style = true → store conditional, return
- :style: skip static style (original logic) → emit
_ssrRenderStyle([{ color: _ctx.color }, cond])
- style:
has_dynamic_style = true → skip (original logic)
Result: <span style="${_ssrRenderStyle([{ color: _ctx.color }, cond])}">x</span> ✓
Great, this approach works! But I need to add:
- A field
v_show_conditional: Option<String> to SsrCodegenContext
- A helper
get_v_show_exp to get the v-show expression from an element
- Modify
process_element_attrs to skip static style when v-show is present and no :style
- Modify
process_v_show_on_element to merge with existing style
- Modify
process_v_bind_on_element for style to merge with v-show conditional
Let me also check if there are edge cases:
- What if v-show comes AFTER :style?
<span :style="{ color }" v-show="ok">
Props: [:style, v-show]
has_v_show = true, has_dynamic_style = true
- :style: skip static style, emit
_ssrRenderStyle({exp}) (v_show_conditional is None at this point)
- v-show:
has_static_style = false, has_dynamic_style = true → store conditional, return
Result: <span style="${_ssrRenderStyle({ color: _ctx.color })}">x</span> - NO MERGE!
Oh no, this is a problem. If v-show comes after :style, the merge doesn't happen.
To fix this, I need to always store the conditional and always merge, regardless of ordering. The issue is that the :style might be processed before v-show stores the conditional.
Alternative: always merge the conditional into style emission, regardless of whether :style or v-show is processed first.
Wait, let me reconsider. What if I handle it differently:
process_v_show_on_element ALWAYS stores the conditional
- When ANY style is emitted (static or dynamic), the conditional is merged
For static style emission in process_element_attrs:
// Emit static style, but merge v-show conditional if present
if let Some(cond) = self.v_show_conditional.take() {
self.use_ssr_helper(RuntimeHelper::SsrRenderStyle);
let quoted = quoted_js_string(&attr.value.as_ref().map(|v| &v.content).unwrap_or_default());
self.push_string_part_dynamic(&cstr!("_ssrRenderStyle([{quoted}, {cond}])"));
self.push_string_part_static("\"");
} else {
// Normal static style emission
self.push_string_part_static(" style=\"");
self.push_string_part_static(&escape_html_attr(&attr.value.as_ref().map(|v| &v.content).unwrap_or_default()));
self.push_string_part_static("\"");
}
But this requires modifying process_element_attrs to conditionally emit style differently.
For :style emission in process_v_bind_on_element:
Some("style") => {
self.use_ssr_helper(RuntimeHelper::SsrRenderStyle);
self.push_string_part_static(" style=\"");
let v_show_cond = self.v_show_conditional.take();
// ... merge conditional
}
And for v-show when there's no style binding:
fn process_v_show_on_element(...) {
// Always store the conditional
self.v_show_conditional = Some(cstr!("(({exp}) ? null : {{ display: \"none\" }})"));
// Don't emit anything here if there's a style binding
// But if there's no style binding, emit standalone style
if !has_static_style && !has_dynamic_style {
// emit standalone
}
}
Hmm, but if v-show is processed after :style, the conditional is stored AFTER :style has already emitted. So :style won't see it.
I think the only reliable approach is to pre-check for v-show in process_element_attrs and process_v_bind_on_element, and always merge the conditional into the style emission.
Let me simplify:
-
process_v_show_on_element:
- If there's an existing style (static or
:style), store the conditional and return (don't emit anything)
- If there's no existing style, emit standalone style
-
In process_element_attrs, when emitting a static style:
- If
v_show_conditional is set, emit merged style using _ssrRenderStyle
- If not, emit static style normally
-
In process_v_bind_on_element for :style:
- If
v_show_conditional is set, emit merged style using _ssrRenderStyle
- If not, emit
:style normally
-
In process_element_attrs, skip static style when there's a :style binding (existing behavior)
But what about the case where v-show comes after :style? The conditional wouldn't be stored until after :style has already emitted.
To handle this, we need to PRE-CHECK for v-show in process_element_attrs and process_v_bind_on_element. That is:
- Before iterating/processing anything, check if v-show is present
- If v-show is present, set the conditional
- Then process the rest normally
fn process_element_attrs(&mut self, el: &ElementNode) {
let has_dynamic_class = self.has_dynamic_bind(el, "class");
let has_dynamic_style = self.has_dynamic_bind(el, "style");
let has_v_show = self.get_v_show_exp(el).is_some();
// If v-show is present and there's an existing style, store the conditional early
if has_v_show {
if self.get_element_attr_value(el, "style").is_some() || has_dynamic_style {
// We'll find the expression when process_v_show_on_element is called
// But we need to store the conditional BEFORE processing styles
// So we can't do it here...
}
}
// ...
}
The problem is that we need the v-show expression to store the conditional, but we get the expression when process_v_show_on_element is called.
Alternative approach: store the raw v-show directive and process it later when needed.
Actually, I think the cleanest approach is to:
- Pre-check for v-show and store its expression in
process_element_attrs BEFORE processing any other props
- Then process all props normally, with v-show emitting nothing and the style handlers merging the conditional
fn process_element_attrs(&mut self, el: &ElementNode) {
let has_dynamic_class = self.has_dynamic_bind(el, "class");
let has_dynamic_style = self.has_dynamic_bind(el, "style");
// Check for v-show and store its expression for merging into style
let v_show_cond = self.get_v_show_conditional(el);
if v_show_cond.is_some() && (self.get_element_attr_value(el, "style").is_some() || has_dynamic_style) {
self.v_show_conditional = v_show_cond;
}
// ... rest of processing
}
And we'd need a get_v_show_conditional helper that gets the v-show expression from an element and returns the conditional format.
OK let me also handle the case where v-show comes after :style. In process_v_bind_on_element, we should check if v-show is present and merge it:
fn process_v_bind_on_element(
&mut self,
el: &ElementNode,
dir: &vize_atelier_core::ast::DirectiveNode,
) {
// ...
Some("style") => {
self.use_ssr_helper(RuntimeHelper::SsrRenderStyle);
self.push_string_part_static(" style=\"");
// Check for v-show expression (in case it hasn't been processed yet)
let v_show_cond = self.v_show_conditional.take()
.or_else(|| self.get_v_show_conditional(el).take());
// ...
}
}
This is getting really messy. Let me think about a cleaner approach...
Actually, I think the simplest approach that handles all cases is:
- Add
v_show_conditional: Option<String> to the context
- In
process_plain_element, BEFORE processing attributes, check for v-show and store the conditional if present
- In
process_element_attrs, merge the conditional when emitting styles
- In
process_v_bind_on_element, merge the conditional when emitting style
- In
process_v_show_on_element, if there's no existing style, emit standalone style
impl<'a> SsrCodegenContext<'a> {
fn process_plain_element(&mut self, el: &ElementNode, inherit_attrs: bool) {
let tag = &el.tag;
// Pre-check for v-show and store conditional for style merging
self.init_v_show_conditional(el);
// Start tag
self.push_string_part_static("<");
self.push_string_part_static(tag);
// ... rest
}
fn init_v_show_conditional(&mut self, el: &ElementNode) {
if let Some(v_show_exp) = self.get_v_show_exp(el) {
if self.get_element_attr_value(el, "style").is_some() || self.has_dynamic_bind(el, "style") {
let exp = self.expression_to_string(v_show_exp);
self.v_show_conditional = Some(cstr!("(({exp}) ? null : {{ display: \"none\" }})"));
}
}
}
// ...
}
Then in process_element_attrs:
fn process_element_attrs(&mut self, el: &ElementNode) {
let has_dynamic_class = self.has_dynamic_bind(el, "class");
let has_dynamic_style = self.has_dynamic_bind(el, "style");
for prop in &el.props {
match prop {
PropNode::Attribute(attr) => {
if (attr.name == "class" && has_dynamic_class)
|| (attr.name == "style" && has_dynamic_style)
{
continue;
}
// For style attribute, merge with v-show conditional if present
if attr.name == "style" {
if let Some(cond) = self.v_show_conditional.take() {
self.use_ssr_helper(RuntimeHelper::SsrRenderStyle);
let quoted = quoted_js_string(&attr.value.as_ref().map(|v| &v.content).unwrap_or_default());
self.push_string_part_dynamic(&cstr!("_ssrRenderStyle([{quoted}, {cond}])"));
self.push_string_part_static("\"");
return; // We've emitted the merged style, skip rest of attributes
}
}
// Normal static attr emission
self.push_string_part_static(" ");
self.push_string_part_static(&attr.name);
if let Some(value) = &attr.value {
self.push_string_part_static("=\"");
self.push_string_part_static(&escape_html_attr(&value.content));
self.push_string_part_static("\"");
}
}
PropNode::Directive(dir) => {
self.process_directive_on_element(el, dir);
}
}
}
}
Wait, that return is wrong. We should not return after emitting the merged style - we should continue processing other attributes. Let me fix:
if attr.name == "style" {
if let Some(cond) = self.v_show_conditional.take() {
self.use_ssr_helper(RuntimeHelper::SsrRenderStyle);
let quoted = quoted_js_string(&attr.value.as_ref().map(|v| &v.content).unwrap_or_default());
self.push_string_part_dynamic(&cstr!("_ssrRenderStyle([{quoted}, {cond}])"));
self.push_string_part_static("\"");
} else {
// Normal static style emission
self.push_string_part_static(" style=\"");
self.push_string_part_static(&escape_html_attr(&attr.value.as_ref().map(|v| &v.content).unwrap_or_default()));
self.push_string_part_static("\"");
}
continue;
}
And in process_v_show_on_element:
fn process_v_show_on_element(
&mut self,
_el: &ElementNode, // No longer need to inspect el
dir: &vize_atelier_core::ast::DirectiveNode,
) {
// v-show conditional was already stored in init_v_show_conditional
// If there was an existing style, it was merged into the style emission above
// or in process_v_bind_on_element.
// If there was no existing style, we need to emit standalone style.
// Check if there was an existing style - if not, emit standalone
// (The conditional would have been taken by the style emission)
// Actually, we need to check if v-show conditional is still set...
// Hmm, this doesn't work well because the conditional is already taken by then.
}
This approach has an issue with the v-show handling. If v-show has no existing style, we need to emit standalone style, but by the time process_v_show_on_element is called, the conditional has already been stored (or taken).
Let me rethink. Maybe process_v_show_on_element should NOT be called when there's an existing style and v-show is handled by the style emission. We need to prevent it from being called, or have it be a no-op.
Alternative: don't use process_v_show_on_element for elements that have an existing style. Just handle everything through the style emission.
fn process_directive_on_element(
&mut self,
el: &ElementNode,
dir: &vize_atelier_core::ast::DirectiveNode,
) {
match dir.name.as_str() {
"show" => {
// Only handle v-show if there's no existing style (style emission handles the merge)
if self.get_element_attr_value(el, "style").is_none() && !self.has_dynamic_bind(el, "style") {
self.process_v_show_on_element(el, dir);
}
// Otherwise, do nothing - v-show was already merged into style
}
// ...
}
}
And process_v_show_on_element would remain as the original standalone-style emission:
fn process_v_show_on_element(
&mut self,
_el: &ElementNode,
dir: &vize_atelier_core::ast::DirectiveNode,
) {
let exp = match &dir.exp {
Some(exp) => self.expression_to_string(exp),
None => return,
};
self.push_string_part_dynamic(&cstr!(
"(({exp}) ? \"\" : \" style=\\\"display: none;\\\"\")"
));
}
And init_v_show_conditional would only set the conditional when there IS an existing style:
fn init_v_show_conditional(&mut self, el: &ElementNode) {
if let Some(v_show_exp) = self.get_v_show_exp(el) {
if self.get_element_attr_value(el, "style").is_some() || self.has_dynamic_bind(el, "style") {
let exp = self.expression_to_string(v_show_exp);
self.v_show_conditional = Some(cstr!("(({exp}) ? null : {{ display: \"none\" }})"));
}
}
}
And the style emissions in process_element_attrs and process_v_bind_on_element merge the conditional when present.
Let me trace through all cases:
Case 1: <span style="color:red" v-show="ok">
Props: [style, v-show]
init_v_show_conditional: v_show_exp = ok, has_static_style = true → store conditional
- style: conditional is set → emit
_ssrRenderStyle(["color:red", cond]), take conditional
- v-show:
get_element_attr_value(el, "style") returns Some, has_dynamic_bind(el, "style") returns false → condition is true, so process_v_show_on_element is NOT called (skip)
Result: <span style="${_ssrRenderStyle(["color:red", ((_ctx.ok) ? null : { display: "none" }])}">x</span> ✓
Case 2: <span v-show="ok" :style="{ color }">
Props: [v-show, :style]
init_v_show_conditional: v_show_exp = ok, has_static_style = false, has_dynamic_style = true → store conditional
- v-show:
get_element_attr_value(el, "style") returns None, has_dynamic_bind(el, "style") returns true → skip process_v_show_on_element
- :style: conditional is set → emit
_ssrRenderStyle([{ color: _ctx.color }, cond]), take conditional
Result: <span style="${_ssrRenderStyle([{ color: _ctx.color }, ((_ctx.ok) ? null : { display: "none" }])}">x</span> ✓
Case 3: <span v-show="ok">
Props: [v-show]
init_v_show_conditional: v_show_exp = ok, has_static_style = false, has_dynamic_style = false → don't store conditional (no existing style)
- v-show:
get_element_attr_value(el, "style") returns None, has_dynamic_bind(el, "style") returns false → call process_v_show_on_element → emit standalone style
Result: <span${((_ctx.ok) ? "" : " style=\"display: none;\"")}>x</span> ✓
Case 4: <span v-show="ok" :style="{ color }" style="color: red;">
Props: [v-show, :style, style]
init_v_show_conditional: v_show_exp = ok, has_static_style = true, has_dynamic_style = true → store conditional
- v-show: skip (has_dynamic_style is true)
- :style: skip static style (original logic:
attr.name == "style" && has_dynamic_style) → emit _ssrRenderStyle([{ color: _ctx.color }, cond]), take conditional
- style: skip (original logic:
attr.name == "style" && has_dynamic_style)
Result: <span style="${_ssrRenderStyle([{ color: _ctx.color }, ((_ctx.ok) ? null : { display: "none" }])}">x</span> ✓
This approach works for all cases!
But wait, there's still a problem with case 4. Let me re-trace:
Props: [v-show, :style, style]
- v-show is Directive, calls process_directive_on_element → checks condition → skips
- :style is Directive, calls process_directive_on_element → process_v_bind_on_element → style case:
get_element_attr_value(_el, "style") returns Some ("color: red") because the static style is still in el.props
- So it emits
_ssrRenderStyle(["color: red", { color: _ctx.color }, cond])
- style is Attribute, but it's skipped by
has_dynamic_style check
Hmm, actually the static style IS in el.props, so get_element_attr_value(_el, "style") would return it. But the :style directive should take precedence over the static style (since the original logic skips the static style when there's a :style binding).
So the style emission in process_v_bind_on_element should be:
Some("style") => {
self.use_ssr_helper(RuntimeHelper::SsrRenderStyle);
self.push_string_part_static(" style=\"");
let v_show_cond = self.v_show_conditional.take();
let style_exp = if let Some(static_style) = self.get_element_attr_value(_el, "style") {
let quoted = quoted_js_string(&static_style);
if let Some(cond) = v_show_cond {
cstr!("_ssrRenderStyle([{quoted}, {exp}, {cond}])")
} else {
cstr!("_ssrRenderStyle([{quoted}, {exp}])")
}
} else {
if let Some(cond) = v_show_cond {
cstr!("_ssrRenderStyle([{exp}, {cond}])")
} else {
cstr!("_ssrRenderStyle({exp})")
}
};
self.push_string_part_dynamic(&style_exp);
self.push_string_part_static("\"");
}
Wait, but for case 4, the static style is "color: red" and the :style is { color: _ctx.color }. The merged result would be _ssrRenderStyle(["color: red", { color: _ctx.color }, cond]). That's correct!
Actually, let me re-check. In case 4:
- Static style:
style="color: red"
- :style:
:style="{ color }" → { color: _ctx.color }
- v-show:
v-show="ok" → conditional
The merged style should be: _ssrRenderStyle(["color: red", { color: _ctx.color }, ((_ctx.ok) ? null : { display: "none" }])]
And that's what process_v_bind_on_element would emit because:
- It sees the static style ("color: red")
- It sees the :style expression (
{ color: _ctx.color })
- It sees the v-show conditional (from the context)
- It emits:
_ssrRenderStyle(["color: red", { color: _ctx.color }, cond])
✓ This is correct!
Now let me also consider what happens when :style comes AFTER v-show in the props:
Case 5: <span style="color:red" :style="{ color }" v-show="ok">
Props: [style, :style, v-show]
init_v_show_conditional: v_show_exp = ok, has_static_style = true, has_dynamic_style = true → store conditional
-
style: has_dynamic_style = true → skip (original logic: attr.name == "style" && has_dynamic_style)
- Wait, but this is the static style. With the
has_dynamic_style check, it's skipped.
-
:style: v_show_conditional is set, get_element_attr_value(_el, "style") returns Some ("color: red") → emit _ssrRenderStyle(["color: red", { color: _ctx.color }, cond])
-
v-show: skips because has_dynamic_style is true
Result: <span style="${_ssrRenderStyle(["color: red", { color: _ctx.color }, cond])}">x</span> ✓
Wait, but the static style was skipped in step 1. That's correct because has_dynamic_style is true.
Actually, hold on. There's a subtle issue. Let me re-read the process_element_attrs code:
fn process_element_attrs(&mut self, el: &ElementNode) {
let has_dynamic_class = self.has_dynamic_bind(el, "class");
let has_dynamic_style = self.has_dynamic_bind(el, "style");
for prop in &el.props {
match prop {
PropNode::Attribute(attr) => {
if (attr.name == "class" && has_dynamic_class)
|| (attr.name == "style" && has_dynamic_style)
{
continue;
}
// For style attribute, merge with v-show conditional if present
if attr.name == "style" {
if let Some(cond) = self.v_show_conditional.take() {
self.use_ssr_helper(RuntimeHelper::SsrRenderStyle);
let quoted = quoted_js_string(&attr.value.as_ref().map(|v| &v.content).unwrap_or_default());
self.push_string_part_dynamic(&cstr!("_ssrRenderStyle([{quoted}, {cond}])"));
self.push_string_part_static("\"");
} else {
// Normal static style emission
self.push_string_part_static(" style=\"");
self.push_string_part_static(&escape_html_attr(&attr.value.as_ref().map(|v| &v.content).unwrap_or_default()));
self.push_string_part_static("\"");
}
continue;
}
// Normal static attr emission
self.push_string_part_static(" ");
self.push_string_part_static(&attr.name);
if let Some(value) = &attr.value {
self.push_string_part_static("=\"");
self.push_string_part_static(&escape_html_attr(&value.content));
self.push_string_part_static("\"");
}
}
PropNode::Directive(dir) => {
self.process_directive_on_element(el, dir);
}
}
}
}
For case 1: <span style="color:red" v-show="ok">
Props: [style, v-show]
has_dynamic_style = false
- style attribute:
attr.name == "style" && has_dynamic_style → false → doesn't skip
attr.name == "style" → true → enters style block
v_show_conditional is Some → merge and emit _ssrRenderStyle(["color:red", cond]), take conditional
- v-show: skip because
get_element_attr_value(el, "style") returns Some
Result: <span style="${_ssrRenderStyle(["color:red", cond])}">x</span> ✓
For case 5: <span style="color:red" :style="{ color }" v-show="ok">
Props: [style, :style, v-show]
has_dynamic_style = true
- style attribute:
attr.name == "style" && has_dynamic_style → true → skip (continue)
- :style directive: process_v_bind_on_element, style case
get_element_attr_value(_el, "style") returns Some ("color: red")
v_show_conditional is Some → emit _ssrRenderStyle(["color: red", { color: _ctx.color }, cond])
- v-show: skip because
has_dynamic_style is true
Result: <span style="${_ssrRenderStyle(["color: red", { color: _ctx.color }, cond])}">x</span> ✓
OK great, this approach works! Let me now implement it.
Summary of changes needed:
- Add
v_show_conditional: Option<String> field to SsrCodegenContext
- Add
get_v_show_exp(&self, el: &ElementNode) -> Option<&ExpressionNode> helper
- Add
init_v_show_conditional(&mut self, el: &ElementNode) method
- Call
init_v_show_conditional(el) at the beginning of process_plain_element
- Modify
process_element_attrs to merge v-show conditional when emitting static style
- Modify
process_v_bind_on_element for style to merge v-show conditional
- Modify
process_directive_on_element for "show" to skip when there's an existing style
- Keep
process_v_show_on_element as the standalone-style emission (only called when no existing style)
Wait, I also need to consider the component case. Let me look at how v-show is handled on components:
<tool_call>
<function=bash_terminal>
<parameter=command>
cat /vize/crates/vize_atelier_ssr/src/codegen/element/component.rs
</tool_call>