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

Commit

Permalink
Escape text used for mentions and links
Browse files Browse the repository at this point in the history
  • Loading branch information
jmartinesp committed Jun 19, 2024
1 parent 5c62c61 commit 6b2d4fb
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions bindings/wysiwyg-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ crate-type = ["cdylib", "staticlib"]
# Keep the uniffi version here in sync with the installed version of
# uniffi-bindgen that is called from
# ../../examples/example-android/app/build.gradle
html-escape = "0.2.11"
matrix_mentions = { path = "../../crates/matrix_mentions" }
uniffi = { workspace = true }
uniffi_macros = { workspace = true }
Expand Down
6 changes: 3 additions & 3 deletions bindings/wysiwyg-ffi/src/ffi_composer_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ impl ComposerModel {
attributes: Vec<Attribute>,
) -> Arc<ComposerUpdate> {
let url = Utf16String::from_str(&url);
let text = Utf16String::from_str(&text);
let text = Utf16String::from_str(&html_escape::encode_safe(&text));
let attrs = attributes
.iter()
.map(|attr| {
Expand Down Expand Up @@ -286,7 +286,7 @@ impl ComposerModel {
_attributes: Vec<Attribute>, // TODO remove attributes
) -> Arc<ComposerUpdate> {
let url = Utf16String::from_str(&url);
let text = Utf16String::from_str(&text);
let text = Utf16String::from_str(&html_escape::encode_safe(&text));
let attrs = vec![];
Arc::new(ComposerUpdate::from(
self.inner.lock().unwrap().insert_mention(url, text, attrs),
Expand Down Expand Up @@ -319,7 +319,7 @@ impl ComposerModel {
_attributes: Vec<Attribute>, // TODO remove attributes
) -> Arc<ComposerUpdate> {
let url = Utf16String::from_str(&url);
let text = Utf16String::from_str(&text);
let text = Utf16String::from_str(&html_escape::encode_safe(&text));
let suggestion = wysiwyg::SuggestionPattern::from(suggestion);
let attrs = vec![];
Arc::new(ComposerUpdate::from(
Expand Down
24 changes: 24 additions & 0 deletions bindings/wysiwyg-ffi/src/ffi_composer_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,30 @@ mod test {
)
}

#[test]
fn test_replace_text_with_escaped_html_in_mention_ffi() {
let mut model = Arc::new(ComposerModel::new());
model.replace_text("hello ".into());

let update = model.replace_text("@alic".into());
let MenuAction::Suggestion { suggestion_pattern } =
update.menu_action()
else {
panic!("No suggestion pattern found")
};
model.insert_mention_at_suggestion(
"https://matrix.to/#/@alice:matrix.org".into(),
":D</a> a broken mention!".into(),
suggestion_pattern,
vec![], // TODO remove argument when function signature changes
);

assert_eq!(
model.get_content_as_html(),
"hello <a data-mention-type=\"user\" href=\"https://matrix.to/#/@alice:matrix.org\" contenteditable=\"false\">:D&lt;&#x2F;a&gt; a broken mention!</a>\u{a0}",
)
}

// TODO remove attributes when Rust model can parse url directly
// https://github.com/matrix-org/matrix-rich-text-editor/issues/709
fn insert_mention_at_cursor(model: &mut Arc<ComposerModel>) {
Expand Down

0 comments on commit 6b2d4fb

Please sign in to comment.