avm2: Add a E4XNode::is_attribute helper

This commit is contained in:
Tom Schuster 2023-10-29 22:35:30 +01:00 committed by TÖRÖK Attila
parent 4403848a4b
commit 1953ab3cf0
2 changed files with 11 additions and 7 deletions

View File

@ -182,6 +182,11 @@ impl<'gc> E4XNode<'gc> {
)) ))
} }
/// Returns true when the node is an attribute (E4XNodeKind::Attribute)
pub fn is_attribute(&self) -> bool {
matches!(self.0.read().kind, E4XNodeKind::Attribute(_))
}
/// Returns an iterator that yields ancestor nodes (including itself). /// Returns an iterator that yields ancestor nodes (including itself).
pub fn ancestors(self) -> impl Iterator<Item = E4XNode<'gc>> { pub fn ancestors(self) -> impl Iterator<Item = E4XNode<'gc>> {
iterators::AnscIter::for_node(self) iterators::AnscIter::for_node(self)
@ -397,7 +402,7 @@ impl<'gc> E4XNode<'gc> {
pub fn child_index(&self) -> Option<usize> { pub fn child_index(&self) -> Option<usize> {
let parent = self.parent()?; let parent = self.parent()?;
if let E4XNodeKind::Attribute(_) = &*self.kind() { if self.is_attribute() {
return None; return None;
} }
@ -491,7 +496,7 @@ impl<'gc> E4XNode<'gc> {
if let Some(xml) = value if let Some(xml) = value
.as_object() .as_object()
.and_then(|x| x.as_xml_object()) .and_then(|x| x.as_xml_object())
.filter(|x| !matches!(*x.node().kind(), E4XNodeKind::Attribute(_))) .filter(|x| !x.node().is_attribute())
{ {
// 5.a. If V.[[Class]] is “element” and (V is x or an ancestor of x) throw an Error exception // 5.a. If V.[[Class]] is “element” and (V is x or an ancestor of x) throw an Error exception
if matches!(*xml.node().kind(), E4XNodeKind::Element { .. }) if matches!(*xml.node().kind(), E4XNodeKind::Element { .. })
@ -918,8 +923,7 @@ impl<'gc> E4XNode<'gc> {
} }
pub fn matches_name(&self, name: &Multiname<'gc>) -> bool { pub fn matches_name(&self, name: &Multiname<'gc>) -> bool {
let self_is_attr = matches!(self.0.read().kind, E4XNodeKind::Attribute(_)); if self.is_attribute() != name.is_attribute() {
if self_is_attr != name.is_attribute() {
return false; return false;
} }

View File

@ -689,7 +689,7 @@ impl<'gc> TObject<'gc> for XmlListObject<'gc> {
index = self.length(); index = self.length();
// 2.c.viii. If (y.[[Class]] is not equal to "attribute") // 2.c.viii. If (y.[[Class]] is not equal to "attribute")
if !matches!(*y.kind(), E4XNodeKind::Attribute(_)) { if !y.is_attribute() {
// 2.c.viii.1. If r is not null // 2.c.viii.1. If r is not null
if let Some(r) = r { if let Some(r) = r {
let j = if let E4XNodeKind::Element { children, .. } = &*r.kind() { let j = if let E4XNodeKind::Element { children, .. } = &*r.kind() {
@ -792,7 +792,7 @@ impl<'gc> TObject<'gc> for XmlListObject<'gc> {
drop(children); drop(children);
// 2.e. If x[i].[[Class]] == "attribute" // 2.e. If x[i].[[Class]] == "attribute"
if matches!(*child.kind(), E4XNodeKind::Attribute(_)) { if child.is_attribute() {
// FIXME: We probably need to take the namespace too. // FIXME: We probably need to take the namespace too.
// 2.e.i. Let z = ToAttributeName(x[i].[[Name]]) // 2.e.i. Let z = ToAttributeName(x[i].[[Name]])
let z = Multiname::attribute( let z = Multiname::attribute(
@ -1031,7 +1031,7 @@ impl<'gc> TObject<'gc> for XmlListObject<'gc> {
let removed = write.children.remove(index); let removed = write.children.remove(index);
let removed_node = removed.node(); let removed_node = removed.node();
if let Some(parent) = removed_node.parent() { if let Some(parent) = removed_node.parent() {
if let E4XNodeKind::Attribute(_) = &*removed_node.kind() { if removed_node.is_attribute() {
parent parent
.remove_attribute(activation.context.gc_context, &removed_node); .remove_attribute(activation.context.gc_context, &removed_node);
} else { } else {