Merge pull request #5096 from dfgodvan/fix/no-break-in-url-query

Layout: no line break after the '?' of a query, nor after a slash before an underscore
This commit is contained in:
FredApps authored and GitHub committed 2026-11-18 16:24:30 +07:00
commit b0d3165ede
3 files changed
+83 -5

No files matched your search

+16 -5
View File
@@ -812,13 +812,24 @@ fn break_text<'a>(source: &'a str, skip: &[std::ops::Range<usize>]) -> (std::bor
}
}
/// A character that continues a word or a path segment after a slash: a letter, a digit or `_`.
fn word_char(c: char) -> bool {
c.is_alphanumeric() || c == '_'
}
fn break_positions(text: &str, rp: &wordcraft_doc::resolve::ResolvedPara) -> Vec<usize> {
let mut found = Vec::new();
for (i, o) in unicode_linebreak::linebreaks(text) {
// Word keeps "and/or" and web addresses whole: no break right after a slash (a word
// too long for the line still breaks anywhere).
let after_slash =
text.get(..i).is_some_and(|t| t.ends_with('/')) && text.get(i..).and_then(|t| t.chars().next()).is_some_and(char::is_alphanumeric);
// Word keeps "and/or" and web addresses whole: no break right after a slash before a letter,
// digit or underscore ("/_back"), nor after the question mark of a query ("/?type="), which
// takes an ASCII one (a question mark in CJK text keeps its break). A word too long for
// the line still breaks anywhere.
let next = text.get(i..).and_then(|t| t.chars().next());
let after_slash = match text.get(..i).and_then(|t| t.chars().next_back()) {
Some('/') => next.is_some_and(word_char),
Some('?') => next.is_some_and(|c| c.is_ascii_alphanumeric() || c == '_'),
_ => false,
};
if (o == unicode_linebreak::BreakOpportunity::Allowed || i < text.len()) && !after_slash {
found.push(i);
}
@@ -830,7 +841,7 @@ fn break_positions(text: &str, rp: &wordcraft_doc::resolve::ResolvedPara) -> Vec
.match_indices('/')
.filter(|&(i, _)| {
text.get(..i).and_then(|t| t.chars().next_back()).is_some_and(|c| c.is_whitespace() && !matches!(c, '\u{a0}' | '\u{2007}' | '\u{202f}'))
&& text.get(i + 1..).and_then(|t| t.chars().next()).is_some_and(char::is_alphanumeric)
&& text.get(i + 1..).and_then(|t| t.chars().next()).is_some_and(word_char)
})
.map(|(i, _)| i)
.collect();
+66
View File
@@ -7931,6 +7931,72 @@ fn no_line_break_right_after_a_slash() {
assert!(lines > 10, "{lines} lines");
}
/// The text of every laid-out line.
fn body_lines(l: &DocLayout, text: &str) -> Vec<String> {
l.pages
.iter()
.flat_map(|p| &p.items)
.filter_map(|it| {
if let Placed::Lines { para, l0, l1, .. } = it {
Some(para.lines[*l0..*l1].iter().map(|line| text[line.start..line.stop].to_string()))
} else {
None
}
})
.flatten()
.collect()
}
#[test]
fn no_line_break_inside_a_query_address() {
// Word keeps "/?type=" and "/_back" whole (measured: a request address went to the next
// line whole, a long one breaking only where it no longer fits).
for addr in ["https://example.org/portal/_back/?type=objects_export&action=get", "https://example.org/portal/_back_office/reports/_export_all"] {
for k in 0..60 {
let text = format!("{}{addr} end", "a ".repeat(k));
let lines = body_lines(&lay(&Document::from_text(&text)), &text);
for (a, b) in lines.iter().zip(&lines[1..]) {
assert!(!a.ends_with('?') && !a.ends_with('/'), "{k}: a line ends inside the address: {a:?} / {b:?}");
}
}
}
}
/// A path that starts after a space ("caption /_back") wraps whole: UAX #14 has no break between
/// the space and the slash, so the break before the slash is the only one there.
#[test]
fn a_path_after_a_space_wraps_whole() {
let text = "enregistrement /_back_office_reports";
let mut wrapped = 0;
// Down to the width where the path still fits on a line (it no longer does at 425 pt).
for m in 100..=420 {
let mut d = Document::from_text(text);
d.last_section.margin_right = m as f32;
let lines = body_lines(&lay(&d), text);
if lines.len() < 2 {
continue; // fits on one line
}
wrapped += 1;
assert_eq!(lines, ["enregistrement ", "/_back_office_reports"], "the path is split at margin {m}");
}
assert!(wrapped > 50, "{wrapped} widths wrapped");
}
/// A question mark in CJK text keeps the break after it: only a query's `?` before an ASCII
/// letter, digit or underscore loses it.
#[test]
fn a_question_mark_in_cjk_text_keeps_its_break() {
let text = "你好?世界".repeat(40);
let mut ended = false;
for m in 300..=440 {
let mut d = Document::from_text(&text);
d.last_section.margin_right = m as f32;
let lines = body_lines(&lay(&d), &text);
ended |= lines[..lines.len() - 1].iter().any(|l| l.ends_with('?'));
}
assert!(ended, "no line ever ends after the question mark");
}
/// #2963: a word after a slash ("caption /заполняется") wraps whole, as in Word. UAX #14 forbids a
/// break between the space and the slash, and the break after the slash is dropped (Word keeps
/// "and/or" whole), so the slash word had no break before it and split between two letters.
+1
View File
@@ -531,6 +531,7 @@ The front-of-text opt-out is retained from the earlier implementation; this patc
| 2026-11-02 | trivial | Beyond Word, for ODT documents: a table's own space above and below (`TableProps::space_before`/`space_after`) moves the text around it, and a header (footer) taller than its margin keeps its spacing between it and the body (`SectionProps::header_gap`/`footer_gap`; Word keeps none, so DOCX documents are unchanged) (`tests::a_tables_own_space_above_and_below_moves_the_text`, `tests::body_starts_right_below_a_tall_header`) |
| 2026-11-02 | trivial | A page break before the paragraph in a table's first row moves the whole table to the next page, as in Word (it was ignored); emphasis marks (Font › Emphasis mark) are drawn over or under each character and make the line taller |
| 2026-11-12 | minor | Added layout.fitOnePage: heuristic search over discrete font half-point steps and spacing compression with anti-clutter safeguards (line-spacing floor and font-size floor) to condense multi-page documents by one page while preserving hierarchy |
| 2026-10-31 | trivial | Web addresses: no line break after the `?` of a query (`/?type=`) or after a slash before an underscore (`/portal/_back`). Word for Mac 16.93 kept such an address whole and moved it to the next line; WordCraft broke it there (UAX #14 allows both). With the address moved along the line one word at a time (60 paragraphs, Times New Roman 12pt), WordCraft's lines now match Word's line for line; 23 (query) and 19 (underscore) paragraphs broke inside the address before. A `?` keeps its break before anything but an ASCII letter, digit or underscore (CJK text), and a path after a space (`caption /_back`) wraps whole. `layout::tests::no_line_break_inside_a_query_address`, `a_path_after_a_space_wraps_whole`, `a_question_mark_in_cjk_text_keeps_its_break` |
Maintenance on 2026-10-12 retains current tracked-deletion, endnote, footer-total, text-box
and balloon layout while supplying explicit cell identity at every box call. The native Word