From ad5bae6aecb466d0fa4aae9f97a1f3aafd99bdfb Mon Sep 17 00:00:00 2001 From: iseulde Date: Tue, 7 Aug 2018 19:27:33 +0200 Subject: [PATCH] Fix e2e tests --- packages/blocks/src/api/index.js | 2 +- .../blocks/src/api/rich-text-structure.js | 25 +++++++++++++++++++ .../editor/src/components/rich-text/index.js | 6 ++--- .../__snapshots__/adding-blocks.test.js.snap | 2 +- test/e2e/specs/adding-inline-tokens.test.js | 2 +- test/e2e/specs/splitting-merging.test.js | 2 +- 6 files changed, 32 insertions(+), 7 deletions(-) diff --git a/packages/blocks/src/api/index.js b/packages/blocks/src/api/index.js index 0f8ac42ce9098d..95bb39f7f5b1d7 100644 --- a/packages/blocks/src/api/index.js +++ b/packages/blocks/src/api/index.js @@ -13,7 +13,7 @@ export { getBlockAttributes, parseWithAttributeSchema, } from './parser'; -export { default as rawHandler, getPhrasingContentSchema, htmlToBlocks } from './raw-handling'; +export { default as rawHandler, getPhrasingContentSchema } from './raw-handling'; export { default as serialize, getBlockContent, diff --git a/packages/blocks/src/api/rich-text-structure.js b/packages/blocks/src/api/rich-text-structure.js index b4ddfa522ba9aa..ab67d9a6e835aa 100644 --- a/packages/blocks/src/api/rich-text-structure.js +++ b/packages/blocks/src/api/rich-text-structure.js @@ -261,6 +261,15 @@ export function apply( value, current, multiline ) { startContainer.insertData( 0, '\uFEFF' ); range.setStart( startContainer, 1 ); range.setEnd( endContainer, 1 ); + } else if ( + collapsed && + startOffset === 0 && + startContainer === TEXT_NODE && + startContainer.nodeValue.length === 0 + ) { + startContainer.insertData( 0, '\uFEFF' ); + range.setStart( startContainer, 1 ); + range.setEnd( endContainer, 1 ); } else { range.setStart( startContainer, startOffset ); range.setEnd( endContainer, endOffset ); @@ -418,6 +427,22 @@ export function toDOM( { value, selection = {} }, multiline, _tag ) { }, body ); } + if ( start === last || end === last ) { + let pointer = body.lastChild; + + if ( pointer.nodeType !== TEXT_NODE ) { + pointer = pointer.parentNode.appendChild( doc.createTextNode( '' ) ); + } + + if ( start === last ) { + startPath = createPathToNode( pointer, body, [ 0 ] ); + } + + if ( end === last ) { + endPath = createPathToNode( pointer, body, [ 0 ] ); + } + } + return { body, selection: { startPath, endPath }, diff --git a/packages/editor/src/components/rich-text/index.js b/packages/editor/src/components/rich-text/index.js index 00e28c8b261026..05e18476a3d30e 100644 --- a/packages/editor/src/components/rich-text/index.js +++ b/packages/editor/src/components/rich-text/index.js @@ -68,7 +68,7 @@ const settings = { }; export class RichText extends Component { - constructor() { + constructor( { value } ) { super( ...arguments ); this.onInit = this.onInit.bind( this ); @@ -95,6 +95,7 @@ export class RichText extends Component { this.containerRef = createRef(); this.patterns = patterns.call( this ); + this.savedContent = value; this.state = { selection: {}, @@ -601,8 +602,7 @@ export class RichText extends Component { event.preventDefault(); if ( event.shiftKey || ! this.props.onSplit ) { - const record = richTextStructure.splice( this.getRecord(), undefined, 0, '\n' ); - this.onChange( record ); + this.editor.execCommand( 'InsertLineBreak', false, event ); } else { this.splitContent(); } diff --git a/test/e2e/specs/__snapshots__/adding-blocks.test.js.snap b/test/e2e/specs/__snapshots__/adding-blocks.test.js.snap index eaf8254a78cc85..30b83349a7030a 100644 --- a/test/e2e/specs/__snapshots__/adding-blocks.test.js.snap +++ b/test/e2e/specs/__snapshots__/adding-blocks.test.js.snap @@ -42,7 +42,7 @@ exports[`adding blocks Should insert content using the placeholder and the regul -
Pre text

Foo
+
Pre text

Foo
diff --git a/test/e2e/specs/adding-inline-tokens.test.js b/test/e2e/specs/adding-inline-tokens.test.js index 3d0457ef80d633..9ec49e5a3df47c 100644 --- a/test/e2e/specs/adding-inline-tokens.test.js +++ b/test/e2e/specs/adding-inline-tokens.test.js @@ -41,7 +41,7 @@ describe( 'adding inline tokens', () => { await page.click( '.media-modal button.media-button-select' ); // Check the content. - const regex = new RegExp( '\\s*

a\\u00A0<\\/p>\\s*' ); + const regex = new RegExp( '\\s*

<\\/p>\\s*' ); expect( await getEditedPostContent() ).toMatch( regex ); } ); } ); diff --git a/test/e2e/specs/splitting-merging.test.js b/test/e2e/specs/splitting-merging.test.js index 25a408988a17e2..2383f1f759a4f1 100644 --- a/test/e2e/specs/splitting-merging.test.js +++ b/test/e2e/specs/splitting-merging.test.js @@ -65,7 +65,7 @@ describe( 'splitting and merging blocks', () => { await page.keyboard.press( 'Backspace' ); // Replace contents of first paragraph with "Bar". - await pressTimes( 'Backspace', 3 ); + await pressTimes( 'Backspace', 4 ); await page.keyboard.type( 'Bar' ); expect( await getEditedPostContent() ).toMatchSnapshot();