diff options
-rw-r--r-- | src/emitter.rs | 11 | ||||
-rw-r--r-- | src/machine.rs | 9 | ||||
-rw-r--r-- | src/spans.rs | 8 | ||||
-rw-r--r-- | tests/span-tests/demo.html | 2 | ||||
-rw-r--r-- | tests/span-tests/demo.out | 18 |
5 files changed, 36 insertions, 12 deletions
diff --git a/src/emitter.rs b/src/emitter.rs index d37c8f8..1ab01f0 100644 --- a/src/emitter.rs +++ b/src/emitter.rs @@ -66,7 +66,7 @@ pub trait Emitter<R> { /// Emit the _current token_, assuming it is a tag. /// /// Also get the current attribute and append it to the to-be-emitted tag. See docstring for - /// [`Emitter::init_attribute`] for how duplicates should be handled. + /// [`Emitter::init_attribute_name`] for how duplicates should be handled. /// /// If a start tag is emitted, update the _last start tag_. /// @@ -129,7 +129,12 @@ pub trait Emitter<R> { /// emitted. /// /// If the current token is no tag at all, this method may panic. - fn init_attribute(&mut self, reader: &R); + fn init_attribute_name(&mut self, reader: &R); + + /// Called before the first push_attribute_value call. + /// + /// If there is no current attribute, this method may panic. + fn init_attribute_value(&mut self, #[allow(unused_variables)] reader: &R) {} /// Append a string to the current attribute's name. /// @@ -369,7 +374,7 @@ impl<R> Emitter<R> for DefaultEmitter<R, ()> { })); } - fn init_attribute(&mut self, _reader: &R) { + fn init_attribute_name(&mut self, _reader: &R) { self.flush_current_attribute(); self.current_attribute = Some((String::new(), String::new())); } diff --git a/src/machine.rs b/src/machine.rs index 931abf1..80b37d6 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -694,13 +694,13 @@ pub fn consume<R: Reader, E: Emitter<R>>( Some('=') => { slf.emitter .emit_error(Error::UnexpectedEqualsSignBeforeAttributeName); - slf.emitter.init_attribute(&slf.reader); + slf.emitter.init_attribute_name(&slf.reader); slf.emitter.push_attribute_name("="); slf.state = State::AttributeName; Ok(ControlToken::Continue) } Some(x) => { - slf.emitter.init_attribute(&slf.reader); + slf.emitter.init_attribute_name(&slf.reader); slf.state = State::AttributeName; slf.unread_char(Some(x)); Ok(ControlToken::Continue) @@ -754,7 +754,7 @@ pub fn consume<R: Reader, E: Emitter<R>>( Ok(ControlToken::Eof) } Some(x) => { - slf.emitter.init_attribute(&slf.reader); + slf.emitter.init_attribute_name(&slf.reader); slf.state = State::AttributeName; slf.unread_char(Some(x)); Ok(ControlToken::Continue) @@ -763,10 +763,12 @@ pub fn consume<R: Reader, E: Emitter<R>>( State::BeforeAttributeValue => match slf.read_char()? { Some(whitespace_pat!()) => Ok(ControlToken::Continue), Some('"') => { + slf.emitter.init_attribute_value(&slf.reader); slf.state = State::AttributeValueDoubleQuoted; Ok(ControlToken::Continue) } Some('\'') => { + slf.emitter.init_attribute_value(&slf.reader); slf.state = State::AttributeValueSingleQuoted; Ok(ControlToken::Continue) } @@ -777,6 +779,7 @@ pub fn consume<R: Reader, E: Emitter<R>>( Ok(ControlToken::Continue) } c => { + slf.emitter.init_attribute_value(&slf.reader); slf.state = State::AttributeValueUnquoted; slf.unread_char(c); Ok(ControlToken::Continue) diff --git a/src/spans.rs b/src/spans.rs index 300d659..ab455f9 100644 --- a/src/spans.rs +++ b/src/spans.rs @@ -262,7 +262,7 @@ impl<R: GetPos> Emitter<R> for SpanEmitter<R> { })); } - fn init_attribute(&mut self, reader: &R) { + fn init_attribute_name(&mut self, reader: &R) { self.flush_current_attribute(); self.current_attribute = Some(( String::new(), @@ -272,6 +272,12 @@ impl<R: GetPos> Emitter<R> for SpanEmitter<R> { }, )); } + + fn init_attribute_value(&mut self, reader: &R) { + let current_attr = self.current_attribute.as_mut().unwrap(); + current_attr.1.value_span = reader.get_pos() - 1..reader.get_pos() - 1; + } + fn push_attribute_name(&mut self, s: &str) { let current_attr = self.current_attribute.as_mut().unwrap(); current_attr.0.push_str(s); diff --git a/tests/span-tests/demo.html b/tests/span-tests/demo.html index 53bbab2..c635846 100644 --- a/tests/span-tests/demo.html +++ b/tests/span-tests/demo.html @@ -1,3 +1,5 @@ this is a tag: <h1>test</h1> tags can have attributes: <div id = foobar> + +Attribute values can be quoted: <input name = 'age' type = "number"> diff --git a/tests/span-tests/demo.out b/tests/span-tests/demo.out index 2b69ce5..156247e 100644 --- a/tests/span-tests/demo.out +++ b/tests/span-tests/demo.out @@ -2,10 +2,18 @@ note: ┌─ test.html:1:1 │ 1 │ this is a tag: <h1>test</h1> - │ ^^^^^^ ^^ ^^ end tag - │ │ │ - │ │ start tag - │ attr value + │ ^^ ^^ end tag + │ │ + │ start tag 2 │ 3 │ tags can have attributes: <div id = foobar> - │ ^^ attr name + │ ^^ ^^^^^^ attr value + │ │ + │ attr name +4 │ +5 │ Attribute values can be quoted: <input name = 'age' type = "number"> + │ ^^^^ ^^^ ^^^^ ^^^^^^ attr value + │ │ │ │ + │ │ │ attr name + │ │ attr value + │ attr name |