Documentation
// Copyright © 2026 Stephan Kunz
//! DOCTYPE declaration node.

use core::ops::Range;

/// A DOCTYPE declaration: `<!DOCTYPE … >`.
///
/// Only the raw content between `<!DOCTYPE` and `>` is exposed; internal
/// subsets and external references are not parsed further.
#[derive(Clone)]
pub struct XmlDoctype<'xml> {
	pub(crate) xml_text: &'xml str,
	/// Byte range of the full `<!DOCTYPE … >` in the source.
	pub(crate) range: Range<usize>,
}

impl<'xml> XmlDoctype<'xml> {
	/// Raw content between `<!DOCTYPE` and `>`, trimmed of leading/trailing
	/// ASCII whitespace.
	pub fn content(&self) -> &'xml str {
		let s = &self.xml_text[self.range.clone()];
		// s starts with "<!DOCTYPE" (9 bytes) and ends with ">"
		s[9..s.len() - 1].trim()
	}

	/// Byte range of this node in the source text.
	pub fn range(&self) -> Range<usize> {
		self.range.clone()
	}
}

#[cfg(test)]
mod tests {
	use super::*;

	const fn is_normal<T: Sized + Send + Sync>() {}

	#[test]
	const fn normal_types() {
		is_normal::<XmlDoctype>();
		is_normal::<&XmlDoctype>();
	}

	#[test]
	fn content_trims_whitespace() {
		let src = "<!DOCTYPE root>";
		let d = XmlDoctype {
			xml_text: src,
			range: 0..src.len(),
		};
		assert_eq!(d.content(), "root");
	}

	#[test]
	fn content_with_system_identifier() {
		let src = r#"<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">"#;
		let d = XmlDoctype {
			xml_text: src,
			range: 0..src.len(),
		};
		assert!(d.content().starts_with("html"));
	}

	#[test]
	fn range_covers_full_doctype() {
		let src = "<!DOCTYPE root>";
		let d = XmlDoctype {
			xml_text: src,
			range: 0..src.len(),
		};
		assert_eq!(&src[d.range()], src);
	}
}