Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(template-compiler): comments should be allowed between if branches #156

Merged
merged 1 commit into from
May 22, 2024
Merged
Show file tree
Hide file tree
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
82 changes: 60 additions & 22 deletions glass-easel-template-compiler/src/parse/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2004,6 +2004,32 @@ impl Element {
};

// wrap if condition
let find_if_element_index = |ret: &Vec<Node>| {
let mut if_index = None;
for (i, elem) in ret.iter().enumerate().rev() {
match elem {
Node::Element(Element {
kind: ElementKind::If { .. },
..
}) => {
if_index = Some(i);
break;
}
Node::Comment(..) => {}
_ => break,
}
}
if_index
};
let wrap_if_children =
|ret: &mut Vec<Node>, if_index: usize, wrapped_element: Element| {
let mut children = wrap_children(wrapped_element);
{
let comments = ret.drain((if_index + 1)..);
children.splice(0..0, comments);
}
children
};
let wrapped_element = match if_condition {
IfCondition::None => Some(wrapped_element),
IfCondition::If(location, value) => {
Expand All @@ -2023,12 +2049,19 @@ impl Element {
Some(elem)
}
IfCondition::Elif(location, value) => {
if let Some(Node::Element(Element {
kind: ElementKind::If { branches, .. },
..
})) = ret.last_mut()
{
let branch = (location, value, wrap_children(wrapped_element));
if let Some(if_index) = find_if_element_index(ret) {
let branch = (
location,
value,
wrap_if_children(ret, if_index, wrapped_element),
);
let Node::Element(Element {
kind: ElementKind::If { branches, .. },
..
}) = &mut ret[if_index]
else {
unreachable!();
};
branches.push(branch);
None
} else {
Expand All @@ -2037,12 +2070,15 @@ impl Element {
}
}
IfCondition::Else(location) => {
if let Some(Node::Element(Element {
kind: ElementKind::If { else_branch, .. },
..
})) = ret.last_mut()
{
let branch = (location, wrap_children(wrapped_element));
if let Some(if_index) = find_if_element_index(ret) {
let branch = (location, wrap_if_children(ret, if_index, wrapped_element));
let Node::Element(Element {
kind: ElementKind::If { else_branch, .. },
..
}) = &mut ret[if_index]
else {
unreachable!();
};
*else_branch = Some(branch);
None
} else {
Expand Down Expand Up @@ -2694,10 +2730,7 @@ impl Value {
return None;
};
if let Some(range) = ps.consume_str("}}") {
ps.add_warning(
ParseErrorKind::EmptyExpression,
range.start..range.start,
);
ps.add_warning(ParseErrorKind::EmptyExpression, range.start..range.start);
return Some(Self::Static {
value: CompactString::new_inline(""),
location: double_brace_left.start..range.end,
Expand Down Expand Up @@ -2966,12 +2999,7 @@ mod test {

#[test]
fn value_parsing() {
case!(
"{{}}",
r#""#,
ParseErrorKind::EmptyExpression,
2..2
);
case!("{{}}", r#""#, ParseErrorKind::EmptyExpression, 2..2);
case!("{ {", r#"{ {"#);
case!("{{ a } }", "", ParseErrorKind::MissingExpressionEnd, 0..2);
case!(
Expand Down Expand Up @@ -3102,6 +3130,12 @@ mod test {
ParseErrorKind::InvalidAttributeName,
5..7
);
case!(
"<div a='1' a='2'></div>",
r#"<div a="1"/>"#,
ParseErrorKind::DuplicatedAttribute,
11..12
);
case!(
"<div a a></div>",
r#"<div a/>"#,
Expand Down Expand Up @@ -3313,6 +3347,10 @@ mod test {
"<block wx:if=''/><include src='a' wx:else />",
r#"<block wx:if/><block wx:else><include src="a"/></block>"#
);
case!(
"<block wx:if='{{a}}'/> <!--abc--> <block wx:elif /><!----><block wx:else />",
r#"<block wx:if="{{a}}"/><block wx:elif><!--abc--></block><block wx:else><!----></block>"#
);
}

#[test]
Expand Down
6 changes: 5 additions & 1 deletion glass-easel-template-compiler/src/stringify/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ impl Stringify for Node {
match self {
Node::Text(value) => value.stringify_write(stringifier)?,
Node::Element(element) => element.stringify_write(stringifier)?,
Node::Comment(..) => {}
Node::Comment(s, location) => {
stringifier.write_str(r#"<!--"#)?;
stringifier.write_token(&s.replace("-->", "-- >"), &s, &location)?;
stringifier.write_str(r#"-->"#)?;
}
Node::UnknownMetaTag(s, location) => {
stringifier.write_str(r#"<!"#)?;
stringifier.write_token(&escape_html_text(&s), &s, &location)?;
Expand Down
Loading