Skip to content
This repository has been archived by the owner on Sep 27, 2024. It is now read-only.

Platform code cannot differentiate between a new line placeholder and an user provided single space node #792

Open
aringenbach opened this issue Aug 30, 2023 · 0 comments

Comments

@aringenbach
Copy link
Contributor

aringenbach commented Aug 30, 2023

Currently we use <p>&nbsp;</p> as a way to have a placeholder for new lines in several situations. The issue is that we also convert trailing space to &nbsp;, hence if a user enters a single space in a paragraph, the Rust code would output the exact same than what it outputs as a placeholder.

This replacement in Rust occurs here:

let mut escaped = html_escape::encode_text(&string).to_string();
// Replace all pairs of spaces with non-breaking ones. Transforms
// `a b` to `a\u{A0}\u{A0}\u{A0}\u{A0} b`, which will render
// exactly as five spaces like in the input.
if !state.is_inside_code_block {
escaped = escaped.replace(" ", "\u{A0}\u{A0}");
if state.is_last_node_in_parent
&& escaped.chars().next_back().map_or(false, |c| c == ' ')
{
// If this is the last node and it ends in a space, replace that
// 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());

The placeholder thing is used by the parser on iOS here:

if child.text() == .nbsp {
// Removing NBSP character from e.g. <p>&nbsp;</p> since it is only used to
// make DTCoreText able to easily parse new lines.
removeAllChildNodes()
let newChild = PlaceholderTextHTMLElement(from: child)
addChildNode(newChild)
newChild.inheritAttributes(from: self)
newChild.interpretAttributes()
} else {

Known issues related to this on iOS:

Not using the HTML and build the attributed string directly from the model would definitely fix that though.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants