1#[cfg(feature = "encoding")]
2use encoding_rs::UTF_8;
34use crate::encoding::Decoder;
5use crate::errors::{Error, Result};
6use crate::events::{BytesCData, BytesDecl, BytesEnd, BytesStart, BytesText, Event};
7#[cfg(feature = "encoding")]
8use crate::reader::EncodingRef;
9use crate::reader::{is_whitespace, BangType, ParseState};
1011use memchr;
1213/// A struct that holds a current reader state and a parser configuration.
14/// It is independent on a way of reading data: the reader feed data into it and
15/// get back produced [`Event`]s.
16#[derive(Clone)]
17pub(super) struct ReaderState {
18/// Number of bytes read from the source of data since the reader was created
19pub offset: usize,
20/// Defines how to process next byte
21pub state: ParseState,
22/// Expand empty element into an opening and closing element
23pub expand_empty_elements: bool,
24/// Trims leading whitespace in Text events, skip the element if text is empty
25pub trim_text_start: bool,
26/// Trims trailing whitespace in Text events.
27pub trim_text_end: bool,
28/// Trims trailing whitespaces from markup names in closing tags `</a >`
29pub trim_markup_names_in_closing_tags: bool,
30/// Check if [`Event::End`] nodes match last [`Event::Start`] node
31pub check_end_names: bool,
32/// Check if comments contains `--` (false per default)
33pub check_comments: bool,
34/// All currently Started elements which didn't have a matching
35 /// End element yet.
36 ///
37 /// For an XML
38 ///
39 /// ```xml
40 /// <root><one/><inner attr="value">|<tag></inner></root>
41 /// ```
42 /// when cursor at the `|` position buffer contains:
43 ///
44 /// ```text
45 /// rootinner
46 /// ^ ^
47 /// ```
48 ///
49 /// The `^` symbols shows which positions stored in the [`Self::opened_starts`]
50 /// (0 and 4 in that case).
51opened_buffer: Vec<u8>,
52/// Opened name start indexes into [`Self::opened_buffer`]. See documentation
53 /// for that field for details
54opened_starts: Vec<usize>,
5556#[cfg(feature = "encoding")]
57/// Reference to the encoding used to read an XML
58pub encoding: EncodingRef,
59}
6061impl ReaderState {
62/// Trims whitespaces from `bytes`, if required, and returns a [`Text`] event.
63 ///
64 /// # Parameters
65 /// - `bytes`: data from the start of stream to the first `<` or from `>` to `<`
66 ///
67 /// [`Text`]: Event::Text
68pub fn emit_text<'b>(&mut self, bytes: &'b [u8]) -> Result<Event<'b>> {
69let mut content = bytes;
7071if self.trim_text_end {
72// Skip the ending '<'
73let len = bytes
74 .iter()
75 .rposition(|&b| !is_whitespace(b))
76 .map_or_else(|| bytes.len(), |p| p + 1);
77 content = &bytes[..len];
78 }
7980Ok(Event::Text(BytesText::wrap(content, self.decoder())))
81 }
8283/// reads `BytesElement` starting with a `!`,
84 /// return `Comment`, `CData` or `DocType` event
85pub fn emit_bang<'b>(&mut self, bang_type: BangType, buf: &'b [u8]) -> Result<Event<'b>> {
86let uncased_starts_with = |string: &[u8], prefix: &[u8]| {
87 string.len() >= prefix.len() && string[..prefix.len()].eq_ignore_ascii_case(prefix)
88 };
8990let len = buf.len();
91match bang_type {
92 BangType::Comment if buf.starts_with(b"!--") => {
93debug_assert!(buf.ends_with(b"--"));
94if self.check_comments {
95// search if '--' not in comments
96if let Some(p) = memchr::memchr_iter(b'-', &buf[3..len - 2])
97 .position(|p| buf[3 + p + 1] == b'-')
98 {
99self.offset += len - p;
100return Err(Error::UnexpectedToken("--".to_string()));
101 }
102 }
103Ok(Event::Comment(BytesText::wrap(
104&buf[3..len - 2],
105self.decoder(),
106 )))
107 }
108 BangType::CData if uncased_starts_with(buf, b"![CDATA[") => {
109debug_assert!(buf.ends_with(b"]]"));
110Ok(Event::CData(BytesCData::wrap(
111&buf[8..len - 2],
112self.decoder(),
113 )))
114 }
115 BangType::DocType if uncased_starts_with(buf, b"!DOCTYPE") => {
116let start = buf[8..]
117 .iter()
118 .position(|b| !is_whitespace(*b))
119 .unwrap_or(len - 8);
120if start + 8 >= len {
121return Err(Error::EmptyDocType);
122 }
123Ok(Event::DocType(BytesText::wrap(
124&buf[8 + start..],
125self.decoder(),
126 )))
127 }
128_ => Err(bang_type.to_err()),
129 }
130 }
131132/// Wraps content of `buf` into the [`Event::End`] event. Does the check that
133 /// end name matches the last opened start name if `self.check_end_names` is set.
134pub fn emit_end<'b>(&mut self, buf: &'b [u8]) -> Result<Event<'b>> {
135// Strip the `/` character. `content` contains data between `</` and `>`
136let content = &buf[1..];
137// XML standard permits whitespaces after the markup name in closing tags.
138 // Let's strip them from the buffer before comparing tag names.
139let name = if self.trim_markup_names_in_closing_tags {
140if let Some(pos_end_name) = content.iter().rposition(|&b| !is_whitespace(b)) {
141&content[..pos_end_name + 1]
142 } else {
143 content
144 }
145 } else {
146 content
147 };
148149let decoder = self.decoder();
150let mismatch_err = |expected: String, found: &[u8], offset: &mut usize| {
151*offset -= buf.len();
152Err(Error::EndEventMismatch {
153 expected,
154 found: decoder.decode(found).unwrap_or_default().into_owned(),
155 })
156 };
157158// Get the index in self.opened_buffer of the name of the last opened tag
159match self.opened_starts.pop() {
160Some(start) => {
161if self.check_end_names {
162let expected = &self.opened_buffer[start..];
163if name != expected {
164let expected = decoder.decode(expected).unwrap_or_default().into_owned();
165// #513: In order to allow error recovery we should drop content of the buffer
166self.opened_buffer.truncate(start);
167168return mismatch_err(expected, name, &mut self.offset);
169 }
170 }
171172self.opened_buffer.truncate(start);
173 }
174None => {
175if self.check_end_names {
176return mismatch_err("".to_string(), &buf[1..], &mut self.offset);
177 }
178 }
179 }
180181Ok(Event::End(BytesEnd::wrap(name.into())))
182 }
183184/// reads `BytesElement` starting with a `?`,
185 /// return `Decl` or `PI` event
186pub fn emit_question_mark<'b>(&mut self, buf: &'b [u8]) -> Result<Event<'b>> {
187let len = buf.len();
188if len > 2 && buf[len - 1] == b'?' {
189if len > 5 && &buf[1..4] == b"xml" && is_whitespace(buf[4]) {
190let event = BytesDecl::from_start(BytesStart::wrap(&buf[1..len - 1], 3));
191192// Try getting encoding from the declaration event
193#[cfg(feature = "encoding")]
194if self.encoding.can_be_refined() {
195if let Some(encoding) = event.encoder() {
196self.encoding = EncodingRef::XmlDetected(encoding);
197 }
198 }
199200Ok(Event::Decl(event))
201 } else {
202Ok(Event::PI(BytesText::wrap(&buf[1..len - 1], self.decoder())))
203 }
204 } else {
205self.offset -= len;
206Err(Error::UnexpectedEof("XmlDecl".to_string()))
207 }
208 }
209210/// Converts content of a tag to a `Start` or an `Empty` event
211 ///
212 /// # Parameters
213 /// - `content`: Content of a tag between `<` and `>`
214pub fn emit_start<'b>(&mut self, content: &'b [u8]) -> Result<Event<'b>> {
215let len = content.len();
216let name_end = content
217 .iter()
218 .position(|&b| is_whitespace(b))
219 .unwrap_or(len);
220if let Some(&b'/') = content.last() {
221// This is self-closed tag `<something/>`
222let name_len = if name_end < len { name_end } else { len - 1 };
223let event = BytesStart::wrap(&content[..len - 1], name_len);
224225if self.expand_empty_elements {
226self.state = ParseState::Empty;
227self.opened_starts.push(self.opened_buffer.len());
228self.opened_buffer.extend(&content[..name_len]);
229Ok(Event::Start(event))
230 } else {
231Ok(Event::Empty(event))
232 }
233 } else {
234// #514: Always store names event when .check_end_names == false,
235 // because checks can be temporary disabled and when they would be
236 // enabled, we should have that information
237self.opened_starts.push(self.opened_buffer.len());
238self.opened_buffer.extend(&content[..name_end]);
239Ok(Event::Start(BytesStart::wrap(content, name_end)))
240 }
241 }
242243#[inline]
244pub fn close_expanded_empty(&mut self) -> Result<Event<'static>> {
245self.state = ParseState::ClosedTag;
246let name = self
247.opened_buffer
248 .split_off(self.opened_starts.pop().unwrap());
249Ok(Event::End(BytesEnd::wrap(name.into())))
250 }
251252/// Get the decoder, used to decode bytes, read by this reader, to the strings.
253 ///
254 /// If [`encoding`] feature is enabled, the used encoding may change after
255 /// parsing the XML declaration, otherwise encoding is fixed to UTF-8.
256 ///
257 /// If [`encoding`] feature is enabled and no encoding is specified in declaration,
258 /// defaults to UTF-8.
259 ///
260 /// [`encoding`]: ../../index.html#encoding
261pub fn decoder(&self) -> Decoder {
262 Decoder {
263#[cfg(feature = "encoding")]
264encoding: self.encoding.encoding(),
265 }
266 }
267}
268269impl Default for ReaderState {
270fn default() -> Self {
271Self {
272 offset: 0,
273 state: ParseState::Init,
274 expand_empty_elements: false,
275 trim_text_start: false,
276 trim_text_end: false,
277 trim_markup_names_in_closing_tags: true,
278 check_end_names: true,
279 check_comments: false,
280 opened_buffer: Vec::new(),
281 opened_starts: Vec::new(),
282283#[cfg(feature = "encoding")]
284encoding: EncodingRef::Implicit(UTF_8),
285 }
286 }
287}