From c29558e8d165df1d3fc1cb32079da9e40f957dc6 Mon Sep 17 00:00:00 2001 From: Martin Fischer Date: Tue, 30 Nov 2021 18:10:53 +0100 Subject: spans: add span tests --- src/lib.rs | 2 +- src/spans.rs | 21 +++++++++++++++------ 2 files changed, 16 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index 40f9588..4b74d61 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,7 +9,7 @@ mod error; mod machine; mod never; mod reader; -mod spans; +pub mod spans; mod tokenizer; mod utils; diff --git a/src/spans.rs b/src/spans.rs index 9d02940..72b30c0 100644 --- a/src/spans.rs +++ b/src/spans.rs @@ -1,3 +1,4 @@ +//! Source code spans. use std::{ collections::{BTreeSet, VecDeque}, marker::PhantomData, @@ -8,13 +9,18 @@ use crate::{Doctype, Emitter, EndTag, Error, Reader, StartTag, Token}; type Span = std::ops::Range; +/// A trait to be implemented by readers that track their own position. pub trait GetPos { + /// Returns the byte index of the current position. fn get_pos(&self) -> usize; } -struct PosTracker { - reader: R, - position: usize, +/// Wraps a [`Reader`] so that it implements [`GetPos`]. +pub struct PosTracker { + /// The wrapped reader. + pub reader: R, + /// The current position. + pub position: usize, } impl GetPos for PosTracker { @@ -145,12 +151,15 @@ impl Emitter for SpanEmitter { fn init_start_tag(&mut self, reader: &R) { self.current_token = Some(Token::StartTag(StartTag { - name_span: reader.get_pos()..reader.get_pos(), + name_span: reader.get_pos() - 1..reader.get_pos() - 1, ..Default::default() })); } - fn init_end_tag(&mut self, _reader: &R) { - self.current_token = Some(Token::EndTag(Default::default())); + fn init_end_tag(&mut self, reader: &R) { + self.current_token = Some(Token::EndTag(EndTag { + name_span: reader.get_pos() - 1..reader.get_pos() - 1, + ..Default::default() + })); self.seen_attributes.clear(); } -- cgit v1.2.3