Skip to content

Commit

Permalink
Add paste schema
Browse files Browse the repository at this point in the history
Comment, test, clean up

Do not allow figures without embedded content

Adjust getContentSchema signature

Destructure where possible

Fix typo

Add test for 315819d

Move schemas to blocks

Simplify

Restore iframe filter

Add Markdown integration test

Address feedback

Separate Markdown converter

Remove unneeded nodeType checks
  • Loading branch information
ellatrix committed Apr 25, 2018
1 parent 5a66935 commit e93177e
Show file tree
Hide file tree
Showing 57 changed files with 1,134 additions and 891 deletions.
2 changes: 1 addition & 1 deletion blocks/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export {
getBlockAttributes,
parseWithAttributeSchema,
} from './parser';
export { default as rawHandler } from './raw-handling';
export { default as rawHandler, getPhrasingContentSchema } from './raw-handling';
export {
default as serialize,
getBlockContent,
Expand Down
9 changes: 0 additions & 9 deletions blocks/api/raw-handling/blockquote-normaliser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,7 @@
*/
import normaliseBlocks from './normalise-blocks';

/**
* Browser dependencies
*/
const { ELEMENT_NODE } = window.Node;

export default function( node ) {
if ( node.nodeType !== ELEMENT_NODE ) {
return;
}

if ( node.nodeName !== 'BLOCKQUOTE' ) {
return;
}
Expand Down
12 changes: 0 additions & 12 deletions blocks/api/raw-handling/comment-remover.js

This file was deleted.

34 changes: 0 additions & 34 deletions blocks/api/raw-handling/create-unwrapper.js

This file was deleted.

47 changes: 0 additions & 47 deletions blocks/api/raw-handling/embedded-content-reducer.js

This file was deleted.

88 changes: 88 additions & 0 deletions blocks/api/raw-handling/figure-content-reducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* External dependencies
*/
import { has } from 'lodash';

/**
* Internal dependencies
*/
import { isPhrasingContent } from './utils';

/**
* Whether or not the given node is figure content.
*
* @param {Node} node The node to check.
* @param {Object} schema The schema to use.
*
* @return {boolean} True if figure content, false if not.
*/
function isFigureContent( node, schema ) {
const tag = node.nodeName.toLowerCase();

// We are looking for tags that can be a child of the figure tag, excluding
// `figcaption` and any phrasing content.
if ( tag === 'figcaption' || isPhrasingContent( node ) ) {
return false;
}

return has( schema, [ 'figure', 'children', tag ] );
}

/**
* Whether or not the given node can have an anchor.
*
* @param {Node} node The node to check.
* @param {Object} schema The schema to use.
*
* @return {boolean} True if it can, false if not.
*/
function canHaveAnchor( node, schema ) {
const tag = node.nodeName.toLowerCase();

return has( schema, [ 'figure', 'children', 'a', 'children', tag ] );
}

/**
* This filter takes figure content out of paragraphs, wraps it in a figure
* element, and moves any anchors with it if needed.
*
* @param {Node} node The node to filter.
* @param {Document} doc The document of the node.
* @param {Object} schema The schema to use.
*
* @return {void}
*/
export default function( node, doc, schema ) {
if ( ! isFigureContent( node, schema ) ) {
return;
}

let nodeToInsert = node;
const parentNode = node.parentNode;

// If the figure content can have an anchor and its parent is an anchor with
// only the figure content, take the anchor out instead of just the content.
if (
canHaveAnchor( node, schema ) &&
parentNode.nodeName === 'A' &&
parentNode.childNodes.length === 1
) {
nodeToInsert = node.parentNode;
}

let wrapper = nodeToInsert;

while ( wrapper && wrapper.nodeName !== 'P' ) {
wrapper = wrapper.parentElement;
}

const figure = doc.createElement( 'figure' );

if ( wrapper ) {
wrapper.parentNode.insertBefore( figure, wrapper );
} else {
nodeToInsert.parentNode.insertBefore( figure, nodeToInsert );
}

figure.appendChild( nodeToInsert );
}
39 changes: 0 additions & 39 deletions blocks/api/raw-handling/formatting-transformer.js

This file was deleted.

17 changes: 17 additions & 0 deletions blocks/api/raw-handling/iframe-remover.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* WordPress dependencies
*/
import { remove } from '@wordpress/utils';

/**
* Removes iframes.
*
* @param {Node} node The node to check.
*
* @return {void}
*/
export default function( node ) {
if ( node.nodeName === 'IFRAME' ) {
remove( node );
}
}
5 changes: 0 additions & 5 deletions blocks/api/raw-handling/image-corrector.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,8 @@ import { createBlobURL } from '@wordpress/utils';
* Browser dependencies
*/
const { atob, Blob } = window;
const { ELEMENT_NODE } = window.Node;

export default function( node ) {
if ( node.nodeType !== ELEMENT_NODE ) {
return;
}

if ( node.nodeName !== 'IMG' ) {
return;
}
Expand Down
Loading

0 comments on commit e93177e

Please sign in to comment.