Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

M6: util.scand reads dates as another viewer does

Eric Kaden86e44dc6d53c
diff --git a/crates/js/src/formcalc.rs b/crates/js/src/formcalc.rs
index dc32d457b..43f32a11b 100644
--- a/crates/js/src/formcalc.rs
+++ b/crates/js/src/formcalc.rs
@@ -1924,7 +1924,7 @@ fn parse_date(text: &str, picture: &str) -> Option<(i64, u32, u32)> {
}
/// Number of days in month `m` (1-12) of year `y`, leap years included.
-fn days_in(y: i64, m: u32) -> u32 {
+pub(crate) fn days_in(y: i64, m: u32) -> u32 {
match m {
2 if (y % 4 == 0 && y % 100 != 0) || y % 400 == 0 => 29,
2 => 28,
diff --git a/crates/js/src/lib.rs b/crates/js/src/lib.rs
index d8d9fa8ba..0e7de9891 100644
--- a/crates/js/src/lib.rs
+++ b/crates/js/src/lib.rs
@@ -14,7 +14,7 @@
//! `isBoxChecked`, `checkThisBox`, `exportValues`, `charLimit`, `textColor`, `fillColor`,
//! `setFocus`.
//! - `app` (`alert`, `beep`, `response`, `launchURL`, `viewerType`, `viewerVersion`,
-//! `platform`, `setTimeOut`), `util` (`printf`, `printd`, `printx`), `console` (`println`,
+//! `platform`, `setTimeOut`), `util` (`printf`, `printd`, `printx`, `scand`), `console` (`println`,
//! `show`, `clear`), `display` and `color`.
//!
//! The engine is a sandbox: no file, network or timer access exists, and loops and recursion
@@ -26,7 +26,7 @@
use std::cell::RefCell;
use std::collections::BTreeSet;
-use boa_engine::object::builtins::{JsArray, JsFunction};
+use boa_engine::object::builtins::{JsArray, JsDate, JsFunction};
use boa_engine::object::{FunctionObjectBuilder, ObjectInitializer};
use boa_engine::property::Attribute;
use boa_engine::{Context, JsError, JsNativeError, JsObject, JsResult, JsString, JsValue, NativeFunction, Source, js_string};
@@ -802,6 +802,318 @@ fn printd(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResult<JsValue>
Ok(s(&out))
}
+/// A field of a `util.scand` format.
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+enum DateUnit {
+ /// `yyyy`: four digits.
+ Year,
+ /// `yy`: two digits, read as 1950–2049.
+ ShortYear,
+ Month,
+ /// `mmm`, `mmmm`: a month's name or its three-letter abbreviation.
+ MonthName,
+ Day,
+ /// `ddd`, `dddd`: a weekday's name, read and not checked.
+ Weekday,
+ Hour,
+ /// `h`, `hh`: 0–12, with `tt` for the afternoon.
+ Hour12,
+ Minute,
+ Second,
+ /// `t`, `tt`: am or pm.
+ AmPm,
+}
+
+impl DateUnit {
+ fn is_date(self) -> bool {
+ matches!(self, Self::Year | Self::ShortYear | Self::Month | Self::MonthName | Self::Day)
+ }
+
+ /// Read from a run of digits, not from letters.
+ fn is_numeric(self) -> bool {
+ !matches!(self, Self::MonthName | Self::Weekday | Self::AmPm)
+ }
+}
+
+/// A piece of a `util.scand` format.
+#[derive(Debug, PartialEq, Eq)]
+enum ScanPart {
+ /// A field, the digits it takes when typed together with the next one (`width`), and whether
+ /// the next one follows it with nothing between them in the format (`joined`).
+ Field { unit: DateUnit, width: usize, joined: bool },
+ /// Letters the format writes as they are, which the text must have there.
+ Word(String),
+}
+
+fn scan_parts(fmt: &str) -> Vec<ScanPart> {
+ let chars: Vec<char> = fmt.chars().collect();
+ let mut parts: Vec<ScanPart> = Vec::new();
+ // The last part is a field written right before this point, or letters still being collected.
+ let (mut after_field, mut in_word) = (false, false);
+ let mut i = 0;
+ while let Some(&c) = chars.get(i) {
+ // Counted for field letters only, so a long run of anything else is read once.
+ let run = if "ymdHhMst".contains(c) { chars.get(i..).unwrap_or_default().iter().take_while(|x| **x == c).count() } else { 1 };
+ let field = match (c, run) {
+ ('y', 4..) => Some((DateUnit::Year, 4)),
+ ('y', _) => Some((DateUnit::ShortYear, 2)),
+ ('m', 3..) => Some((DateUnit::MonthName, 0)),
+ ('m', n) => Some((DateUnit::Month, n)),
+ ('d', 3..) => Some((DateUnit::Weekday, 0)),
+ ('d', n) => Some((DateUnit::Day, n)),
+ ('H', n) => Some((DateUnit::Hour, n.min(2))),
+ ('h', n) => Some((DateUnit::Hour12, n.min(2))),
+ ('M', n) => Some((DateUnit::Minute, n.min(2))),
+ ('s', n) => Some((DateUnit::Second, n.min(2))),
+ ('t', _) => Some((DateUnit::AmPm, 0)),
+ _ => None,
+ };
+ if let Some((unit, width)) = field {
+ if after_field
+ && unit.is_numeric()
+ && let Some(ScanPart::Field { unit: last, joined, .. }) = parts.last_mut()
+ && last.is_numeric()
+ {
+ *joined = true;
+ }
+ parts.push(ScanPart::Field { unit, width, joined: false });
+ (after_field, in_word) = (true, false);
+ i += run;
+ continue;
+ }
+ // `\` writes the next character as it is.
+ let (literal, step) = if c == '\\' { (chars.get(i + 1).copied(), 2) } else { (Some(c), 1) };
+ i += step;
+ after_field = false;
+ match literal {
+ Some(l) if l.is_alphanumeric() => match parts.last_mut() {
+ Some(ScanPart::Word(w)) if in_word => w.push(l),
+ _ => {
+ parts.push(ScanPart::Word(l.to_string()));
+ in_word = true;
+ }
+ },
+ _ => in_word = false,
+ }
+ }
+ parts
+}
+
+/// A run of digits or of letters in the text `util.scand` reads.
+enum ScanToken<'a> {
+ Digits(&'a str),
+ Letters(&'a str),
+}
+
+/// The text's runs of digits and of letters, in order; anything else only separates them, and
+/// may not begin or end the text. `None` when it does.
+fn scan_tokens(text: &str) -> Option<Vec<ScanToken<'_>>> {
+ // Some(true) for a digit, Some(false) for a letter, None for a separator.
+ let kind = |c: char| (c.is_ascii_digit() || c.is_alphabetic()).then_some(c.is_ascii_digit());
+ kind(text.chars().next()?)?;
+ kind(text.chars().next_back()?)?;
+ let mut out = Vec::new();
+ let mut start: Option<(usize, bool)> = None;
+ for (at, c) in text.char_indices().chain(std::iter::once((text.len(), ' '))) {
+ let k = kind(c);
+ if let Some((from, digits)) = start
+ && k != Some(digits)
+ {
+ let piece = text.get(from..at)?;
+ out.push(if digits { ScanToken::Digits(piece) } else { ScanToken::Letters(piece) });
+ start = None;
+ }
+ if let Some(digits) = k
+ && start.is_none()
+ {
+ start = Some((at, digits));
+ }
+ }
+ Some(out)
+}
+
+/// What `util.scand` reads.
+#[derive(Debug, PartialEq, Eq)]
+enum Scanned {
+ /// The text is empty: today, at noon.
+ Today,
+ /// A date and time: year, month (1–12), day, hours, minutes, seconds.
+ At(i32, u32, u32, u32, u32, u32),
+}
+
+/// The fields `util.scand` has read so far.
+#[derive(Default)]
+struct ScanValues {
+ y: Option<i32>,
+ mo: Option<u32>,
+ d: Option<u32>,
+ h: Option<u32>,
+ mi: Option<u32>,
+ sec: Option<u32>,
+ pm: Option<bool>,
+ /// The hour was read from `h` or `hh`.
+ twelve: bool,
+}
+
+impl ScanValues {
+ fn set(&mut self, unit: DateUnit, v: u32) {
+ match unit {
+ DateUnit::Year => self.y = i32::try_from(v).ok(),
+ DateUnit::ShortYear => self.y = i32::try_from(v).ok().map(|v| if v < 50 { 2000 + v } else { 1900 + v }),
+ DateUnit::Month => self.mo = Some(v),
+ DateUnit::Day => self.d = Some(v),
+ DateUnit::Hour => self.h = Some(v),
+ DateUnit::Hour12 => {
+ self.h = Some(v);
+ self.twelve = true;
+ }
+ DateUnit::Minute => self.mi = Some(v),
+ DateUnit::Second => self.sec = Some(v),
+ DateUnit::MonthName | DateUnit::Weekday | DateUnit::AmPm => {}
+ }
+ }
+}
+
+/// Reads `text` by a `util.scand` format, as another viewer was observed to (black-box):
+///
+/// - Fields may be separated by anything but digits and letters, whether or not the format
+/// separates them, but nothing may come before the first or after the last.
+/// - Digits typed together fill the format's fields written together with exactly as many
+/// digits as each has letters (`yyyymd`: 4, 1 and 1). A run of digits for one field alone may
+/// have one or two, except a four-digit year.
+/// - `yy` is 1950–2049; month names may be written in full or as three letters, in any case.
+/// - Impossible dates are refused, but a month or day of 0 is read as 1.
+/// - A format with no year, month or day reads nothing; an empty text is today, at noon.
+///
+/// Not observed: a year left out is `this_year`, and a month or day left out is 1.
+fn scan_date(fmt: &str, text: &str, this_year: i32) -> Option<Scanned> {
+ let parts = scan_parts(fmt);
+ if !parts.iter().any(|p| matches!(p, ScanPart::Field { unit, .. } if unit.is_date())) {
+ return None;
+ }
+ if text.is_empty() {
+ return Some(Scanned::Today);
+ }
+ let tokens = scan_tokens(text)?;
+ let mut v = ScanValues::default();
+ let (mut p, mut t) = (0, 0);
+ while let Some(part) = parts.get(p) {
+ let token = tokens.get(t)?;
+ t += 1;
+ match (part, token) {
+ (ScanPart::Word(w), ScanToken::Letters(l) | ScanToken::Digits(l)) => {
+ if w.to_lowercase() != l.to_lowercase() {
+ return None;
+ }
+ p += 1;
+ }
+ (ScanPart::Field { unit: DateUnit::MonthName, .. }, ScanToken::Letters(l)) => {
+ v.mo = Some(named(&MONTHS, l)?.saturating_add(1));
+ p += 1;
+ }
+ (ScanPart::Field { unit: DateUnit::Weekday, .. }, ScanToken::Letters(l)) => {
+ named(&DAYS, l)?;
+ p += 1;
+ }
+ (ScanPart::Field { unit: DateUnit::AmPm, .. }, ScanToken::Letters(l)) => {
+ v.pm = Some(match l.to_lowercase().as_str() {
+ "am" | "a" => false,
+ "pm" | "p" => true,
+ _ => return None,
+ });
+ p += 1;
+ }
+ (ScanPart::Field { unit, width, joined }, ScanToken::Digits(run)) if unit.is_numeric() => {
+ // The fields written together from here, no more than the run's digits can fill,
+ // and the fewest of them the run fills.
+ let mut group = vec![(*unit, *width)];
+ let (mut more, mut widths) = (*joined, *width);
+ while more
+ && widths < run.len()
+ && let Some(ScanPart::Field { unit, width, joined }) = parts.get(p + group.len())
+ {
+ group.push((*unit, *width));
+ widths = widths.saturating_add(*width);
+ more = *joined;
+ }
+ let mut sum = 0;
+ let fills = group.iter().position(|(_, w)| {
+ sum += w;
+ sum == run.len()
+ });
+ if let Some(last) = fills {
+ let mut at = 0;
+ for (unit, w) in group.iter().take(last + 1) {
+ v.set(*unit, run.get(at..at + w)?.parse().ok()?);
+ at += w;
+ }
+ p += last + 1;
+ } else if *unit != DateUnit::Year && run.len() <= 2 {
+ v.set(*unit, run.parse().ok()?);
+ p += 1;
+ } else {
+ return None;
+ }
+ }
+ _ => return None,
+ }
+ }
+ if t != tokens.len() {
+ return None;
+ }
+ let y = v.y.unwrap_or(this_year);
+ let mo = v.mo.unwrap_or(1).max(1);
+ let d = v.d.unwrap_or(1).max(1);
+ if mo > 12 || i64::from(d) > i64::from(formcalc::days_in(i64::from(y), mo)) {
+ return None;
+ }
+ let mut h = v.h.unwrap_or(0);
+ if v.twelve && h > 12 {
+ return None;
+ }
+ match v.pm {
+ Some(true) if h < 12 => h += 12,
+ Some(false) if h == 12 => h = 0,
+ _ => {}
+ }
+ let (mi, sec) = (v.mi.unwrap_or(0), v.sec.unwrap_or(0));
+ (h <= 23 && mi <= 59 && sec <= 59).then_some(Scanned::At(y, mo, d, h, mi, sec))
+}
+
+/// The index of the name `word` is, in full or as its first three letters, in any case.
+fn named(names: &[&str], word: &str) -> Option<u32> {
+ let word = word.to_lowercase();
+ let i = names.iter().position(|n| {
+ let n = n.to_lowercase();
+ word == n || n.get(..3) == Some(word.as_str())
+ })?;
+ u32::try_from(i).ok()
+}
+
+/// `util.scand(cFormat, cDate)`: the date `cDate` gives by a `util.printd` format, as a local
+/// `Date`, or `null` when it doesn't read as one (see [`scan_date`]). Only text is read.
+fn scand(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResult<JsValue> {
+ let Some(typed) = arg(args, 1).as_string().map(|t| t.to_std_string_escaped()) else {
+ return Ok(JsValue::null());
+ };
+ let fmt = text(&arg(args, 0), ctx)?;
+ let date = JsDate::new(ctx);
+ let this_year = date.get_full_year(ctx)?.to_number(ctx)?;
+ let this_year = if this_year.is_finite() { this_year as i32 } else { 2000 };
+ match scan_date(&fmt, &typed, this_year) {
+ None => return Ok(JsValue::null()),
+ Some(Scanned::Today) => {
+ date.set_hours(&[12.into(), 0.into(), 0.into(), 0.into()], ctx)?;
+ }
+ Some(Scanned::At(y, mo, d, h, mi, sec)) => {
+ // setFullYear, not the constructor, so years 0–99 stay as they are.
+ date.set_full_year(&[y.into(), (mo.saturating_sub(1)).into(), d.into()], ctx)?;
+ date.set_hours(&[h.into(), mi.into(), sec.into(), 0.into()], ctx)?;
+ }
+ }
+ Ok(date.into())
+}
+
/// `util.printx(cFormat, cSource)`: `?` any character, `X` letter or digit, `A` letter, `9`
/// digit, `*` the rest, `\` escape, `>` / `<` / `=` upper / lower / as is.
fn printx(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResult<JsValue> {
@@ -947,6 +1259,7 @@ fn install(ctx: &mut Context) -> JsResult<()> {
.function(NativeFunction::from_fn_ptr(printf), js_string!("printf"), 1)
.function(NativeFunction::from_fn_ptr(printd), js_string!("printd"), 2)
.function(NativeFunction::from_fn_ptr(printx), js_string!("printx"), 2)
+ .function(NativeFunction::from_fn_ptr(scand), js_string!("scand"), 2)
.build();
ctx.register_global_property(js_string!("util"), util, rw)?;
let console = ObjectInitializer::new(ctx)
diff --git a/crates/js/src/tests.rs b/crates/js/src/tests.rs
index 61d3a71e5..791da93a2 100644
--- a/crates/js/src/tests.rs
+++ b/crates/js/src/tests.rs
@@ -1132,3 +1132,182 @@ mod session {
assert!(o.abandoned || o.error.as_deref().is_some_and(|e| e.contains("too long")), "{o:?}");
}
}
+
+mod scand {
+ use super::*;
+ use crate::xfa::{XfaDoc, XfaEvent, run_xfa};
+
+ /// Runs `util.scand(format, text)` for each case and checks the date it gives, written as
+ /// `yyyy-mm-dd HH:MM:ss`, or `null`.
+ fn assert_scand(cases: &[(&str, &str, &str)]) {
+ let list: Vec<String> = cases.iter().map(|(f, t, _)| format!("['{f}', '{t}']")).collect();
+ let script = format!(
+ "event.value = [{}].map(function (c) {{ var d = util.scand(c[0], c[1]); \
+ return d === null ? 'null' : util.printd('yyyy-mm-dd HH:MM:ss', d); }}).join('|');",
+ list.join(", ")
+ );
+ let o = go(&script, &Event::field("Calculate", "total", ""));
+ assert_eq!(o.error, None);
+ let got: Vec<&str> = o.value.split('|').collect();
+ assert_eq!(got.len(), cases.len());
+ for ((f, t, want), got) in cases.iter().zip(got) {
+ assert_eq!(got, *want, "util.scand('{f}', '{t}')");
+ }
+ }
+
+ // The cases below are another viewer's answers, observed black-box with a synthetic file.
+
+ #[test]
+ fn scand_allows_separators_between_fields_but_nothing_before_or_after_them() {
+ assert_scand(&[
+ ("yyyymmdd", "20261018", "2026-10-18 00:00:00"),
+ ("yyyymmdd", "2026-10-18", "2026-10-18 00:00:00"),
+ ("yyyymmdd", "2026 10 18", "2026-10-18 00:00:00"),
+ ("mm/dd/yyyy", "10/18/2026", "2026-10-18 00:00:00"),
+ ("mm/dd/yyyy", "10-18-2026", "2026-10-18 00:00:00"),
+ ("yyyymmdd", " 20261018 ", "null"),
+ ("yyyymmdd", "20261018x", "null"),
+ ("yyyymmdd", "x20261018", "null"),
+ ]);
+ }
+
+ #[test]
+ fn scand_splits_digits_typed_together_by_the_formats_letter_counts() {
+ assert_scand(&[
+ ("yyyymmdd", "261018", "null"),
+ ("yyyymmdd", "2026118", "null"),
+ ("yyyymmdd", "202610181", "null"),
+ ("yymmdd", "20261018", "null"),
+ ("yyyymdd", "2026118", "2026-01-18 00:00:00"),
+ ("yyyymmd", "2026118", "2026-11-08 00:00:00"),
+ ("yyyymd", "2026118", "null"),
+ ("yyyymd", "202618", "2026-01-08 00:00:00"),
+ ("yyyymd", "20261018", "null"),
+ ("yymd", "2618", "2026-01-08 00:00:00"),
+ ("yymd", "261018", "null"),
+ ]);
+ }
+
+ #[test]
+ fn scand_refuses_impossible_dates_and_reads_a_zero_month_or_day_as_1() {
+ assert_scand(&[
+ ("yyyymmdd", "20261318", "null"),
+ ("yyyymmdd", "20260230", "null"),
+ ("yyyymmdd", "20260229", "null"),
+ ("yyyymmdd", "20240229", "2024-02-29 00:00:00"),
+ ("mm/dd/yyyy", "13/45/2026", "null"),
+ ("yyyymmdd", "20260018", "2026-01-18 00:00:00"),
+ ("yyyymmdd", "20261000", "2026-10-01 00:00:00"),
+ ]);
+ }
+
+ #[test]
+ fn scand_reads_lone_fields_two_digit_years_and_month_and_weekday_names() {
+ assert_scand(&[
+ ("mm/dd/yyyy", "1/5/2026", "2026-01-05 00:00:00"),
+ ("m/d/yyyy", "1/5/2026", "2026-01-05 00:00:00"),
+ ("m/d/yyyy", "01/05/2026", "2026-01-05 00:00:00"),
+ ("m/d/yy", "1/5/26", "2026-01-05 00:00:00"),
+ ("mm/dd/yyyy", "10/18/26", "null"),
+ ("yyyy/mm/dd", "26/10/18", "null"),
+ ("yymmdd", "261018", "2026-10-18 00:00:00"),
+ ("yymmdd", "491018", "2049-10-18 00:00:00"),
+ ("yymmdd", "501018", "1950-10-18 00:00:00"),
+ ("mmm d, yyyy", "Oct 18, 2026", "2026-10-18 00:00:00"),
+ ("mmm d, yyyy", "October 18, 2026", "2026-10-18 00:00:00"),
+ ("mmm d, yyyy", "oct 18, 2026", "2026-10-18 00:00:00"),
+ ("mmmm d, yyyy", "Oct 18, 2026", "2026-10-18 00:00:00"),
+ ("mmmm d, yyyy", "October 18, 2026", "2026-10-18 00:00:00"),
+ ("dd-mmm-yyyy", "18-Oct-2026", "2026-10-18 00:00:00"),
+ ("dddd, mmmm d, yyyy", "Sunday, October 18, 2026", "2026-10-18 00:00:00"),
+ ]);
+ }
+
+ #[test]
+ fn scand_reads_a_time_only_with_a_date() {
+ assert_scand(&[
+ ("mm/dd/yyyy HH:MM:ss", "10/18/2026 21:07:05", "2026-10-18 21:07:05"),
+ ("HH:MM", "09:07", "null"),
+ ("HH:MM", "9:07", "null"),
+ ("h:MM tt", "9:07 pm", "null"),
+ ("h:MM tt", "9:07 am", "null"),
+ ("h:MM tt", "12:07 am", "null"),
+ ("h:MM tt", "9:07pm", "null"),
+ ]);
+ }
+
+ #[test]
+ fn scand_reads_only_text_and_an_empty_text_is_today_at_noon() {
+ let o = go(
+ "var d = util.scand('yyyymmdd', ''), n = new Date(); \
+ event.value = [d.toDateString() == n.toDateString(), d.getHours(), d.getMinutes(), d.getSeconds(), \
+ util.scand('yyyymmdd', 20261018), util.scand(), util.scand('yyyymmdd')].join('|');",
+ &Event::field("Calculate", "total", ""),
+ );
+ assert_eq!(o.error, None);
+ assert_eq!(o.value, "true|12|0|0|||");
+ }
+
+ // Not observed: the choices made where the other viewer wasn't asked.
+
+ #[test]
+ fn scand_takes_this_year_when_none_is_given_and_reads_12_hour_times_and_written_letters() {
+ assert_scand(&[
+ ("mm/dd/yyyy h:MM tt", "10/18/2026 9:07 pm", "2026-10-18 21:07:00"),
+ ("mm/dd/yyyy h:MM tt", "10/18/2026 12:07 AM", "2026-10-18 00:07:00"),
+ ("mm/dd/yyyy h:MM tt", "10/18/2026 13:07 pm", "null"),
+ ("mm/dd/yyyy HH:MM", "10/18/2026 24:00", "null"),
+ ("yyyy-mm-ddTHH:MM", "2026-10-18T21:07", "2026-10-18 21:07:00"),
+ ("yyyy-mm-ddTHH:MM", "2026-10-18X21:07", "null"),
+ ]);
+ let o = go(
+ "var d = util.scand('mm/dd', '10/18'); event.value = [d.getFullYear() == new Date().getFullYear(), d.getMonth(), d.getDate()].join('|');",
+ &Event::field("Calculate", "total", ""),
+ );
+ assert_eq!(o.error, None);
+ assert_eq!(o.value, "true|9|18");
+ }
+
+ #[test]
+ fn scand_reads_long_formats_and_texts_in_linear_time() {
+ // Scripts can pass anything; native code isn't bounded by the loop limit.
+ let started = std::time::Instant::now();
+ let o = go(
+ "var x = new Array(50001).join('x'), md = new Array(50001).join('md'), ones = new Array(100000).join('1 ') + '1'; \
+ event.value = [util.scand(x + 'yyyy', x + '2026') !== null, util.scand('yyyy' + md, '2026 ' + ones) !== null].join('|');",
+ &Event::field("Calculate", "total", ""),
+ );
+ assert_eq!(o.error, None);
+ assert_eq!(o.value, "true|true");
+ assert!(started.elapsed() < std::time::Duration::from_secs(2), "{:?}", started.elapsed());
+ }
+
+ #[test]
+ fn xfa_scripts_check_typed_dates_with_util_scand() {
+ // The check a form runs when a date field is left: the first of six formats that reads
+ // the typed text, written back as yyyymmdd; or an invalid date. Results as another viewer gives them.
+ let check = |typed: &str| {
+ let script = format!(
+ "var six = ['yyyymmdd', 'yymmdd', 'yyyymdd', 'yyyymmd', 'yyyymd', 'yymd'], out = 'invalid'; \
+ for (var i = 0; i < six.length; i++) {{ var d = util.scand(six[i], '{typed}'); \
+ if (d !== null) {{ out = util.printd('yyyymmdd', d); break; }} }} out"
+ );
+ let event = XfaEvent { activity: "exit".into(), target: "form1[0].page1[0].total[0]".into(), ..Default::default() };
+ run_xfa(&script, &event, &XfaDoc::default(), &super::xfa_model::form(), Limits::default())
+ };
+ for (typed, want) in [
+ ("20261018", "20261018"),
+ ("261018", "20261018"),
+ ("2026118", "20260118"),
+ ("202618", "20260108"),
+ ("2618", "20260108"),
+ ("2026-10-18", "20261018"),
+ ("20261318", "invalid"),
+ ("20260230", "invalid"),
+ ] {
+ let o = check(typed);
+ assert_eq!(o.error, None, "{typed}: {o:?}");
+ assert_eq!(o.result.as_deref(), Some(want), "{typed}");
+ }
+ }
+}
diff --git a/crates/js/src/xfa.rs b/crates/js/src/xfa.rs
index a2361da7e..9a48ff16a 100644
--- a/crates/js/src/xfa.rs
+++ b/crates/js/src/xfa.rs
@@ -15,7 +15,7 @@ use boa_engine::object::builtins::{JsArray, JsProxy};
use boa_engine::property::Attribute;
use boa_engine::{Context, JsObject, JsResult, JsString, JsValue, NativeFunction, Source, js_string};
-use crate::{Limits, arg, as_number, error, function, platform, printd, printf, printx, refuse, s, text};
+use crate::{Limits, arg, as_number, error, function, platform, printd, printf, printx, refuse, s, scand, text};
/// What kind of template object a node is.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -1208,6 +1208,7 @@ fn install(ctx: &mut Context, event: &XfaEvent, doc: &XfaDoc, current: usize) ->
.function(NativeFunction::from_fn_ptr(printf), js_string!("printf"), 1)
.function(NativeFunction::from_fn_ptr(printd), js_string!("printd"), 2)
.function(NativeFunction::from_fn_ptr(printx), js_string!("printx"), 2)
+ .function(NativeFunction::from_fn_ptr(scand), js_string!("scand"), 2)
.build();
ctx.register_global_property(js_string!("util"), util, rw)?;
// Unqualified names: the current node's children, its siblings, then each ancestor's
diff --git a/parity/acrobat-features.toml b/parity/acrobat-features.toml
index 7aca7d5c8..b8c0c9f36 100644
--- a/parity/acrobat-features.toml
+++ b/parity/acrobat-features.toml
@@ -5136,8 +5136,8 @@ milestone = "M6"
status = "partial"
commands = ["tools.js_console"]
tools = ["js_run"]
-evidence = ["crates/js/src/tests.rs::calculate_scripts_read_fields_and_set_the_value", "crates/engine/src/tests.rs::form_javascript_validates_calculates_and_formats", "crates/automation/tests/automation.rs::javascript_through_tools"]
-notes = "boa (pure Rust) with the forms object model: event, getField/Field (value, readonly, display, colours, items, check boxes), app, util.printf/printd/printx, console, display, color; no annotations, layers or dialogs (app.response returns null) yet."
+evidence = ["crates/js/src/tests.rs::calculate_scripts_read_fields_and_set_the_value", "crates/js/src/tests.rs::scand_allows_separators_between_fields_but_nothing_before_or_after_them", "crates/engine/src/tests.rs::form_javascript_validates_calculates_and_formats", "crates/automation/tests/automation.rs::javascript_through_tools"]
+notes = "boa (pure Rust) with the forms object model: event, getField/Field (value, readonly, display, colours, items, check boxes), app, util.printf/printd/printx/scand (scand reads dates as another viewer was observed to, in AcroForm and XFA scripts), console, display, color; no annotations, layers or dialogs (app.response returns null) yet."
[[feature]]
id = "form.js-console"