diff --git a/crates/js/src/xmp.rs b/crates/js/src/xmp.rs
index 60d07d4cce..c81533b1f5 100644
--- a/crates/js/src/xmp.rs
+++ b/crates/js/src/xmp.rs
@@ -529,12 +529,25 @@ fn read_property(p: &El, scope: &mut Scope, registry: &mut Registry) -> Result
MAX_DEPTH {
+ return Err(XmpError::TooLarge);
+ }
other(p, &own, &children, scope, mark)
};
scope.truncate(mark);
Ok(Prop { ns, name: local.to_string(), value })
}
+/// The elements a written packet holds around a property.
+const PROPERTY_DEPTH: usize = 3;
+
+/// The elements `el` nests, itself included.
+fn height(el: &El) -> usize {
+ 1 + el.elements().map(height).max().unwrap_or(0)
+}
+
/// The array of simple items the only child of a property element holds, when it is one.
fn array_of(children: &[&El], scope: &mut Scope) -> Option {
let [container] = children else { return None };
diff --git a/crates/js/src/xmp_classes.rs b/crates/js/src/xmp_classes.rs
index 0efd7fb53b..0987011607 100644
--- a/crates/js/src/xmp_classes.rs
+++ b/crates/js/src/xmp_classes.rs
@@ -696,22 +696,25 @@ fn same_lang(lang: Option<&str>, want: &str) -> bool {
lang.is_some_and(|l| l.eq_ignore_ascii_case(want))
}
+/// Is language tag `lang` the language `generic`, or one of its variants (`generic-…`), ignoring
+/// case? Never for an empty `generic`.
+fn generic_lang(lang: Option<&str>, generic: &str) -> bool {
+ !generic.is_empty()
+ && lang.is_some_and(|l| {
+ l.eq_ignore_ascii_case(generic)
+ || l.len() > generic.len()
+ && l.get(..generic.len()).is_some_and(|p| p.eq_ignore_ascii_case(generic))
+ && l.as_bytes().get(generic.len()) == Some(&b'-')
+ })
+}
+
/// The item of `items` for `specific`, else one whose language starts with `generic`, else the
/// `x-default` item, else the first.
fn pick_lang<'a>(items: &'a [Item], generic: &str, specific: &str) -> Option<&'a Item> {
- let generic_match = |i: &&Item| {
- !generic.is_empty()
- && i.lang.as_deref().is_some_and(|l| {
- l.eq_ignore_ascii_case(generic)
- || l.len() > generic.len()
- && l.get(..generic.len()).is_some_and(|p| p.eq_ignore_ascii_case(generic))
- && l.as_bytes().get(generic.len()) == Some(&b'-')
- })
- };
items
.iter()
.find(|i| same_lang(i.lang.as_deref(), specific))
- .or_else(|| items.iter().find(generic_match))
+ .or_else(|| items.iter().find(|i| generic_lang(i.lang.as_deref(), generic)))
.or_else(|| items.iter().find(|i| same_lang(i.lang.as_deref(), "x-default")))
.or_else(|| items.first())
}
@@ -745,6 +748,7 @@ fn set_localized_text(i: &mut Interp, this: Value, args: &[Value], _: bool) -> J
let what = "XMPMeta.setLocalizedText";
let o = this_meta(i, &this, what)?;
let (ns, name) = array_target(i, args, what)?;
+ let generic = lang(i, &arg(args, 2), what)?;
let specific = lang(i, &arg(args, 3), what)?;
if specific.is_empty() || specific.contains(char::is_whitespace) {
return Err(not_handled(i, what, &specific));
@@ -763,15 +767,16 @@ fn set_localized_text(i: &mut Interp, this: Value, args: &[Value], _: bool) -> J
let grow = 2 * (64 + value.len() + specific.len());
change(i, o, grow, |m, _| {
let Some(xmp::Value::Array { items, .. }) = m.props.get_mut(at).map(|p| &mut p.value) else { return };
- set_lang(items, &specific, value);
+ set_lang(items, &generic, &specific, value);
})?;
Ok(Value::Undefined)
}
-/// Sets the item of `items` in `specific` to `value`. VectorCraft's own rule: the `x-default` item
-/// is first; it is added with the value when there is none, and follows a change to an item that
-/// had the same text.
-fn set_lang(items: &mut Vec- , specific: &str, value: String) {
+/// Sets the item of `items` in `specific` to `value`, or, when there is none, the one item in the
+/// language `generic` or a variant of it. VectorCraft's own rule: the `x-default` item is first; it
+/// is added with the value when there is none, and follows a change to an item that had the same
+/// text.
+fn set_lang(items: &mut Vec
- , generic: &str, specific: &str, value: String) {
let default = items.iter().position(|i| same_lang(i.lang.as_deref(), "x-default"));
if specific.eq_ignore_ascii_case("x-default") {
match default {
@@ -784,7 +789,14 @@ fn set_lang(items: &mut Vec
- , specific: &str, value: String) {
}
return;
}
- let old = items.iter().position(|i| same_lang(i.lang.as_deref(), specific));
+ let old = items.iter().position(|i| same_lang(i.lang.as_deref(), specific)).or_else(|| {
+ let mut generic_items =
+ items.iter().enumerate().filter(|(_, i)| !same_lang(i.lang.as_deref(), "x-default") && generic_lang(i.lang.as_deref(), generic));
+ match (generic_items.next(), generic_items.next()) {
+ (Some((at, _)), None) => Some(at),
+ _ => None,
+ }
+ });
let follows = match (old, default) {
(Some(o), Some(d)) => items.get(o).map(|i| &i.text) == items.get(d).map(|i| &i.text),
(None, Some(_)) => false,
diff --git a/crates/js/src/xmp_tests.rs b/crates/js/src/xmp_tests.rs
index 653cd913e4..872f338cab 100644
--- a/crates/js/src/xmp_tests.rs
+++ b/crates/js/src/xmp_tests.rs
@@ -315,6 +315,40 @@ fn error_messages_hold_a_bounded_part_of_the_script_text() {
}
}
+/// With no item in the specific language, `setLocalizedText` changes the one item in the generic
+/// language, and adds the specific language when there are several.
+#[test]
+fn localized_text_updates_a_single_generic_language_item() {
+ let src = "var ns = 'http://example.com/lt/'; XMPMeta.registerNamespace(ns, 'lt'); var m = new XMPMeta();
+ m.setLocalizedText(ns, 'label', null, 'en-GB', 'Colour');
+ m.setLocalizedText(ns, 'label', 'en', 'en-US', 'Color');
+ var one = [m.countArrayItems(ns, 'label'), m.getLocalizedText(ns, 'label', null, 'en-GB'), m.getLocalizedText(ns, 'label', null, 'x-default')];
+ m.setLocalizedText(ns, 'label', null, 'en-AU', 'Colour');
+ m.setLocalizedText(ns, 'label', 'en', 'en-CA', 'Couleur');
+ one.concat([m.countArrayItems(ns, 'label'), m.getLocalizedText(ns, 'label', null, 'en-CA'), m.getLocalizedText(ns, 'label', null, 'en-GB')]).join('|')";
+ assert_eq!(string(src), "2|Color|Color|4|Couleur|Color");
+}
+
+/// A packet read from `rdf:RDF` alone reads back from what it serializes to, which adds the
+/// `x:xmpmeta` element around it, also with values nested near the depth limit.
+#[test]
+fn deeply_nested_values_read_back_after_serializing() {
+ let mut read = 0;
+ for height in 55..=66 {
+ let nest = format!("{}x{}", "".repeat(height - 1), "".repeat(height - 1));
+ let packet = format!(
+ "{nest}"
+ );
+ let mut registry = Registry::default();
+ let Ok(meta) = xmp::parse(&packet, &mut registry) else { continue };
+ read += 1;
+ let out = xmp::serialize(&meta, ®istry, true);
+ let again = out.as_ref().ok().map(|out| xmp::parse(out, &mut registry));
+ assert_eq!(again, Some(Ok(meta)), "a value {height} elements high");
+ }
+ assert!(read > 0, "some of the packets are read");
+}
+
#[test]
fn array_forms_follow_their_options() {
assert_eq!(Form::from_options(0x200), Form::Bag);
diff --git a/docs/mcp.md b/docs/mcp.md
index a1c0de7c34..b61b5471ef 100644
--- a/docs/mcp.md
+++ b/docs/mcp.md
@@ -3081,7 +3081,8 @@ rules for these objects:
`Script file could not be written` and the `File` keeps its changes. `getXMP`, `getPacketInfo`, `getFileInfo` and `canPutXMP` are available. An `XMPFile` is open only in the
run that opened it.
- A packet over 8 MiB, one that is not well-formed XML, one with a document type declaration or an undefined entity,
- and one nested more than 64 elements deep throws. Members of the help page that are not built yet (iterators,
+ and one nested more than 64 elements deep throws; a value is counted as deep as `serialize()` would write it,
+ under `x:xmpmeta`, `rdf:RDF` and `rdf:Description`. Members of the help page that are not built yet (iterators,
structure fields, qualifiers, aliases, sorting, byte-array packets and `XMPFile.getFormatInfo`) throw
`… is not available yet`, and so does a flag, format or path form that is not handled yet.