diff options
Diffstat (limited to 'src/default_emitter.rs')
-rw-r--r-- | src/default_emitter.rs | 254 |
1 files changed, 131 insertions, 123 deletions
diff --git a/src/default_emitter.rs b/src/default_emitter.rs index c9a390a..e975392 100644 --- a/src/default_emitter.rs +++ b/src/default_emitter.rs @@ -52,14 +52,14 @@ impl<O: Offset> Emitter<O> for DefaultEmitter<O> { self.errors.push_back((error, span)); } - fn emit_eof(&mut self) { - self.emit_token(Token::EndOfFile); - } - fn emit_char(&mut self, c: char) { self.emit_token(Token::Char(c)); } + fn emit_eof(&mut self) { + self.emit_token(Token::EndOfFile); + } + fn init_start_tag(&mut self, tag_offset: O, name_offset: O) { self.current_token = Some(Token::StartTag(StartTag { span: tag_offset..O::default(), @@ -69,6 +69,7 @@ impl<O: Offset> Emitter<O> for DefaultEmitter<O> { name_span: name_offset..O::default(), })); } + fn init_end_tag(&mut self, tag_offset: O, name_offset: O) { self.current_token = Some(Token::EndTag(EndTag { span: tag_offset..O::default(), @@ -78,48 +79,71 @@ impl<O: Offset> Emitter<O> for DefaultEmitter<O> { self.seen_attributes.clear(); } - fn init_comment(&mut self, data_start_offset: O) { - self.current_token = Some(Token::Comment(Comment { - data: String::new(), - data_span: data_start_offset..O::default(), - })); - } - fn emit_current_tag(&mut self, offset: O) { - self.flush_current_attribute(); - let mut token = self.current_token.take().unwrap(); - match &mut token { - Token::EndTag(tag) => { - if !self.seen_attributes.is_empty() { - let span = self.attr_in_end_tag_span.take().unwrap(); - self.report_error(Error::EndTagWithAttributes, span); - } - self.seen_attributes.clear(); - tag.span.end = offset; + fn push_tag_name(&mut self, s: &str) { + match self.current_token { + Some(Token::StartTag(StartTag { ref mut name, .. })) => { + name.push_str(s); } - Token::StartTag(tag) => { - tag.span.end = offset; + Some(Token::EndTag(EndTag { ref mut name, .. })) => { + name.push_str(s); } _ => debug_assert!(false), } - self.emit_token(token); } - fn emit_current_comment(&mut self, data_end_offset: O) { - let mut token = self.current_token.take().unwrap(); - if let Token::Comment(comment) = &mut token { - comment.data_span.end = data_end_offset; - } else { - debug_assert!(false); + + fn terminate_tag_name(&mut self, offset: O) { + match self.current_token { + Some(Token::StartTag(StartTag { + ref mut name_span, .. + })) => { + name_span.end = offset; + } + Some(Token::EndTag(EndTag { + ref mut name_span, .. + })) => { + name_span.end = offset; + } + _ => debug_assert!(false), } - self.emit_token(token); } - fn emit_current_doctype(&mut self, offset: O) { - let Some(Token::Doctype(mut doctype)) = self.current_token.take() else { - debug_assert!(false); - return; - }; - doctype.span.end = offset; - self.emit_token(Token::Doctype(doctype)); + fn init_attribute_name(&mut self, offset: O) { + self.flush_current_attribute(); + self.current_attribute = Some(( + String::new(), + crate::token::AttrInternal { + name_span: offset..O::default(), + value: String::new(), + value_span: O::default()..O::default(), + value_syntax: None, + }, + )); + } + + fn push_attribute_name(&mut self, s: &str) { + let current_attr = self.current_attribute.as_mut().unwrap(); + current_attr.0.push_str(s); + } + + fn terminate_attribute_name(&mut self, offset: O) { + let current_attr = self.current_attribute.as_mut().unwrap(); + current_attr.1.name_span.end = offset; + } + + fn init_attribute_value(&mut self, syntax: AttrValueSyntax, offset: O) { + let (_, current_attribute) = self.current_attribute.as_mut().unwrap(); + current_attribute.value_span.start = offset; + current_attribute.value_syntax = Some(syntax); + } + + fn push_attribute_value(&mut self, s: &str) { + let current_attr = self.current_attribute.as_mut().unwrap(); + current_attr.1.value.push_str(s); + } + + fn terminate_attribute_value(&mut self, offset: O) { + let current_attr = self.current_attribute.as_mut().unwrap(); + current_attr.1.value_span.end = offset; } fn set_self_closing(&mut self, slash_span: Range<O>) { @@ -139,38 +163,32 @@ impl<O: Offset> Emitter<O> for DefaultEmitter<O> { } } } - fn set_force_quirks(&mut self) { - match self.current_token { - Some(Token::Doctype(ref mut doctype)) => doctype.force_quirks = true, - _ => debug_assert!(false), - } - } - fn push_tag_name(&mut self, s: &str) { - match self.current_token { - Some(Token::StartTag(StartTag { ref mut name, .. })) => { - name.push_str(s); + + fn emit_current_tag(&mut self, offset: O) { + self.flush_current_attribute(); + let mut token = self.current_token.take().unwrap(); + match &mut token { + Token::EndTag(tag) => { + if !self.seen_attributes.is_empty() { + let span = self.attr_in_end_tag_span.take().unwrap(); + self.report_error(Error::EndTagWithAttributes, span); + } + self.seen_attributes.clear(); + tag.span.end = offset; } - Some(Token::EndTag(EndTag { ref mut name, .. })) => { - name.push_str(s); + Token::StartTag(tag) => { + tag.span.end = offset; } _ => debug_assert!(false), } + self.emit_token(token); } - fn terminate_tag_name(&mut self, offset: O) { - match self.current_token { - Some(Token::StartTag(StartTag { - ref mut name_span, .. - })) => { - name_span.end = offset; - } - Some(Token::EndTag(EndTag { - ref mut name_span, .. - })) => { - name_span.end = offset; - } - _ => debug_assert!(false), - } + fn init_comment(&mut self, data_start_offset: O) { + self.current_token = Some(Token::Comment(Comment { + data: String::new(), + data_span: data_start_offset..O::default(), + })); } fn push_comment(&mut self, s: &str) { @@ -180,6 +198,29 @@ impl<O: Offset> Emitter<O> for DefaultEmitter<O> { } } + fn emit_current_comment(&mut self, data_end_offset: O) { + let mut token = self.current_token.take().unwrap(); + if let Token::Comment(comment) = &mut token { + comment.data_span.end = data_end_offset; + } else { + debug_assert!(false); + } + self.emit_token(token); + } + + fn init_doctype(&mut self, offset: O) { + self.current_token = Some(Token::Doctype(Doctype { + name: None, + force_quirks: false, + public_id: None, + system_id: None, + span: offset..O::default(), + name_span: O::default()..O::default(), + public_id_span: O::default()..O::default(), + system_id_span: O::default()..O::default(), + })); + } + fn init_doctype_name(&mut self, offset: O) { let Some(Token::Doctype(doctype)) = &mut self.current_token else { debug_assert!(false); @@ -207,57 +248,6 @@ impl<O: Offset> Emitter<O> for DefaultEmitter<O> { doctype.name_span.end = offset; } - fn init_doctype(&mut self, offset: O) { - self.current_token = Some(Token::Doctype(Doctype { - name: None, - force_quirks: false, - public_id: None, - system_id: None, - span: offset..O::default(), - name_span: O::default()..O::default(), - public_id_span: O::default()..O::default(), - system_id_span: O::default()..O::default(), - })); - } - - fn init_attribute_name(&mut self, offset: O) { - self.flush_current_attribute(); - self.current_attribute = Some(( - String::new(), - crate::token::AttrInternal { - name_span: offset..O::default(), - value: String::new(), - value_span: O::default()..O::default(), - value_syntax: None, - }, - )); - } - fn init_attribute_value(&mut self, syntax: AttrValueSyntax, offset: O) { - let (_, current_attribute) = self.current_attribute.as_mut().unwrap(); - current_attribute.value_span.start = offset; - current_attribute.value_syntax = Some(syntax); - } - - fn push_attribute_name(&mut self, s: &str) { - let current_attr = self.current_attribute.as_mut().unwrap(); - current_attr.0.push_str(s); - } - - fn terminate_attribute_name(&mut self, offset: O) { - let current_attr = self.current_attribute.as_mut().unwrap(); - current_attr.1.name_span.end = offset; - } - - fn push_attribute_value(&mut self, s: &str) { - let current_attr = self.current_attribute.as_mut().unwrap(); - current_attr.1.value.push_str(s); - } - - fn terminate_attribute_value(&mut self, offset: O) { - let current_attr = self.current_attribute.as_mut().unwrap(); - current_attr.1.value_span.end = offset; - } - fn init_doctype_public_id(&mut self, offset: O) { let Some(Token::Doctype(doctype)) = &mut self.current_token else { debug_assert!(false); @@ -266,14 +256,7 @@ impl<O: Offset> Emitter<O> for DefaultEmitter<O> { doctype.public_id = Some("".to_owned()); doctype.public_id_span.start = offset; } - fn init_doctype_system_id(&mut self, offset: O) { - let Some(Token::Doctype(doctype)) = &mut self.current_token else { - debug_assert!(false); - return; - }; - doctype.system_id = Some("".to_owned()); - doctype.system_id_span.start = offset; - } + fn push_doctype_public_id(&mut self, s: &str) { if let Some(Token::Doctype(Doctype { public_id: Some(ref mut id), @@ -298,6 +281,15 @@ impl<O: Offset> Emitter<O> for DefaultEmitter<O> { } } + fn init_doctype_system_id(&mut self, offset: O) { + let Some(Token::Doctype(doctype)) = &mut self.current_token else { + debug_assert!(false); + return; + }; + doctype.system_id = Some("".to_owned()); + doctype.system_id_span.start = offset; + } + fn push_doctype_system_id(&mut self, s: &str) { if let Some(Token::Doctype(Doctype { system_id: Some(ref mut id), @@ -321,6 +313,22 @@ impl<O: Offset> Emitter<O> for DefaultEmitter<O> { debug_assert!(false); } } + + fn set_force_quirks(&mut self) { + match self.current_token { + Some(Token::Doctype(ref mut doctype)) => doctype.force_quirks = true, + _ => debug_assert!(false), + } + } + + fn emit_current_doctype(&mut self, offset: O) { + let Some(Token::Doctype(mut doctype)) = self.current_token.take() else { + debug_assert!(false); + return; + }; + doctype.span.end = offset; + self.emit_token(Token::Doctype(doctype)); + } } impl<O> DefaultEmitter<O> { |