aboutsummaryrefslogtreecommitdiff
path: root/src/emitter.rs
diff options
context:
space:
mode:
authorMartin Fischer <martin@push-f.com>2023-08-12 08:15:45 +0200
committerMartin Fischer <martin@push-f.com>2023-08-19 13:41:55 +0200
commitae5d8185a5b419f89d520504c1cb4c59c26879bf (patch)
tree0198c59440111154e2aaca1e8d4e8fb0809699c4 /src/emitter.rs
parent180f6d6111b966627aa00a4017b6fb9751f7386c (diff)
feat: make attribute value syntax recognizable
Note that while making this breaking change, we're also swapping the parameter order for more consistency so that the reader parameter always comes last in Emitter methods.
Diffstat (limited to 'src/emitter.rs')
-rw-r--r--src/emitter.rs56
1 files changed, 52 insertions, 4 deletions
diff --git a/src/emitter.rs b/src/emitter.rs
index 17a4882..63ef4b1 100644
--- a/src/emitter.rs
+++ b/src/emitter.rs
@@ -4,6 +4,7 @@ use std::collections::VecDeque;
use std::mem;
use std::ops::Range;
+use crate::attr::AttrValueSyntax;
use crate::offset::NoopOffset;
use crate::offset::Offset;
use crate::Error;
@@ -121,11 +122,10 @@ pub trait Emitter<O> {
fn init_attribute_name(&mut self, offset: O);
/// Called before the first push_attribute_value call.
- /// If the value is wrappend in double or single quotes `quoted` is set to true, otherwise false.
///
/// If there is no current attribute, this method may panic.
#[allow(unused_variables)]
- fn init_attribute_value(&mut self, offset: O, quoted: bool) {}
+ fn init_attribute_value(&mut self, syntax: AttrValueSyntax, offset: O) {}
/// Append a string to the current attribute's name.
///
@@ -385,11 +385,14 @@ impl<O: Offset> Emitter<O> for DefaultEmitter<O> {
name_offset: offset,
value: String::new(),
value_offset: O::default(),
+ value_syntax: None,
},
));
}
- fn init_attribute_value(&mut self, offset: O, _quoted: bool) {
- self.current_attribute.as_mut().unwrap().1.value_offset = offset;
+ fn init_attribute_value(&mut self, syntax: AttrValueSyntax, offset: O) {
+ let (_, current_attribute) = self.current_attribute.as_mut().unwrap();
+ current_attribute.value_offset = offset;
+ current_attribute.value_syntax = Some(syntax);
}
fn push_attribute_name(&mut self, s: &str) {
@@ -554,3 +557,48 @@ pub enum Token<O> {
span: Range<O>,
},
}
+
+/// The majority of our testing of the [`DefaultEmitter`] is done against the
+/// html5lib-tests in the html5lib integration test. This module only tests
+/// details that aren't present in the html5lib test data.
+#[cfg(test)]
+mod tests {
+ use super::{DefaultEmitter, Token};
+ use crate::{attr::AttrValueSyntax, Tokenizer};
+
+ #[test]
+ fn test_attribute_value_syntax() {
+ let mut tokenizer = Tokenizer::new(
+ "<div empty unquoted=foo single-quoted='foo' double-quoted=\"foo\">",
+ DefaultEmitter::default(),
+ )
+ .flatten();
+ let Token::StartTag(start_tag) = tokenizer.next().unwrap() else {
+ panic!("expected start tag");
+ };
+ assert_eq!(
+ start_tag.attributes.get("empty").unwrap().value_syntax(),
+ None
+ );
+ assert_eq!(
+ start_tag.attributes.get("unquoted").unwrap().value_syntax(),
+ Some(AttrValueSyntax::Unquoted)
+ );
+ assert_eq!(
+ start_tag
+ .attributes
+ .get("single-quoted")
+ .unwrap()
+ .value_syntax(),
+ Some(AttrValueSyntax::SingleQuoted)
+ );
+ assert_eq!(
+ start_tag
+ .attributes
+ .get("double-quoted")
+ .unwrap()
+ .value_syntax(),
+ Some(AttrValueSyntax::DoubleQuoted)
+ );
+ }
+}