From 1f99ea9e16f85945e2606905ed6345519ce16e4e Mon Sep 17 00:00:00 2001 From: Martin Fischer Date: Tue, 30 Nov 2021 17:09:08 +0100 Subject: spans: make Emitter generic over Span --- src/emitter.rs | 32 +++++++++++++++++++------------- src/tokenizer.rs | 2 +- 2 files changed, 20 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/emitter.rs b/src/emitter.rs index 0a80544..2c4ba41 100644 --- a/src/emitter.rs +++ b/src/emitter.rs @@ -173,17 +173,17 @@ pub trait Emitter { /// The default implementation of [`crate::Emitter`], used to produce ("emit") tokens. #[derive(Default)] -pub struct DefaultEmitter { +pub struct DefaultEmitter { current_characters: String, - current_token: Option, + current_token: Option>, last_start_tag: String, current_attribute: Option<(String, String)>, seen_attributes: BTreeSet, - emitted_tokens: VecDeque, + emitted_tokens: VecDeque>, } -impl DefaultEmitter { - fn emit_token(&mut self, token: Token) { +impl DefaultEmitter<()> { + fn emit_token(&mut self, token: Token<()>) { self.flush_current_characters(); self.emitted_tokens.push_front(token); } @@ -226,8 +226,8 @@ impl DefaultEmitter { } } -impl Emitter for DefaultEmitter { - type Token = Token; +impl Emitter for DefaultEmitter<()> { + type Token = Token<()>; fn set_last_start_tag(&mut self, last_start_tag: Option<&str>) { self.last_start_tag.clear(); @@ -417,7 +417,7 @@ impl Emitter for DefaultEmitter { /// A HTML end/close tag, such as `

` or ``. #[derive(Debug, Default, Eq, PartialEq)] -pub struct StartTag { +pub struct StartTag { /// Whether this tag is self-closing. If it is self-closing, no following [`EndTag`] should be /// expected. pub self_closing: bool, @@ -430,9 +430,12 @@ pub struct StartTag { /// Duplicate attributes are ignored after the first one as per WHATWG spec. Implement your own /// [`Emitter`] to tweak this behavior. pub attributes: BTreeMap, + + /// The source code span of the tag name. + pub name_span: S, } -impl StartTag { +impl StartTag { /// Returns the next tokenizer state according to /// [Parsing HTML fragments](https://html.spec.whatwg.org/multipage/parsing.html#concept-frag-parse-context). /// If `scripting` is set to true [`State::RawText`] is returned if this is a `

` or ``. #[derive(Debug, Default, Eq, PartialEq)] -pub struct EndTag { +pub struct EndTag { /// The ending tag's name, such as `"p"` or `"a"`. pub name: String, + + /// The source code span of the tag name. + pub name_span: S, } /// A doctype. Some examples: @@ -480,11 +486,11 @@ pub struct Doctype { /// The token type used by default. You can define your own token type by implementing the /// [`crate::Emitter`] trait and using [`crate::Tokenizer::new_with_emitter`]. #[derive(Debug, Eq, PartialEq)] -pub enum Token { +pub enum Token { /// A HTML start tag. - StartTag(StartTag), + StartTag(StartTag), /// A HTML end tag. - EndTag(EndTag), + EndTag(EndTag), /// A literal string. String(String), /// A HTML comment. diff --git a/src/tokenizer.rs b/src/tokenizer.rs index b5a2edf..377dd01 100644 --- a/src/tokenizer.rs +++ b/src/tokenizer.rs @@ -33,7 +33,7 @@ impl Stack2 { } /// A HTML tokenizer. See crate-level docs for basic usage. -pub struct Tokenizer { +pub struct Tokenizer> { eof: bool, pub(crate) state: InternalState, pub(crate) emitter: E, -- cgit v1.2.3