Skip to content

Commit 296d50c

Browse files
committed
Fix: prevent editing of the span that wraps math.
1 parent db37085 commit 296d50c

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

server/src/processing.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,11 +1176,21 @@ fn replace_math_node(child: &Rc<Node>, is_hydrate: bool) -> Option<Rc<Node>> {
11761176
{
11771177
// Final test: if the class is correct, this is math; otherwise, perform
11781178
// no transformation.
1179+
//
1180+
// Add/remove the `mceNonEditable` class to prevent accidental edits of this span.
11791181
let attr_value_str: &str = attr_value;
1180-
let delim = match attr_value_str {
1181-
"math math-inline" => Some(("\\(", "\\)")),
1182-
"math math-display" => Some(("$$", "$$")),
1183-
_ => None,
1182+
let delim = if is_hydrate {
1183+
match attr_value_str {
1184+
"math math-inline" => Some(("\\(", "\\)", "math math-inline mceNonEditable")),
1185+
"math math-display" => Some(("$$", "$$", "math math-display mceNonEditable")),
1186+
_ => None,
1187+
}
1188+
} else {
1189+
match attr_value_str {
1190+
"math math-inline mceNonEditable" => Some(("\\(", "\\)", "math math-inline")),
1191+
"math math-display mceNonEditable" => Some(("$$", "$$", "math math-display")),
1192+
_ => None,
1193+
}
11841194
};
11851195

11861196
// Since we've already borrowed `child`, we can't `borrow_mut` to modify
@@ -1205,7 +1215,7 @@ fn replace_math_node(child: &Rc<Node>, is_hydrate: bool) -> Option<Rc<Node>> {
12051215
name: QualName::new(None, Namespace::from(""), LocalName::from("span")),
12061216
attrs: RefCell::new(vec![Attribute {
12071217
name: QualName::new(None, Namespace::from(""), LocalName::from("class")),
1208-
value: attr_value.clone(),
1218+
value: delim.2.into(),
12091219
}]),
12101220
template_contents: RefCell::new(None),
12111221
mathml_annotation_xml_integration_point: false,

server/src/processing/tests.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,12 +1293,12 @@ fn test_hydrate_html_1() {
12931293
.unwrap(),
12941294
indoc!(
12951295
r#"
1296-
<p><span class="math math-inline">\({a}_1, b_{2}\)</span>
1297-
<span class="math math-inline">\(a*1, b*2\)</span>
1298-
<span class="math math-inline">\([a](b)\)</span>
1299-
<span class="math math-inline">\(3 &lt;a&gt; b\)</span>
1300-
<span class="math math-inline">\(a \; b\)</span></p>
1301-
<p><span class="math math-display">$${a}_1, b_{2}, a*1, b*2, [a](b), 3 &lt;a&gt; b, a \; b$$</span></p>
1296+
<p><span class="math math-inline mceNonEditable">\({a}_1, b_{2}\)</span>
1297+
<span class="math math-inline mceNonEditable">\(a*1, b*2\)</span>
1298+
<span class="math math-inline mceNonEditable">\([a](b)\)</span>
1299+
<span class="math math-inline mceNonEditable">\(3 &lt;a&gt; b\)</span>
1300+
<span class="math math-inline mceNonEditable">\(a \; b\)</span></p>
1301+
<p><span class="math math-display mceNonEditable">$${a}_1, b_{2}, a*1, b*2, [a](b), 3 &lt;a&gt; b, a \; b$$</span></p>
13021302
"#
13031303
)
13041304
);
@@ -1380,12 +1380,12 @@ fn test_dehydrate_html_1() {
13801380
.convert(
13811381
&dehydrate_html(indoc!(
13821382
r#"
1383-
<p><span class="math math-inline">\({a}_1, b_{2}\)</span>
1384-
<span class="math math-inline">\(a*1, b*2\)</span>
1385-
<span class="math math-inline">\([a](b)\)</span>
1386-
<span class="math math-inline">\(3 &lt;a&gt; b\)</span>
1387-
<span class="math math-inline">\(a \; b\)</span></p>
1388-
<p><span class="math math-display">$${a}_1, b_{2}, a*1, b*2, [a](b), 3 &lt;a&gt; b, a \; b$$</span></p>
1383+
<p><span class="math math-inline mceNonEditable">\({a}_1, b_{2}\)</span>
1384+
<span class="math math-inline mceNonEditable">\(a*1, b*2\)</span>
1385+
<span class="math math-inline mceNonEditable">\([a](b)\)</span>
1386+
<span class="math math-inline mceNonEditable">\(3 &lt;a&gt; b\)</span>
1387+
<span class="math math-inline mceNonEditable">\(a \; b\)</span></p>
1388+
<p><span class="math math-display mceNonEditable">$${a}_1, b_{2}, a*1, b*2, [a](b), 3 &lt;a&gt; b, a \; b$$</span></p>
13891389
"#
13901390
))
13911391
.unwrap()

0 commit comments

Comments
 (0)