mirror of
https://github.com/storytold/wordcraft.git
synced 2026-11-06 02:09:46 -06:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f2f5eda5e6 |
+2
-1
File diff suppressed because one or more lines are too long
@@ -1,6 +1,9 @@
|
||||
use super::xml::{Attrs, Ev, events, get, length, local};
|
||||
use super::{Align, Arc, Columns, Fmt, HashMap, Kind, Rgb, VAlign, model};
|
||||
|
||||
/// Longest style name looked at for a paragraph kind ("Heading 1", "Preformatted Text"…).
|
||||
const MAX_KIND_NAME: usize = 256;
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// Import
|
||||
|
||||
@@ -346,8 +349,11 @@ impl Styles {
|
||||
let mut kind_at = chain.len();
|
||||
for (i, (n, s)) in chain.iter().enumerate() {
|
||||
kind_at = i;
|
||||
let dn = s.display.as_deref().unwrap_or(n).to_ascii_lowercase().replace("_20_", " ");
|
||||
let nn = n.to_ascii_lowercase().replace("_20_", " ");
|
||||
// The names that give a kind are short: a longer one isn't normalised, so a huge
|
||||
// display name isn't copied again for every paragraph that uses the style.
|
||||
let norm = |v: &str| if v.len() <= MAX_KIND_NAME { v.to_ascii_lowercase().replace("_20_", " ") } else { String::new() };
|
||||
let dn = norm(s.display.as_deref().unwrap_or(n));
|
||||
let nn = norm(n);
|
||||
for cand in [dn.as_str(), nn.as_str()] {
|
||||
if let Some(d) = cand.strip_prefix("heading ").and_then(|r| r.trim().parse::<u8>().ok()) {
|
||||
kind = Kind::Heading(d.clamp(1, 6));
|
||||
|
||||
@@ -1135,3 +1135,18 @@ fn meta_creation_date_timezones_and_fallbacks() {
|
||||
assert_eq!(m2.created, "2024-03-05T13:45:00Z");
|
||||
assert_eq!(m2.author, "Solo Creator");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn style_kinds_come_from_short_names_only() {
|
||||
let mut s = styles::Styles::default();
|
||||
let long = "x".repeat(1 << 20);
|
||||
let xml = format!(
|
||||
r#"<office:document-styles xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"><office:styles><style:style style:name="Heading_20_2" style:display-name="Heading 2" style:family="paragraph"/><style:style style:name="Big" style:display-name="{long}" style:family="paragraph"/></office:styles></office:document-styles>"#
|
||||
);
|
||||
s.read(xml.as_bytes(), false);
|
||||
assert_eq!(s.para("Heading_20_2").0, Kind::Heading(2));
|
||||
// A huge display name isn't normalised (again) for every paragraph that names it.
|
||||
for _ in 0..10_000 {
|
||||
assert_eq!(s.para("Big").0, Kind::Normal);
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user