| |
| |
| |
| |
| |
| |
| |
|
|
| use vibao_ast::{get_prop, AnimationProps, Element, LapValue}; |
| use crate::codegen::css::{esc_attr, indent2, layout_css_to_string, layout_css_to_string_inline, style_map_to_string, OrderedMap}; |
| use crate::codegen::expr::{expr_to_js_default, resolve_value, ResolvedValue}; |
| use crate::codegen::layout::{is_layout_tag, resolve_layout_css, resolve_responsive_css, build_media_query}; |
| use crate::codegen::props::expand_props; |
|
|
| |
| |
| |
| |
| pub const BUILTIN_COMPLEX: [&str; 10] = [ |
| "form", |
| "modal", |
| "tabs", |
| "accordion", |
| "carousel", |
| "xuong_trang", |
| "bang", |
| "bieu_do", |
| "ban_do", |
| "trinh_soan_thao", |
| ]; |
|
|
| pub fn is_builtin_complex(tag: &str) -> bool { |
| BUILTIN_COMPLEX.contains(&tag) |
| } |
|
|
| |
| |
| fn tag_to_html(tag: &str) -> &str { |
| match tag { |
| "text" => "p", |
| "h1" => "h1", |
| "h2" => "h2", |
| "h3" => "h3", |
| "p" => "p", |
| "nhan" => "span", |
| "button" => "button", |
| "link" | "lien_ket" => "a", |
| "image" => "img", |
| "video" => "video", |
| "icon" => "span", |
| "input" => "input", |
| "spacer" => "div", |
| "divider" => "hr", |
| "vong_quay" => "div", |
| "thanh_tien_trinh" => "div", |
| "scroll" => "div", |
| "container" => "div", |
| _ => "div", |
| } |
| } |
|
|
| const SELF_CLOSING: [&str; 4] = ["img", "input", "hr", "br"]; |
|
|
| fn is_self_closing(html_tag: &str) -> bool { |
| SELF_CLOSING.contains(&html_tag) |
| } |
|
|
| |
| |
| |
| |
| |
| pub trait ElementCodegenHost { |
| |
| fn next_id(&mut self, tag: &str) -> String; |
| |
| fn gen_children(&mut self, children: &[vibao_ast::Child]) -> String; |
| |
| fn add_js(&mut self, code: &str); |
| |
| fn add_css(&mut self, code: &str); |
| |
| fn add_media_query(&mut self, code: &str); |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| fn compile_event_handler(&self, event: &vibao_ast::EventNode, id: &str) -> String; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| fn compile_event_handler_registry(&self, event: &vibao_ast::EventNode) -> (String, String) { |
| crate::codegen::action::compile_event_handler_registry(event) |
| } |
| fn compile_hover_animation(&self, id: &str, effect: &str, duration_ms: u32) -> String; |
| fn compile_scroll_animation(&self, id: &str, effect: &str, duration_ms: u32, delay_ms: u32) -> String; |
| } |
|
|
| |
| pub fn gen_element(node: &Element, host: &mut dyn ElementCodegenHost) -> String { |
| let id = host.next_id(&node.tag); |
| if is_builtin_complex(&node.tag) { |
| gen_complex_component(node, &id) |
| } else if is_layout_tag(&node.tag) { |
| gen_layout_element(node, &id, host) |
| } else { |
| gen_simple_element(node, &id, host) |
| } |
| } |
|
|
| |
| |
| |
|
|
| fn gen_simple_element(node: &Element, id: &str, host: &mut dyn ElementCodegenHost) -> String { |
| let expanded = expand_props(&node.tag, &node.props); |
| let style_str = style_map_to_string(&expanded.style); |
| let attrs_str = attrs_to_string_ordered(&expanded.attrs, &["noi_dung"]); |
| let dynamic_attrs = expanded |
| .dynamic |
| .iter() |
| .map(|(k, v)| format!("data-vb-bind-{}=\"{}\"", k, esc_attr(v))) |
| .collect::<Vec<_>>() |
| .join(" "); |
| let anim_attrs = gen_anim_attrs(&node.animation); |
|
|
| |
| |
| |
| |
| |
| |
| let event_attrs = node |
| .events |
| .iter() |
| .map(|e| { |
| let (attr_name, action_id) = host.compile_event_handler_registry(e); |
| format!("{}=\"{}\"", attr_name, action_id) |
| }) |
| .collect::<Vec<_>>() |
| .join(" "); |
|
|
| |
| |
| |
| |
| |
| |
|
|
| if !node.responsive.is_empty() { |
| let bp_css = resolve_responsive_css(&node.tag, &node.responsive); |
| for bp in &bp_css { |
| let mq = build_media_query(&format!("#{}", id), bp); |
| if !mq.is_empty() { |
| host.add_media_query(&mq); |
| } |
| } |
| } |
|
|
| let content = get_content(&node.tag, &node.props, &node.children, host); |
| let html_tag = tag_to_html(&node.tag); |
| let self_closing = is_self_closing(html_tag); |
|
|
| let all_attrs = [ |
| format!("id=\"{}\"", id), |
| if style_str.is_empty() { String::new() } else { format!("style=\"{}\"", style_str) }, |
| attrs_str, |
| dynamic_attrs, |
| anim_attrs, |
| event_attrs, |
| if !node.events.is_empty() { "data-vb-interactive".to_string() } else { String::new() }, |
| ] |
| .into_iter() |
| .filter(|s| !s.is_empty()) |
| .collect::<Vec<_>>() |
| .join(" "); |
|
|
| if self_closing { |
| format!("<{} {} />", html_tag, all_attrs) |
| } else { |
| format!("<{} {}>{}</{}>", html_tag, all_attrs, content, html_tag) |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| fn get_content(tag: &str, props: &vibao_ast::PropsMap, children: &[vibao_ast::Child], host: &mut dyn ElementCodegenHost) -> String { |
| let _ = tag; |
| if let Some(content_expr) = get_prop(props, "noi_dung") { |
| let resolved = resolve_value(content_expr); |
| return match resolved { |
| ResolvedValue::Dynamic(_) => { |
| format!("<span data-vb-text=\"{}\"></span>", esc_attr(&expr_to_js_default(content_expr))) |
| } |
| ResolvedValue::Static(s) => s, |
| |
| |
| |
| |
| _ => String::new(), |
| }; |
| } |
| if !children.is_empty() { |
| format!("\n{}\n", indent2(&host.gen_children(children))) |
| } else { |
| String::new() |
| } |
| } |
|
|
| fn attrs_to_string_ordered(attrs: &OrderedMap, skip: &[&str]) -> String { |
| attrs |
| .iter() |
| .filter(|(k, _)| !skip.contains(&k.as_str())) |
| .map(|(k, v)| format!("{}=\"{}\"", k, v)) |
| .collect::<Vec<_>>() |
| .join(" ") |
| } |
|
|
| |
| |
| |
|
|
| fn gen_layout_element(node: &Element, id: &str, host: &mut dyn ElementCodegenHost) -> String { |
| let layout_css = resolve_layout_css(&node.tag, &node.props); |
| let style_str = layout_css_to_string_inline(&layout_css); |
|
|
| |
| |
| |
| |
| let dynamic_color = match get_prop(&node.props, "color") { |
| Some(expr @ vibao_ast::Expr::Variable(_, _)) => { |
| format!("data-vb-bind-backgroundColor=\"{}\"", esc_attr(&expr_to_js_default(expr))) |
| } |
| _ => String::new(), |
| }; |
| let anim_attrs = gen_anim_attrs(&node.animation); |
|
|
| host.add_css(&layout_css_to_string(&format!("#{}", id), &layout_css)); |
|
|
| if !node.responsive.is_empty() { |
| let bp_css = resolve_responsive_css(&node.tag, &node.responsive); |
| for bp in &bp_css { |
| let mq = build_media_query(&format!("#{}", id), bp); |
| if !mq.is_empty() { |
| host.add_media_query(&mq); |
| } |
| } |
| } |
| if let Some(hover) = &node.animation.hieu_ung_hover { |
| host.add_js(&host.compile_hover_animation(id, hover, node.animation.thoi_gian.unwrap_or(300))); |
| } |
| if let Some(scroll) = &node.animation.hieu_ung_cuon { |
| host.add_js(&host.compile_scroll_animation( |
| id, |
| scroll, |
| node.animation.thoi_gian.unwrap_or(600), |
| node.animation.tre.unwrap_or(0), |
| )); |
| } |
|
|
| let children_html = if node.tag == "stack" { |
| node.children |
| .iter() |
| .map(|c| { |
| let html = host.gen_children(std::slice::from_ref(c)); |
| if html.is_empty() { String::new() } else { wrap_stack_child(&html) } |
| }) |
| .filter(|s| !s.is_empty()) |
| .collect::<Vec<_>>() |
| .join("\n") |
| } else { |
| host.gen_children(&node.children) |
| }; |
|
|
| let extra_style = if node.tag == "layer" { "position:relative;" } else { "" }; |
|
|
| format!( |
| "<div id=\"{}\" style=\"{}{}\" {} {}>\n{}\n</div>", |
| id, |
| extra_style, |
| style_str, |
| dynamic_color, |
| anim_attrs, |
| indent2(&children_html) |
| ) |
| } |
|
|
| fn wrap_stack_child(html: &str) -> String { |
| format!("<div style=\"grid-area:1/1/2/2\">{}</div>", html) |
| } |
|
|
| |
| |
| |
|
|
| fn gen_complex_component(node: &Element, id: &str) -> String { |
| format!("<div id=\"{}\" data-vb-component=\"{}\"><!-- {} --></div>", id, node.tag, node.tag) |
| } |
|
|
| |
| |
| |
| |
| pub fn make_class_name(tag: &str, index: u32) -> String { |
| format!("vb-{}-{}", tag, index) |
| } |
|
|
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| pub fn gen_anim_attrs(anim: &AnimationProps) -> String { |
| let mut parts: Vec<String> = Vec::new(); |
|
|
| if let Some(hieu_ung) = &anim.hieu_ung { |
| parts.push(format!("data-vb-anim=\"{}\"", hieu_ung)); |
| if let Some(ms) = anim.thoi_gian { |
| parts.push(format!("data-vb-anim-duration=\"{}\"", ms)); |
| } |
| if let Some(ms) = anim.tre { |
| parts.push(format!("data-vb-anim-delay=\"{}\"", ms)); |
| } |
| if let Some(lap) = &anim.lap { |
| let lap_str = match lap { |
| LapValue::Count(n) => n.to_string(), |
| LapValue::MaiMai => "infinite".to_string(), |
| }; |
| parts.push(format!("data-vb-anim-repeat=\"{}\"", lap_str)); |
| } |
| } |
|
|
| |
| |
| |
| |
| if let Some(hover) = &anim.hieu_ung_hover { |
| let dur = anim.thoi_gian.unwrap_or(300); |
| parts.push(format!("data-vb-anim-hover=\"{}:{}\"", hover, dur)); |
| } |
|
|
| |
| if let Some(scroll) = &anim.hieu_ung_cuon { |
| let dur = anim.thoi_gian.unwrap_or(600); |
| let delay = anim.tre.unwrap_or(0); |
| parts.push(format!("data-vb-anim-scroll=\"{}:{}:{}\"", scroll, dur, delay)); |
| } |
|
|
| parts.join(" ") |
| } |
|
|
| |
| |
| |
|
|
| #[cfg(test)] |
| mod tests { |
| use super::*; |
| use vibao_ast::{Child, Element, Expr, Pos}; |
|
|
| fn p() -> Pos { |
| Pos { line: 1, column: 1 } |
| } |
|
|
| |
| |
| struct FakeHost { |
| counter: u32, |
| js: Vec<String>, |
| css: Vec<String>, |
| media: Vec<String>, |
| } |
|
|
| impl FakeHost { |
| fn new() -> Self { |
| FakeHost { counter: 0, js: vec![], css: vec![], media: vec![] } |
| } |
| } |
|
|
| impl ElementCodegenHost for FakeHost { |
| fn next_id(&mut self, tag: &str) -> String { |
| self.counter += 1; |
| format!("vb-{}-{}", tag, self.counter) |
| } |
| fn gen_children(&mut self, children: &[Child]) -> String { |
| children |
| .iter() |
| .map(|c| match c { |
| Child::Element(el) => gen_element(el, self), |
| _ => String::new(), |
| }) |
| .collect::<Vec<_>>() |
| .join("\n") |
| } |
| fn add_js(&mut self, code: &str) { |
| self.js.push(code.to_string()); |
| } |
| fn add_css(&mut self, code: &str) { |
| self.css.push(code.to_string()); |
| } |
| fn add_media_query(&mut self, code: &str) { |
| self.media.push(code.to_string()); |
| } |
| fn compile_event_handler(&self, _event: &vibao_ast::EventNode, _id: &str) -> String { |
| String::new() |
| } |
| fn compile_hover_animation(&self, _id: &str, _effect: &str, _duration_ms: u32) -> String { |
| String::new() |
| } |
| fn compile_scroll_animation(&self, _id: &str, _effect: &str, _duration_ms: u32, _delay_ms: u32) -> String { |
| String::new() |
| } |
| } |
|
|
| fn make_element(tag: &str, props: vibao_ast::PropsMap) -> Element { |
| Element { |
| tag: tag.to_string(), |
| props, |
| children: vec![], |
| events: vec![], |
| responsive: vec![], |
| animation: AnimationProps::default(), |
| pos: p(), |
| } |
| } |
|
|
| #[test] |
| fn test_tag_to_html_mapping() { |
| assert_eq!(tag_to_html("text"), "p"); |
| assert_eq!(tag_to_html("link"), "a"); |
| assert_eq!(tag_to_html("lien_ket"), "a"); |
| assert_eq!(tag_to_html("image"), "img"); |
| assert_eq!(tag_to_html("unknown_custom_tag"), "div"); |
| } |
|
|
| #[test] |
| fn test_self_closing_tags() { |
| assert!(is_self_closing("img")); |
| assert!(is_self_closing("input")); |
| assert!(!is_self_closing("div")); |
| assert!(!is_self_closing("p")); |
| } |
|
|
| #[test] |
| fn test_simple_element_text_content() { |
| let mut host = FakeHost::new(); |
| let el = make_element("text", vec![("noi_dung".to_string(), Expr::literal_str("Xin chào", p()))]); |
| let html = gen_simple_element(&el, "vb-text-1", &mut host); |
| assert!(html.starts_with("<p ")); |
| assert!(html.contains(">Xin chào</p>")); |
| } |
|
|
| #[test] |
| fn test_simple_element_self_closing_image() { |
| let mut host = FakeHost::new(); |
| let el = make_element("image", vec![("alt".to_string(), Expr::literal_str("logo", p()))]); |
| let html = gen_simple_element(&el, "vb-image-1", &mut host); |
| assert!(html.starts_with("<img ")); |
| assert!(html.ends_with("/>")); |
| } |
|
|
| #[test] |
| fn test_simple_element_dynamic_content_uses_span_binding() { |
| let mut host = FakeHost::new(); |
| let el = make_element("text", vec![("noi_dung".to_string(), Expr::Variable("ten".to_string(), p()))]); |
| let html = gen_simple_element(&el, "vb-text-1", &mut host); |
| assert!(html.contains("data-vb-text=")); |
| } |
|
|
| #[test] |
| fn test_layout_element_is_div_with_css_registered() { |
| let mut host = FakeHost::new(); |
| let el = make_element("flex", vec![]); |
| let html = gen_layout_element(&el, "vb-flex-1", &mut host); |
| assert!(html.starts_with("<div id=\"vb-flex-1\"")); |
| assert_eq!(host.css.len(), 1); |
| assert!(host.css[0].contains("display: flex;")); |
| } |
|
|
| #[test] |
| fn test_layout_element_layer_gets_relative_position() { |
| let mut host = FakeHost::new(); |
| let el = make_element("layer", vec![]); |
| let html = gen_layout_element(&el, "vb-layer-1", &mut host); |
| assert!(html.contains("position:relative;")); |
| } |
|
|
| #[test] |
| fn test_complex_component_placeholder() { |
| let el = make_element("modal", vec![]); |
| let html = gen_complex_component(&el, "vb-modal-1"); |
| assert_eq!(html, "<div id=\"vb-modal-1\" data-vb-component=\"modal\"><!-- modal --></div>"); |
| } |
|
|
| #[test] |
| fn test_gen_element_dispatches_to_complex_for_builtin() { |
| let mut host = FakeHost::new(); |
| let el = make_element("tabs", vec![]); |
| let html = gen_element(&el, &mut host); |
| assert!(html.contains("data-vb-component=\"tabs\"")); |
| } |
|
|
| #[test] |
| fn test_gen_element_dispatches_to_layout_for_flex() { |
| let mut host = FakeHost::new(); |
| let el = make_element("flex", vec![]); |
| let html = gen_element(&el, &mut host); |
| assert!(html.starts_with("<div id=\"vb-flex-1\"")); |
| } |
|
|
| #[test] |
| fn test_gen_anim_attrs_hover_only() { |
| let anim = AnimationProps { |
| hieu_ung: None, |
| thoi_gian: Some(400), |
| tre: None, |
| lap: None, |
| hieu_ung_hover: Some("phong_to".to_string()), |
| hieu_ung_cuon: None, |
| }; |
| let out = gen_anim_attrs(&anim); |
| assert_eq!(out, "data-vb-anim-hover=\"phong_to:400\""); |
| } |
|
|
| #[test] |
| fn test_gen_anim_attrs_hover_default_duration() { |
| let anim = AnimationProps { |
| hieu_ung_hover: Some("phong_to".to_string()), |
| ..AnimationProps::default() |
| }; |
| let out = gen_anim_attrs(&anim); |
| assert_eq!(out, "data-vb-anim-hover=\"phong_to:300\""); |
| } |
|
|
| #[test] |
| fn test_gen_anim_attrs_scroll_with_delay() { |
| let anim = AnimationProps { |
| hieu_ung_cuon: Some("truot_len".to_string()), |
| thoi_gian: Some(500), |
| tre: Some(150), |
| ..AnimationProps::default() |
| }; |
| let out = gen_anim_attrs(&anim); |
| assert_eq!(out, "data-vb-anim-scroll=\"truot_len:500:150\""); |
| } |
|
|
| #[test] |
| fn test_gen_anim_attrs_combines_load_and_hover() { |
| let anim = AnimationProps { |
| hieu_ung: Some("fade_in".to_string()), |
| thoi_gian: Some(200), |
| hieu_ung_hover: Some("phong_to".to_string()), |
| ..AnimationProps::default() |
| }; |
| let out = gen_anim_attrs(&anim); |
| assert!(out.contains("data-vb-anim=\"fade_in\"")); |
| assert!(out.contains("data-vb-anim-hover=\"phong_to:200\"")); |
| } |
|
|
| #[test] |
| fn test_gen_anim_attrs_empty_without_hieu_ung() { |
| let anim = AnimationProps::default(); |
| assert_eq!(gen_anim_attrs(&anim), ""); |
| } |
|
|
| #[test] |
| fn test_gen_anim_attrs_full() { |
| let anim = AnimationProps { |
| hieu_ung: Some("fade_in".to_string()), |
| thoi_gian: Some(500), |
| tre: Some(100), |
| lap: Some(LapValue::MaiMai), |
| hieu_ung_hover: None, |
| hieu_ung_cuon: None, |
| }; |
| let out = gen_anim_attrs(&anim); |
| assert!(out.contains("data-vb-anim=\"fade_in\"")); |
| assert!(out.contains("data-vb-anim-duration=\"500\"")); |
| assert!(out.contains("data-vb-anim-delay=\"100\"")); |
| assert!(out.contains("data-vb-anim-repeat=\"infinite\"")); |
| } |
|
|
| #[test] |
| fn test_make_class_name() { |
| assert_eq!(make_class_name("box", 3), "vb-box-3"); |
| } |
| } |
|
|