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

omit tag escape pattern from resulting RawString element output #170

Merged
merged 3 commits into from
Aug 1, 2017
Merged
Changes from 2 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
45 changes: 41 additions & 4 deletions src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@ impl Template {
}
}

fn unescape_tags<T>(txt: T) -> String
where
T: Into<String>,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just use &str here to avoid creating a String just for replace?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. I didn't see str had that impl. I'll update and push that change tonight

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

{
txt.into().replace("\\{{", "{{")
}

fn push_element(&mut self, e: TemplateElement, line: usize, col: usize) {
self.elements.push(e);
if let Some(ref mut maps) = self.mapping {
Expand Down Expand Up @@ -379,12 +386,20 @@ impl Template {
if token.rule == Rule::raw_block_end {
let text = &source[prev_end..token.start];
let mut t = Template::new(mapping);
t.push_element(RawString(text.to_owned()), line_no, col_no);
t.push_element(
RawString(Template::unescape_tags(text)),
line_no,
col_no,
);
template_stack.push_front(t);
} else {
let text = &source[prev_end..token.start];
let mut t = template_stack.front_mut().unwrap();
t.push_element(RawString(text.to_owned()), line_no, col_no);
t.push_element(
RawString(Template::unescape_tags(text)),
line_no,
col_no,
);
}
}
}
Expand All @@ -400,7 +415,7 @@ impl Template {
text = text.trim_left();
}
let mut t = template_stack.front_mut().unwrap();
t.push_element(RawString(text.to_owned()), line_no, col_no);
t.push_element(RawString(Template::unescape_tags(text)), line_no, col_no);
}
Rule::helper_block_start |
Rule::raw_block_start |
Expand Down Expand Up @@ -465,7 +480,7 @@ impl Template {
text = text.trim_left();
}
let mut t = Template::new(mapping);
t.push_element(RawString(text.to_owned()), line_no, col_no);
t.push_element(RawString(Template::unescape_tags(text)), line_no, col_no);
template_stack.push_front(t);
}
Rule::expression |
Expand Down Expand Up @@ -628,6 +643,28 @@ pub enum TemplateElement {
Comment(String),
}

#[test]
fn test_parse_escaped_tag_raw_string() {
let source = "foo \\{{bar}}";
let t = Template::compile(source.to_string()).ok().unwrap();
assert_eq!(t.elements.len(), 1);
assert_eq!(
*t.elements.get(0).unwrap(),
RawString("foo {{bar}}".to_string())
);
}

#[test]
fn test_parse_escaped_block_raw_string() {
let source = "\\{{{{foo}}}} bar";
let t = Template::compile(source.to_string()).ok().unwrap();
assert_eq!(t.elements.len(), 1);
assert_eq!(
*t.elements.get(0).unwrap(),
RawString("{{{{foo}}}} bar".to_string())
);
}

#[test]
fn test_parse_template() {
let source = "<h1>{{title}} 你好</h1> {{{content}}}
Expand Down