From 0259291cac6048461f6c225e86c6200a11d1a7f5 Mon Sep 17 00:00:00 2001 From: Vineeth Date: Fri, 29 Nov 2024 07:25:42 +0530 Subject: [PATCH] fix: resolve whitespace bug and bump version to 2.30.8 --- docs/CHANGELOG.md | 4 +++ package.json | 2 +- src/components/dom.ts | 2 +- .../tests/modules/BlockEvents/Backspace.cy.ts | 29 +++++++++++++++++-- test/cypress/tests/ui/Placeholders.cy.ts | 17 +++++++++++ 5 files changed, 49 insertions(+), 5 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 12942e0fa..88cc6db85 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +### 2.30.8 + +- `Fix` - Handle whitespace input in empty placeholder elements to prevent caret from moving unexpectedly to the end of the placeholder + ### 2.30.7 - `Fix` - Link insertion in Safari fixed diff --git a/package.json b/package.json index 7c44ed982..d4b9a237a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@editorjs/editorjs", - "version": "2.30.7", + "version": "2.30.8", "description": "Editor.js — open source block-style WYSIWYG editor with JSON output", "main": "dist/editorjs.umd.js", "module": "dist/editorjs.mjs", diff --git a/src/components/dom.ts b/src/components/dom.ts index 83e875413..241041315 100644 --- a/src/components/dom.ts +++ b/src/components/dom.ts @@ -373,7 +373,7 @@ export default class Dom { nodeText = nodeText.replace(new RegExp(ignoreChars, 'g'), ''); } - return nodeText.trim().length === 0; + return nodeText.length === 0; } /** diff --git a/test/cypress/tests/modules/BlockEvents/Backspace.cy.ts b/test/cypress/tests/modules/BlockEvents/Backspace.cy.ts index ad39440e1..d27fb92c0 100644 --- a/test/cypress/tests/modules/BlockEvents/Backspace.cy.ts +++ b/test/cypress/tests/modules/BlockEvents/Backspace.cy.ts @@ -118,6 +118,29 @@ describe('Backspace keydown', function () { .last() .should('have.text', '12'); }); + + + it('   | — should delete visible and invisble whitespaces in the abscence of any non whitespace characters', function () { + createEditorWithTextBlocks([ + '1', + '   ', + ]); + + cy.get('[data-cy=editorjs]') + .find('.ce-paragraph') + .last() + .click() + .type('{downArrow}') + .type('{backspace}') + .type('{backspace}') + .type('{backspace}') + .type('{backspace}'); + + cy.get('[data-cy=editorjs]') + .find('div.ce-block') + .last() + .should('have.text', '1'); + }); }); it('should just delete chars (native behaviour) when some fragment is selected', function () { @@ -184,7 +207,7 @@ describe('Backspace keydown', function () { * Saving logic is not necessary for this test */ // eslint-disable-next-line @typescript-eslint/no-empty-function - public save(): void {} + public save(): void { } } cy.createEditor({ @@ -545,7 +568,7 @@ describe('Backspace keydown', function () { * Saving logic is not necessary for this test */ // eslint-disable-next-line @typescript-eslint/no-empty-function - public save(): void {} + public save(): void { } } cy.createEditor({ @@ -678,7 +701,7 @@ describe('Backspace keydown', function () { describe('at the start of the first Block', function () { it('should do nothing if Block is not empty', function () { - createEditorWithTextBlocks([ 'The only block. Not empty' ]); + createEditorWithTextBlocks(['The only block. Not empty']); cy.get('[data-cy=editorjs]') .find('.ce-paragraph') diff --git a/test/cypress/tests/ui/Placeholders.cy.ts b/test/cypress/tests/ui/Placeholders.cy.ts index 79f19e699..d4a7ce968 100644 --- a/test/cypress/tests/ui/Placeholders.cy.ts +++ b/test/cypress/tests/ui/Placeholders.cy.ts @@ -77,4 +77,21 @@ describe('Placeholders', function () { .getPseudoElementContent('::before') .should('eq', 'none'); }); + + it('should be hidden when user adds trailing whitespace characters', function () { + cy.createEditor({ + placeholder: PLACEHOLDER_TEXT, + }); + + cy.get('[data-cy=editorjs]') + .find('.ce-paragraph') + .as('firstBlock') + .getPseudoElementContent('::before') + .should('eq', PLACEHOLDER_TEXT); + + cy.get('@firstBlock') + .type(' ') + .getPseudoElementContent('::before') + .should('eq', 'none'); + }); });