fix(anatomy): R5.4 guard covers links, autolinks, references, CJK underscores; ids validated at the op; prescan before the drawing comparison

This commit is contained in:
bchanot committed 2026-10-23 12:10:35 +02:00
1 parent 68dec25e5e
commit df82424107
9 files changed
+195 -12

No files matched your search

+9
View File
@@ -98,6 +98,15 @@ pub fn split_field(field: &str) -> Result<Vec<&str>, PipelineError> {
"field \"{field}\" has more than {MAX_FIELD_SEGMENTS} segments"
)));
}
// A control character or a backtick would reach the reports' code spans through the record.
if path
.iter()
.any(|key| key.chars().any(|c| c.is_control() && c != '\0') || key.contains('`'))
{
return Err(fail(format!(
"field {field:?} holds a control character or a backtick"
)));
}
if let Some(key) = path.iter().find(|key| key.starts_with('\0')) {
return Err(fail(format!("reserved key {key:?} in field \"{field}\"")));
}
+35
View File
@@ -17,6 +17,7 @@ use super::targets::{
id_text, item_at_mut, list_at, list_at_mut, locate, top_collection_path,
};
use crate::curation::PipelineError;
use crate::domain::js;
fn collection_kind(field: DocumentCollection) -> TargetKind {
match field {
@@ -190,6 +191,7 @@ fn apply_rename(
return Err(fail("the document has no id to rename"));
};
let key = located.target.kind.key_field();
assert_stable_id(Some(&Value::from(op.id.as_str())), key)?;
assert_free(list_at(doc, list)?, key, &op.id, Some(located.index))?;
let item = item_at_mut(doc, &located.item_path())?;
let before = Value::Object(item.clone());
@@ -246,6 +248,35 @@ fn add_destination(doc: &mut Value, op: &AddOp) -> Result<Destination, PipelineE
/// TypeScript met a null or a primitive there inconsistently (a TypeError when it walked it,
/// silently skipped when a rename walked past it, refused by the gate when it stayed); the
/// port refuses it at the add (RUST.md §11.12). Walked with an explicit stack.
/// An id an op writes must already be what the final document requires (a stable id; a region
/// code for a region): an id that passed only because a later op removed or renamed its item
/// would still reach the records, and from them the reports' code spans (a line break there
/// forged sections). Refused at the op (RUST.md §11.14).
fn assert_stable_id(value: Option<&Value>, key: &str) -> Result<(), PipelineError> {
let valid = match value.and_then(Value::as_str) {
Some(id) if key == "code" => js::is_region_code(id),
Some(id) => js::is_stable_id(id),
None => false,
};
if !valid {
return Err(fail(format!(
"the {} {:?} is not a valid {}",
key,
value.map(js_string_value).unwrap_or_default(),
if key == "code" {
"region code"
} else {
"stable id"
}
)));
}
Ok(())
}
fn js_string_value(value: &Value) -> String {
super::records::js_string(Some(value))
}
fn assert_object_children(value: &Value) -> Result<(), PipelineError> {
let mut pending = vec![value];
while let Some(item) = pending.pop() {
@@ -267,6 +298,9 @@ fn assert_object_children(value: &Value) -> Result<(), PipelineError> {
"the added item holds a \"{key}\" entry that is not an object"
)));
}
for entry in list {
assert_stable_id(entry.get("id"), "id")?;
}
pending.extend(list);
}
}
@@ -289,6 +323,7 @@ fn apply_add(
dest.key
)));
};
assert_stable_id(value.get(dest.key), dest.key)?;
let list = list_at(doc, &dest.list)?;
assert_free(list, dest.key, id, None)?;
let index = insertion_index(list, dest.key, op.position.as_ref())?;
+6 -5
View File
@@ -159,12 +159,13 @@ fn number(geometry: &Value, key: &str) -> f64 {
.unwrap_or(f64::NAN)
}
/// `String(number)`, NaN included.
/// `String(number)`, NaN and the infinities included.
fn text_of(value: f64) -> String {
if value.is_nan() {
"NaN".to_owned()
} else {
js_number(value)
match value {
v if v.is_nan() => "NaN".to_owned(),
f64::INFINITY => "Infinity".to_owned(),
f64::NEG_INFINITY => "-Infinity".to_owned(),
v => js_number(v),
}
}
@@ -126,8 +126,9 @@ pub fn compare_drawing(
let Some(rule) = rule else {
return Err("bytes differ and no tree rule is declared".to_owned());
};
let (prolog, epilog) = outside_root(committed).map_err(|e| format!("committed: {e}"))?;
// The core reader first: its prescan bounds the depth before any recursive parse.
let tree = parse_svg(committed).map_err(|e| format!("committed: {}", e.reason()))?;
let (prolog, epilog) = outside_root(committed).map_err(|e| format!("committed: {e}"))?;
if format!("{prolog}{}{epilog}", serialize(&tree)) != committed {
return Err("the committed file holds what its tree does not carry".to_owned());
}
+55 -2
View File
@@ -70,7 +70,8 @@ pub fn code(value: Option<&str>) -> String {
/// The plain text of a raw line Prettier would rewrite, found in its parts outside code spans
/// (`oracles/r5/markdown-sweep.json`): `*` outside a `**strong**` span; an underscore not between two letters or digits; a
/// backslash before a space or at the end, or an authored one before the cell escape `\|`
/// (`\"` in a JSON value is kept as written); two spaces in a row, a tab, a carriage return.
/// (`\"` in a JSON value is kept as written); two spaces in a row, a tab, a carriage return;
/// the inline constructs of `rewritten_construct`.
/// The parts of plain text outside the `**strong**` spans Prettier keeps as written (the
/// opening and closing pairs bounded by a non-word character or the line's edge, the inside
/// non-empty, without `*` and without an outer space). `None` when a `*` is left outside
@@ -101,14 +102,61 @@ fn outside_strong(plain: &str) -> Option<String> {
(!out.contains('*')).then_some(out)
}
/// Inline constructs Prettier re-prints (the sweep's link, wiki-link, autolink and reference
/// probes): a link, image or reference (`](`, `][`), a wiki link with inner spaces, an autolink
/// (`<scheme:…>`, `<…@…>`), a character reference (`&name;`, `&#n;`). The last three are refused
/// whole although Prettier keeps some of them: a loud refusal, never met by the equine data.
fn rewritten_construct(plain: &str) -> bool {
// `[[ a ]]` loses its inner spaces; `[[a]]` and a polygon's `[[10,10],…]` stay as written.
let padded_wiki = plain.split("[[").skip(1).any(|after| {
let inside = after.split("]]").next().unwrap_or_default();
after.contains("]]")
&& (inside.starts_with(char::is_whitespace) || inside.ends_with(char::is_whitespace))
});
if plain.contains("](") || plain.contains("][") || padded_wiki {
return true;
}
let autolink = plain.split('<').skip(1).any(|after| {
let inside = after.split('>').next().unwrap_or_default();
let closed = after.contains('>');
let scheme = inside.split(':').next().unwrap_or_default();
let is_scheme = inside.contains(':')
&& (2..=32).contains(&scheme.len())
&& scheme
.chars()
.next()
.is_some_and(|c| c.is_ascii_alphabetic())
&& scheme
.chars()
.all(|c| c.is_ascii_alphanumeric() || "+.-".contains(c));
closed && !inside.contains(' ') && (is_scheme || inside.contains('@'))
});
let reference = plain.split('&').skip(1).any(|after| {
let name = after.split(';').next().unwrap_or_default();
after.contains(';')
&& !name.is_empty()
&& name.len() <= 32
&& name
.trim_start_matches('#')
.chars()
.all(|c| c.is_ascii_alphanumeric())
});
autolink || reference
}
fn plain_rewrite(plain: &str) -> bool {
if rewritten_construct(plain) {
return true;
}
let Some(plain) = outside_strong(plain) else {
return true;
};
let chars: Vec<char> = plain.chars().collect();
// An underscore Prettier keeps sits between two ASCII letters or digits: the sweep shows it
// escapes one next to an accented letter, a CJK, kana or Hangul character.
let word = |i: Option<usize>| {
i.and_then(|i| chars.get(i))
.is_some_and(|c| c.is_alphanumeric())
.is_some_and(char::is_ascii_alphanumeric)
};
chars.iter().enumerate().any(|(i, &c)| match c {
'*' | '\t' | '\r' => true,
@@ -140,6 +188,11 @@ fn guard_line(line: &str) -> Result<(), PipelineError> {
fn guard_text(line: &str) -> Result<(), PipelineError> {
let parts: Vec<&str> = line.split('`').collect();
let refuse = || Err(PipelineError::UnsupportedMarkdown(line.to_owned()));
// No carriage return or tab anywhere, code spans included (a CR there broke a table row;
// the other control characters align as Prettier aligns them, `oracles/r5/markdown-tables.json`).
if line.contains(['\r', '\t']) {
return refuse();
}
if parts.len().is_multiple_of(2) {
return refuse();
}