Skip to content

Commit

Permalink
Merge pull request #1032 from matrix-org/langleyd/wasm_escape_text_in…
Browse files Browse the repository at this point in the history
…_mentions_and_links

Escape text in links and mentions for wasm bindings
  • Loading branch information
langleyd committed Aug 28, 2024
2 parents e8b5f33 + 4fa23e8 commit 2d8f0c7
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 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-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}',
);
});
});

0 comments on commit 2d8f0c7

Please sign in to comment.