diff --git a/crates/io/src/xml.rs b/crates/io/src/xml.rs index f152d4e0006..13e02004d1e 100644 --- a/crates/io/src/xml.rs +++ b/crates/io/src/xml.rs @@ -24,24 +24,28 @@ pub fn parse<'i>(text: &'i str, opts: ParsingOptions<'i>) -> Result /// Does a DOCTYPE have an internal subset (``)? Every `` in the XML /// declaration must not hide it. One inside a comment may be refused needlessly; that fails safe. +/// +/// Each `` ends a DOCTYPE without one. Scans in +/// the same quote state go on alike, so one pass keeps just three flags (outside quotes, in `"`, +/// in `'`) and stays linear however many ` bool { const DOCTYPE: &[u8] = b"` ends a DOCTYPE without one. - let mut quote = None; - for &b in xml.iter().skip(at.saturating_add(DOCTYPE.len())) { - match quote { - Some(q) if b == q => quote = None, - Some(_) => {} - None if b == b'"' || b == b'\'' => quote = Some(b), - None if b == b'[' => return true, - None if b == b'>' => return false, - None => {} - } + let (mut bare, mut double, mut single) = (false, false, false); + for (at, &b) in xml.iter().enumerate() { + if at.checked_sub(DOCTYPE.len()).and_then(|start| xml.get(start..at)) == Some(DOCTYPE) { + bare = true; } - false - }) + if bare && b == b'[' { + return true; + } + (bare, double, single) = ( + (bare && !matches!(b, b'"' | b'\'' | b'>')) || (double && b == b'"') || (single && b == b'\''), + (double && b != b'"') || (bare && b == b'"'), + (single && b != b'\'') || (bare && b == b'\''), + ); + } + false } #[cfg(test)] @@ -82,6 +86,54 @@ mod tests { assert!(parse(&xml, ParsingOptions::default()).is_ok()); } + /// The scan per ` bool { + const DOCTYPE: &[u8] = b" quote = None, + Some(_) => {} + None if b == b'"' || b == b'\'' => quote = Some(b), + None if b == b'[' => return true, + None if b == b'>' => return false, + None => {} + } + } + false + }) + } + + #[test] + fn the_one_pass_doctype_scan_agrees_with_a_scan_per_doctype() { + let parts: [&[u8]; 7] = [b"", b"a", b"", "\n\n";