diff --git a/crates/xfl/src/document.rs b/crates/xfl/src/document.rs index 1b1fc8dc8..7ecda87f0 100644 --- a/crates/xfl/src/document.rs +++ b/crates/xfl/src/document.rs @@ -138,6 +138,24 @@ pub struct Frame<'a, E: XflElement = El> { element: &'a E, } +/// An `Include` entry in the document's `` library list. +#[derive(Debug)] +pub struct SymbolInclude<'a, E: XflElement = El> { + element: &'a E, +} + +/// A folder entry in the document's `` library list. +#[derive(Debug)] +pub struct LibraryFolder<'a, E: XflElement = El> { + element: &'a E, +} + +/// Native metadata stored on a library `DOMSymbolItem`. +#[derive(Debug)] +pub struct SymbolItem<'a, E: XflElement = El> { + element: &'a E, +} + impl Copy for Frame<'_, E> {} impl Clone for Frame<'_, E> { @@ -146,6 +164,30 @@ impl Clone for Frame<'_, E> { } } +impl Copy for SymbolInclude<'_, E> {} + +impl Clone for SymbolInclude<'_, E> { + fn clone(&self) -> Self { + *self + } +} + +impl Copy for LibraryFolder<'_, E> {} + +impl Clone for LibraryFolder<'_, E> { + fn clone(&self) -> Self { + *self + } +} + +impl Copy for SymbolItem<'_, E> {} + +impl Clone for SymbolItem<'_, E> { + fn clone(&self) -> Self { + *self + } +} + fn attr_last<'a>(element: &'a impl XflElement, name: &str) -> Option<&'a str> { element.attr(name) } @@ -411,6 +453,112 @@ impl<'a, E: XflElement> Frame<'a, E> { } } +impl<'a, E: XflElement> SymbolInclude<'a, E> { + /// Creates a borrowed view over a native `Include` element. + pub fn new(element: &'a E) -> Self { + Self { element } + } + + /// The package-relative library XML path from `href`, kept in native string form. + pub fn href(self) -> Option<&'a str> { + attr_last(self.element, "href") + } + + /// The source element for application-specific path resolution. + pub fn element(self) -> &'a E { + self.element + } +} + +impl<'a, E: XflElement> LibraryFolder<'a, E> { + /// Creates a borrowed view over a native `DOMFolderItem` element. + pub fn new(element: &'a E) -> Self { + Self { element } + } + + /// The folder path from `name`, kept in native string form. + pub fn name(self) -> Option<&'a str> { + attr_last(self.element, "name") + } + + /// The source element for application-specific path validation. + pub fn element(self) -> &'a E { + self.element + } +} + +impl<'a, E: XflElement> SymbolItem<'a, E> { + /// Creates a borrowed view over a native `DOMSymbolItem` element. + pub fn new(element: &'a E) -> Self { + Self { element } + } + + pub fn name(self) -> Option<&'a str> { + attr_last(self.element, "name") + } + + pub fn symbol_type(self) -> Option<&'a str> { + attr_last(self.element, "symbolType") + } + + pub fn linkage_export_for_action_script(self) -> Option<&'a str> { + attr_last(self.element, "linkageExportForAS") + } + + pub fn linkage_class_name(self) -> Option<&'a str> { + attr_last(self.element, "linkageClassName") + } + + pub fn linkage_identifier(self) -> Option<&'a str> { + attr_last(self.element, "linkageIdentifier") + } + + pub fn linkage_base_class(self) -> Option<&'a str> { + attr_last(self.element, "linkageBaseClass") + } + + /// Whether the symbol declares any native 9-slice scale-grid attribute, even if malformed. + pub fn has_scale_grid(self) -> bool { + ["scaleGridLeft", "scaleGridRight", "scaleGridTop", "scaleGridBottom"].iter().any(|name| attr_last(self.element, name).is_some()) + } + + pub fn scale_grid_left(self) -> Option { + number_f32(self.element, "scaleGridLeft") + } + + pub fn scale_grid_right(self) -> Option { + number_f32(self.element, "scaleGridRight") + } + + pub fn scale_grid_top(self) -> Option { + number_f32(self.element, "scaleGridTop") + } + + pub fn scale_grid_bottom(self) -> Option { + number_f32(self.element, "scaleGridBottom") + } + + /// The symbol's optional timeline wrapper, for the consumer to map into its own model. + pub fn timeline(self) -> Option<&'a E> { + self.element.child("timeline") + } + + /// The source element for application-specific symbol validation and mapping. + pub fn element(self) -> &'a E { + self.element + } +} + +/// Symbol library entries in source order, skipping non-`Include` extension nodes. +pub fn symbol_includes<'a, E: XflElement>(root: &'a E) -> impl Iterator> + 'a { + root.child("symbols").into_iter().flat_map(XflElement::elements).filter(|element| element.local_name() == "Include").map(SymbolInclude::new) +} + +/// Library folders in source order, skipping non-`DOMFolderItem` extension nodes. +pub fn library_folders<'a, E: XflElement>(root: &'a E) -> impl Iterator> + 'a { + root.child("folders").into_iter().flat_map(XflElement::elements).filter(|element| element.local_name() == "DOMFolderItem").map(LibraryFolder::new) +} + #[cfg(test)] mod tests { use super::*; @@ -650,4 +798,33 @@ mod tests { assert_eq!(settings.action_script_version.as_deref(), Some("3")); assert_eq!(settings.document_class.as_deref(), Some("RootClass")); } + + #[test] + fn reads_symbol_library_entries_and_native_symbol_metadata() { + let document = Document::parse( + br#""#, + ) + .unwrap(); + let includes: Vec<_> = symbol_includes(document.root()).map(SymbolInclude::href).collect(); + assert_eq!(includes, [Some("LIBRARY/Shape.xml"), Some("LIBRARY/Button.xml")]); + let folders: Vec<_> = library_folders(document.root()).map(LibraryFolder::name).collect(); + assert_eq!(folders, [Some("Characters/Hero")]); + + let symbol = Document::parse( + br#""#, + ) + .unwrap(); + let item = SymbolItem::new(symbol.root()); + assert_eq!(item.name(), Some("Characters/Final"), "the last duplicate attribute wins"); + assert_eq!(item.symbol_type(), Some("graphic")); + assert_eq!(item.linkage_export_for_action_script(), Some("true")); + assert_eq!(item.linkage_class_name(), Some("example.Hero")); + assert_eq!(item.linkage_identifier(), Some("legacy.Hero")); + assert_eq!(item.linkage_base_class(), Some("flash.display.MovieClip")); + assert_eq!( + (item.scale_grid_left(), item.scale_grid_right(), item.scale_grid_top(), item.scale_grid_bottom()), + (Some(10.0), Some(90.0), Some(5.0), Some(75.0)) + ); + assert!(item.timeline().is_some()); + } }