From d56686deab81c8b50207b75a485cf26ec8502383 Mon Sep 17 00:00:00 2001 From: Martin Fischer Date: Sun, 3 Sep 2023 10:47:44 +0200 Subject: break!: make Doctype name field optional --- src/emitter.rs | 22 +++++++++++++++++++--- src/machine.rs | 2 ++ 2 files changed, 21 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/emitter.rs b/src/emitter.rs index db3da78..69baec2 100644 --- a/src/emitter.rs +++ b/src/emitter.rs @@ -153,6 +153,11 @@ pub trait Emitter { /// If there is no current attribute, this method may panic. fn push_attribute_value(&mut self, s: &str); + /// Assuming the _current token_ is a doctype, set its name to the empty string. + /// + /// If the current token is not a doctype, this method may panic. + fn init_doctype_name(&mut self, offset: O) {} + /// Assuming the _current token_ is a doctype, set its "public identifier" to the empty string. /// /// If the current token is not a doctype, this method may panic. @@ -411,15 +416,26 @@ impl Emitter for DefaultEmitter { } } + fn init_doctype_name(&mut self, offset: O) { + let Some(Token::Doctype(doctype)) = &mut self.current_token else { + debug_assert!(false); + return; + }; + doctype.name = Some("".into()); + } + fn push_doctype_name(&mut self, s: &str) { match self.current_token { - Some(Token::Doctype(ref mut doctype)) => doctype.name.push_str(s), + Some(Token::Doctype(Doctype { + name: Some(ref mut name), + .. + })) => name.push_str(s), _ => debug_assert!(false), } } fn init_doctype(&mut self, offset: O) { self.current_token = Some(Token::Doctype(Doctype { - name: String::new(), + name: None, force_quirks: false, public_id: None, system_id: None, @@ -601,7 +617,7 @@ pub struct Doctype { /// The doctype's name. Uppercase ASCII characters (A-Z) have been /// converted to lowercase. For HTML documents this should be "html". - pub name: String, + pub name: Option, /// The doctype's public identifier. pub public_id: Option, diff --git a/src/machine.rs b/src/machine.rs index 26e1652..6c4558c 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -1208,6 +1208,7 @@ where Some('\0') => { slf.emit_error(Error::UnexpectedNullCharacter); slf.init_doctype(); + slf.emitter.init_doctype_name(slf.position_before_match); slf.emitter.push_doctype_name("\u{fffd}"); slf.state = State::DoctypeName; Ok(ControlToken::Continue) @@ -1229,6 +1230,7 @@ where } Some(x) => { slf.init_doctype(); + slf.emitter.init_doctype_name(slf.position_before_match); slf.emitter .push_doctype_name(ctostr!(x.to_ascii_lowercase())); slf.state = State::DoctypeName; -- cgit v1.2.3