Skip to content

Commit b3eb152

Browse files
committed
Fix both a parsing bug I introduced as well as one that already existed.
1 parent 6fff23c commit b3eb152

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

esi/src/parse.rs

+31
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,31 @@ where
117117
let otherwise_events = &mut Vec::new();
118118

119119
let mut buffer = Vec::new();
120+
121+
// When you are in the top level of a try or choose block, the
122+
// only allowable tags are attempt/except or when/otherwise. All
123+
// other data should be eaten.
124+
let mut in_try = false;
125+
let mut in_choose = false;
126+
120127
// Parse tags and build events vec
121128
loop {
122129
match reader.read_event_into(&mut buffer) {
130+
// Skip any events received while in the top level of a Try block
131+
Ok(XmlEvent::Start(e))
132+
if in_try
133+
&& !(e.name() == QName(&tag.attempt) || e.name() == QName(&tag.except)) =>
134+
{
135+
continue
136+
}
137+
// Skip any events received while in the top level of a When block
138+
Ok(XmlEvent::Start(e))
139+
if in_choose
140+
&& !(e.name() == QName(&tag.when) || e.name() == QName(&tag.otherwise)) =>
141+
{
142+
continue
143+
}
144+
123145
// Handle <esi:remove> tags
124146
Ok(XmlEvent::Start(e)) if e.name() == QName(&tag.remove) => {
125147
is_remove_tag = true;
@@ -161,6 +183,7 @@ where
161183
Ok(XmlEvent::Start(ref e)) if e.name() == QName(&tag.r#try) => {
162184
*current_arm = Some(TryTagArms::Try);
163185
*try_depth += 1;
186+
in_try = true;
164187
continue;
165188
}
166189

@@ -200,6 +223,8 @@ where
200223

201224
Ok(XmlEvent::End(ref e)) if e.name() == QName(&tag.r#try) => {
202225
*current_arm = None;
226+
in_try = false;
227+
203228
if *try_depth == 0 {
204229
return unexpected_closing_tag_error(e);
205230
}
@@ -256,9 +281,11 @@ where
256281

257282
// when/choose
258283
Ok(XmlEvent::Start(ref e)) if e.name() == QName(&tag.choose) => {
284+
in_choose = true;
259285
*choose_depth += 1;
260286
}
261287
Ok(XmlEvent::End(ref e)) if e.name() == QName(&tag.choose) => {
288+
in_choose = false;
262289
*choose_depth -= 1;
263290
choose_tag_handler(when_branches, otherwise_events, callback, task, use_queue)?;
264291
}
@@ -318,6 +345,10 @@ where
318345
break;
319346
}
320347
Ok(e) => {
348+
if in_try || in_choose {
349+
continue;
350+
}
351+
321352
let event = if open_vars {
322353
Event::VarsContent(e.into_owned())
323354
} else {

esi/tests/parse.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ fn parse_try_nested() -> Result<(), ExecutionError> {
269269
parse_tags("esi", &mut Reader::from_str(input), &mut |event| {
270270
assert_eq!(
271271
format!("{event:?}"),
272-
r#"ESI(Try { attempt_events: [XML(Text(BytesText { content: Owned("0xA ") })), ESI(Include { src: "/abc", alt: None, continue_on_error: false }), XML(Text(BytesText { content: Owned("0xA ") })), XML(Text(BytesText { content: Owned("0xA ") })), XML(Text(BytesText { content: Owned("0xA ") })), XML(Text(BytesText { content: Owned("0xA ") })), ESI(Try { attempt_events: [XML(Text(BytesText { content: Owned("0xA ") })), ESI(Include { src: "/foo", alt: None, continue_on_error: false }), XML(Text(BytesText { content: Owned("0xA ") }))], except_events: [XML(Text(BytesText { content: Owned("0xA ") })), ESI(Include { src: "/bar", alt: None, continue_on_error: false }), XML(Text(BytesText { content: Owned("0xA ") }))] }), XML(Text(BytesText { content: Owned("0xA ") }))], except_events: [XML(Text(BytesText { content: Owned("0xA ") })), ESI(Include { src: "/xyz", alt: None, continue_on_error: false }), XML(Text(BytesText { content: Owned("0xA ") })), XML(Empty(BytesStart { buf: Owned("a href=\"/efg\""), name_len: 1 })), XML(Text(BytesText { content: Owned("0xA just text0xA ") }))] })"#
272+
r#"ESI(Try { attempt_events: [XML(Text(BytesText { content: Owned("0xA ") })), ESI(Include { src: "/abc", alt: None, continue_on_error: false }), XML(Text(BytesText { content: Owned("0xA ") })), ESI(Try { attempt_events: [XML(Text(BytesText { content: Owned("0xA ") })), ESI(Include { src: "/foo", alt: None, continue_on_error: false }), XML(Text(BytesText { content: Owned("0xA ") }))], except_events: [XML(Text(BytesText { content: Owned("0xA ") })), ESI(Include { src: "/bar", alt: None, continue_on_error: false }), XML(Text(BytesText { content: Owned("0xA ") }))] }), XML(Text(BytesText { content: Owned("0xA ") }))], except_events: [XML(Text(BytesText { content: Owned("0xA ") })), ESI(Include { src: "/xyz", alt: None, continue_on_error: false }), XML(Text(BytesText { content: Owned("0xA ") })), XML(Empty(BytesStart { buf: Owned("a href=\"/efg\""), name_len: 1 })), XML(Text(BytesText { content: Owned("0xA just text0xA ") }))] })"#
273273
);
274274
if let Event::ESI(Tag::Try {
275275
attempt_events,

0 commit comments

Comments
 (0)