Repository navigation
fix(id-web): html_inline passthrough — inline HTML round-trips unescaped (#943) #1009
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -379,8 +379,14 @@ fn convert_node<'a>(node: &'a AstNode<'a>, active_marks: &[Mark]) -> Value { | |
| if let Some(img_attrs) = parse_linked_img_tag(html).or_else(|| parse_img_tag(html)) { | ||
| return img_attrs; | ||
| } | ||
| // Other inline HTML becomes plain text | ||
| text_node_with_marks(html, active_marks) | ||
| // #943: other inline HTML passes through as an html_inline | ||
| // atom node carrying the raw tag, so the save direction emits | ||
| // it verbatim (NodeValue::HtmlInline) instead of the comrak | ||
| // text renderer backslash-escaping it as literal text. | ||
| json!({ | ||
| "type": "html_inline", | ||
| "attrs": {"html": html} | ||
| }) | ||
|
Comment on lines
+386
to
+389
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔎 Supported by static analysis
Carry active marks on inline HTML nodes. The For example, Add the active marks to the
|
||
| } | ||
|
|
||
| NodeValue::Table(table) => { | ||
|
|
@@ -1059,6 +1065,12 @@ fn json_to_ast<'a>(arena: &'a Arena<'a>, json: &Value) -> Result<&'a AstNode<'a> | |
| } | ||
| } | ||
|
|
||
| // #943: raw inline HTML (e.g. <sub>, <sup>, <u>, <kbd>) carried | ||
| // verbatim; comrak renders HtmlInline without escaping. | ||
| "html_inline" => { | ||
| NodeValue::HtmlInline(json["attrs"]["html"].as_str().unwrap_or("").to_owned()) | ||
| } | ||
|
|
||
| "hard_break" => NodeValue::LineBreak, | ||
|
|
||
| "text" => { | ||
|
|
@@ -2226,4 +2238,63 @@ mod tests { | |
| assert!(md.contains("fn a() {}"), "first node kept: {md}"); | ||
| assert!(md.contains("fn b() {}"), "second node kept: {md}"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_inline_html_sub_sup_u_roundtrip_unescaped() { | ||
| // #943: inline HTML must round-trip open->save as HTML, not as | ||
| // backslash-escaped literal text. | ||
| for original in [ | ||
| "H<sub>2</sub>O\n", | ||
| "x<sup>2</sup> + y\n", | ||
| "some <u>underlined</u> text\n", | ||
| ] { | ||
| let (first, second) = two_saves(original); | ||
| assert!( | ||
| !first.contains("\\<"), | ||
| "no backslash-escaping in save of {original:?}: {first}" | ||
| ); | ||
| for tag in ["<sub>", "</sub>", "<sup>", "</sup>", "<u>", "</u>"] { | ||
| if original.contains(tag) { | ||
| assert!( | ||
| first.contains(tag), | ||
| "{tag} survives save of {original:?}: {first}" | ||
| ); | ||
| } | ||
| } | ||
| assert_eq!(first, second, "fixpoint for {original:?}"); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_inline_html_parse_shape_is_html_inline_node() { | ||
| // #943: the parse direction represents a tag as one html_inline | ||
| // atom node carrying the raw tag, not as a text node. | ||
| let doc = markdown_to_prosemirror("H<sub>2</sub>O\n"); | ||
| let content = &doc["content"][0]["content"]; | ||
| let html_nodes: Vec<&Value> = content | ||
| .as_array() | ||
| .unwrap() | ||
| .iter() | ||
| .filter(|n| n["type"] == "html_inline") | ||
| .collect(); | ||
| assert_eq!(html_nodes.len(), 2, "open+close tags: {content}"); | ||
| assert_eq!(html_nodes[0]["attrs"]["html"], "<sub>"); | ||
| assert_eq!(html_nodes[1]["attrs"]["html"], "</sub>"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_inline_html_generality_nonformatting_tag_roundtrips() { | ||
| // #943 generality: passthrough is not a sub/sup/u whitelist — any | ||
| // inline tag (here <kbd> and a classed <span>) round-trips. | ||
| let (first, second) = two_saves("press <kbd>Ctrl</kbd> then <span class=\"x\">go</span>\n"); | ||
| assert!(first.contains("<kbd>"), "kbd tag survives: {first}"); | ||
| assert!(first.contains("</kbd>"), "kbd close survives: {first}"); | ||
| assert!( | ||
| first.contains("<span class=\"x\">"), | ||
| "span with attrs survives verbatim: {first}" | ||
| ); | ||
| assert!(first.contains("</span>"), "span close survives: {first}"); | ||
| assert!(!first.contains("\\<"), "no escaping: {first}"); | ||
| assert_eq!(first, second, "fixpoint"); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -186,6 +186,28 @@ export const richSchema = new Schema({ | |
| return ["th", 0]; | ||
| }, | ||
| }, | ||
| // #943: raw inline HTML (e.g. <sub>, <sup>, <u>, <kbd>) as an atom | ||
| // node carrying the tag verbatim in `html`; the markdown codec | ||
| // reads/writes it. Display shows the tag as inert text — the attr | ||
| // is never rendered via innerHTML or as live markup. | ||
| html_inline: { | ||
| inline: true, | ||
| group: "inline", | ||
| atom: true, | ||
| selectable: true, | ||
| attrs: { html: { default: "" } }, | ||
|
Comment on lines
+195
to
+198
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When users copy or cut the visibly rendered Useful? React with 👍 / 👎. |
||
| parseDOM: [ | ||
| { | ||
| tag: "span[data-html-inline]", | ||
| getAttrs(dom) { | ||
| return { html: (dom as HTMLElement).dataset.htmlInline ?? "" }; | ||
| }, | ||
| }, | ||
| ], | ||
| toDOM(node) { | ||
| return ["span", { "data-html-inline": node.attrs.html as string }, node.attrs.html as string]; | ||
| }, | ||
| }, | ||
| }), | ||
| marks: basicSchema.spec.marks.append({ | ||
| strikethrough: { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When inline HTML occurs inside a Markdown link, the new node discards
active_marks, unlike the previoustext_node_with_markscall. For example,[<kbd></kbd>](https://example.com)now produces two unmarked atoms with no copy of the link destination, so merely opening and saving removes the link entirely. Preserve enclosing marks on these atoms and serialize their wrappers in the save direction; the newjson_to_astarm currently ignores marks onhtml_inlinenodes as well.Useful? React with 👍 / 👎.