mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-27 16:59:43 +01:00
Scripts: XMP generic-language updates and packets that serialize too deep
- setLocalizedText reads its generic language argument. With no item in the specific language and exactly one item in the generic language or a variant of it, that item changes, as the XMP Toolkit SDK's SetLocalizedText does (XMPCore/source/XMPMeta-GetSet.cpp); with several, the specific language is added. - A packet read from rdf:RDF alone could hold a value nested 64 elements deep that serialize() then wrote under x:xmpmeta, 65 deep, so the written packet did not read back. A value is now measured as deep as serialize() writes it, and one past 64 elements throws when the packet is read. Tests: localized_text_updates_a_single_generic_language_item and deeply_nested_values_read_back_after_serializing in crates/js/src/xmp_tests.rs. Docs: docs/mcp.md.
This commit is contained in:
1 parent
7fb823a462
commit
1a33ff7326
4 files changed
+77
-17
No files matched your search
@@ -529,12 +529,25 @@ fn read_property(p: &El, scope: &mut Scope, registry: &mut Registry) -> Result<P
|
||||
} else if let (Some(array), true) = (array_of(&children, scope), own.is_empty()) {
|
||||
array
|
||||
} else {
|
||||
// A written packet puts a property 3 elements deep (`x:xmpmeta`, `rdf:RDF`,
|
||||
// `rdf:Description`), which may be deeper than the packet read put it.
|
||||
if PROPERTY_DEPTH + height(p) > 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<Value> {
|
||||
let [container] = children else { return None };
|
||||
|
||||
@@ -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<Item>, 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<Item>, 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<Item>, 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,
|
||||
|
||||
@@ -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{}", "<a:q>".repeat(height - 1), "</a:q>".repeat(height - 1));
|
||||
let packet = format!(
|
||||
"<rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'><rdf:Description xmlns:a='http://a/'><a:p>{nest}</a:p></rdf:Description></rdf:RDF>"
|
||||
);
|
||||
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);
|
||||
|
||||
+2
-1
@@ -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.
|
||||
|
||||
|
||||
Reference in new issue
Block a user