Skip to content

Commit 3b8b034

Browse files
committed
Add stricter checks/tests around lists
As we are now parsing from non-matrix sources, adding some rigour around validation of a valid dom. E.g. making sure nodes other than list items are not added to lists or that list items are not added to containers other than lists.
1 parent bc5d2c7 commit 3b8b034

File tree

2 files changed

+674
-435
lines changed

2 files changed

+674
-435
lines changed

crates/wysiwyg/src/composer_model/replace_html.rs

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,101 @@ mod test {
193193
let html_str = html.to_string();
194194
assert_eq!(html_str, "<p>Existing content</p>");
195195
}
196+
197+
#[test]
198+
fn test_insert_list_item_without_list_parent() {
199+
let mut model = cm("hello|");
200+
let html = "<li>list item</li>";
201+
202+
let _ = model.replace_html(html.into(), HtmlSource::UnknownExternal);
203+
204+
let html = model.get_content_as_html();
205+
let html_str = html.to_string();
206+
assert_eq!(html_str, "<p>hello</p><p>list item</p>");
207+
}
208+
}
209+
210+
#[cfg(all(test, target_arch = "wasm32"))]
211+
mod wasm_tests {
212+
use crate::dom::html_source::HtmlSource;
213+
use crate::tests::testutils_composer_model::cm;
214+
use wasm_bindgen_test::*;
215+
216+
wasm_bindgen_test_configure!(run_in_browser);
217+
218+
#[wasm_bindgen_test]
219+
fn test_replace_html_with_existing_selection() {
220+
let mut model = cm("Hello{world}|test");
221+
let new_html = "<p><em>replacement</em></p>";
222+
223+
let _ =
224+
model.replace_html(new_html.into(), HtmlSource::UnknownExternal);
225+
226+
let html = model.get_content_as_html();
227+
let html_str = html.to_string();
228+
assert_eq!(
229+
html_str,
230+
"<p>Hello</p><p><em>replacement</em></p><p>test</p>"
231+
);
232+
}
233+
234+
#[wasm_bindgen_test]
235+
fn test_replace_html_cursor_position_after_insert() {
236+
let mut model = cm("Start|");
237+
let new_html = "<strong>Bold text</strong>";
238+
let _ = model.replace_html(new_html.into(), HtmlSource::Matrix);
239+
// Cursor should be positioned after the inserted content
240+
let (start, end) = model.safe_selection();
241+
assert_eq!(start, end); // No selection, just cursor
242+
model.bold();
243+
model.enter();
244+
// Insert more text to verify cursor position
245+
let _ = model.replace_text("End".into());
246+
let html = model.get_content_as_html();
247+
let html_str = html.to_string();
248+
assert_eq!(
249+
html_str,
250+
"<p>Start</p><p><strong>Bold text</strong></p><p>End</p>"
251+
);
252+
}
253+
254+
#[wasm_bindgen_test]
255+
fn test_replace_html_multiple_meta_tags() {
256+
let mut model = cm("|");
257+
let html_with_multiple_metas = r#"<meta charset="utf-8"><meta name="viewport" content="width=device-width"><meta http-equiv="X-UA-Compatible" content="IE=edge"><p>Content after metas</p>"#;
258+
259+
let _ = model.replace_html(
260+
html_with_multiple_metas.into(),
261+
HtmlSource::UnknownExternal,
262+
);
263+
264+
let html = model.get_content_as_html();
265+
let html_str = html.to_string();
266+
assert!(!html_str.contains("<meta"));
267+
assert_eq!(html_str, "<p>Content after metas</p>");
268+
}
269+
270+
#[wasm_bindgen_test]
271+
fn test_replace_html_empty_content() {
272+
let mut model = cm("Existing content|");
273+
let empty_html = "";
274+
275+
let _ = model.replace_html(empty_html.into(), HtmlSource::Matrix);
276+
277+
let html = model.get_content_as_html();
278+
let html_str = html.to_string();
279+
assert_eq!(html_str, "<p>Existing content</p>");
280+
}
281+
282+
#[wasm_bindgen_test]
283+
fn test_insert_list_item_without_list_parent() {
284+
let mut model = cm("hello|");
285+
let html = "<li>list item</li>";
286+
287+
let _ = model.replace_html(html.into(), HtmlSource::UnknownExternal);
288+
289+
let html = model.get_content_as_html();
290+
let html_str = html.to_string();
291+
assert_eq!(html_str, "<p>hello</p><p>list item</p>");
292+
}
196293
}

0 commit comments

Comments
 (0)