Skip to content

Commit

Permalink
TINY-9794: pressing enter in an empty line with only a format in a bl…
Browse files Browse the repository at this point in the history
…ockquote should leave the blockquote (#8722)

* TINY-9794: first implementation

* add changelod entry

* TINY-9794: refactor

* TINY-9794: add assertion

* TINY-9794: refactor

* TINY-9794: fix changelog entry

* TINY-9794: rename
  • Loading branch information
lorenzo-pomili committed May 16, 2023
1 parent 27227cc commit b4c1a77
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 5 deletions.
1 change: 1 addition & 0 deletions modules/tinymce/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Popups were not constrained within the scrollable container when in a shadow root. #TINY-9743
- Pressing arrow keys inside RTL elements would move the caret in an incorrect direction when moving over elements with the `contenteditable` attribute set to `false`. #TINY-9565
- Inserting table consecutively without focus in the editor would result in the table being inserted at the wrong position. #TINY-3909
- In some cases, the exiting a `blockquote` element could fail when the cursor was positioned at the end of the `blockquote`. #TINY-9794

## 6.4.2 - 2023-04-26

Expand Down
8 changes: 4 additions & 4 deletions modules/tinymce/src/core/main/ts/api/dom/DOMUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as NodeType from '../../dom/NodeType';
import * as Position from '../../dom/Position';
import * as StyleSheetLoaderRegistry from '../../dom/StyleSheetLoaderRegistry';
import * as TrimNode from '../../dom/TrimNode';
import { isWhitespaceText } from '../../text/Whitespace';
import { isWhitespaceText, isZwsp } from '../../text/Whitespace';
import { GeomRect } from '../geom/Rect';
import Entities from '../html/Entities';
import Schema from '../html/Schema';
Expand Down Expand Up @@ -181,7 +181,7 @@ interface DOMUtils {
findCommonAncestor: (a: Node, b: Node) => Node | null;
run <R, T extends Node>(this: DOMUtils, elm: T | T[], func: (node: T) => R, scope?: any): typeof elm extends Array<any> ? R[] : R;
run <R, T extends Node>(this: DOMUtils, elm: RunArguments<T>, func: (node: T) => R, scope?: any): RunResult<typeof elm, R>;
isEmpty: (node: Node, elements?: Record<string, any>) => boolean;
isEmpty: (node: Node, elements?: Record<string, any>, options?: ({ includeZwsp?: boolean })) => boolean;
createRng: () => Range;
nodeIndex: (node: Node, normalized?: boolean) => number;
split: {
Expand Down Expand Up @@ -937,7 +937,7 @@ const DOMUtils = (doc: Document, settings: Partial<DOMUtilsSettings> = {}): DOMU
return false;
};

const isEmpty = (node: Node, elements?: Record<string, any>) => {
const isEmpty = (node: Node, elements?: Record<string, any>, options?: ({ includeZwsp?: boolean })) => {
let brCount = 0;

// Keep elements with data-bookmark attributes, name attributes or are named anchors
Expand Down Expand Up @@ -986,7 +986,7 @@ const DOMUtils = (doc: Document, settings: Partial<DOMUtilsSettings> = {}): DOMU
}

// Keep non whitespace text nodes
if (NodeType.isText(tempNode) && !isWhitespaceText(tempNode.data)) {
if (NodeType.isText(tempNode) && !isWhitespaceText(tempNode.data) && (!options?.includeZwsp || !isZwsp(tempNode.data))) {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion modules/tinymce/src/core/main/ts/newline/InsertBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ const insert = (editor: Editor, evt?: EditorEvent<KeyboardEvent>): void => {
}

// Split the current container block element if enter is pressed inside an empty inner block element
if (shouldEndContainer(editor, containerBlock) && canSplitBlock(dom, containerBlock) && dom.isEmpty(parentBlock)) {
if (shouldEndContainer(editor, containerBlock) && canSplitBlock(dom, containerBlock) && dom.isEmpty(parentBlock, undefined, { includeZwsp: true })) {
// Split container block for example a BLOCKQUOTE at the current blockParent location for example a P
block = dom.split(containerBlock, parentBlock) as Element;
} else {
Expand Down
11 changes: 11 additions & 0 deletions modules/tinymce/src/core/main/ts/text/Whitespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ import { Arr, Strings, Unicode } from '@ephox/katamari';
const whiteSpaceRegExp = /^[ \t\r\n]*$/;

const isWhitespaceText = (text: string): boolean => whiteSpaceRegExp.test(text);

const isZwsp = (text: string): boolean => {
for (const c of text) {
if (!Unicode.isZwsp(c)) {
return false;
}
}
return true;
};

// Don't compare other unicode spaces here, as we're only concerned about whitespace the browser would collapse
const isCollapsibleWhitespace = (c: string): boolean => ' \f\t\v'.indexOf(c) !== -1;
const isNewLineChar = (c: string): boolean => c === '\n' || c === '\r';
Expand Down Expand Up @@ -37,6 +47,7 @@ const normalize = (text: string, tabSpaces: number = 4, isStartOfContent: boolea

export {
isWhitespaceText,
isZwsp,
isNewline,
normalize
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { TinyAssertions, TinyHooks, TinySelections, TinyState } from '@ephox/wra

import Editor from 'tinymce/core/api/Editor';
import { EditorEvent } from 'tinymce/core/api/util/EventDispatcher';
import * as CaretFormat from 'tinymce/core/fmt/CaretFormat';
import * as InsertNewLine from 'tinymce/core/newline/InsertNewLine';

describe('browser.tinymce.core.newline.InsertNewLineTest', () => {
Expand Down Expand Up @@ -755,4 +756,15 @@ describe('browser.tinymce.core.newline.InsertNewLineTest', () => {
});
});
});

it('TINY-9794: Press Enter in a blockquote and then add format and then press Enter again should exit from the blockquote', () => {
const editor = hook.editor();
editor.setContent('<blockquote><p>A</p></blockquote>');
TinySelections.setCursor(editor, [ 0, 0 ], 1);
insertNewline(editor, { });
CaretFormat.applyCaretFormat(editor, 'bold');
insertNewline(editor, { });
TinyAssertions.assertContent(editor, '<blockquote><p>A</p></blockquote><p>&nbsp;</p>');
TinyAssertions.assertCursor(editor, [ 1, 0, 0, 0 ], 0);
});
});

0 comments on commit b4c1a77

Please sign in to comment.