Skip to content

Commit

Permalink
Fix leading whitespace getting ignored by parsers (#598)
Browse files Browse the repository at this point in the history
* Fix leading whitespace getting ignored by parsers

* Add test for multiple leading/trailing whitespaces
  • Loading branch information
aringenbach committed Mar 1, 2023
1 parent d61425e commit 28034ad
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 7 deletions.
2 changes: 1 addition & 1 deletion crates/wysiwyg/src/composer_model/code_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ mod test {
model.code_block();
assert_eq!(
tx(&model),
"<p>Text</p><p><b>code|</b><i> and italic</i></p>"
"<p>Text</p><p><b>code|</b><i>&nbsp;and italic</i></p>"
);
}

Expand Down
6 changes: 3 additions & 3 deletions crates/wysiwyg/src/composer_model/new_lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ mod test {
fn test_new_line_in_plain_text() {
let mut model = cm("Test| lines");
model.enter();
assert_eq!(tx(&model), "<p>Test</p><p>| lines</p>");
assert_eq!(tx(&model), "<p>Test</p><p>|&nbsp;lines</p>");
}

#[test]
Expand All @@ -364,14 +364,14 @@ mod test {
fn test_new_line_in_formatted_text() {
let mut model = cm("<b>Test| lines</b>");
model.enter();
assert_eq!(tx(&model), "<p><b>Test</b></p><p><b>| lines</b></p>");
assert_eq!(tx(&model), "<p><b>Test</b></p><p><b>|&nbsp;lines</b></p>");
}

#[test]
fn test_new_line_in_paragraph() {
let mut model = cm("<p>Test| lines</p>");
model.enter();
assert_eq!(tx(&model), "<p>Test</p><p>| lines</p>");
assert_eq!(tx(&model), "<p>Test</p><p>|&nbsp;lines</p>");
}

#[test]
Expand Down
8 changes: 8 additions & 0 deletions crates/wysiwyg/src/dom/nodes/text_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,14 @@ where
// space with a non-breaking one.
escaped.replace_range(escaped.len() - 1.., "\u{A0}");
}

if state.is_first_node_in_parent
&& escaped.chars().next().map_or(false, |c| c == ' ')
{
// If this is the first node and it starts with a space, replace that
// space with a non-breaking one.
escaped.replace_range(..1, "\u{A0}");
}
}
buf.push(escaped.as_str());

Expand Down
36 changes: 36 additions & 0 deletions crates/wysiwyg/src/tests/test_characters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,42 @@ fn insert_text_between_line_breaks_in_format_node() {
assert_eq!(tx(&model), "A<br /><b>C|<br />B</b>");
}

#[test]
fn leading_whitespace_is_replaced_with_nbsp() {
let model = cm("<p> text|</p>");
assert_eq!(tx(&model), "<p>&nbsp;text|</p>")
}

#[test]
fn multiple_leading_whitespaces_are_replaced_with_nbsp() {
let model = cm("<p> text|</p>");
assert_eq!(tx(&model), "<p>&nbsp;&nbsp;text|</p>")
}

#[test]
fn trailing_whitespace_is_replaced_with_nbsp() {
let model = cm("<p>text |</p>");
assert_eq!(tx(&model), "<p>text&nbsp;|</p>")
}

#[test]
fn multiple_trailing_whitespaces_are_replaced_with_nbsp() {
let model = cm("<p>text |</p>");
assert_eq!(tx(&model), "<p>text&nbsp;&nbsp;|</p>")
}

#[test]
fn leading_and_trailing_whitespace_are_both_replaced_with_nbsp() {
let model = cm("<p> text |</p>");
assert_eq!(tx(&model), "<p>&nbsp;text&nbsp;|</p>");
}

#[test]
fn multiple_leading_and_trailing_whitespace_are_all_replaced_with_nbsp() {
let model = cm("<p> text |</p>");
assert_eq!(tx(&model), "<p>&nbsp;&nbsp;text&nbsp;&nbsp;|</p>");
}

fn replace_text(model: &mut ComposerModel<Utf16String>, new_text: &str) {
model.replace_text(utf16(new_text));
}
2 changes: 1 addition & 1 deletion crates/wysiwyg/src/tests/test_deleting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ fn deleting_selection_in_multiple_containers() {
fn deleting_selection_of_a_container_in_multiple_containers() {
let mut model = cm("<i><b>{test}|</b> test</i>");
model.backspace();
assert_eq!(tx(&model), "<i>| test</i>");
assert_eq!(tx(&model), "<i>|&nbsp;test</i>");
model.state.dom.explicitly_assert_invariants();
}

Expand Down
2 changes: 1 addition & 1 deletion crates/wysiwyg/src/tests/test_formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn format_several_nodes_with_empty_text_nodes() {
model.italic();
model.select(Location::from(2), Location::from(17));
model.strike_through();
assert_eq!(tx(&model), "<strong>so<del>{me</del></strong><del>&nbsp;</del><em><del>different</del></em><del> no}|</del>des")
assert_eq!(tx(&model), "<strong>so<del>{me</del></strong><del>&nbsp;</del><em><del>different</del></em><del>&nbsp;no}|</del>des")
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion crates/wysiwyg/src/tests/test_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ fn set_link_accross_quote() {
test_<a href=\"https://element.io\">{block_quote</a>\
</blockquote>\
<p>\
<a href=\"https://element.io\"> test}|</a>\
<a href=\"https://element.io\">&nbsp;test}|</a>\
</p>"
);
}
Expand Down

0 comments on commit 28034ad

Please sign in to comment.