Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Apr 4, 2018
1 parent 3d143d0 commit 07664af
Show file tree
Hide file tree
Showing 27 changed files with 416 additions and 580 deletions.
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.

9 changes: 8 additions & 1 deletion blocks/api/raw-handling/embedded-content-reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export default function( node ) {
}

let nodeToInsert = node;

// if the embedded is an image and its parent is an anchor with just the image
// take the anchor out instead of just the image
if (
Expand All @@ -41,7 +42,13 @@ export default function( node ) {
wrapper = wrapper.parentElement;
}

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

if ( wrapper ) {
wrapper.parentNode.insertBefore( nodeToInsert, 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.

38 changes: 6 additions & 32 deletions blocks/api/raw-handling/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* External dependencies
*/
import { compact } from 'lodash';
import showdown from 'showdown';

/**
Expand All @@ -11,20 +10,15 @@ import { createBlock, getBlockTransforms, findTransform } from '../factory';
import { getBlockType, 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';
import formattingTransformer from './formatting-transformer';
import phrasingContentReducer from './phrasing-content-reducer';
import msListConverter from './ms-list-converter';
import listReducer from './list-reducer';
import imageCorrector from './image-corrector';
import blockquoteNormaliser from './blockquote-normaliser';
import tableNormaliser from './table-normaliser';
import inlineContentConverter from './inline-content-converter';
import embeddedContentReducer from './embedded-content-reducer';
import { deepFilterHTML, isInvalidInline, isNotWhitelisted, isPlain, isInline } from './utils';
import { deepFilterHTML, isPlain, getContentSchema, getPhrasingContentSchema, removeInvalidHTML } from './utils';
import shortcodeConverter from './shortcode-converter';
import slackMarkdownVariantCorrector from './slack-markdown-variant-corrector';

Expand Down Expand Up @@ -94,14 +88,8 @@ export default function rawHandler( { HTML, plainText = '', mode = 'AUTO', tagNa

// Return filtered HTML if condition is true
if ( mode === 'INLINE' || isAutoModeInline ) {
HTML = deepFilterHTML( HTML, [
// Add semantic formatting before attributes are stripped.
formattingTransformer,
stripAttributes,
specialCommentConverter,
commentRemover,
createUnwrapper( ( node ) => ! isInline( node, tagName ) ),
] );
HTML = deepFilterHTML( HTML, [ phrasingContentReducer ] );
HTML = removeInvalidHTML( HTML, getPhrasingContentSchema() );

// Allows us to ask for this information when we get a report.
window.console.log( 'Processed inline HTML:\n\n', HTML );
Expand All @@ -116,31 +104,17 @@ export default function rawHandler( { HTML, plainText = '', mode = 'AUTO', tagNa
return [ ...accu, piece ];
}

// Context dependent filters. Needs to run before we remove nodes.
piece = deepFilterHTML( piece, [
msListConverter,
] );

piece = deepFilterHTML( piece, compact( [
listReducer,
imageCorrector,
// Add semantic formatting before attributes are stripped.
formattingTransformer,
stripAttributes,
phrasingContentReducer,
specialCommentConverter,
commentRemover,
! canUserUseUnfilteredHTML && createUnwrapper( ( element ) => element.nodeName === 'IFRAME' ),
embeddedContentReducer,
createUnwrapper( isNotWhitelisted ),
blockquoteNormaliser,
tableNormaliser,
inlineContentConverter,
] ) );

piece = deepFilterHTML( piece, [
createUnwrapper( isInvalidInline ),
] );

piece = removeInvalidHTML( piece, getContentSchema( { iframe: canUserUseUnfilteredHTML } ) );
piece = normaliseBlocks( piece );

// Allows us to ask for this information when we get a report.
Expand Down
27 changes: 0 additions & 27 deletions blocks/api/raw-handling/inline-content-converter.js

This file was deleted.

26 changes: 22 additions & 4 deletions blocks/api/raw-handling/list-reducer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
/**
* WordPress dependencies
*/
import { unwrap } from '@wordpress/utils';

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

function isList( node ) {
return node.nodeName === 'OL' || node.nodeName === 'UL';
}

function shallowTextContent( element ) {
return [ ...element.childNodes ]
.map( ( { nodeValue = '' } ) => nodeValue )
Expand All @@ -14,9 +23,7 @@ export default function( node ) {
return;
}

const type = node.nodeName;

if ( type !== 'OL' && type !== 'UL' ) {
if ( ! isList( node ) ) {
return;
}

Expand All @@ -28,7 +35,7 @@ export default function( node ) {
// * There is only one list item.
if (
prevElement &&
prevElement.nodeName === type &&
prevElement.nodeName === node.nodeName &&
list.children.length === 1
) {
prevElement.appendChild( list.firstChild );
Expand Down Expand Up @@ -56,4 +63,15 @@ export default function( node ) {
parentList.parentNode.removeChild( parentList );
}
}

// Invalid: OL/UL > OL/UL.
if ( parentElement && isList( parentElement ) ) {
const prevListItem = node.previousElementSibling;

if ( prevListItem ) {
prevListItem.appendChild( node );
} else {
unwrap( node );
}
}
}
47 changes: 47 additions & 0 deletions blocks/api/raw-handling/phrasing-content-reducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* WordPress dependencies
*/
import { unwrap, replaceTag } from '@wordpress/utils';

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

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

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

if ( node.nodeName === 'SPAN' ) {
const fontWeight = node.style.fontWeight;
const fontStyle = node.style.fontStyle;

if ( fontWeight === 'bold' || fontWeight === '700' ) {
node = replaceTag( node, 'strong' );
} else if ( fontStyle === 'italic' ) {
node = replaceTag( node, 'em' );
}
}

if ( node.nodeName === 'B' ) {
node = replaceTag( node, 'strong' );
}

if ( node.nodeName === 'I' ) {
node = replaceTag( node, 'em' );
}

if (
isPhrasingContent( node ) &&
node.hasChildNodes() &&
Array.from( node.childNodes ).some( isBlockContent )
) {
unwrap( node );
}
}
46 changes: 0 additions & 46 deletions blocks/api/raw-handling/strip-attributes.js

This file was deleted.

18 changes: 0 additions & 18 deletions blocks/api/raw-handling/table-normaliser.js

This file was deleted.

Loading

0 comments on commit 07664af

Please sign in to comment.