mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-27 16:59:43 +01:00
Scripts: XMPFile methods take only XMPFile objects, and x-default updates its matches
- XMPFile's methods refuse an object the XMPFile constructor did not make, so a plain object or one made from XMPFile.prototype with properties of the same names is not read or written as a file. - setLocalizedText for x-default also changes the items that had the text of the x-default item. Tests: a_script_cannot_change_the_state_of_an_xmp_file in crates/engine/src/tests/script_xmp.rs and localized_text_for_x_default_changes_the_items_that_matched_it in crates/js/src/xmp_tests.rs.
This commit is contained in:
1 parent
72c4875990
commit
606157cc34
4 files changed
+38
-11
No files matched your search
@@ -163,7 +163,8 @@ fn a_scanned_sidecar_is_read_only() {
|
||||
|
||||
/// The state an `XMPFile` keeps cannot be changed by the script: a sidecar opened for reading is
|
||||
/// not written by `closeFile` after the script assigns, defines or deletes the properties that
|
||||
/// hold the packet, the path and the open flags.
|
||||
/// hold the packet, the path and the open flags, or calls `closeFile` on another object that holds
|
||||
/// properties of the same names, plain or made from `XMPFile.prototype`.
|
||||
#[test]
|
||||
fn a_script_cannot_change_the_state_of_an_xmp_file() {
|
||||
let base = folder("state");
|
||||
@@ -177,10 +178,14 @@ fn a_script_cannot_change_the_state_of_an_xmp_file() {
|
||||
x.__vcXmpPending = 'not a packet'; x.__vcXmpFlags = XMPConst.OPEN_FOR_UPDATE;\n\
|
||||
try {{ Object.defineProperty(x, '__vcXmpPending', {{ value: 'not a packet' }}); out.push('defined'); }} catch (e) {{ out.push(e.name); }}\n\
|
||||
out.push(typeof x.__vcXmpPending, x.getFileInfo().openFlags == XMPConst.OPEN_FOR_READ);\n\
|
||||
[{{}}, Object.create(XMPFile.prototype)].forEach(function (fake) {{\n\
|
||||
fake.__vcXmpPath = p; fake.__vcXmpRun = x.__vcXmpRun; fake.__vcXmpOpen = true; fake.__vcXmpPending = 'not a packet';\n\
|
||||
try {{ XMPFile.prototype.closeFile.call(fake); out.push('closed'); }} catch (e) {{ out.push(e.name); }}\n\
|
||||
}});\n\
|
||||
x.closeFile(); out.push(delete x.__vcXmpPath); vc.log(out); vc.log(p);"
|
||||
),
|
||||
);
|
||||
assert_eq!(log[0], json!(["TypeError", "undefined", true, false]));
|
||||
assert_eq!(log[0], json!(["TypeError", "undefined", true, "TypeError", "TypeError", false]));
|
||||
let text = std::fs::read_to_string(log[1].as_str().unwrap()).unwrap();
|
||||
assert_eq!(text, PACKET);
|
||||
let _ = std::fs::remove_dir_all(base);
|
||||
|
||||
@@ -248,6 +248,8 @@ pub(crate) enum Class {
|
||||
XmlFilter(Box<XmlFilterState>),
|
||||
/// An `XMPMeta` object and its metadata ([`crate::xmp_classes`]).
|
||||
Xmp(Box<crate::xmp::Meta>),
|
||||
/// An object the `XMPFile` constructor made ([`crate::xmp_classes`]).
|
||||
XmpFile,
|
||||
}
|
||||
|
||||
/// What an XML value is.
|
||||
@@ -311,7 +313,7 @@ impl Class {
|
||||
Class::XmlList(_) => "XMLList",
|
||||
Class::Namespace(_) => "Namespace",
|
||||
Class::QName { .. } => "QName",
|
||||
Class::XmlFilter(_) | Class::Xmp(_) => "Object",
|
||||
Class::XmlFilter(_) | Class::Xmp(_) | Class::XmpFile => "Object",
|
||||
Class::Math => "Math",
|
||||
Class::Json => "JSON",
|
||||
Class::Ordinary | Class::ForIn(_) => "Object",
|
||||
|
||||
@@ -770,7 +770,15 @@ fn set_localized_text(i: &mut Interp, this: Value, args: &[Value], _: bool) -> J
|
||||
meta_of(i, o)?.find(&ns, &name).unwrap_or(0)
|
||||
}
|
||||
};
|
||||
let grow = 2 * (64 + value.len() + specific.len());
|
||||
// An item and the `x-default` item change, or every item with the text of the `x-default` one.
|
||||
let changed = match meta_of(i, o)?.props.get(at).map(|p| &p.value) {
|
||||
Some(xmp::Value::Array { items, .. }) if specific.eq_ignore_ascii_case("x-default") => {
|
||||
let old = items.iter().find(|i| same_lang(i.lang.as_deref(), "x-default")).map(|i| &i.text);
|
||||
items.iter().filter(|i| Some(&i.text) == old).count().max(2)
|
||||
}
|
||||
_ => 2,
|
||||
};
|
||||
let grow = changed.saturating_mul(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, &generic, &specific, value);
|
||||
@@ -781,14 +789,14 @@ fn set_localized_text(i: &mut Interp, this: Value, args: &[Value], _: bool) -> J
|
||||
/// 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.
|
||||
/// text; a change to the `x-default` item also changes the items that had its 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 {
|
||||
Some(d) => {
|
||||
if let Some(item) = items.get_mut(d) {
|
||||
item.text = value;
|
||||
match default.and_then(|d| items.get(d)).map(|i| i.text.clone()) {
|
||||
Some(old) => {
|
||||
for item in items.iter_mut().filter(|i| i.text == old) {
|
||||
item.text.clone_from(&value);
|
||||
}
|
||||
}
|
||||
None => items.insert(0, Item { text: value, lang: Some("x-default".into()) }),
|
||||
@@ -917,9 +925,10 @@ not_yet_fns! {
|
||||
|
||||
// ---- XMPFile ----
|
||||
|
||||
/// The `XMPFile` `this`, refused when it is not one.
|
||||
/// The `XMPFile` `this`, refused when the `XMPFile` constructor did not make it.
|
||||
fn this_file(i: &mut Interp, this: &Value, what: &str) -> JsResult<ObjId> {
|
||||
if let Some(o) = this.as_object()
|
||||
&& matches!(i.heap.object(o).map(|ob| &ob.class), Some(Class::XmpFile))
|
||||
&& matches!(slot(i, o, PATH)?, Value::String(_))
|
||||
{
|
||||
return Ok(o);
|
||||
@@ -970,7 +979,7 @@ fn file_ctor(i: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Valu
|
||||
let reported = if format == FILE_UNKNOWN && sidecar { FILE_XML } else { format };
|
||||
let file_proto = ids(i)?.file_proto;
|
||||
i.reserve_heap(OBJECT_BYTES + 12 * PROP_BYTES)?;
|
||||
let o = i.heap.alloc_object(Object::new(Class::Ordinary, Some(file_proto)));
|
||||
let o = i.heap.alloc_object(Object::new(Class::XmpFile, Some(file_proto)));
|
||||
i.rooted(&[Value::Object(o)], |i| {
|
||||
let packet = field(i, "packet")?;
|
||||
let fields = [
|
||||
|
||||
@@ -329,6 +329,17 @@ fn localized_text_updates_a_single_generic_language_item() {
|
||||
assert_eq!(string(src), "2|Color|Color|4|Couleur|Color");
|
||||
}
|
||||
|
||||
/// Setting the `x-default` item also changes the items that had its text, and leaves the others.
|
||||
#[test]
|
||||
fn localized_text_for_x_default_changes_the_items_that_matched_it() {
|
||||
let src = "var ns = 'http://example.com/lt/'; XMPMeta.registerNamespace(ns, 'lt'); var m = new XMPMeta();
|
||||
m.setLocalizedText(ns, 'label', null, 'en-US', 'old');
|
||||
m.setLocalizedText(ns, 'label', null, 'fr-FR', 'vieux');
|
||||
m.setLocalizedText(ns, 'label', null, 'x-default', 'new');
|
||||
['x-default', 'en-US', 'fr-FR'].map(function (l) { return m.getLocalizedText(ns, 'label', null, l); }).join('|')";
|
||||
assert_eq!(string(src), "new|new|vieux");
|
||||
}
|
||||
|
||||
/// 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]
|
||||
|
||||
Reference in new issue
Block a user