diff options
author | Martin Fischer <martin@push-f.com> | 2023-08-17 15:21:32 +0200 |
---|---|---|
committer | Martin Fischer <martin@push-f.com> | 2023-08-19 11:41:55 +0200 |
commit | 4d9cf7171836625b61dcfe675bdf9452766166c0 (patch) | |
tree | 90bc623a08b945439b6a5077bdfdf54de7eacc37 /src | |
parent | c169e78f120ea9be451f337306b8bff6c1fb4955 (diff) |
feat!: add offset to comments
Diffstat (limited to 'src')
-rw-r--r-- | src/emitter.rs | 27 | ||||
-rw-r--r-- | src/lib.rs | 2 |
2 files changed, 24 insertions, 5 deletions
diff --git a/src/emitter.rs b/src/emitter.rs index b3fdb99..caf7b55 100644 --- a/src/emitter.rs +++ b/src/emitter.rs @@ -270,8 +270,11 @@ impl<O: Offset, R: Position<O>> Emitter<R> for DefaultEmitter<R, O> { self.seen_attributes.clear(); } - fn init_comment(&mut self, _reader: &R) { - self.current_token = Some(Token::Comment(String::new())); + fn init_comment(&mut self, reader: &R) { + self.current_token = Some(Token::Comment(Comment { + data: String::new(), + data_offset: reader.position(), + })); } fn emit_current_tag(&mut self) { self.flush_current_attribute(); @@ -348,7 +351,7 @@ impl<O: Offset, R: Position<O>> Emitter<R> for DefaultEmitter<R, O> { fn push_comment(&mut self, s: &str) { match self.current_token { - Some(Token::Comment(ref mut data)) => data.push_str(s), + Some(Token::Comment(Comment { ref mut data, .. })) => data.push_str(s), _ => debug_assert!(false), } } @@ -483,6 +486,22 @@ pub struct EndTag<O> { pub name_span: Range<O>, } +/// An HTML comment. +#[derive(PartialEq, Eq, Debug)] +pub struct Comment<O> { + /// The text within the comment. + pub data: String, + /// The source offset of the comment data. + pub data_offset: O, +} + +impl<O: Offset> Comment<O> { + /// Calculates the span for the comment data and returns it. + pub fn data_span(&self) -> Range<O> { + self.data_offset..self.data_offset + self.data.len() + } +} + /// A doctype. Some examples: /// /// * `<!DOCTYPE {name}>` @@ -515,7 +534,7 @@ pub enum Token<O> { /// A literal string. String(String), /// A HTML comment. - Comment(String), + Comment(Comment<O>), /// A HTML doctype declaration. Doctype(Doctype), /// A HTML parsing error. @@ -15,6 +15,6 @@ mod utils; #[cfg(feature = "integration-tests")] pub use utils::State as InternalState; -pub use emitter::{Attribute, DefaultEmitter, Doctype, Emitter, EndTag, StartTag, Token}; +pub use emitter::{Attribute, Comment, DefaultEmitter, Doctype, Emitter, EndTag, StartTag, Token}; pub use error::Error; pub use tokenizer::{State, Tokenizer}; |