diff --git a/crates/compose/src/grep.rs b/crates/compose/src/grep.rs index b387384f9..5a56920dc 100644 --- a/crates/compose/src/grep.rs +++ b/crates/compose/src/grep.rs @@ -432,15 +432,36 @@ impl Grep { Ok((out, more)) } - /// Matches in `text` and their replacements, `template` expanded per match (`$1`…; see - /// [`translate_replacement`]). Stops after the first match when `first_only`. - pub fn replacements(&self, text: &str, template: &str, first_only: bool) -> Result, String)>, GrepError> { + /// Matches in `text` that `keep` accepts and their replacements, `template` expanded per match + /// (`$1`…; see [`translate_replacement`]). Stops after the first kept match when `first_only`, + /// and with an error once the expanded replacements pass `max_bytes` in all (before any more + /// are made). + pub fn replacements( + &self, + text: &str, + template: &str, + first_only: bool, + max_bytes: usize, + keep: impl Fn(&Range) -> bool, + ) -> Result, String)>, GrepError> { let exp = fancy_regex::Expander::default(); let mut out = Vec::new(); + let mut bytes = 0usize; for caps in self.re.captures_iter(text) { let caps = caps.map_err(run_err)?; let Some(m) = caps.get(0) else { continue }; - out.push((m.range(), exp.expansion(template, &caps))); + if !keep(&m.range()) { + continue; + } + // Written into a buffer that refuses bytes past the budget, so an oversized + // expansion is rejected before it is allocated. + let mut w = BoundedBuf { buf: Vec::new(), left: max_bytes.saturating_sub(bytes) }; + if exp.write_expansion(&mut w, template, &caps).is_err() { + return Err(err(format!("the replacements would put more than {max_bytes} bytes in"))); + } + bytes += w.buf.len(); + let expanded = String::from_utf8(w.buf).map_err(|_| err("the replacement is not valid UTF-8".to_string()))?; + out.push((m.range(), expanded)); if first_only { break; } @@ -449,6 +470,27 @@ impl Grep { } } +/// A byte sink that fails a write that would pass its remaining budget (nothing is copied then). +struct BoundedBuf { + buf: Vec, + left: usize, +} + +impl std::io::Write for BoundedBuf { + fn write(&mut self, data: &[u8]) -> std::io::Result { + if data.len() > self.left { + return Err(std::io::ErrorKind::OutOfMemory.into()); + } + self.left -= data.len(); + self.buf.extend_from_slice(data); + Ok(data.len()) + } + + fn flush(&mut self) -> std::io::Result<()> { + Ok(()) + } +} + fn run_err(e: fancy_regex::Error) -> GrepError { err(format!("GREP search stopped: {e}")) } @@ -520,7 +562,13 @@ mod tests { assert_eq!(g.find_all("axb A.B a.b", 1).unwrap().first(), Some(&(4..7))); let g = grep(r"(\d+)\h(\l+)"); let tpl = translate_replacement(r"$2~S$1x\r\$$"); - assert_eq!(g.replacements("5 kg", &tpl, false).unwrap(), [(0..4, "kg\u{A0}5x\n$$".to_string())]); + assert_eq!(g.replacements("5 kg", &tpl, false, usize::MAX, |_| true).unwrap(), [(0..4, "kg\u{A0}5x\n$$".to_string())]); + // The total expanded size is bounded while the replacements are made, over the matches kept. + let g = grep("a"); + assert!(g.replacements("aaaa", "xxxx", false, 15, |_| true).is_err()); + assert_eq!(g.replacements("aaaa", "xxxx", false, 16, |_| true).unwrap().len(), 4); + assert_eq!(g.replacements("aaaa", "xxxx", false, 8, |m| m.start < 2).unwrap().len(), 2); + assert_eq!(g.replacements("aaaa", "xxxx", true, 4, |m| m.start >= 2).unwrap(), [(2..3, "xxxx".to_string())]); } #[test] diff --git a/crates/engine/src/cmd/book.rs b/crates/engine/src/cmd/book.rs index a4217f825..29f95741d 100644 --- a/crates/engine/src/cmd/book.rs +++ b/crates/engine/src/cmd/book.rs @@ -1,6 +1,8 @@ //! Books (File › New / Open Book): an ordered list of documents with continuous page numbers, //! styles synchronised from a style source, and one PDF for the whole book. Saved as `.dcbook`. +use std::collections::{HashMap, HashSet}; + use serde::{Deserialize, Serialize}; use serde_json::{Value, json}; @@ -177,6 +179,15 @@ fn paginate(s: &Session) -> Result { Ok(json!({"pages": next - 1, "chapters": chapters})) } +/// `list` with only the last entry of each name, in the order of those last entries. +#[cfg(not(target_arch = "wasm32"))] +fn keep_last(list: &[T], name: impl Fn(&T) -> &str) -> Vec<&T> { + let mut seen = HashSet::new(); + let mut out: Vec<&T> = list.iter().rev().filter(|x| seen.insert(name(x))).collect(); + out.reverse(); + out +} + #[cfg(not(target_arch = "wasm32"))] fn sync_styles(s: &Session) -> Result { let b = s.book.as_ref().ok_or_else(no_book)?; @@ -187,6 +198,11 @@ fn sync_styles(s: &Session) -> Result { let styles = src.styles.clone(); let swatches = src.swatches.clone(); drop(src); + // A name the source defines twice keeps its last definition, at its last position. + let paragraph = keep_last(&styles.paragraph, |x| x.name.as_str()); + let character = keep_last(&styles.character, |x| x.name.as_str()); + let (paragraph_names, character_names): (HashSet<&str>, HashSet<&str>) = + (paragraph.iter().map(|x| x.name.as_str()).collect(), character.iter().map(|x| x.name.as_str()).collect()); let mut n = 0; for (i, path) in paths.iter().enumerate() { if i == b.style_source { @@ -195,21 +211,27 @@ fn sync_styles(s: &Session) -> Result { writable_chapter(s, path, "book.syncStyles")?; let mut d = load(path)?; { + // The source's styles replace the chapter's of the same names and go last, in the + // source's order (one pass over each list, not one per style). let st = d.styles_mut(); - for ps in &styles.paragraph { - st.paragraph.retain(|x| x.name != ps.name); - st.paragraph.push(ps.clone()); - } - for cs in &styles.character { - st.character.retain(|x| x.name != cs.name); - st.character.push(cs.clone()); - } + st.paragraph.retain(|x| !paragraph_names.contains(x.name.as_str())); + st.paragraph.extend(paragraph.iter().map(|x| (*x).clone())); + st.character.retain(|x| !character_names.contains(x.name.as_str())); + st.character.extend(character.iter().map(|x| (*x).clone())); + } + // First index per name, as Document::swatch resolves colours. + let mut at: HashMap = HashMap::new(); + for (i, w) in d.swatches.iter().enumerate() { + at.entry(w.name.clone()).or_insert(i); } for sw in &swatches { - match d.swatches.iter_mut().find(|w| w.name == sw.name) { + match at.get(&sw.name).and_then(|i| d.swatches.get_mut(*i)) { Some(w) if !w.locked => *w = sw.clone(), Some(_) => {} - None => d.swatches.push(sw.clone()), + None => { + at.insert(sw.name.clone(), d.swatches.len()); + d.swatches.push(sw.clone()); + } } } batch.stage(path, &d)?; @@ -431,4 +453,13 @@ mod tests { assert_eq!(t.execute("book.open", &json!({"path": p("book.dcbook")})).unwrap()["documents"], 2); std::fs::remove_dir_all(&dir).ok(); } + + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn a_source_style_defined_twice_keeps_its_last_definition() { + let list = [("a", 1), ("b", 2), ("a", 3), ("c", 4)]; + let kept: Vec<_> = super::keep_last(&list, |x| x.0).into_iter().copied().collect(); + // As the one-style-at-a-time loop left them: each name once, at its last position. + assert_eq!(kept, [("b", 2), ("a", 3), ("c", 4)]); + } } diff --git a/crates/engine/src/cmd/find.rs b/crates/engine/src/cmd/find.rs index b9ab42550..63a1debb9 100644 --- a/crates/engine/src/cmd/find.rs +++ b/crates/engine/src/cmd/find.rs @@ -316,19 +316,27 @@ impl Search { ) -> Result, String)>> { let limit = if first_only { 1 } else { usize::MAX }; match (&self.re, self.spans(d, sid, cell, r)) { - (Some(re), None) if grep => re.replacements(text, rep, first_only).map_err(|e| bad("find.change", e)), - (Some(re), Some(spans)) if grep => Ok(re - .replacements(text, rep, false) - .map_err(|e| bad("find.change", e))? - .into_iter() - .filter(|(m, _)| within(&spans, m)) - .take(limit) - .collect()), - _ => Ok(self.find_all(d, sid, cell, r, text, limit)?.into_iter().map(|m| (m, rep.to_string())).collect()), + (Some(re), spans) if grep => re + .replacements(text, rep, first_only, MAX_CHANGE_BYTES, |m| spans.as_ref().is_none_or(|s| within(s, m))) + .map_err(|e| bad("find.change", e)), + _ => { + let found = self.find_all(d, sid, cell, r, text, limit)?; + // Every match gets its own copy of the change: bounded before any is made. + if found.len().saturating_mul(rep.len()) > MAX_CHANGE_BYTES { + return Err(bad( + "find.change", + format!("changing {} matches to {} bytes each would put more than {MAX_CHANGE_BYTES} bytes in", found.len(), rep.len()), + )); + } + Ok(found.into_iter().map(|m| (m, rep.to_string())).collect()) + } } } } +/// The most text one Change All may put into a story: the matches times the change's length. +const MAX_CHANGE_BYTES: usize = 64 << 20; + #[derive(Clone, Debug, PartialEq, Eq)] enum Domain { Document, diff --git a/crates/engine/src/cmd/find/tests.rs b/crates/engine/src/cmd/find/tests.rs index 348acd294..edea872ad 100644 --- a/crates/engine/src/cmd/find/tests.rs +++ b/crates/engine/src/cmd/find/tests.rs @@ -959,3 +959,21 @@ fn change_takes_any_selection_find_next_made() { assert_eq!(s.doc().unwrap().doc.story(sid).unwrap().text, if grep { "a" } else { "ab" }); } } + +/// Change All with a long change and many matches is refused before any copy of the change is +/// made, not after the memory for all of them has gone. +#[test] +fn change_all_is_bounded_by_what_it_would_put_in() { + let mut s = Session::new(); + s.execute("file.new", &json!({})).unwrap(); + let text = "x ".repeat(2048); + s.execute("frame.create", &json!({"rect": [36, 36, 300, 300], "content": "text", "text": text})).unwrap(); + let huge = "y".repeat(40_000); + let e = s.execute("find.change", &json!({"find": "x", "change": huge})).unwrap_err().to_string(); + assert!(e.contains("would put more than"), "{e}"); + // GREP replacements are held to the same total while they are made. + let e = s.execute("find.change", &json!({"find": "x", "change": huge, "grep": true})).unwrap_err().to_string(); + assert!(e.contains("would put more than"), "{e}"); + let r = s.execute("find.change", &json!({"find": "x", "change": "yy"})).unwrap(); + assert_eq!(r["count"], 2048); +} diff --git a/crates/engine/src/cmd/paste_svg.rs b/crates/engine/src/cmd/paste_svg.rs index a5d738cef..fa3c1cffc 100644 --- a/crates/engine/src/cmd/paste_svg.rs +++ b/crates/engine/src/cmd/paste_svg.rs @@ -3,6 +3,8 @@ //! clipboard as SVG markup; each path becomes a DesignCraft path with its fill and stroke, groups //! stay groups, and text comes in as outlines. +use std::collections::{HashMap, HashSet}; + use designcraft_doc::{Content, Fill, Item, ItemId, Selection, Shape, Stroke, StrokeType}; use designcraft_geom::{Affine, BezPath, PathData, Point, Rect}; use designcraft_images::usvg; @@ -201,16 +203,46 @@ fn bounds(art: &[Art]) -> Option { .reduce(|a, b| a.union(b)) } +/// The document's RGB colour swatches by colour and its swatch names, looked up once per paste +/// (a pasted SVG can hold tens of thousands of colours). +struct PasteSwatches { + by_rgb: HashMap<[u32; 3], String>, + names: HashSet, +} + +impl PasteSwatches { + /// An RGB colour as a key: equal colours, equal keys (`+ 0.0` makes -0 and 0 one key). + fn key(r: f32, g: f32, b: f32) -> [u32; 3] { + [(r + 0.0).to_bits(), (g + 0.0).to_bits(), (b + 0.0).to_bits()] + } + + fn new(d: &designcraft_doc::Document) -> Self { + use designcraft_color::swatch::SwatchValue; + let mut by_rgb = HashMap::new(); + for w in &d.swatches { + if let SwatchValue::Color { color: designcraft_color::Color::Rgb { r, g, b }, .. } = &w.value { + // The first swatch of a colour is the one, as a search from the start finds it. + by_rgb.entry(Self::key(*r, *g, *b)).or_insert_with(|| w.name.clone()); + } + } + Self { by_rgb, names: d.swatches.iter().map(|w| w.name.clone()).collect() } + } +} + /// The swatch holding exactly `rgb`: an existing process colour, else a new unnamed one (hidden /// from the Swatches panel until Add to Swatches), as InDesign does for pasted colours. -fn swatch_for(d: &mut designcraft_doc::Document, rgb: [u8; 3]) -> String { +fn swatch_for(d: &mut designcraft_doc::Document, known: &mut PasteSwatches, rgb: [u8; 3]) -> String { use designcraft_color::swatch::{Swatch, SwatchValue}; let color = designcraft_color::Color::rgb8(rgb[0], rgb[1], rgb[2]); - if let Some(w) = d.swatches.iter().find(|w| matches!(&w.value, SwatchValue::Color { color: c, .. } if *c == color)) { - return w.name.clone(); + let designcraft_color::Color::Rgb { r, g, b } = color else { return String::new() }; + let key = PasteSwatches::key(r, g, b); + if let Some(name) = known.by_rgb.get(&key) { + return name.clone(); } let base = format!("R={} G={} B={}", rgb[0], rgb[1], rgb[2]); - let name = designcraft_doc::Styles::unique_name(|n| d.swatch(n).is_some(), &base); + let name = designcraft_doc::Styles::unique_name(|n| known.names.contains(n), &base); + known.names.insert(name.clone()); + known.by_rgb.insert(key, name.clone()); d.swatches.push(Swatch { name: name.clone(), value: SwatchValue::Color { color, color_type: designcraft_color::ColorType::Process }, @@ -221,7 +253,7 @@ fn swatch_for(d: &mut designcraft_doc::Document, rgb: [u8; 3]) -> String { name } -fn build(d: &mut designcraft_doc::Document, art: Vec, layer: designcraft_doc::LayerId, shift: Affine) -> Vec { +fn build(d: &mut designcraft_doc::Document, known: &mut PasteSwatches, art: Vec, layer: designcraft_doc::LayerId, shift: Affine) -> Vec { let mut out = Vec::new(); for a in art { match a { @@ -239,12 +271,12 @@ fn build(d: &mut designcraft_doc::Document, art: Vec, layer: designcraft_do let paint = fill.map(|f| f.opacity).or(stroke.as_ref().map(|(c, _)| c.opacity)).unwrap_or(1.0); let mut it = Item::new(ItemId(d.alloc()), layer, if line { Shape::GraphicLine } else { Shape::Path }, data); it.fill = match fill { - Some(f) => Fill::swatch(&swatch_for(d, f.rgb)), + Some(f) => Fill::swatch(&swatch_for(d, known, f.rgb)), None => Fill::none(), }; it.stroke = match stroke { Some((c, s)) => Stroke { - swatch: swatch_for(d, c.rgb), + swatch: swatch_for(d, known, c.rgb), weight: s.width, cap: s.cap, join: s.join, @@ -258,7 +290,7 @@ fn build(d: &mut designcraft_doc::Document, art: Vec, layer: designcraft_do out.push(it); } Art::Group(items, opacity) => { - let children = build(d, items, layer, shift); + let children = build(d, known, items, layer, shift); if children.is_empty() { continue; } @@ -312,7 +344,8 @@ fn paste_svg(s: &mut Session, p: &Value) -> Result { warnings.push(format!("{} embedded image(s) left out (place images with File › Place)", notes.images)); } s.edit(|d, sel| { - let items = build(d, art, layer, shift); + let mut known = PasteSwatches::new(d); + let items = build(d, &mut known, art, layer, shift); let mut ids = Vec::new(); for it in items { ids.push(it.id); diff --git a/crates/engine/src/cmd/select.rs b/crates/engine/src/cmd/select.rs index 0f77d5372..e351fe837 100644 --- a/crates/engine/src/cmd/select.rs +++ b/crates/engine/src/cmd/select.rs @@ -133,7 +133,8 @@ fn convert_shape(s: &mut Session, p: &Value) -> Result { const ID: &str = "object.convertShape"; let to = str_param(p, "to").ok_or_else(|| bad(ID, "`to` required"))?.to_string(); let ids = targets(s, p)?; - let sides = s.prefs.polygon_sides; + // The preference as object.polygon takes it: 3–100 sides, whatever prefs.set was given. + let sides = s.prefs.polygon_sides.clamp(3, 100); let inset = s.prefs.star_inset; s.edit(|d, _| { let mut changed = 0; diff --git a/crates/engine/src/cmd/spelling.rs b/crates/engine/src/cmd/spelling.rs index 4a43ab752..8ff9ac94d 100644 --- a/crates/engine/src/cmd/spelling.rs +++ b/crates/engine/src/cmd/spelling.rs @@ -161,6 +161,9 @@ const COMMON: &[&str] = &[ "yay", ]; +/// Longest word [`suggest`] makes candidates for. +pub const MAX_SUGGEST_CHARS: usize = 40; + /// Is `w` (lowercase) a known word, allowing common inflections of dictionary stems? pub fn known(dict: &Dictionary, user: &[String], w: &str) -> bool { // A typographer's apostrophe (don’t, we’re) is the word list's straight one. @@ -207,6 +210,11 @@ pub fn known(dict: &Dictionary, user: &[String], w: &str) -> bool { /// Up to 6 suggestions at edit distance 1, ranked: transpositions, replacements, deletions, insertions. pub fn suggest(dict: &Dictionary, w: &str) -> Vec { + // Each candidate is a copy of the word: 53 per position, quadratic in its length. No word of + // a dictionary is this long, so no edit of such a "word" is one either. Checked before collecting. + if w.chars().take(MAX_SUGGEST_CHARS + 1).count() > MAX_SUGGEST_CHARS { + return Vec::new(); + } let chars: Vec = w.chars().collect(); let mut out: Vec = Vec::new(); let mut push = |c: Vec| { @@ -325,6 +333,18 @@ mod tests { assert!(known(d, &["designcraft".into()], "designcraft")); } + /// A "word" longer than any in the dictionary gets no candidates: there would be more than + /// fifty copies of it per character. + #[test] + fn very_long_words_get_no_suggestions() { + // The dictionary holds the longest allowed word, so deleting one letter of the 41-letter + // input would be a hit if the length cap were not applied. + let d = Dictionary::from_entries(vec![("a".repeat(MAX_SUGGEST_CHARS), 0)]); + assert_eq!(suggest(&d, &"a".repeat(MAX_SUGGEST_CHARS)), Vec::::new()); + assert_eq!(suggest(&d, &"a".repeat(MAX_SUGGEST_CHARS - 1)), vec!["a".repeat(MAX_SUGGEST_CHARS)]); + assert!(suggest(&d, &"a".repeat(MAX_SUGGEST_CHARS + 1)).is_empty()); + } + #[test] fn user_dictionary_replaces_and_counts() { let mut s = Session::new(); diff --git a/crates/engine/src/cmd/table/styles.rs b/crates/engine/src/cmd/table/styles.rs index fa3717977..046904fef 100644 --- a/crates/engine/src/cmd/table/styles.rs +++ b/crates/engine/src/cmd/table/styles.rs @@ -133,6 +133,11 @@ fn reapply_cell_styles(d: &mut Document, names: &[String]) { /// Include descendants when editing an inherited style, with the resolver's depth limit. fn dependent_styles<'a>(name: &str, styles: impl Iterator)>) -> Vec { let styles: Vec<_> = styles.collect(); + // The first style of a name is the one, as the resolver's lookup finds it. + let mut based_on: std::collections::HashMap<&str, Option<&str>> = std::collections::HashMap::new(); + for (n, base) in &styles { + based_on.entry(n).or_insert(*base); + } styles .iter() .filter(|(candidate, _)| { @@ -142,7 +147,7 @@ fn dependent_styles<'a>(name: &str, styles: impl Iterator Result { let mut changes = Vec::new(); for seg in segs.iter().rev() { let old = st.slice(seg.clone()).to_string(); - let new: String = starts.iter().zip(&mapped).filter(|(b, _)| seg.contains(b)).map(|(_, m)| m.as_str()).collect(); + // The characters in this run: `starts` is sorted, so a search finds the slice. + let (from, to) = (starts.partition_point(|b| *b < seg.start), starts.partition_point(|b| *b < seg.end)); + let new: String = mapped.get(from..to).unwrap_or_default().iter().map(String::as_str).collect(); if new != old { delta += new.len() as isize - old.len() as isize; st.replace(seg.clone(), &new); // Per-character changes (back to front), so ranges inside the run keep their boundaries. - for (i, (b, m)) in starts.iter().zip(&mapped).enumerate().rev().filter(|(_, (b, _))| seg.contains(b)) { + for i in (from..to.min(mapped.len())).rev() { + let (b, m) = (&starts[i], &mapped[i]); let ce = starts.get(i + 1).copied().unwrap_or(r.end); if m.len() != ce - b { changes.push((*b..ce, m.len())); diff --git a/crates/engine/src/cmd/xml.rs b/crates/engine/src/cmd/xml.rs index 81b57b5b3..949d96436 100644 --- a/crates/engine/src/cmd/xml.rs +++ b/crates/engine/src/cmd/xml.rs @@ -60,10 +60,13 @@ fn plain(t: &str) -> String { } /// A paragraph's text with inline-tagged runs as elements. -fn para_xml(st: &designcraft_doc::Story, r: std::ops::Range) -> String { +/// `runs` are the story's runs in order (collected once per story): only those reaching the +/// paragraph are visited, found by binary search. +fn para_xml(st: &designcraft_doc::Story, runs: &[(std::ops::Range, &designcraft_doc::CharFormat)], r: std::ops::Range) -> String { let mut out = String::new(); let mut open: Option = None; - for (rr, f) in st.runs() { + let first = runs.partition_point(|(rr, _)| rr.end <= r.start); + for (rr, f) in runs.iter().skip(first).take_while(|(rr, _)| rr.start < r.end) { let (a, b) = (rr.start.max(r.start), rr.end.min(r.end)); if a >= b { continue; @@ -97,8 +100,10 @@ pub fn export_xml(d: &Document) -> String { let Some(st) = d.story(tf.story) else { continue }; let mapped = |style: &str| d.xml.style_map.iter().find(|(s, _)| s == style).map(|(_, t)| t.clone()); // Paragraph text with inline tags already escaped. - let paras: Vec<(Option, String)> = - st.para_ranges().into_iter().enumerate().map(|(i, r)| (mapped(&st.paras[i].style), para_xml(st, r))).collect(); + let paras: Vec<(Option, String)> = { + let runs: Vec<_> = st.runs().collect(); + st.para_ranges().into_iter().enumerate().map(|(i, r)| (mapped(&st.paras[i].style), para_xml(st, &runs, r))).collect() + }; if paras.iter().any(|(m, _)| m.is_some()) { out.push_str(&format!(" <{tag}>\n")); for (m, text) in paras { @@ -341,8 +346,10 @@ pub fn specs() -> Vec { let dtd = crate::dtd::parse(&text).map_err(|e| bad("xml.loadDtd", e))?; let names: Vec = dtd.element_names().map(str::to_string).collect(); s.edit(|d, _| { + // The tags there are, then those added: one set, not a search per element. + let mut known: std::collections::HashSet = d.xml.tags.iter().map(|t| t.name.clone()).collect(); for n in &names { - if valid_name(n) && !d.xml.tags.iter().any(|t| t.name == *n) { + if valid_name(n) && known.insert(n.clone()) { let color = COLORS[d.xml.tags.len() % COLORS.len()]; d.xml.tags.push(XmlTag { name: n.clone(), color }); } diff --git a/crates/engine/src/dtd.rs b/crates/engine/src/dtd.rs index 7128ba84e..c0d45d820 100644 --- a/crates/engine/src/dtd.rs +++ b/crates/engine/src/dtd.rs @@ -29,6 +29,8 @@ enum Model { #[derive(Clone, Debug, Default)] pub struct Dtd { elements: Vec<(String, Model)>, + /// Each declared name's first declaration (which wins), by name. + index: HashMap, /// Element → its `#REQUIRED` attributes. required: HashMap>, } @@ -47,7 +49,7 @@ impl Dtd { } fn model(&self, name: &str) -> Option<&Model> { - self.elements.iter().find(|(n, _)| n == name).map(|(_, m)| m) + self.elements.get(*self.index.get(name)?).map(|(_, m)| m) } } @@ -252,6 +254,7 @@ pub fn parse(src: &str) -> Result { let rest = rest.trim_start(); let name: String = rest.chars().take_while(|c| !c.is_whitespace()).collect(); let model = parse_model(&rest[name.len()..]).map_err(|e| format!(": {e}"))?; + dtd.index.entry(name.clone()).or_insert(dtd.elements.len()); dtd.elements.push((name, model)); } else if let Some(rest) = d.strip_prefix("ATTLIST") { // name (attr type default)*; only `#REQUIRED` matters here. @@ -609,6 +612,15 @@ mod tests { assert!(parse("just text").is_err()); } + /// An element declared twice keeps its first model (the index points there). + #[test] + fn the_first_declaration_of_an_element_wins() { + let dtd = parse("\n\n").unwrap(); + assert_eq!(dtd.element_names().collect::>(), ["a", "b", "a"]); + assert_eq!(validate(&dtd, ""), vec![]); + assert!(!validate(&dtd, "").is_empty()); + } + /// A few hundred bytes of nested parameter entities used to inflate multiplicatively across /// the eight wholly-string-replacing passes (#713); expansion is capped. #[test]