Preserve localized XMP properties and optional argument positions

This commit is contained in:
dotCooCoo committed 2026-11-26 20:43:11 -08:00
1 parent ece63ddd2e
commit 47dcb19bca
4 files changed
+92 -13

No files matched your search

+48 -10
View File
@@ -736,13 +736,16 @@ fn append_array_item(i: &mut Interp, this: Value, args: &[Value], _: bool) -> Js
let what = "XMPMeta.appendArrayItem";
let o = this_meta(i, &this, what)?;
let Target { ns, name, .. } = array_target(i, args, what)?;
// `itemOptions` may be left out: a string or `null` third argument is the item's value.
let (value, array_options) = match arg(args, 2) {
Value::Number(_) => {
item_options(i, &arg(args, 2), what)?;
(arg(args, 3), arg(args, 4))
}
v => (v, arg(args, 3)),
// Full calls keep their argument positions. Short calls can omit `itemOptions`.
let third = arg(args, 2);
let positional = args.len() >= 5
|| matches!(&third, Value::Number(_) | Value::Undefined)
|| (matches!(&third, Value::Null) && matches!(arg(args, 3), Value::String(_)));
let (value, array_options) = if positional {
item_options(i, &third, what)?;
(arg(args, 3), arg(args, 4))
} else {
(third, arg(args, 3))
};
let array_options = flags(i, &array_options, what)?;
only(i, array_options, xmp::PROP_IS_ARRAY | xmp::ARRAY_IS_ORDERED | xmp::ARRAY_IS_ALTERNATIVE, what)?;
@@ -829,16 +832,22 @@ fn get_localized_text(i: &mut Interp, this: Value, args: &[Value], _: bool) -> J
let Target { ns, name, .. } = array_target(i, args, what)?;
let generic = lang(i, &arg(args, 2), what)?;
let specific = lang(i, &arg(args, 3), what)?;
let Some((at, _, _)) = array_items(i, o, &ns, &name, what)? else { return Ok(Value::Undefined) };
let Some((at, _, len)) = array_items(i, o, &ns, &name, what)? else { return Ok(Value::Undefined) };
spend(i, len.saturating_mul(std::mem::size_of::<Item>()).saturating_mul(6))?;
let language_bytes = match &meta_of(i, o)?.props[at].value {
xmp::Value::Array { items, .. } => items.iter().fold(0usize, |bytes, item| bytes.saturating_add(item.lang.as_ref().map_or(0, String::len))),
_ => 0,
};
spend(i, language_bytes.saturating_mul(3))?;
if !is_alt_text(i, o, at)? {
return Err(not_alt_text(i, what, &name));
}
let found = match &meta_of(i, o)?.props[at].value {
xmp::Value::Array { items, .. } => pick_lang(items, &generic, &specific).map(|i| i.text.clone()),
xmp::Value::Array { items, .. } => pick_lang(items, &generic, &specific).cloned(),
_ => None,
};
match found {
Some(t) => string(i, &t),
Some(item) => property(i, "", "", Some(&item.text), item.options(), item.lang.as_deref().unwrap_or("")),
None => Ok(Value::Undefined),
}
}
@@ -1468,6 +1477,35 @@ mod namespace_heap_tests {
}
}
#[test]
fn localized_text_lookup_charges_for_language_scans() {
let mut interp = Interp::new(crate::Options::default(), crate::Limits::default());
install(&mut interp).unwrap();
let value = new_meta(
&mut interp,
Meta {
props: vec![xmp::Prop {
ns: xmp::DC.into(),
name: "items".into(),
value: xmp::Value::Array { form: Form::Alt, items: vec![Item { text: "text".into(), lang: Some("en-US".into()) }; 10_000] },
}],
..Meta::default()
},
)
.unwrap();
interp
.rooted(std::slice::from_ref(&value), |interp| {
let args = [Value::str(xmp::DC), Value::str("items"), Value::str("fr"), Value::str("fr-FR")];
interp.fuel = 1_000;
let result = get_localized_text(interp, value.clone(), &args, false);
assert!(matches!(result, Err(Abrupt::Limit(crate::LimitKind::Fuel))), "{result:?}");
interp.fuel = 1_000_000;
assert!(get_localized_text(interp, value.clone(), &args, false).is_ok());
Ok(())
})
.unwrap();
}
#[test]
fn inserting_array_items_charges_for_shifted_items() {
let mut interp = Interp::new(crate::Options::default(), crate::Limits::default());
+37 -2
View File
@@ -61,6 +61,21 @@ fn inherited_description_language_is_not_silently_discarded() {
assert!(xmp::serialize(&meta, &registry, false).unwrap().contains("xml:lang=\"en\""));
}
#[test]
fn localized_text_returns_the_selected_property_and_language() {
let specific = with_packet(
"var value = m.getLocalizedText(XMPConst.NS_DC, 'title', null, 'fr-FR'); [typeof value, value.value, value.locale, value.options, value.namespace, value.path, String(value)].join('|')",
);
assert_eq!(string(&specific), "object|Bonjour|fr-FR|80|||Bonjour");
let fallback = with_packet("var value = m.getLocalizedText(XMPConst.NS_DC, 'title', null, 'de-DE'); [value.value, value.locale].join('|')");
assert_eq!(string(&fallback), "Hello|x-default");
truth(&with_packet("m.getLocalizedText(XMPConst.NS_DC, 'missing', null, 'fr-FR') === undefined"));
truth(&with_packet(
"m.appendArrayItem(XMPConst.NS_DC, 'emptyAlt', 0, 'value', XMPConst.PROP_IS_ARRAY | XMPConst.ARRAY_IS_ORDERED | XMPConst.ARRAY_IS_ALTERNATIVE);
m.deleteArrayItem(XMPConst.NS_DC, 'emptyAlt', 1); m.doesPropertyExist(XMPConst.NS_DC, 'emptyAlt') && m.getLocalizedText(XMPConst.NS_DC, 'emptyAlt', null, 'fr-FR') === undefined",
));
}
#[test]
fn rdf_qualified_names_validate_both_parts() {
let packet = PACKET.replace("ex:a>", "ex:a:b>");
@@ -262,8 +277,8 @@ fn only_the_xmp_library_loads() {
fn a_packet_reads_into_properties() {
assert_eq!(string(&with_packet("m.getProperty(XMPConst.NS_DC, 'format').value")), "image/svg+xml");
assert_eq!(string(&with_packet("String(m.getProperty(XMPConst.NS_XMP, 'xmp:CreatorTool'))")), "Tool & Co");
assert_eq!(string(&with_packet("m.getLocalizedText(XMPConst.NS_DC, 'title', null, 'fr-FR')")), "Bonjour");
assert_eq!(string(&with_packet("m.getLocalizedText(XMPConst.NS_DC, 'title', null, 'de-DE')")), "Hello");
assert_eq!(string(&with_packet("m.getLocalizedText(XMPConst.NS_DC, 'title', null, 'fr-FR').value")), "Bonjour");
assert_eq!(string(&with_packet("m.getLocalizedText(XMPConst.NS_DC, 'title', null, 'de-DE').value")), "Hello");
assert_eq!(string(&with_packet("m.getArrayItem(XMPConst.NS_DC, 'subject', XMPConst.ARRAY_LAST_ITEM).value")), "two");
assert_eq!(run(&with_packet("m.countArrayItems(XMPConst.NS_DC, 'subject')")), JsValue::Number(2.0));
assert_eq!(run(&with_packet("m.getProperty(XMPConst.NS_DC, 'title').options")), JsValue::Number(f64::from(0x200 | 0x400 | 0x800 | 0x1000)));
@@ -316,6 +331,26 @@ fn properties_arrays_and_namespaces_round_trip() {
assert_eq!(string("XMPMeta.getNamespaceURI('dc:')"), "http://purl.org/dc/elements/1.1/");
}
#[test]
fn append_array_item_preserves_explicit_optional_slots() {
assert_eq!(
string(
"var out = []; var ns = XMPConst.NS_DC;
function append(option, full) { var m = new XMPMeta(); try {
if (full) m.appendArrayItem(ns, 'items', option, 'value', XMPConst.PROP_IS_ARRAY);
else { m.appendArrayItem(ns, 'items', 'first', XMPConst.PROP_IS_ARRAY);
m.appendArrayItem(ns, 'items', option, 'value'); m.deleteArrayItem(ns, 'items', 1); }
out.push(m.getArrayItem(ns, 'items', 1).value);
} catch (e) { out.push('error'); } }
append(undefined, true); append(null, true); append(undefined, false); append(null, false);
var m = new XMPMeta(); m.appendArrayItem(ns, 'empty', null, XMPConst.PROP_IS_ARRAY);
m.appendArrayItem(ns, 'short', 'short', XMPConst.PROP_IS_ARRAY);
out.push(m.getArrayItem(ns, 'empty', 1).value, m.getArrayItem(ns, 'short', 1).value); out.join('|')"
),
"value|value|value|value||short"
);
}
#[test]
fn array_last_insertion_precedes_the_last_existing_item() {
assert_eq!(
+5 -1
View File
@@ -3410,7 +3410,11 @@ rules for these objects:
`XMPConst.ARRAY_LAST_ITEM` inserts before the last existing item and rejects an empty array. `insertArrayItem`
and `setArrayItem` store `null` as an empty item value. Arrays hold simple items; a structure
or a value with qualifiers that a packet holds is kept as it was written, and `getProperty` gives its options. An
array item with an `xml:lang` qualifier has the qualifier and language options in `getProperty` and `getArrayItem`. A
array item with an `xml:lang` qualifier has the qualifier and language options in `getProperty` and `getArrayItem`.
`getLocalizedText` returns an `XMPProperty` with the selected text, locale and options; its `namespace` and `path` are empty strings.
VectorCraft returns `undefined` when the property is absent or its language alternative array is empty.
`appendArrayItem` keeps explicit item options, value and array options in their parameter slots, including `undefined` or `null` item options.
A string value can use the shorter call form; a null value with numeric array options can also use that form. A
value with qualifiers in the `rdf:value` form also gives the text of its `rdf:value`, and an empty property element
whose attributes are fields is a structure. The namespaces that a packet's property, element and attribute names use are registered with the prefix the packet
gives them, or `prefix_1_` when that prefix names another namespace; a namespace the packet declares but no name
+2
View File
@@ -97,6 +97,8 @@ Artboard Options forwards the active Artboard tool's Scale Artwork with Artboard
`XMPFile` updates restart open `File` readers with a UTF-8 byte order mark or UTF-16 encoding at the beginning of the new UTF-8 packet. Unmarked UTF-8 readers keep their position, limited to the new packet's length. Written packets omit a leading XML declaration and retain the following packet bytes. Nonempty language inherited by an `rdf:Description` is rejected as unavailable; explicit property-level language qualifiers are retained. Native file state is separate from script properties, and a successful close or the next script file run releases its packet buffers. Registered namespaces and expanded XML values count toward the script's heap limit. String-packet validation does not retain expanded XML values. On Unix, a folder flush failure after writing reports the written replacement and refreshes open readers while keeping the pending packet for retry. A failed `Realm::define_external_object` call can be retried; a call after a successful installation returns an internal error and preserves registered namespaces.
`XMPMeta.getLocalizedText` returns the selected `XMPProperty`, including its locale and option flags. VectorCraft returns `undefined` for an absent property or empty language alternative. `appendArrayItem` preserves explicit optional argument positions.
### Feature revisions
| Date | Change | Summary |