summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMartin Fischer <martin@push-f.com>2021-11-30 18:10:53 +0100
committerMartin Fischer <martin@push-f.com>2021-12-05 02:52:36 +0100
commitc29558e8d165df1d3fc1cb32079da9e40f957dc6 (patch)
tree57708a17596e021aadef0ccd3e5d9de1e7008aa8 /src
parentcb8c8b260dc95317b16efddd921e7de2cae57245 (diff)
spans: add span tests
Diffstat (limited to 'src')
-rw-r--r--src/lib.rs2
-rw-r--r--src/spans.rs21
2 files changed, 16 insertions, 7 deletions
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<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();
}