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

Escape text in links and mentions for wasm bindings #1032

Merged
merged 3 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ crate-type = ["cdylib"]

[dependencies]
console_error_panic_hook = "0.1.7"
html-escape = "0.2.11"
js-sys = "0.3.60"
wasm-bindgen = "0.2.83"
wasm-bindgen-futures = "0.4.33"
Expand Down
6 changes: 3 additions & 3 deletions bindings/wysiwyg-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ impl ComposerModel {
) -> ComposerUpdate {
ComposerUpdate::from(self.inner.set_link_with_text(
Utf16String::from_str(url),
Utf16String::from_str(text),
Utf16String::from_str(&html_escape::encode_safe(&text)),
attributes.into_vec(),
))
}
Expand Down Expand Up @@ -360,7 +360,7 @@ impl ComposerModel {
) -> ComposerUpdate {
ComposerUpdate::from(self.inner.insert_mention(
Utf16String::from_str(url),
Utf16String::from_str(text),
Utf16String::from_str(&html_escape::encode_safe(&text)),
attributes.into_vec(),
))
}
Expand Down Expand Up @@ -389,7 +389,7 @@ impl ComposerModel {
) -> ComposerUpdate {
ComposerUpdate::from(self.inner.insert_mention_at_suggestion(
Utf16String::from_str(url),
Utf16String::from_str(text),
Utf16String::from_str(&html_escape::encode_safe(&text)),
wysiwyg::SuggestionPattern::from(suggestion.clone()),
attributes.into_vec(),
))
Expand Down
37 changes: 36 additions & 1 deletion platforms/web/lib/suggestion.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,22 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { SuggestionPattern } from '../generated/wysiwyg';
import init, {
// eslint-disable-next-line camelcase
new_composer_model,
SuggestionPattern,
} from '../generated/wysiwyg';
import { SUGGESTIONS } from './constants';
import {
getSuggestionChar,
getSuggestionType,
mapSuggestion,
} from './suggestion';

beforeAll(async () => {
await init();
});

describe('getSuggestionChar', () => {
it('returns the expected character', () => {
SUGGESTIONS.forEach((suggestionCharacter, index) => {
Expand Down Expand Up @@ -92,3 +100,30 @@ describe('mapSuggestion', () => {
});
});
});

describe('suggestionPattern', () => {
it('Content should be encoded', () => {
// Given
const model = new_composer_model();
model.replace_text('hello ');
const update = model.replace_text('@alic');
const suggestion = update.menu_action().suggestion();

// When
if (!suggestion) {
fail('There should be an suggestion!');
}

model.insert_mention_at_suggestion(
'https://matrix.to/#/@alice:matrix.org',
':D</a> a broken mention!',
suggestion.suggestion_pattern,
new Map(),
);

// Then
expect(model.get_content_as_html()).toBe(
'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}',
);
});
});
Loading