Skip to content

Commit 1c3b7a4

Browse files
committed
Don't bail on empty stack
1 parent e43440b commit 1c3b7a4

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

html5ever/src/serialize/mod.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ impl Default for SerializeOpts {
3737
}
3838
}
3939

40+
#[derive(Default)]
4041
struct ElemInfo {
4142
html_name: Option<LocalName>,
4243
ignore_children: bool,
@@ -66,16 +67,16 @@ impl<Wr: Write> HtmlSerializer<Wr> {
6667
HtmlSerializer {
6768
writer: writer,
6869
opts: opts,
69-
stack: vec!(ElemInfo {
70-
html_name: None,
71-
ignore_children: false,
72-
processed_first_child: false,
73-
}),
70+
stack: vec![Default::default()],
7471
}
7572
}
7673

7774
fn parent(&mut self) -> &mut ElemInfo {
78-
self.stack.last_mut().expect("no parent ElemInfo")
75+
if self.stack.len() == 0 {
76+
warn!("ElemInfo stack empty, creating new parent");
77+
self.stack.push(Default::default());
78+
}
79+
self.stack.last_mut().unwrap()
7980
}
8081

8182
fn write_escaped(&mut self, text: &str, attr_mode: bool) -> io::Result<()> {
@@ -159,7 +160,10 @@ impl<Wr: Write> Serializer for HtmlSerializer<Wr> {
159160
}
160161

161162
fn end_elem(&mut self, name: QualName) -> io::Result<()> {
162-
let info = self.stack.pop().expect("no ElemInfo");
163+
let info = self.stack.pop().unwrap_or_else(|| {
164+
warn!("missing ElemInfo, creating default.");
165+
Default::default()
166+
});
163167
if info.ignore_children {
164168
return Ok(());
165169
}

0 commit comments

Comments
 (0)