diff options
author | Martin Fischer <martin@push-f.com> | 2021-11-30 18:39:22 +0100 |
---|---|---|
committer | Martin Fischer <martin@push-f.com> | 2021-12-05 02:52:36 +0100 |
commit | fff865a98aa288930d6e363f03ad57570e429e92 (patch) | |
tree | a0cf87f5cb3eb7d285ea4085a3d322a655b27758 /src | |
parent | 76408590349f7f132c1dfeb9db3fb1dea964227c (diff) |
spans: support attribute values
Diffstat (limited to 'src')
-rw-r--r-- | src/emitter.rs | 11 | ||||
-rw-r--r-- | src/machine.rs | 9 | ||||
-rw-r--r-- | src/spans.rs | 8 |
3 files changed, 21 insertions, 7 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); |