mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-30 18:27:00 +01:00
Merge pull request #4190 from storytold/integration-craft-libs-20261129-4
Integration batch integration-craft-libs-20261129-4
This commit is contained in:
6 files changed
+121
-11
No files matched your search
@@ -15,7 +15,7 @@ pub type Migration = fn(Value) -> Result<Value, String>;
|
||||
/// edit a shipped one.
|
||||
pub const MIGRATIONS: &[Migration] = &[
|
||||
v1_to_v2, v2_to_v3, v3_to_v4, v4_to_v5, v5_to_v6, v6_to_v7, v7_to_v8, v8_to_v9, v9_to_v10, v10_to_v11, v11_to_v12, v12_to_v13, v13_to_v14,
|
||||
v14_to_v15, v15_to_v16, v16_to_v17, v17_to_v18, v18_to_v19,
|
||||
v14_to_v15, v15_to_v16, v16_to_v17, v17_to_v18, v18_to_v19, v19_to_v20,
|
||||
];
|
||||
|
||||
/// The schema version this build writes (and the newest it reads).
|
||||
@@ -269,6 +269,12 @@ fn v18_to_v19(doc: Value) -> Result<Value, String> {
|
||||
Ok(doc)
|
||||
}
|
||||
|
||||
/// v19 → v20: effect masks may carry a colour range. Existing masks have none; older builds must
|
||||
/// refuse such projects instead of silently dropping saved colour ranges.
|
||||
fn v19_to_v20(doc: Value) -> Result<Value, String> {
|
||||
Ok(doc)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -340,10 +346,28 @@ mod tests {
|
||||
assert_eq!(file.project["items"]["2"]["kind"], script);
|
||||
assert_eq!(file.project["items"]["1"]["kind"], media);
|
||||
}
|
||||
/// Schema 20 (mask colour ranges): a v19 project upgrades with its data untouched, and a v20
|
||||
/// project with a colour range on a mask is read rather than refused as too new.
|
||||
#[test]
|
||||
fn v19_upgrades_and_v20_mask_colour_ranges_are_read() {
|
||||
let mask = json!({"mode": "add"});
|
||||
let v19 = json!({"format": FORMAT_ID, "schema_version": 19, "project": {"items": {}, "root": {}, "effects": [{"masks": [mask]}]}});
|
||||
let up = migrate_with(MIGRATIONS, v19.clone(), 19).unwrap();
|
||||
assert!(up["schema_version"].as_u64().unwrap() > 19);
|
||||
assert_eq!(up["project"], v19["project"]);
|
||||
let range = json!({"hue": [0.1, 0.2], "luma": [0.0, 0.2]});
|
||||
let mut v20 = up;
|
||||
v20["schema_version"] = json!(20);
|
||||
v20["project"]["effects"][0]["masks"][0]["color"] = range.clone();
|
||||
let bytes = serde_json::to_vec(&v20).unwrap();
|
||||
let (file, version): (EnvelopeIn<Value>, _) = decode(&bytes).unwrap();
|
||||
assert_eq!(version, 20);
|
||||
assert_eq!(file.project["effects"][0]["masks"][0]["color"], range);
|
||||
}
|
||||
#[test]
|
||||
fn rejects_wrong_future_and_damaged_envelopes() {
|
||||
assert!(matches!(detect_bytes(br#"{"format":"other","schema_version":1}"#), Err(FormatError::NotAProject(_))));
|
||||
assert!(matches!(detect_bytes(br#"{"schema_version":20}"#), Err(FormatError::TooNew { found: 20, supported: 19 })));
|
||||
assert!(matches!(detect_bytes(br#"{"schema_version":21}"#), Err(FormatError::TooNew { found: 21, supported: 20 })));
|
||||
assert!(matches!(detect_bytes(br#"{"schema_version":0}"#), Err(FormatError::TooOld { found: 0, oldest: 1 })));
|
||||
assert!(matches!(detect_bytes(b"{"), Err(FormatError::Corrupt(_))));
|
||||
assert!(matches!(migrate_with(MIGRATIONS, json!([]), 1), Err(FormatError::Migration { from: 1, .. })));
|
||||
|
||||
@@ -377,7 +377,20 @@ fn tokenize(s: &str, base: &Fmt, depth: usize, refs: &Refs) -> Vec<Node> {
|
||||
}
|
||||
b'&' => {
|
||||
let rest = s.get(i..).unwrap_or("");
|
||||
match within(rest, 33).find(';').and_then(|k| decode_entity(rest.get(1..k)?).map(|ch| (ch, k))) {
|
||||
match within(rest, 33).find(';').and_then(|k| {
|
||||
let name = rest.get(1..k)?;
|
||||
// CommonMark limits numeric references to seven decimal or six hexadecimal digits.
|
||||
if let Some(num) = name.strip_prefix('#') {
|
||||
let (digits, max) = match num.strip_prefix(['x', 'X']) {
|
||||
Some(hex) => (hex, 6),
|
||||
None => (num, 7),
|
||||
};
|
||||
if digits.len() > max {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
decode_entity(name).map(|ch| (ch, k))
|
||||
}) {
|
||||
Some((ch, k)) => {
|
||||
flush!();
|
||||
push_text(&mut nodes, &ch, base);
|
||||
@@ -415,7 +428,7 @@ fn angle_inlines(s: &str, start: usize, base: &Fmt) -> Option<(usize, Vec<Node>)
|
||||
} else if matches!(lower.trim_end_matches('/').trim(), "br") {
|
||||
push_text(&mut nodes, "\n", base);
|
||||
Some((end, nodes))
|
||||
} else if lower.starts_with("a ") && rest.get(inner.len() + 1..).is_some_and(|tail| tail.to_ascii_lowercase().starts_with("</a>")) {
|
||||
} else if lower.starts_with("a ") && rest.get(inner.len() + 1..).and_then(|tail| tail.get(..4)).is_some_and(|p| p.eq_ignore_ascii_case("</a>")) {
|
||||
if let Some(name) = tag_attr(inner, "id").or_else(|| tag_attr(inner, "name")) {
|
||||
nodes.push(Node::Inl(Inline::Anchor(name)));
|
||||
}
|
||||
|
||||
@@ -21,6 +21,29 @@ fn emphasis() {
|
||||
assert!(matches!(&s[0], Inline::Text(t, f) if t == "gone" && f.strike));
|
||||
}
|
||||
|
||||
/// An empty `<a id>` or `<a name>` is an anchor whatever the case of its `</a>` (wordcraft#3715).
|
||||
#[test]
|
||||
fn empty_html_anchor() {
|
||||
let anchors = |s: &str| inl(s).into_iter().filter_map(|i| if let Inline::Anchor(a) = i { Some(a) } else { None }).collect::<Vec<_>>();
|
||||
assert_eq!(anchors(r#"x <a id="n"></a> y"#), vec!["n"]);
|
||||
assert_eq!(anchors(r#"x <a name="m"></A> y"#), vec!["m"]);
|
||||
assert_eq!(anchors(r#"<A id="p"></a>"#), vec!["p"]);
|
||||
assert!(anchors(r#"<a id="n">text</a>"#).is_empty());
|
||||
assert!(anchors(r#"<a id="n"></"#).is_empty());
|
||||
assert!(anchors(r#"<a id="n"></aé"#).is_empty());
|
||||
}
|
||||
|
||||
/// Every `<a …>` tag lower-cased a copy of the rest of its paragraph to look for `</a>`
|
||||
/// (wordcraft#3715): quadratic on a paragraph of tags.
|
||||
#[test]
|
||||
fn a_paragraph_of_anchor_tags_parses_in_linear_time() {
|
||||
let src = "<a x>".repeat(1_000_000);
|
||||
let t = std::time::Instant::now();
|
||||
inl(&src);
|
||||
// Well under a second now; the per-tag copy took 40 s in a release build.
|
||||
assert!(t.elapsed() < std::time::Duration::from_secs(10), "{:?}", t.elapsed());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn links_code_escapes() {
|
||||
let s = inl("see [the *site*](http://x.org \"T\") and `a*b*` \\*lit\\* & <https://y.com>");
|
||||
@@ -36,6 +59,28 @@ fn links_code_escapes() {
|
||||
assert!(text.contains("*lit* & "), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn numeric_references_respect_commonmark_digit_limits() {
|
||||
for (input, expected) in [
|
||||
("�", "�"),
|
||||
("�", "�"),
|
||||
("�", "�"),
|
||||
("�", "�"),
|
||||
("A", "A"),
|
||||
("A", "A"),
|
||||
("A", "A"),
|
||||
("A", "A"),
|
||||
("A", "A"),
|
||||
("�", "\u{FFFD}"),
|
||||
("", "\u{10FFFF}"),
|
||||
("�", "\u{FFFD}"),
|
||||
("�", "\u{FFFD}"),
|
||||
("&", "&"),
|
||||
] {
|
||||
assert_eq!(spans(input), vec![(expected.into(), false, false)], "{input}");
|
||||
}
|
||||
}
|
||||
|
||||
/// Reference links: `[text][label]`, `[text][]` and `[text]` take their destination from a
|
||||
/// definition line, and the definition itself leaves the document. Before, none of the three
|
||||
/// was a link and the definition stayed as a paragraph of its own (#3238).
|
||||
|
||||
@@ -14,7 +14,7 @@ craft-proof = { git = "https://github.com/storytold/craft-libs", rev = "<commit>
|
||||
| Language | Spelling | Grammar | Hyphenation |
|
||||
|---|---|---|---|
|
||||
| English (en-US, en-CA, en-AU, en-GB and the British-spelling Englishes such as en-NZ) | SCOWL 2026 Hunspell dictionary per English + `en-extra.txt`, dialect policy (`en-variants.txt`); a dictionaries-folder English first; without SCOWL the installed Hunspell English, else the built-in Moby list ([below](#english-is-always-checked)) | yes | Moby list, our patterns, heuristic |
|
||||
| Polish, Portuguese, Italian | the installed Hunspell dictionary | yes | TeX patterns (embedded, or a Language pack) |
|
||||
| Polish, Portuguese (pt-BR; pt-PT and the regions that write like Portugal, such as pt-AO and pt-MZ, read `pt_PT`), Italian | the installed Hunspell dictionary | yes | TeX patterns (embedded, or a Language pack) |
|
||||
| Croatian, Turkish, Russian, Czech, Indonesian, Spanish (es, es-MX, es-419, es-UY) | the installed Hunspell dictionary (Russian also `ru-extra.txt`) | yes | Turkish and Russian rules; English fallback otherwise |
|
||||
| French (fr-FR, fr-CA and other French pack locales) | the installed Language pack or Hunspell dictionary; dictionary-driven elisions and accent-oriented suggestions | conservative typography and basic checks, with Quebec punctuation spacing in fr-CA | the Language pack (generic fr fallback) |
|
||||
| German | — | — | TeX patterns (embedded, or a Language pack) |
|
||||
|
||||
@@ -30,8 +30,9 @@ pub enum ProofLang {
|
||||
/// Bulgarian (`bg`, `bg-BG`): hyphenation by the Bulgarian syllable rules ([`crate::bg`]), and
|
||||
/// spelling when a Bulgarian Hunspell dictionary or `bg` pack is found. No grammar rules yet.
|
||||
Bg,
|
||||
/// Portuguese (`pt`, `pt-BR`; `pt-PT` is [`ProofLang::Pack`] `pt-pt`, with its own dictionary
|
||||
/// and these grammar rules and patterns): the installed Hunspell dictionary (VERO and
|
||||
/// Portuguese (`pt`, `pt-BR`; `pt-PT` and the regions that write like Portugal, `pt-AO`,
|
||||
/// `pt-MZ`…, are [`ProofLang::Pack`] `pt-pt`, with its own dictionary and these grammar rules
|
||||
/// and patterns): the installed Hunspell dictionary (VERO and
|
||||
/// friends, when there is one), the Portuguese grammar rules and the Portuguese hyphenation
|
||||
/// patterns.
|
||||
Pt,
|
||||
@@ -122,9 +123,11 @@ impl ProofLang {
|
||||
|
||||
/// The proofing language of a BCP 47 tag (`pl-PL`, `en_GB`, `PL`…). The primary subtag
|
||||
/// counts, and for English and Spanish the region too (`ProofLang::english`,
|
||||
/// `ProofLang::spanish`). No tag (or an empty one) is English, WordCraft's default editing
|
||||
/// language. Any other valid language tag is [`ProofLang::Pack`] with its [`PackLocale`];
|
||||
/// `x-none`, `und`, `zxx`, `mul`, `mis` and malformed tags are [`ProofLang::Other`].
|
||||
/// `ProofLang::spanish`), for Portuguese (Portugal and the regions CLDR gives `pt_PT` as
|
||||
/// parent) and for German (`CH`, `LI`). No tag (or an empty one) is English, WordCraft's
|
||||
/// default editing language. Any other valid language tag is [`ProofLang::Pack`] with its
|
||||
/// [`PackLocale`]; `x-none`, `und`, `zxx`, `mul`, `mis` and malformed tags are
|
||||
/// [`ProofLang::Other`].
|
||||
pub fn from_tag(tag: Option<&str>) -> ProofLang {
|
||||
let Some(tag) = tag.map(str::trim).filter(|t| !t.is_empty()) else { return ProofLang::En };
|
||||
let primary = tag.split(['-', '_', '.', '@']).next().unwrap_or("");
|
||||
@@ -136,7 +139,7 @@ impl ProofLang {
|
||||
ProofLang::Bg
|
||||
} else if primary.eq_ignore_ascii_case("pt") || primary.eq_ignore_ascii_case("por") {
|
||||
// European Portuguese spells differently: its own dictionary (`pt_PT`, a `pt-pt` pack).
|
||||
if region(tag).is_some_and(|r| r.eq_ignore_ascii_case("PT")) {
|
||||
if region(tag).is_some_and(|r| EUROPEAN_PORTUGUESE.iter().any(|e| e.eq_ignore_ascii_case(r))) {
|
||||
PackLocale::new("pt-PT").map_or(ProofLang::Pt, ProofLang::Pack)
|
||||
} else {
|
||||
ProofLang::Pt
|
||||
@@ -408,6 +411,12 @@ pub(crate) fn region(tag: &str) -> Option<&str> {
|
||||
const LATIN_AMERICA: [&str; 22] =
|
||||
["419", "005", "013", "029", "AR", "BO", "CL", "CO", "CR", "CU", "DO", "EC", "GT", "HN", "NI", "PA", "PE", "PR", "PY", "SV", "US", "VE"];
|
||||
|
||||
/// Regions whose Portuguese is Portugal's (the `pt-pt` [`ProofLang::Pack`]): Portugal and the
|
||||
/// regions CLDR gives `pt_PT` as their parent locale: Angola, Cape Verde, Equatorial Guinea,
|
||||
/// Guinea-Bissau, Macao, Mozambique, São Tomé and Príncipe and Timor-Leste, and Portuguese
|
||||
/// written in Switzerland, France and Luxembourg.
|
||||
const EUROPEAN_PORTUGUESE: [&str; 12] = ["PT", "AO", "CH", "CV", "FR", "GQ", "GW", "LU", "MO", "MZ", "ST", "TL"];
|
||||
|
||||
/// Regions whose English uses British spelling ([`ProofLang::EnGb`]; Australia has its own,
|
||||
/// [`ProofLang::EnAu`]): the United Kingdom (and the non-standard `UK`), Ireland, New Zealand, South Africa, India and other
|
||||
/// Commonwealth countries, the Crown dependencies and Europe (`150`).
|
||||
@@ -460,6 +469,23 @@ pub fn normalize_tag(tag: &str) -> Option<String> {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// The regions CLDR gives `pt_PT` as parent write like Portugal: they read the `pt-pt` pack,
|
||||
/// however the tag is written; other regions, no region and private use stay Brazilian.
|
||||
#[test]
|
||||
fn european_portuguese_regions_read_the_pt_pt_pack() {
|
||||
let pt_pt = ProofLang::Pack(PackLocale::new("pt-pt").unwrap());
|
||||
for tag in [" pt-PT ", "pt_PT.UTF-8", "por-PT", "pt_PT@euro", "pt-Latn-PT", "pt-AO", "pt_MZ", "PT-cv", "pt-GW", "pt-ST", "pt-TL"] {
|
||||
assert_eq!(ProofLang::from_tag(Some(tag)), pt_pt, "{tag}");
|
||||
}
|
||||
for tag in ["pt-MO", "pt-GQ", "pt-CH", "pt-FR", "pt-LU", "pt_AO.UTF-8"] {
|
||||
assert_eq!(ProofLang::from_tag(Some(tag)), pt_pt, "{tag}");
|
||||
}
|
||||
for tag in ["pt", "pt-BR", "PT_br", "pt-BR.UTF-8", "por", "pt-Latn-BR", "pt-US", "pt-x-ao"] {
|
||||
assert_eq!(ProofLang::from_tag(Some(tag)), ProofLang::Pt, "{tag}");
|
||||
}
|
||||
assert_eq!((pt_pt.built_in(), ProofLang::Pt.built_in()), (ProofLang::Pt, ProofLang::Pt));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tags_normalize() {
|
||||
assert_eq!(normalize_tag("PL-pl").as_deref(), Some("pl-PL"));
|
||||
|
||||
@@ -228,11 +228,13 @@ fn regional_text_reads_its_own_regions_pack() {
|
||||
let (pt_br, pt_pt) = (pack("pt-br"), pack("pt-pt"));
|
||||
packs::reload();
|
||||
assert_eq!(chosen_dictionary(tag("pt-PT"), &system), Some(pt_pt.clone()));
|
||||
assert_eq!(chosen_dictionary(tag("pt-AO"), &system), Some(pt_pt.clone()), "Angola writes like Portugal");
|
||||
assert_eq!(chosen_dictionary(tag("pt-BR"), &system), Some(pt_br.clone()));
|
||||
std::fs::remove_dir_all(pt_br.parent().unwrap()).unwrap();
|
||||
let sys_pt_br = sys("pt_BR");
|
||||
packs::reload();
|
||||
assert_eq!(chosen_dictionary(tag("pt-PT"), &system), Some(pt_pt.clone()));
|
||||
assert_eq!(chosen_dictionary(tag("pt-MZ"), &system), Some(pt_pt.clone()), "never the system's Brazilian one");
|
||||
assert_eq!(chosen_dictionary(tag("pt-BR"), &system), Some(sys_pt_br), "another region's pack after the system's right region");
|
||||
std::fs::remove_dir_all(&packs_dir).unwrap();
|
||||
|
||||
|
||||
Reference in new issue
Block a user