Skip to content

Commit

Permalink
Move to separate rawHandling transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
mcsf committed Feb 19, 2018
1 parent cad761f commit a1c273f
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 45 deletions.
45 changes: 0 additions & 45 deletions blocks/api/raw-handling/comment-remover.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,5 @@ export default function( node ) {
return;
}

if ( node.nodeValue.indexOf( 'more' ) === 0 ) {
// Grab any custom text in the comment
const customText = node.nodeValue.slice( 4 ).trim();

// When a <!--more--> comment is found, we need to look for any
// <!--noteaser--> sibling, but it may not be a direct sibling
// (whitespace typically lies in between)
let sibling = node;
let noTeaser = false;
while ( ( sibling = sibling.nextSibling ) ) {
if (
sibling.nodeType === COMMENT_NODE &&
sibling.nodeValue === 'noteaser'
) {
noTeaser = true;
sibling.parentNode.removeChild( sibling );
break;
}
}

// Conjure up a custom More element
const more = createMore( customText, noTeaser );

// Find the first ancestor to which the More element can be appended;
// appending to the closer P parents fails
let parent = node.parentNode;
while ( parent.nodeName.toLowerCase() === 'p' && parent.parentNode ) {
parent = parent.parentNode;
}
parent.appendChild( more );
}

node.parentNode.removeChild( node );
}

function createMore( customText, noTeaser ) {
const node = document.createElement( 'wp-block' );
node.dataset.block = 'core/more';
if ( customText ) {
node.dataset.customText = customText;
}
if ( noTeaser ) {
// "Boolean" data attribute
node.dataset.noTeaser = '';
}
return node;
}
3 changes: 3 additions & 0 deletions blocks/api/raw-handling/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { getBlockTypes, getUnknownTypeHandlerName } from '../registration';
import { getBlockAttributes, parseWithGrammar } from '../parser';
import normaliseBlocks from './normalise-blocks';
import stripAttributes from './strip-attributes';
import specialCommentConverter from './special-comment-converter';
import commentRemover from './comment-remover';
import createUnwrapper from './create-unwrapper';
import isInlineContent from './is-inline-content';
Expand Down Expand Up @@ -96,6 +97,7 @@ export default function rawHandler( { HTML, plainText = '', mode = 'AUTO', tagNa
// Add semantic formatting before attributes are stripped.
formattingTransformer,
stripAttributes,
specialCommentConverter,
commentRemover,
createUnwrapper( ( node ) => ! isInline( node, tagName ) ),
] );
Expand Down Expand Up @@ -124,6 +126,7 @@ export default function rawHandler( { HTML, plainText = '', mode = 'AUTO', tagNa
// Add semantic formatting before attributes are stripped.
formattingTransformer,
stripAttributes,
specialCommentConverter,
commentRemover,
! canUserUseUnfilteredHTML && createUnwrapper( ( element ) => element.nodeName === 'IFRAME' ),
embeddedContentReducer,
Expand Down
74 changes: 74 additions & 0 deletions blocks/api/raw-handling/special-comment-converter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* Browser dependencies
*/
const { COMMENT_NODE } = window.Node;

/**
* Looks for `<!--more-->` comments, as well as the `<!--more Some text-->`
* variant and its `<!--noteaser-->` companion, and replaces them with a custom
* element representing a future block.
*
* The custom element is a way to bypass the rest of the `raw-handling`
* transforms, which would eliminate other kinds of node with which to carry
* `<!--more-->`'s data: nodes with `data` attributes, empty paragraphs, etc.
*
* The custom element is then expected to be recognized by any registered
* block's `raw` transform.
*
* @param {Node} node The node to be processed.
* @return {void}
*/
export default function( node ) {
if (
node.nodeType !== COMMENT_NODE ||
node.nodeValue.indexOf( 'more' ) !== 0
) {
// We don't specificially look for `noteaser`, meaning that if one is
// found on its own (and not adjacent to `more`), it will be lost.
return;
}

// Grab any custom text in the comment
const customText = node.nodeValue.slice( 4 ).trim();

// When a `<!--more-->` comment is found, we need to look for any
// `<!--noteaser-->` sibling, but it may not be a direct sibling
// (whitespace typically lies in between)
let sibling = node;
let noTeaser = false;
while ( ( sibling = sibling.nextSibling ) ) {
if (
sibling.nodeType === COMMENT_NODE &&
sibling.nodeValue === 'noteaser'
) {
noTeaser = true;
sibling.parentNode.removeChild( sibling );
break;
}
}

// Conjure up a custom More element
const more = createMore( customText, noTeaser );

// Find the first ancestor to which the More element can be appended;
// appending to the closer P parents fails
let parent = node.parentNode;
while ( parent.nodeName.toLowerCase() === 'p' && parent.parentNode ) {
parent = parent.parentNode;
}
parent.appendChild( more );
node.parentNode.removeChild( node );
}

function createMore( customText, noTeaser ) {
const node = document.createElement( 'wp-block' );
node.dataset.block = 'core/more';
if ( customText ) {
node.dataset.customText = customText;
}
if ( noTeaser ) {
// "Boolean" data attribute
node.dataset.noTeaser = '';
}
return node;
}

0 comments on commit a1c273f

Please sign in to comment.