Skip to content
Open
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
292 changes: 292 additions & 0 deletions .playwright/tests/mentions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const sel = {
setChannelButton: '[data-testid="mention-set-channel-button"]',
startUserButton: '[data-testid="mention-start-user-button"]',
startChannelButton: '[data-testid="mention-start-channel-button"]',
removeInvalidToggle: '[data-testid="mention-remove-invalid-toggle"]',
} as const;

async function gotoMentionTest(page: Page): Promise<void> {
Expand Down Expand Up @@ -313,3 +314,294 @@ test("inserting a mention between text doesn't produce a double space", async ({
'<html><p>example <mention text="Jane" indicator="@" id="1">Jane</mention> test</p></html>'
);
});

// Inserts "Jane" plus the space setMention adds, then moves the caret
// `steps` characters left from the end: 1 is right after the mention,
// 3 is between "Ja" and "ne"
async function insertMentionAndMoveLeft(
page: Page,
steps: number
): Promise<void> {
const editor = mentionEditor(page);
await editor.click();
await editor.pressSequentially('@', { delay: 80 });
await page.locator(sel.setUserButton).click();
await expect
.poll(async () =>
(await htmlOutput(page).textContent())?.includes('<mention')
)
.toBe(true);
await editor.press('End');
for (let i = 0; i < steps; i++) {
await editor.press('ArrowLeft');
}
}

test('typing inside a mention keeps its text by default', async ({ page }) => {
await gotoMentionTest(page);
await insertMentionAndMoveLeft(page, 3);
await mentionEditor(page).press('x');
await expect
.poll(async () => await htmlOutput(page).textContent())
.toEqual('<html><p>Jaxne </p></html>');
});

test('removeInvalidMentionContent: typing inside a mention removes it', async ({
page,
}) => {
await gotoMentionTest(page);
await page.locator(sel.removeInvalidToggle).click();
await insertMentionAndMoveLeft(page, 3);
await mentionEditor(page).press('x');
await expect
.poll(async () => await htmlOutput(page).textContent())
.toEqual('<html><p> </p></html>');
});

test('removeInvalidMentionContent: backspace inside a mention removes it', async ({
page,
}) => {
await gotoMentionTest(page);
await page.locator(sel.removeInvalidToggle).click();
await insertMentionAndMoveLeft(page, 3);
await mentionEditor(page).press('Backspace');
await expect
.poll(async () => await htmlOutput(page).textContent())
.toEqual('<html><p> </p></html>');
});

test('removeInvalidMentionContent: typing right after a mention keeps it', async ({
page,
}) => {
await gotoMentionTest(page);
await page.locator(sel.removeInvalidToggle).click();
await insertMentionAndMoveLeft(page, 1);
await mentionEditor(page).press('x');
await expect
.poll(async () => await htmlOutput(page).textContent())
.toEqual(
'<html><p><mention text="Jane" indicator="@" id="1">Jane</mention>x </p></html>'
);
});

test('removeInvalidMentionContent: replacing everything keeps only the new text', async ({
page,
}) => {
await gotoMentionTest(page);
await page.locator(sel.removeInvalidToggle).click();
const editor = mentionEditor(page);
await editor.click();
await editor.pressSequentially('Hi @', { delay: 80 });
await page.locator(sel.setUserButton).click();
await editor.press('End');
await editor.pressSequentially('and more', { delay: 40 });
await editor.press('ControlOrMeta+a');
await editor.press('x');
await expect
.poll(async () => await htmlOutput(page).textContent())
.toEqual('<html><p>x</p></html>');
});

test('removeInvalidMentionContent: replacing a selection around a mention keeps only the new text', async ({
page,
}) => {
await gotoMentionTest(page);
await page.locator(sel.removeInvalidToggle).click();
const editor = mentionEditor(page);
await editor.click();
await editor.pressSequentially('Hi @', { delay: 80 });
await page.locator(sel.setUserButton).click();
await editor.press('End');
await editor.pressSequentially('end', { delay: 40 });
// "Hi Jane end": select "i Jane e"
await editor.press('Home');
await editor.press('ArrowRight');
for (let i = 0; i < 8; i++) {
await editor.press('Shift+ArrowRight');
}
await editor.press('Z');
await expect
.poll(async () => await htmlOutput(page).textContent())
.toEqual('<html><p>HZnd</p></html>');
});

test('removeInvalidMentionContent: replacing exactly a mention keeps the new text', async ({
page,
}) => {
await gotoMentionTest(page);
await page.locator(sel.removeInvalidToggle).click();
const editor = mentionEditor(page);
await editor.click();
await editor.pressSequentially('Hi @', { delay: 80 });
await page.locator(sel.setUserButton).click();
await editor.press('End');
await editor.pressSequentially('end', { delay: 40 });
// "Hi Jane end": select "Jane"
await editor.press('Home');
for (let i = 0; i < 3; i++) {
await editor.press('ArrowRight');
}
for (let i = 0; i < 4; i++) {
await editor.press('Shift+ArrowRight');
}
await editor.press('Z');
await expect
.poll(async () => await htmlOutput(page).textContent())
.toEqual('<html><p>Hi Z end</p></html>');
});

const pasteCases = [
{
name: 'a valid mention split by formatting is kept',
pasted: 'a <mention text="@Jane" indicator="@">@Ja<b>ne</b></mention> z',
expected: 'a <mention text="@Jane" indicator="@">@Ja<b>ne</b></mention> z',
},
{
name: 'a mention that does not match its text is removed',
pasted: 'a <mention text="@Jane" indicator="@">@Jake</mention> z',
expected: 'a z',
},
];

for (const { name, pasted, expected } of pasteCases) {
test(`removeInvalidMentionContent: pasting, ${name}`, async ({ page }) => {
await gotoMentionTest(page);
await page.locator(sel.removeInvalidToggle).click();
const editor = page.locator(
'[data-testid="mention-test-editor"] .ProseMirror'
);
await editor.click();
await editor.evaluate((el, html) => {
const data = new DataTransfer();
data.setData('text/html', html);
el.dispatchEvent(
new ClipboardEvent('paste', {
clipboardData: data,
bubbles: true,
cancelable: true,
})
);
}, pasted);
await expect
.poll(async () => await htmlOutput(page).textContent())
.toEqual(`<html><p>${expected}</p></html>`);
});
}

const JANE = '<mention text="Jane" indicator="@" id="1">Jane</mention>';

// "Jane Jane " with both as mentions and the caret at the start
async function insertTwoMentions(page: Page): Promise<void> {
const editor = mentionEditor(page);
await editor.click();
for (let i = 1; i <= 2; i++) {
await editor.pressSequentially('@', { delay: 80 });
await page.locator(sel.setUserButton).click();
await expect
.poll(async () => await htmlOutput(page).textContent())
.toEqual(`<html><p>${`${JANE} `.repeat(i)}</p></html>`);
await editor.press('End');
}
await editor.press('Home');
}

async function moveRight(page: Page, steps: number): Promise<void> {
for (let i = 0; i < steps; i++) {
await mentionEditor(page).press('ArrowRight');
}
// the editor picks up the new caret position asynchronously
await page.waitForTimeout(200);
}

test('removeInvalidMentionContent: equal mentions that end up touching are kept', async ({
page,
}) => {
await gotoMentionTest(page);
await page.locator(sel.removeInvalidToggle).click();
await insertTwoMentions(page);
await moveRight(page, 4);
await mentionEditor(page).press('Delete');
await expect
.poll(async () => await htmlOutput(page).textContent())
.toEqual(`<html><p>${JANE.replace('>Jane<', '>JaneJane<')} </p></html>`);
});

test('removeInvalidMentionContent: editing one of two touching equal mentions keeps the other', async ({
page,
}) => {
await gotoMentionTest(page);
await page.locator(sel.removeInvalidToggle).click();
await insertTwoMentions(page);
await moveRight(page, 4);
await mentionEditor(page).press('Delete');
await moveRight(page, 2);
await mentionEditor(page).press('x');
await expect
.poll(async () => await htmlOutput(page).textContent())
.toEqual(`<html><p>${JANE} </p></html>`);
});

test('removeInvalidMentionContent: a space typed between touching equal mentions keeps both', async ({
page,
}) => {
await gotoMentionTest(page);
await page.locator(sel.removeInvalidToggle).click();
await insertTwoMentions(page);
await moveRight(page, 4);
await mentionEditor(page).press('Delete');
await mentionEditor(page).press('Space');
await expect
.poll(async () => await htmlOutput(page).textContent())
.toEqual(`<html><p>${JANE} ${JANE} </p></html>`);
});

test('removeInvalidMentionContent: undo brings the removed mention back', async ({
page,
}) => {
await gotoMentionTest(page);
await page.locator(sel.removeInvalidToggle).click();
await insertMentionAndMoveLeft(page, 3);
await page.waitForTimeout(700);
await mentionEditor(page).press('x');
await expect
.poll(async () => await htmlOutput(page).textContent())
.toEqual('<html><p> </p></html>');
await mentionEditor(page).press('ControlOrMeta+z');
await expect
.poll(async () => await htmlOutput(page).textContent())
.toEqual(`<html><p>${JANE} </p></html>`);
});

test('removeInvalidMentionContent: undo and redo restore touching equal mentions', async ({
page,
}) => {
const errors: string[] = [];
page.on('pageerror', (error) => errors.push(error.message));
await gotoMentionTest(page);
await page.locator(sel.removeInvalidToggle).click();
await insertTwoMentions(page);
await moveRight(page, 4);
await mentionEditor(page).press('Delete');
const touching = `<html><p>${JANE.replace('>Jane<', '>JaneJane<')} </p></html>`;
await expect
.poll(async () => await htmlOutput(page).textContent())
.toEqual(touching);
// longer than the history's grouping delay
await page.waitForTimeout(700);
await moveRight(page, 2);
await mentionEditor(page).press('Backspace');
const edited = `<html><p>${JANE} </p></html>`;
await expect
.poll(async () => await htmlOutput(page).textContent())
.toEqual(edited);
for (const [key, html] of [
['ControlOrMeta+z', touching],
['ControlOrMeta+Shift+z', edited],
] as const) {
await mentionEditor(page).press(key);
await expect
.poll(async () => await htmlOutput(page).textContent())
.toEqual(html);
}
expect(errors).toEqual([]);
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import android.text.Editable
import android.text.InputType
import android.text.Spannable
import android.text.SpannableString
import android.text.SpannableStringBuilder
import android.util.AttributeSet
import android.util.Log
import android.util.Patterns
Expand Down Expand Up @@ -477,9 +478,17 @@ class EnrichedTextInputView :
item.htmlText != null -> {
val parsed = parseText(item.htmlText)
// Not parsed (e.g. external HTML with the normalizer off): fall back to the plain text item
(parsed as? Spannable)
?: item.text?.let { SpannableString(it.toString()) }
?: return
val pasted =
(parsed as? Spannable)
?: item.text?.let { SpannableString(it.toString()) }
?: return
if (parametrizedStyles?.removeInvalidMentionContent == true) {
// the watcher below keeps text that replaced a mention, so pasted
// mismatched mentions are removed before they are merged in
SpannableStringBuilder(pasted).also { parametrizedStyles?.removeInvalidMentions(it) }
} else {
pasted
}
}

item.text != null -> {
Expand Down Expand Up @@ -544,7 +553,12 @@ class EnrichedTextInputView :
if (value == null) return

runAsATransaction {
val newText = if (shouldParseHtml) parseText(value) else value
var newText = if (shouldParseHtml) parseText(value) else value
// the text watcher doesn't run during the transaction, so mentions whose
// text doesn't match them are removed before the text is set
if (shouldParseHtml && parametrizedStyles?.removeInvalidMentionContent == true) {
newText = SpannableStringBuilder(newText).also { parametrizedStyles?.removeInvalidMentions(it) }
}
setText(newText)
applyLineSpacing()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,14 @@ class EnrichedTextInputViewManager :
view?.parametrizedStyles?.mentionIndicators = indicatorsArray
}

@ReactProp(name = "removeInvalidMentionContent", defaultBoolean = false)
override fun setRemoveInvalidMentionContent(
view: EnrichedTextInputView?,
removeInvalidMentionContent: Boolean,
) {
view?.parametrizedStyles?.removeInvalidMentionContent = removeInvalidMentionContent
}

@ReactProp(name = "htmlStyle")
override fun setHtmlStyle(
view: EnrichedTextInputView?,
Expand Down
Loading