1use crate::common::{is_name_char, is_name_start_char, is_whitespace_char};
2use crate::reader::error::SyntaxError;
34use crate::reader::events::XmlEvent;
5use crate::reader::lexer::Token;
67use super::{DeclarationSubstate, Encountered, ProcessingInstructionSubstate, PullParser, Result, State};
89impl PullParser {
10pub fn inside_processing_instruction(&mut self, t: Token, s: ProcessingInstructionSubstate) -> Option<Result> {
11match s {
12 ProcessingInstructionSubstate::PIInsideName => match t {
13 Token::Character(c) if self.buf.is_empty() && is_name_start_char(c) ||
14self.buf_has_data() && is_name_char(c) => {
15if self.buf.len() > self.config.max_name_length {
16return Some(self.error(SyntaxError::ExceededConfiguredLimit));
17 }
18self.buf.push(c);
19None
20},
2122 Token::ProcessingInstructionEnd => {
23// self.buf contains PI name
24let name = self.take_buf();
2526// Don't need to check for declaration because it has mandatory attributes
27 // but there is none
28match &*name {
29// Name is empty, it is an error
30"" => Some(self.error(SyntaxError::ProcessingInstructionWithoutName)),
3132// Found <?xml-like PI not at the beginning of a document,
33 // it is an error - see section 2.6 of XML 1.1 spec
34n if "xml".eq_ignore_ascii_case(n) =>
35Some(self.error(SyntaxError::InvalidXmlProcessingInstruction(name.into()))),
3637// All is ok, emitting event
38_ => {
39debug_assert!(self.next_event.is_none(), "{:?}", self.next_event);
40// can't have a PI before `<?xml`
41let event1 = self.set_encountered(Encountered::Declaration);
42let event2 = Some(Ok(XmlEvent::ProcessingInstruction {
43 name,
44 data: None
45}));
46// emitting two events at once is cumbersome
47let event1 = if event1.is_some() {
48self.next_event = event2;
49 event1
50 } else {
51 event2
52 };
53self.into_state(State::OutsideTag, event1)
54 },
55 }
56 },
5758 Token::Character(c) if is_whitespace_char(c) => {
59// self.buf contains PI name
60let name = self.take_buf();
6162match &*name {
63// We have not ever encountered an element and have not parsed XML declaration
64"xml" if self.encountered == Encountered::None =>
65self.into_state_continue(State::InsideDeclaration(DeclarationSubstate::BeforeVersion)),
6667// Found <?xml-like PI after the beginning of a document,
68 // it is an error - see section 2.6 of XML 1.1 spec
69n if "xml".eq_ignore_ascii_case(n) =>
70Some(self.error(SyntaxError::InvalidXmlProcessingInstruction(name.into()))),
7172// All is ok, starting parsing PI data
73_ => {
74self.data.name = name;
75// can't have a PI before `<?xml`
76let next_event = self.set_encountered(Encountered::Declaration);
77self.into_state(State::InsideProcessingInstruction(ProcessingInstructionSubstate::PIInsideData), next_event)
78 },
79 }
80 },
8182_ => {
83let buf = self.take_buf();
84Some(self.error(SyntaxError::UnexpectedProcessingInstruction(buf.into(), t)))
85 },
86 },
8788 ProcessingInstructionSubstate::PIInsideData => match t {
89 Token::ProcessingInstructionEnd => {
90let name = self.data.take_name();
91let data = self.take_buf();
92self.into_state_emit(
93 State::OutsideTag,
94Ok(XmlEvent::ProcessingInstruction { name, data: Some(data) }),
95 )
96 },
9798 Token::Character(c) if !self.is_valid_xml_char(c) => {
99Some(self.error(SyntaxError::InvalidCharacterEntity(c as u32)))
100 },
101102// Any other token should be treated as plain characters
103_ => {
104if self.buf.len() > self.config.max_data_length {
105return Some(self.error(SyntaxError::ExceededConfiguredLimit));
106 }
107 t.push_to_string(&mut self.buf);
108None
109},
110 },
111 }
112 }
113}