Skip to content

Commit

Permalink
fix: retrieve component closing element
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcAntoine-Arnaud committed Aug 26, 2023
1 parent 453fe4d commit d4656ff
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/html_element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub(crate) struct HtmlPartWithLine {
pub(crate) struct Element {
pub(crate) name: String,
pub(crate) self_closing: bool,
pub(crate) is_component: bool,
pub(crate) open_attrs: Vec<(String, String)>,
pub(crate) close_attrs: Vec<(String, String)>,
pub(crate) children: Vec<HtmlPartWithLine>,
Expand Down
37 changes: 29 additions & 8 deletions src/sink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,51 @@ impl<'a> TokenSink for HtmlSink<'a> {
let mut element = Element {
name: tag.name.to_string(),
self_closing: tag.self_closing,
is_component: false,
open_attrs: tag.attrs.into_iter().map(|a| (a.name.local.to_string(), a.value.to_string())).collect(),
close_attrs: Vec::new(),
children: Vec::new(),
};

if element.name == "comp" || element.name == "component" {
let Some(real_name) = element.open_attrs.iter().find(|(k, _)| k == "name").map(|(_, v)| v) else {
abort!(self.args.path_span, "Missing name attribute on component tag at line {line_number}");
};

element.name = real_name.to_owned();
element.is_component = true;
element.open_attrs.retain(|(k, _)| k != "name");
}
match element.self_closing {
true => match self.opened_elements.last_mut() {

if element.self_closing {
match self.opened_elements.last_mut() {
Some(container) => container.children.push(HtmlPartWithLine { part: HtmlPart::Element(element), line: line_number as usize }),
None => self.html_parts.push(HtmlPartWithLine { part: HtmlPart::Element(element), line: line_number as usize }),
},
false => self.opened_elements.push(element)
}
} else {
self.opened_elements.push(element)
}
},
TagKind::EndTag => {
let mut element = self.opened_elements.pop().unwrap_or_else(|| abort!(self.args.path_span, "Unexpected closing tag {} at line {line_number}", tag.name));
if tag.name != element.name {
abort!(self.args.path_span, "Unexpected closing tag {} at line {line_number}", tag.name);

if element.is_component {
if !["comp", "component"].contains(&tag.name.to_string().as_str()) {
abort!(self.args.path_span, "Unexpected closing tag {} at line {line_number}", tag.name);
}
} else {
if tag.name != element.name {
abort!(self.args.path_span, "Unexpected closing tag {} at line {line_number}", tag.name);
}
}

element.close_attrs = tag.attrs.into_iter().map(|a| (a.name.local.to_string(), a.value.to_string())).collect();

let html_part = HtmlPartWithLine { part: HtmlPart::Element(element), line: line_number as usize };

match self.opened_elements.last_mut() {
Some(container) => container.children.push(HtmlPartWithLine { part: HtmlPart::Element(element), line: line_number as usize }),
None => self.html_parts.push(HtmlPartWithLine { part: HtmlPart::Element(element), line: line_number as usize }),
Some(container) => container.children.push(html_part),
None => self.html_parts.push(html_part),
}
},
},
Expand All @@ -64,20 +81,24 @@ pub(crate) fn read_template(args: &Args) -> Element {
Ok(template) => template,
Err(e) => abort!(args.path_span, "Failed to read template file at {}: {}", args.path, e),
};

let mut html_parts = Vec::new();
let html_sink = HtmlSink { html_parts: &mut html_parts, opened_elements: Vec::new(), args };
let mut html_tokenizer = Tokenizer::new(html_sink, TokenizerOpts::default());
let mut buffer_queue = BufferQueue::new();
buffer_queue.push_back(template.into());
let _ = html_tokenizer.feed(&mut buffer_queue);
html_tokenizer.end();

let mut root = Element {
name: "".to_string(),
open_attrs: Vec::new(),
close_attrs: Vec::new(),
self_closing: false,
is_component: false,
children: html_parts,
};

root.clean_text();
root
}

0 comments on commit d4656ff

Please sign in to comment.