Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add paste schema (fix various issues, simplify) #5966

Merged
merged 2 commits into from
May 2, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion blocks/api/index.js
Original file line number Diff line number Diff line change
@@ -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,
9 changes: 0 additions & 9 deletions blocks/api/raw-handling/blockquote-normaliser.js
Original file line number Diff line number Diff line change
@@ -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;
}
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
@@ -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;
}
Loading