diff options
author | Martin Fischer <martin@push-f.com> | 2021-11-30 18:10:53 +0100 |
---|---|---|
committer | Martin Fischer <martin@push-f.com> | 2021-12-05 02:52:36 +0100 |
commit | c29558e8d165df1d3fc1cb32079da9e40f957dc6 (patch) | |
tree | 57708a17596e021aadef0ccd3e5d9de1e7008aa8 /src | |
parent | cb8c8b260dc95317b16efddd921e7de2cae57245 (diff) |
spans: add span tests
Diffstat (limited to 'src')
-rw-r--r-- | src/lib.rs | 2 | ||||
-rw-r--r-- | src/spans.rs | 21 |
2 files changed, 16 insertions, 7 deletions
@@ -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<usize>; +/// 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<R> { - reader: R, - position: usize, +/// Wraps a [`Reader`] so that it implements [`GetPos`]. +pub struct PosTracker<R> { + /// The wrapped reader. + pub reader: R, + /// The current position. + pub position: usize, } impl<R> GetPos for PosTracker<R> { @@ -145,12 +151,15 @@ impl<R: GetPos> Emitter<R> for SpanEmitter<R> { 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(); } |