avm2: Add a E4XNode::is_text helper

This commit is contained in:
Tom Schuster 2023-10-30 19:10:23 +01:00 committed by TÖRÖK Attila
parent 28476d83be
commit fca17a0061
4 changed files with 21 additions and 10 deletions

View File

@ -192,6 +192,14 @@ impl<'gc> E4XNode<'gc> {
matches!(self.0.read().kind, E4XNodeKind::Element { .. })
}
/// Returns true when the node is text (E4XNodeKind::Text or E4XNodeKind::CData)
pub fn is_text(&self) -> bool {
matches!(
self.0.read().kind,
E4XNodeKind::Text(_) | E4XNodeKind::CData(_)
)
}
/// Returns an iterator that yields ancestor nodes (including itself).
pub fn ancestors(self) -> impl Iterator<Item = E4XNode<'gc>> {
iterators::AnscIter::for_node(self)

View File

@ -474,7 +474,7 @@ pub fn text<'gc>(
let nodes = if let E4XNodeKind::Element { children, .. } = &*xml.node().kind() {
children
.iter()
.filter(|node| matches!(&*node.kind(), E4XNodeKind::Text(_)))
.filter(|node| node.is_text())
.map(|node| E4XOrXml::E4X(*node))
.collect()
} else {

View File

@ -766,6 +766,7 @@ impl<'gc> TObject<'gc> for XmlListObject<'gc> {
.xml_object_child(0, activation)
.expect("List length was just verified");
// NOTE: avmplus contrary to specification doesn't consider CData here.
if matches!(
*xml.node().kind(),
E4XNodeKind::Attribute(_) | E4XNodeKind::Text(_)
@ -776,6 +777,7 @@ impl<'gc> TObject<'gc> for XmlListObject<'gc> {
}
}
} else if let Some(xml) = value.as_object().and_then(|x| x.as_xml_object()) {
// NOTE: This also doesn't consider CData.
if matches!(
*xml.node().kind(),
E4XNodeKind::Attribute(_) | E4XNodeKind::Text(_)

View File

@ -176,25 +176,26 @@ impl<'gc> XmlObject<'gc> {
// 3.a. If both x and y are the same type (XML)
if let Value::Object(obj) = other {
if let Some(xml_obj) = obj.as_xml_object() {
if (matches!(
&*self.node().kind(),
E4XNodeKind::Text(_) | E4XNodeKind::CData(_) | E4XNodeKind::Attribute(_)
) && xml_obj.node().has_simple_content())
|| (matches!(
&*xml_obj.node().kind(),
E4XNodeKind::Text(_) | E4XNodeKind::CData(_) | E4XNodeKind::Attribute(_)
) && self.node().has_simple_content())
// 3.a.i. If ((x.[[Class]] ∈ {"text", "attribute"}) and (y.hasSimpleContent())
// or ((y.[[Class]] ∈ {"text", "attribute"}) and (x.hasSimpleContent())
if ((self.node().is_text() || self.node().is_attribute())
&& xml_obj.node().has_simple_content())
|| ((xml_obj.node().is_text() || xml_obj.node().is_attribute())
&& self.node().has_simple_content())
{
// 3.a.i.1. Return the result of the comparison ToString(x) == ToString(y)
return Ok(self.node().xml_to_string(activation)
== xml_obj.node().xml_to_string(activation));
}
// 3.a.i. Else return the result of calling the [[Equals]] method of x with argument y
return self.equals(other, activation);
}
}
// 4. If (Type(x) is XML) and x.hasSimpleContent() == true)
// 4. If (Type(x) is XML and x.hasSimpleContent() == true)
if self.node().has_simple_content() {
// 4.a. Return the result of the comparison ToString(x) == ToString(y)
return Ok(self.node().xml_to_string(activation) == other.coerce_to_string(activation)?);
}