1use common::{
2 is_name_start_char, is_name_char,
3};
45use reader::events::XmlEvent;
6use reader::lexer::Token;
78use super::{Result, PullParser, State, ProcessingInstructionSubstate, DeclarationSubstate};
910impl PullParser {
11pub fn inside_processing_instruction(&mut self, t: Token, s: ProcessingInstructionSubstate) -> Option<Result> {
12match s {
13 ProcessingInstructionSubstate::PIInsideName => match t {
14 Token::Character(c) if !self.buf_has_data() && is_name_start_char(c) ||
15self.buf_has_data() && is_name_char(c) => self.append_char_continue(c),
1617 Token::ProcessingInstructionEnd => {
18// self.buf contains PI name
19let name = self.take_buf();
2021// Don't need to check for declaration because it has mandatory attributes
22 // but there is none
23match &name[..] {
24// Name is empty, it is an error
25"" => Some(self_error!(self; "Encountered processing instruction without name")),
2627// Found <?xml-like PI not at the beginning of a document,
28 // it is an error - see section 2.6 of XML 1.1 spec
29"xml"|"xmL"|"xMl"|"xML"|"Xml"|"XmL"|"XMl"|"XML" =>
30Some(self_error!(self; "Invalid processing instruction: <?{}", name)),
3132// All is ok, emitting event
33_ => {
34self.into_state_emit(
35 State::OutsideTag,
36Ok(XmlEvent::ProcessingInstruction {
37 name: name,
38 data: None
39})
40 )
41 }
42 }
43 }
4445 Token::Whitespace(_) => {
46// self.buf contains PI name
47let name = self.take_buf();
4849match &name[..] {
50// We have not ever encountered an element and have not parsed XML declaration
51"xml" if !self.encountered_element && !self.parsed_declaration =>
52self.into_state_continue(State::InsideDeclaration(DeclarationSubstate::BeforeVersion)),
5354// Found <?xml-like PI after the beginning of a document,
55 // it is an error - see section 2.6 of XML 1.1 spec
56"xml"|"xmL"|"xMl"|"xML"|"Xml"|"XmL"|"XMl"|"XML"
57if self.encountered_element || self.parsed_declaration =>
58Some(self_error!(self; "Invalid processing instruction: <?{}", name)),
5960// All is ok, starting parsing PI data
61_ => {
62self.lexer.disable_errors(); // data is arbitrary, so disable errors
63self.data.name = name;
64self.into_state_continue(State::InsideProcessingInstruction(ProcessingInstructionSubstate::PIInsideData))
65 }
6667 }
68 }
6970_ => Some(self_error!(self; "Unexpected token: <?{}{}", self.buf, t))
71 },
7273 ProcessingInstructionSubstate::PIInsideData => match t {
74 Token::ProcessingInstructionEnd => {
75self.lexer.enable_errors();
76let name = self.data.take_name();
77let data = self.take_buf();
78self.into_state_emit(
79 State::OutsideTag,
80Ok(XmlEvent::ProcessingInstruction {
81 name: name,
82 data: Some(data)
83 })
84 )
85 },
8687// Any other token should be treated as plain characters
88_ => {
89 t.push_to_string(&mut self.buf);
90None
91}
92 },
93 }
94 }
9596}