diff --git a/core/src/avm1/globals/xml.rs b/core/src/avm1/globals/xml.rs index ed64f3840..c6a00fed5 100644 --- a/core/src/avm1/globals/xml.rs +++ b/core/src/avm1/globals/xml.rs @@ -99,17 +99,9 @@ fn parse_xml<'gc>( "".into() }; - let mut node = document.as_node(); - for child in node.children().rev() { - let result = node.remove_child(activation.context.gc_context, child); - if let Err(e) = result { - avm_warn!( - activation, - "XML.parseXML: Error removing node contents: {}", - e - ); - return Ok(Value::Undefined); - } + let node = document.as_node(); + for mut child in node.children().rev() { + child.remove_node(activation.context.gc_context); } let ignore_whitespace = this diff --git a/core/src/avm1/globals/xml_node.rs b/core/src/avm1/globals/xml_node.rs index 11019c4d0..97ae7f9ea 100644 --- a/core/src/avm1/globals/xml_node.rs +++ b/core/src/avm1/globals/xml_node.rs @@ -194,12 +194,8 @@ fn remove_node<'gc>( this: Object<'gc>, _args: &[Value<'gc>], ) -> Result, Error<'gc>> { - if let Some(node) = this.as_xml_node() { - if let Some(mut parent) = node.parent() { - if let Err(e) = parent.remove_child(activation.context.gc_context, node) { - avm_warn!(activation, "Error in XML.removeNode: {}", e); - } - } + if let Some(mut node) = this.as_xml_node() { + node.remove_node(activation.context.gc_context); } Ok(Value::Undefined) diff --git a/core/src/xml/error.rs b/core/src/xml/error.rs index 1c8efa9b6..3c3c9593b 100644 --- a/core/src/xml/error.rs +++ b/core/src/xml/error.rs @@ -29,9 +29,6 @@ pub enum Error { #[error("Not an element")] NotAnElement, - - #[error("Target node is not a child of this one!")] - CantRemoveNonChild, } impl From for Error { diff --git a/core/src/xml/tree.rs b/core/src/xml/tree.rs index d93af0d03..86b89300d 100644 --- a/core/src/xml/tree.rs +++ b/core/src/xml/tree.rs @@ -435,32 +435,24 @@ impl<'gc> XmlNode<'gc> { self.insert_child(mc, self.children_len(), child) } - /// Remove a previously added node from this tree. - /// - /// If the node is not a child of this one, or this node cannot accept - /// children, then this function yields an error. - pub fn remove_child( - &mut self, - mc: MutationContext<'gc, '_>, - mut child: XmlNode<'gc>, - ) -> Result<(), Error> { - if let Some(position) = self.child_position(child) { - match &mut *self.0.write(mc) { - XmlNodeData::Element { children, .. } - | XmlNodeData::DocumentRoot { children, .. } => children.remove(position), + /// Remove this node from its parent. + pub fn remove_node(&mut self, mc: MutationContext<'gc, '_>) { + if let Some(parent) = self.parent() { + // This is guaranteed to succeed, as `self` is a child of `parent`. + let position = parent.child_position(*self).unwrap(); + + match &mut *parent.0.write(mc) { + XmlNodeData::DocumentRoot { children, .. } + | XmlNodeData::Element { children, .. } => children.remove(position), XmlNodeData::Text { .. } => { // This cannot happen, as `adopt_child` refuses to adopt children into text nodes. unreachable!(); } }; - child.disown_siblings(mc); - child.disown_parent(mc); - } else { - return Err(Error::CantRemoveNonChild); + self.disown_siblings(mc); + self.disown_parent(mc); } - - Ok(()) } /// Returns the type of this node as an integer.