Skip to content

Commit

Permalink
Address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Nov 25, 2019
1 parent d9c1382 commit 29d2f30
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 21 deletions.
27 changes: 9 additions & 18 deletions packages/blocks/src/api/raw-handling/html-formatting-remover.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,24 @@ import { isPhrasingContent } from './phrasing-content';
function getSibling( node, which ) {
const sibling = node[ `${ which }Sibling` ];

if ( ! sibling || ! isPhrasingContent( sibling ) ) {
return;
}

if ( sibling ) {
return sibling;
}

const { parentNode } = node;

if ( ! parentNode ) {
return;
}

if ( ! isPhrasingContent( parentNode ) ) {
return;
}

return getSibling( parentNode, which );
}

function isFormattingSpace( character ) {
return character === ' ' || character === '\t' || character === '\n';
}

/**
* Removes spacing that formats HTML.
*
* @see https://www.w3.org/TR/css-text-3/#white-space-processing
*
* @param {Node} node The node to be processed.
* @return {void}
*/
Expand All @@ -44,7 +38,7 @@ export default function( node ) {
}

// First, replace any sequence of HTML formatting space with a single space.
let newData = node.data.replace( /[ \n\t]+/g, ' ' );
let newData = node.data.replace( /[ \r\n\t]+/g, ' ' );

// Remove the leading space if the text element is at the start of a block,
// is preceded by a line break element, or has a space in the previous
Expand All @@ -62,16 +56,13 @@ export default function( node ) {
}

// Remove the trailing space if the text element is at the end of a block,
// is succeded by a line break element, or has a space in the next element.
// or is succeded by a line break element.
if ( newData[ newData.length - 1 ] === ' ' ) {
const nextSibling = getSibling( node, 'next' );

if (
! nextSibling ||
nextSibling.nodeName === 'BR' ||
// Note that any next node data has not yet been replaced, so we
// have to check for any formatting space.
isFormattingSpace( nextSibling.textContent[ 0 ] )
nextSibling.nodeName === 'BR'
) {
newData = newData.slice( 0, -1 );
}
Expand Down
4 changes: 2 additions & 2 deletions packages/blocks/src/api/raw-handling/paste-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import shortcodeConverter from './shortcode-converter';
import markdownConverter from './markdown-converter';
import iframeRemover from './iframe-remover';
import googleDocsUIDRemover from './google-docs-uid-remover';
import HTMLFormattingRemover from './html-formatting-remover';
import htmlFormattingRemover from './html-formatting-remover';
import { getPhrasingContentSchema } from './phrasing-content';
import {
deepFilterHTML,
Expand Down Expand Up @@ -225,7 +225,7 @@ export function pasteHandler( { HTML = '', plainText = '', mode = 'AUTO', tagNam

piece = deepFilterHTML( piece, filters, blockContentSchema );
piece = removeInvalidHTML( piece, schema );
piece = deepFilterHTML( piece, [ HTMLFormattingRemover ], blockContentSchema );
piece = deepFilterHTML( piece, [ htmlFormattingRemover ], blockContentSchema );
piece = normaliseBlocks( piece );

// Allows us to ask for this information when we get a report.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe( 'HTMLFormattingRemover', () => {
</strong>
</div>
`;
const output = '<div><strong>a</strong><strong> b</strong></div>';
const output = '<div><strong>a</strong> <strong>b</strong></div>';
expect( deepFilterHTML( input, [ filter ] ) ).toEqual( output );
} );

Expand Down

0 comments on commit 29d2f30

Please sign in to comment.