documents: alerts read their marker from parsed text; GFM's filtered tags show as text; block elements close what opened inside them; a TOC press relocates its heading after a write-back or a pending read and acts on press
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
2eec25140d
commit
98f19a8aac
7 files changed
+211
-69
No files matched your search
@@ -13,7 +13,7 @@ Ely GPUI Component. A component library for GPUI, in light and dark.
|
||||
- Web views: `wry` 0.57 (Apache-2.0 OR MIT, default features off, macOS only) lays a child WKWebView over the window.
|
||||
- Terminal: `alacritty_terminal` 0.26 (Apache-2.0, default features off, native only) for the grid, its parser and the pseudo-terminal; `futures` carries its events. `vte` 0.15 (Apache-2.0 OR MIT), the parser it re-exports, reads `AnsiText`'s codes alone, so colored output builds for wasm32.
|
||||
- Diffs: `similar` 3.2 (Apache-2.0) for line and word diffs and three-way merges; its `unicode` feature splits words at punctuation.
|
||||
- Markdown: `markdown` 1.0.0 (markdown-rs, MIT) reads GitHub's flavor and math into a syntax tree with byte positions for `MarkdownRenderer`, `MarkdownEditor`, the highlights, block ranges and outline. `MarkdownRenderer` draws display math through `typography::Latex`, a ```` ```mermaid ```` block through `documents::Diagram`, and pictures from the owner's folder (`base`) or `data:` addresses (`base64` 0.22, MIT OR Apache-2.0); web pictures stay unloaded. Inline math shows as TeX: gpui text carries no inline pictures. Inline it reads as Typora's defaults: `:shortcode:` turns to its emoji as GitHub reads it (the `emojis` crate's names, GitHub's today, so `:cricket:` is the insect; read after escapes and references, so `\:smile:` turns too; inside an autolink or `<code>` it stays text), inline HTML's `u`, `b`/`strong`, `i`/`em`, `s`/`del`/`strike`, `code`/`kbd`, `mark`, `br` and `a href` draw as what they mean and other tags, hyphenated names too, vanish, leaving their text; attribute values split on ASCII whitespace alone and decode as HTML decodes them; each Markdown container is the element GitHub writes (`em`, `strong`, `del`, `a`), on one stack with the tags and repaired as HTML5 repairs: an end tag closes the innermost element of its name and all above it, of which only formatting elements (`a`, `b`, `code`, `em`, `i`, `s`, `strike`, `strong`, `u`) reopen, a new `a` ends the open one and an `a` without `href` links nothing; other tags are not tracked, and `</br>` breaks as `<br>` does; and a footnote reference is its number in superscript, numbered by first reference, as its definition is; one `~` still strikes. At the root, blocks read as GitHub shows them: a quote whose first line is `[!NOTE]`, `[!TIP]`, `[!IMPORTANT]`, `[!WARNING]` or `[!CAUTION]` in any case, spaces around it, with more after it, is an alert under its title (Important in the palette's violet, `syntax.keyword`, read at 4.5:1); a paragraph that is `[toc]` alone, as Typora reads it, lists every heading by depth, and a press on one reveals it as a link press does (`MarkdownDocument`'s `reveal`); front matter shows in a box without its fences; an HTML block shows its text with the inline tags above, white space collapsed and owed as a browser lays it, block elements breaking lines, comments, `script` and `style` dropped, and an unclosed tag dropping the rest; a rule is a 2px `border_strong` line; bullets go •, ◦, ▪ by list depth, ordered lists counted. `MarkdownDocument` holds a whole file in one `CodeEditor` (text and undo, colored by `CodeEditor::highlighter` with `markdown_highlights`): visual draws the file's top-level blocks in a gpui `list`, a pressed block opening as its source in a `TextInput` whose typing is the document's text at once and whose write-back is one undo step; source is the editor; preview draws without presses. A written-back block, a mode switch or a new text is read at once up to 128 KiB, longer ones on the background executor; typing in source or split reads 300 ms after the last key, off the main thread (markdown-rs grows with the square of a paragraph's lines: 40,000 lines take 0.9 s). Texts past `LARGEST_READ` (2 MiB) are never read: no blocks, no outline. A new read keeps the list's blocks alike at both ends. One caret serves every mode: the editor's selection, which a written-back block moves; `set_caret` and `set_top_line` set caret and view apart; a top line asked (numbered, so a late frame callback finishes only its own ask; new text places it again, clamped to the text as it then is) waits for the read, lands on its block, then a frame later at its share of the block's height; `reveal` is such an ask with the caret at its line; a switch between source and the list keeps the top line, one between visual and preview keeps the list's scroll as it is. A block opens only when the read is of the text as it is now. A pressed block keeps its identity across the write-back of the one before it. Blocks are Tab stops that Enter or Space opens; preview takes no presses, undo or redo. `CodeEditor` keeps at most 64 MiB of undo and redo text, dropping the oldest undo and then the farthest redo after every record, composition, undo and redo. `BlockData::from_markdown` and its export still read with `pulldown-cmark` 0.13 (MIT, default features off); it adds GitHub's alerts, a callout's form, and reads each field's inline Markdown from the source by offset, as written.
|
||||
- Markdown: `markdown` 1.0.0 (markdown-rs, MIT) reads GitHub's flavor and math into a syntax tree with byte positions for `MarkdownRenderer`, `MarkdownEditor`, the highlights, block ranges and outline. `MarkdownRenderer` draws display math through `typography::Latex`, a ```` ```mermaid ```` block through `documents::Diagram`, and pictures from the owner's folder (`base`) or `data:` addresses (`base64` 0.22, MIT OR Apache-2.0); web pictures stay unloaded. Inline math shows as TeX: gpui text carries no inline pictures. Inline it reads as Typora's defaults: `:shortcode:` turns to its emoji as GitHub reads it (the `emojis` crate's names, GitHub's today, so `:cricket:` is the insect; read after escapes and references, so `\:smile:` turns too; inside an autolink or `<code>` it stays text), inline HTML's `u`, `b`/`strong`, `i`/`em`, `s`/`del`/`strike`, `code`/`kbd`, `mark`, `br` and `a href` draw as what they mean GFM's filtered tags (`script`, `style`, `title`, `textarea`, `iframe`, `xmp`, `noembed`, `noframes`, `plaintext`) show as text, and other tags, hyphenated names too, vanish, leaving their text; attribute values split on ASCII whitespace alone and decode as HTML decodes them; each Markdown container is the element GitHub writes (`em`, `strong`, `del`, `a`), on one stack with the tags and repaired as HTML5 repairs: an end tag closes the innermost element of its name and all above it, of which only formatting elements (`a`, `b`, `code`, `em`, `i`, `s`, `strike`, `strong`, `u`) reopen, a new `a` ends the open one and an `a` without `href` links nothing; other tags are not tracked, and `</br>` breaks as `<br>` does; and a footnote reference is its number in superscript, numbered by first reference, as its definition is; one `~` still strikes. At the root, blocks read as GitHub shows them: a quote whose first line is plain text reading `[!NOTE]`, `[!TIP]`, `[!IMPORTANT]`, `[!WARNING]` or `[!CAUTION]` in any case, after escapes and references, spaces and tabs around it, with more after it, is an alert under its title (Important in the palette's violet, `syntax.keyword`, read at 4.5:1); a paragraph that is `[toc]` alone, as Typora reads it, lists every heading by depth, and a press on one reveals it as a link press does (`MarkdownDocument`'s `reveal`), the heading found again in the text as it is now, and a secondary press acts on press, since a block writing back moves the list; front matter shows in a box without its fences; an HTML block shows its text with the inline tags above, white space collapsed and owed as a browser lays it, block elements breaking lines and closing what opened inside them, comments dropped, the filtered tags shown as text, and an unclosed tag dropping the rest; a rule is a 2px `border_strong` line; bullets go •, ◦, ▪ by list depth, ordered lists counted. `MarkdownDocument` holds a whole file in one `CodeEditor` (text and undo, colored by `CodeEditor::highlighter` with `markdown_highlights`): visual draws the file's top-level blocks in a gpui `list`, a pressed block opening as its source in a `TextInput` whose typing is the document's text at once and whose write-back is one undo step; source is the editor; preview draws without presses. A written-back block, a mode switch or a new text is read at once up to 128 KiB, longer ones on the background executor; typing in source or split reads 300 ms after the last key, off the main thread (markdown-rs grows with the square of a paragraph's lines: 40,000 lines take 0.9 s). Texts past `LARGEST_READ` (2 MiB) are never read: no blocks, no outline. A new read keeps the list's blocks alike at both ends. One caret serves every mode: the editor's selection, which a written-back block moves; `set_caret` and `set_top_line` set caret and view apart; a top line asked (numbered, so a late frame callback finishes only its own ask; new text places it again, clamped to the text as it then is) waits for the read, lands on its block, then a frame later at its share of the block's height; `reveal` is such an ask with the caret at its line; a switch between source and the list keeps the top line, one between visual and preview keeps the list's scroll as it is. A block opens only when the read is of the text as it is now. A pressed block keeps its identity across the write-back of the one before it. Blocks are Tab stops that Enter or Space opens; preview takes no presses, undo or redo. `CodeEditor` keeps at most 64 MiB of undo and redo text, dropping the oldest undo and then the farthest redo after every record, composition, undo and redo. `BlockData::from_markdown` and its export still read with `pulldown-cmark` 0.13 (MIT, default features off); it adds GitHub's alerts, a callout's form, and reads each field's inline Markdown from the source by offset, as written.
|
||||
- Pictures: `image` 0.25 (MIT OR Apache-2.0), the crate gpui decodes with, default features off; it turns decoded frames and rims map tiles.
|
||||
- Math and diagrams: RaTeX 0.1.14 (`ratex-parser`, `ratex-layout`, `ratex-svg` with `embed-fonts`, `ratex-types`; MIT, KaTeX's fonts OFL) typesets `Latex` to SVG with outlined glyphs; `mermaid-rs-renderer` 0.3.1 (MIT, default features off) draws `Diagram`. Both go through gpui's `SvgRenderer` at the window's scale, loaded as gpui assets off the main thread; `usvg` 0.46, gpui's own, reads a diagram's size. A RaTeX or Mermaid panic on parsed input becomes the element's error. Each element holds its picture through `primitives::held`: a new source, or the element leaving the window, removes the old asset from gpui's cache and drops its image from the atlas after the frame.
|
||||
- Maps: Natural Earth's 110m countries (public domain). `assets/maps/countries.geojson`, trimmed by `scripts/countries.py` to an ISO code, a name and positions to 0.01°, backs `maps::WorldMap`; the gallery's tiles are cut from the same file, z0 to z3, by `scripts/tiles.py` with Pillow.
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use gpui::{App, Context, Focusable as _, ListOffset, ScrollStrategy, Window, px};
|
||||
|
||||
use super::{MarkdownDocument, Placement};
|
||||
use super::{DocumentHeading, MarkdownDocument, Placement};
|
||||
use crate::documents::{MarkdownMode, modes::joint};
|
||||
|
||||
/// Line and column, from zero, of byte `at` in `text`.
|
||||
@@ -189,13 +189,27 @@ impl MarkdownDocument {
|
||||
.saturating_sub(1)
|
||||
}
|
||||
|
||||
/// Reveals the line holding byte `at` of the text.
|
||||
/// Reveals the pressed heading, found again in the text.
|
||||
pub(super) fn reveal_at(&mut self, at: usize, window: &mut Window, cx: &mut Context<Self>) {
|
||||
let editor = self.editor.read(cx);
|
||||
if at > editor.text().len() {
|
||||
return log::error!("markdown document: a heading past the text");
|
||||
let headings = &self.parsed.headings;
|
||||
let Some(ix) = headings.iter().position(|heading| heading.at == at) else {
|
||||
return log::warn!("markdown document: a heading read away");
|
||||
};
|
||||
let pressed = headings[ix].clone();
|
||||
let same = |heading: &&DocumentHeading| {
|
||||
(heading.depth, &heading.title) == (pressed.depth, &pressed.title)
|
||||
};
|
||||
let nth = headings[..ix].iter().filter(same).count();
|
||||
if self.commit(cx).is_some() || !self.read_now(cx) {
|
||||
self.reparse(true, cx);
|
||||
}
|
||||
let line = editor.buffer.line_of(at);
|
||||
if !self.read_now(cx) {
|
||||
return log::warn!("markdown document: a heading pressed while the text is read again");
|
||||
}
|
||||
let Some(found) = self.parsed.headings.iter().filter(same).nth(nth) else {
|
||||
return log::warn!("markdown document: the pressed heading changed as one wrote");
|
||||
};
|
||||
let line = self.editor.read(cx).buffer.line_of(found.at);
|
||||
self.reveal(line, window, cx);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use gpui::{Modifiers, MouseButton, TestAppContext, VisualTestContext, point, px};
|
||||
|
||||
use super::{MarkdownMode, open};
|
||||
use super::{MarkdownMode, frames, open, type_in};
|
||||
|
||||
const LISTED: &str = "[toc]\n\n# A\n\ntext\n\n## B\n";
|
||||
|
||||
@@ -34,3 +34,35 @@ fn visual_reveals_on_a_secondary_press_and_edits_on_a_plain_one(cx: &mut TestApp
|
||||
press_entry(0, Modifiers::none(), cx);
|
||||
assert!(document.read_with(cx, |document, _| document.open.is_some()));
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn a_press_after_an_earlier_block_grew_finds_the_heading(cx: &mut TestAppContext) {
|
||||
let text = "before\n\n[toc]\n\n# A\n\ntext\n\n## B\n";
|
||||
let (document, cx) = open(text, MarkdownMode::Visual, cx);
|
||||
type_in(&document, Some(0), Some("before\nextra\nextra"), cx);
|
||||
frames(cx);
|
||||
press_entry(1, Modifiers::secondary_key(), cx);
|
||||
frames(cx);
|
||||
assert_eq!(
|
||||
document.read_with(cx, |document, cx| document.caret(cx)),
|
||||
(10, 0)
|
||||
);
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn a_press_while_the_text_waits_to_be_read_finds_the_heading(cx: &mut TestAppContext) {
|
||||
let (document, cx) = open(LISTED, MarkdownMode::Split, cx);
|
||||
document.update(cx, |document, cx| {
|
||||
document.editor.update(cx, |editor, cx| {
|
||||
editor.edit([(0..0, "inserted\n".repeat(20))], cx);
|
||||
});
|
||||
});
|
||||
cx.run_until_parked();
|
||||
assert!(!document.read_with(cx, |document, cx| document.read_now(cx)));
|
||||
press_entry(1, Modifiers::none(), cx);
|
||||
frames(cx);
|
||||
assert_eq!(
|
||||
document.read_with(cx, |document, cx| document.caret(cx)),
|
||||
(26, 0)
|
||||
);
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
use gpui::{AnyElement, App, FontWeight, IntoElement, ParentElement, Styled, div, prelude::*};
|
||||
use gpui::{
|
||||
AnyElement, App, FontWeight, IntoElement, MouseButton, ParentElement, Styled, div, prelude::*,
|
||||
};
|
||||
|
||||
use super::Draw;
|
||||
use crate::{
|
||||
@@ -144,13 +146,25 @@ impl Draw<'_> {
|
||||
let Some(reveal) = self.on_heading.clone() else {
|
||||
return entry.text_color(colors.fg_muted).into_any_element();
|
||||
};
|
||||
let at = heading.at;
|
||||
let (at, pressed) = (heading.at, reveal.clone());
|
||||
entry
|
||||
.text_color(colors.link)
|
||||
.tab_index(0)
|
||||
.focus_ring(cx)
|
||||
.cursor_pointer()
|
||||
.on_click(move |_, window, cx| reveal(at, window, cx))
|
||||
// On press: the layout may move before release.
|
||||
.on_mouse_down(MouseButton::Left, move |event, window, cx| {
|
||||
if event.modifiers.secondary() {
|
||||
window.prevent_default();
|
||||
cx.stop_propagation();
|
||||
pressed(at, window, cx);
|
||||
}
|
||||
})
|
||||
.on_click(move |event, window, cx| {
|
||||
if !event.modifiers().secondary() {
|
||||
reveal(at, window, cx);
|
||||
}
|
||||
})
|
||||
.into_any_element()
|
||||
}))
|
||||
.into_any_element()
|
||||
|
||||
@@ -36,7 +36,10 @@ impl AlertKind {
|
||||
|
||||
/// The kind `line` marks, as `[!NOTE]` in any case.
|
||||
fn marked(line: &str) -> Option<Self> {
|
||||
let name = line.trim().strip_prefix("[!")?.strip_suffix(']')?;
|
||||
let name = line
|
||||
.trim_matches([' ', '\t'])
|
||||
.strip_prefix("[!")?
|
||||
.strip_suffix(']')?;
|
||||
[
|
||||
Self::Note,
|
||||
Self::Tip,
|
||||
@@ -68,12 +71,10 @@ impl Reader<'_> {
|
||||
let mdast::Node::Blockquote(quote) = node else {
|
||||
return None;
|
||||
};
|
||||
let [first @ mdast::Node::Paragraph(paragraph), rest @ ..] = "e.children[..] else {
|
||||
let [mdast::Node::Paragraph(paragraph), rest @ ..] = "e.children[..] else {
|
||||
return None;
|
||||
};
|
||||
let line = self.source[span(first)].lines().next().unwrap_or_default();
|
||||
let kind = AlertKind::marked(line)?;
|
||||
let after = after_first_line(¶graph.children);
|
||||
let (kind, after) = marker(¶graph.children)?;
|
||||
if after.is_empty() && rest.is_empty() {
|
||||
return None;
|
||||
}
|
||||
@@ -115,13 +116,16 @@ impl Reader<'_> {
|
||||
let (written, after) = rest.split_at(end);
|
||||
rest = after;
|
||||
match html::tag(written) {
|
||||
Some(tag) if !tag.closing && matches!(tag.name.as_str(), "script" | "style") => {
|
||||
let closing = format!("</{}", tag.name);
|
||||
let skipped = rest.to_ascii_lowercase().find(&closing);
|
||||
let left = skipped.map_or("", |at| &rest[at..]);
|
||||
rest = html::tag_end(left).map_or("", |end| &left[end..]);
|
||||
Some(tag) if html::filtered(&tag.name) => run.words(written),
|
||||
// A block closes what opened inside it.
|
||||
Some(tag) if html::breaks(&tag.name) => {
|
||||
run.line();
|
||||
match tag.closing {
|
||||
true => run.end(&tag.name),
|
||||
false if tag.name != "hr" => run.start(&tag.name, None, None),
|
||||
false => {}
|
||||
}
|
||||
}
|
||||
Some(tag) if html::breaks(&tag.name) => run.line(),
|
||||
// A line's end drops its space.
|
||||
Some(tag) if tag.closing || tag.name == "br" => {
|
||||
run.owed = run.owed.filter(|_| tag.name != "br");
|
||||
@@ -165,39 +169,44 @@ impl Run {
|
||||
|
||||
/// Writes the white space owed, before a word or tag.
|
||||
fn pay(&mut self) {
|
||||
if let Some(owed) = self.owed.take() {
|
||||
self.text.text.push(owed);
|
||||
let Some(owed) = self.owed.take() else {
|
||||
return;
|
||||
};
|
||||
let at = self.text.text.len();
|
||||
self.text.text.push(owed);
|
||||
// Reopened elements start after it.
|
||||
for opened in self.open.iter_mut().filter(|opened| opened.start == at) {
|
||||
opened.start = self.text.text.len();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Inlines past the first line ending.
|
||||
fn after_first_line(children: &[mdast::Node]) -> Vec<mdast::Node> {
|
||||
let mut found = None;
|
||||
/// A plain first line naming a kind, and what follows.
|
||||
fn marker(children: &[mdast::Node]) -> Option<(AlertKind, Vec<mdast::Node>)> {
|
||||
let mut line = String::new();
|
||||
for (at, child) in children.iter().enumerate() {
|
||||
match child {
|
||||
mdast::Node::Text(text) => {
|
||||
if let Some((_, rest)) = text.value.split_once('\n') {
|
||||
let mut kept = text.clone();
|
||||
kept.value = rest.to_string();
|
||||
found = Some((at + 1, (!rest.is_empty()).then_some(kept)));
|
||||
break;
|
||||
let head = match child {
|
||||
mdast::Node::Text(text) => match text.value.find(['\n', '\r']) {
|
||||
None => {
|
||||
line.push_str(&text.value);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
mdast::Node::Break(_) => {
|
||||
found = Some((at + 1, None));
|
||||
break;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
Some(end) => {
|
||||
line.push_str(&text.value[..end]);
|
||||
let after = &text.value[end..];
|
||||
let after = after.strip_prefix("\r\n").unwrap_or(&after[1..]);
|
||||
let mut kept = text.clone();
|
||||
kept.value = after.to_string();
|
||||
(!after.is_empty()).then_some(mdast::Node::Text(kept))
|
||||
}
|
||||
},
|
||||
mdast::Node::Break(_) => None,
|
||||
_ => return None,
|
||||
};
|
||||
let after = head.into_iter().chain(children[at + 1..].iter().cloned());
|
||||
return Some((AlertKind::marked(&line)?, after.collect()));
|
||||
}
|
||||
let Some((next, head)) = found else {
|
||||
return Vec::new();
|
||||
};
|
||||
let head = head.map(mdast::Node::Text);
|
||||
head.into_iter()
|
||||
.chain(children[next..].iter().cloned())
|
||||
.collect()
|
||||
Some((AlertKind::marked(&line)?, Vec::new()))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -239,6 +248,15 @@ mod tests {
|
||||
("> [!IMPORTANT]", None),
|
||||
("> [!FOO]\n> five", None),
|
||||
("> text\n> [!NOTE]", None),
|
||||
("> \\[!NOTE]\n> escaped", Some(AlertKind::Note)),
|
||||
("> [!NOTE]\n> referenced", Some(AlertKind::Note)),
|
||||
("> [!NOTE] \n> hard line", Some(AlertKind::Note)),
|
||||
("> [!NOTE]\rcarriage", Some(AlertKind::Note)),
|
||||
("> [!NOTE]\nlazy", Some(AlertKind::Note)),
|
||||
("> [!NOTE]\u{a0}\n> no-break space", None),
|
||||
("> **[!TIP]**\n> strong", None),
|
||||
("> <span>[!WARNING]</span>\n> tag", None),
|
||||
("[!NOTE]: https://example.com\n\n> [!NOTE]\n> a link", None),
|
||||
];
|
||||
for (source, kind) in cases {
|
||||
assert_eq!(alert(source).map(|found| found.0), kind, "{source:?}");
|
||||
@@ -303,12 +321,34 @@ mod tests {
|
||||
html("<div>\na <br> b</br>c</div>").expect("text").text,
|
||||
"a\nb\nc"
|
||||
);
|
||||
let hidden = "<div>\n<script>alert(':smile:')</script>\n<!-- note -->\nshown<style>p{}</style>\n</div>";
|
||||
assert_eq!(html(hidden).expect("text").text, "shown");
|
||||
assert_eq!(html("<style>\np { color: red }\n</style>"), None);
|
||||
let filtered = "<div>\n<script>x</script>\n<!-- note -->\nshown<style>p{}</style>\n</div>";
|
||||
assert_eq!(
|
||||
html(filtered).expect("text").text,
|
||||
"<script>x</script> shown<style>p{}</style>"
|
||||
);
|
||||
let crossed = "<div>before<script>a</scriptx>b</script>after</div>";
|
||||
assert_eq!(
|
||||
html(crossed).expect("text").text,
|
||||
"before<script>ab</script>after"
|
||||
);
|
||||
let split = parse("<div>\n<script>\nfirst\n\nsecond\n</script>\n</div>");
|
||||
assert_eq!(text(&split), ["<script> first", "second </script>"]);
|
||||
let inline = html("a <script>b</script> c").expect("text");
|
||||
assert_eq!(inline.text, "a <script>b</script> c");
|
||||
assert_eq!(html("<div>\na < b <i").expect("text").text, "a < b");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_closing_block_ends_what_opened_inside_it() {
|
||||
let marked = html("<div><mark>x</div>y").expect("text");
|
||||
assert_eq!(
|
||||
(marked.text.as_str(), &marked.styles[..]),
|
||||
("x\ny", &[(0..1, Style::Mark)][..])
|
||||
);
|
||||
let bold = html("<div><b>x</div>y").expect("text");
|
||||
assert_eq!(bold.styles, [(0..1, Style::Strong), (2..3, Style::Strong)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hostile_html_reads_in_linear_time() {
|
||||
let started = web_time::Instant::now();
|
||||
@@ -316,6 +356,9 @@ mod tests {
|
||||
"<".repeat(1 << 17),
|
||||
"<a".repeat(1 << 16),
|
||||
"<!--".repeat(1 << 16),
|
||||
"<b>".repeat(1 << 16),
|
||||
"<b>".repeat(1 << 15) + &"</i>".repeat(1 << 15),
|
||||
"<script></script>".repeat(1 << 13),
|
||||
] {
|
||||
let block = format!("<div>\n{source}");
|
||||
parse(&block);
|
||||
|
||||
@@ -41,6 +41,22 @@ pub(crate) fn reopens(name: &str) -> bool {
|
||||
)
|
||||
}
|
||||
|
||||
/// GFM's tag filter: these tags show as text.
|
||||
pub(crate) fn filtered(name: &str) -> bool {
|
||||
matches!(
|
||||
name,
|
||||
"title"
|
||||
| "textarea"
|
||||
| "style"
|
||||
| "xmp"
|
||||
| "iframe"
|
||||
| "noembed"
|
||||
| "noframes"
|
||||
| "script"
|
||||
| "plaintext"
|
||||
)
|
||||
}
|
||||
|
||||
/// Elements a browser lays as blocks: a line each side.
|
||||
pub(crate) fn breaks(name: &str) -> bool {
|
||||
matches!(
|
||||
|
||||
+41
-18
@@ -98,6 +98,8 @@ struct Opened {
|
||||
struct Run {
|
||||
text: Inline,
|
||||
open: Vec<Opened>,
|
||||
/// How many of each name are open.
|
||||
named: HashMap<String, usize>,
|
||||
/// Inside an autolink: its text stays as written.
|
||||
verbatim: bool,
|
||||
/// White space an HTML block owes the next word.
|
||||
@@ -116,9 +118,28 @@ impl Run {
|
||||
done
|
||||
}
|
||||
|
||||
fn count(&self, name: &str) -> usize {
|
||||
self.named.get(name).copied().unwrap_or(0)
|
||||
}
|
||||
|
||||
/// Inside HTML `<code>`, where shortcodes stay text.
|
||||
fn in_code(&self) -> bool {
|
||||
self.open.iter().any(|opened| opened.name == "code")
|
||||
self.count("code") > 0
|
||||
}
|
||||
|
||||
/// Text as shown: shortcodes read, lines joined.
|
||||
fn push_text(&mut self, written: &str) {
|
||||
let shown = match self.verbatim || self.in_code() {
|
||||
true => written.to_string(),
|
||||
false => html::emoji(written),
|
||||
};
|
||||
let soft = shown.replace("\r\n", " ").replace(['\r', '\n'], " ");
|
||||
self.text.text.push_str(&soft);
|
||||
}
|
||||
|
||||
fn push(&mut self, opened: Opened) {
|
||||
*self.named.entry(opened.name.clone()).or_default() += 1;
|
||||
self.open.push(opened);
|
||||
}
|
||||
|
||||
/// The innermost open link's address.
|
||||
@@ -134,6 +155,9 @@ impl Run {
|
||||
if tag.name == "br" {
|
||||
return self.text.text.push('\n');
|
||||
}
|
||||
if html::filtered(&tag.name) {
|
||||
return self.push_text(&html::text(written));
|
||||
}
|
||||
let Some(style) = html::inline_style(&tag.name) else {
|
||||
return;
|
||||
};
|
||||
@@ -153,7 +177,7 @@ impl Run {
|
||||
if name == "a" {
|
||||
self.end("a");
|
||||
}
|
||||
self.open.push(Opened {
|
||||
self.push(Opened {
|
||||
name: name.to_string(),
|
||||
start: self.text.text.len(),
|
||||
style,
|
||||
@@ -163,17 +187,23 @@ impl Run {
|
||||
|
||||
/// Ends the innermost `name`, as HTML5 repairs nesting.
|
||||
fn end(&mut self, name: &str) {
|
||||
let Some(at) = self.open.iter().rposition(|opened| opened.name == name) else {
|
||||
if self.count(name) == 0 {
|
||||
return;
|
||||
};
|
||||
}
|
||||
let at = self
|
||||
.open
|
||||
.iter()
|
||||
.rposition(|opened| opened.name == name)
|
||||
.expect("a counted name is open");
|
||||
let end = self.text.text.len();
|
||||
let above = self.open.split_off(at + 1);
|
||||
let ended = self.open.pop().expect("found at `at`");
|
||||
close(&mut self.text, &ended, end);
|
||||
for opened in above {
|
||||
close(&mut self.text, &opened, end);
|
||||
let closed = self.open.split_off(at);
|
||||
for opened in &closed {
|
||||
close(&mut self.text, opened, end);
|
||||
*self.named.get_mut(&opened.name).expect("counted") -= 1;
|
||||
}
|
||||
for opened in closed.into_iter().skip(1) {
|
||||
if html::reopens(&opened.name) {
|
||||
self.open.push(Opened {
|
||||
self.push(Opened {
|
||||
start: end,
|
||||
..opened
|
||||
});
|
||||
@@ -357,14 +387,7 @@ impl<'a> Reader<'a> {
|
||||
fn inline_into(&self, node: &mdast::Node, run: &mut Run, images: &mut Images) {
|
||||
let text = &mut run.text;
|
||||
let (children, (name, style), url) = match node {
|
||||
mdast::Node::Text(words) => {
|
||||
let shown = match run.verbatim || run.in_code() {
|
||||
true => words.value.clone(),
|
||||
false => html::emoji(&words.value),
|
||||
};
|
||||
let soft = shown.replace("\r\n", " ").replace(['\r', '\n'], " ");
|
||||
return run.text.text.push_str(&soft);
|
||||
}
|
||||
mdast::Node::Text(words) => return run.push_text(&words.value),
|
||||
mdast::Node::Break(_) => return text.text.push('\n'),
|
||||
mdast::Node::InlineCode(code) => return styled(text, &code.value, Style::Code),
|
||||
mdast::Node::InlineMath(math) => return styled(text, &math.value, Style::Math),
|
||||
|
||||
Reference in new issue
Block a user