Skip to content

Commit

Permalink
Merge branch 'master' into fix/3412-role-textbox-editable
Browse files Browse the repository at this point in the history
  • Loading branch information
tg-ephox committed Jan 5, 2018
2 parents 0c94891 + a6948bd commit 809daed
Show file tree
Hide file tree
Showing 295 changed files with 6,431 additions and 4,870 deletions.
14 changes: 7 additions & 7 deletions bin/get-server-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@
define( 'WPINC', 'wp-includes' );
define( 'WP_SETUP_CONFIG', true );
define( 'WP_USE_THEMES', false );
require_once( ABSPATH . WPINC . '/load.php' );
require_once( ABSPATH . WPINC . '/default-constants.php' );
require_once ABSPATH . WPINC . '/load.php';
require_once ABSPATH . WPINC . '/default-constants.php';
wp_fix_server_vars();
wp_initial_constants();
require_once( ABSPATH . WPINC . '/functions.php' );
require_once ABSPATH . WPINC . '/functions.php';
wp_load_translations_early();
wp_set_lang_dir();
require_once( dirname( dirname( __FILE__ ) ) . '/lib/blocks.php' );
require_once( dirname( dirname( __FILE__ ) ) . '/lib/class-wp-block-type-registry.php' );
require_once( dirname( dirname( __FILE__ ) ) . '/lib/class-wp-block-type.php' );
require_once( dirname( dirname( __FILE__ ) ) . '/lib/client-assets.php' );
require_once dirname( dirname( __FILE__ ) ) . '/lib/blocks.php';
require_once dirname( dirname( __FILE__ ) ) . '/lib/class-wp-block-type-registry.php';
require_once dirname( dirname( __FILE__ ) ) . '/lib/class-wp-block-type.php';
require_once dirname( dirname( __FILE__ ) ) . '/lib/client-assets.php';

// Register server-side code for individual blocks.
foreach ( glob( dirname( dirname( __FILE__ ) ) . '/blocks/library/*/index.php' ) as $block_logic ) {
Expand Down
2 changes: 1 addition & 1 deletion blocks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ add_action( 'enqueue_block_editor_assets', 'random_image_enqueue_block_editor_as

icon: 'format-image',

category: 'media',
category: 'common',

attributes: {
category: {
Expand Down
6 changes: 3 additions & 3 deletions blocks/api/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function createBlock( name, blockAttributes = {} ) {
const value = blockAttributes[ key ];
if ( undefined !== value ) {
result[ key ] = value;
} else if ( source.default ) {
} else if ( source.hasOwnProperty( 'default' ) ) {
result[ key ] = source.default;
}

Expand Down Expand Up @@ -222,9 +222,9 @@ export function switchToBlockType( blocks, name ) {
*/
export function createReusableBlock( type, attributes ) {
return {
id: +uniqueId(), // Temorary id replaced when the block is saved server side
id: -uniqueId(), // Temorary id replaced when the block is saved server side
isTemporary: true,
name: __( 'Untitled block' ),
title: __( 'Untitled block' ),
type,
attributes,
};
Expand Down
22 changes: 18 additions & 4 deletions blocks/api/raw-handling/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,30 @@ export default function rawHandler( { HTML, plainText = '', mode = 'AUTO', tagNa
return parseWithGrammar( HTML );
}

// If there is a plain text version, the HTML version has no formatting,
// and there is at least a double line break,
// parse any Markdown inside the plain text.
if ( plainText && isPlain( HTML ) && plainText.indexOf( '\n\n' ) !== -1 ) {
// Parse Markdown (and HTML) if:
// * There is a plain text version.
// * The HTML version has no formatting.
if ( plainText && isPlain( HTML ) ) {
const converter = new showdown.Converter();

converter.setOption( 'noHeaderId', true );
converter.setOption( 'tables', true );

HTML = converter.makeHtml( plainText );

// Switch to inline mode if:
// * The current mode is AUTO.
// * The original plain text had no line breaks.
// * The original plain text was not an HTML paragraph.
// * The converted text is just a paragraph.
if (
mode === 'AUTO' &&
plainText.indexOf( '\n' ) === -1 &&
plainText.indexOf( '<p>' ) !== 0 &&
HTML.indexOf( '<p>' ) === 0
) {
mode = 'INLINE';
}
}

// An array of HTML strings and block objects. The blocks replace matched shortcodes.
Expand Down
56 changes: 30 additions & 26 deletions blocks/api/raw-handling/shortcode-converter.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { find, get, dropRight, last, mapValues, pickBy } from 'lodash';
import { castArray, find, get, dropRight, last, mapValues, pickBy } from 'lodash';

/**
* Internal dependencies
Expand All @@ -25,33 +25,37 @@ export default function( HTML ) {
return acc;
}

const transformTags = castArray( transform.tag );

let match;
let lastIndex = 0;

while ( ( match = shortcode.next( transform.tag, HTML, lastIndex ) ) ) {
lastIndex = match.index + match.content.length;

const attributes = mapValues(
pickBy( transform.attributes, ( schema ) => schema.shortcode ),
// Passing all of `match` as second argument is intentionally
// broad but shouldn't be too relied upon. See
// https://github.com/WordPress/gutenberg/pull/3610#discussion_r152546926
( schema ) => schema.shortcode( match.shortcode.attrs, match ),
);

const block = createBlock(
blockType.name,
getBlockAttributes(
{
...blockType,
attributes: transform.attributes,
},
match.shortcode.content,
attributes,
)
);

acc[ match.index ] = { block, lastIndex };
for ( const transformTag of transformTags ) {
while ( ( match = shortcode.next( transformTag, HTML, lastIndex ) ) ) {
lastIndex = match.index + match.content.length;

const attributes = mapValues(
pickBy( transform.attributes, ( schema ) => schema.shortcode ),
// Passing all of `match` as second argument is intentionally
// broad but shouldn't be too relied upon. See
// https://github.com/WordPress/gutenberg/pull/3610#discussion_r152546926
( schema ) => schema.shortcode( match.shortcode.attrs, match ),
);

const block = createBlock(
blockType.name,
getBlockAttributes(
{
...blockType,
attributes: transform.attributes,
},
match.shortcode.content,
attributes,
)
);

acc[ match.index ] = { block, lastIndex };
}
}

return acc;
Expand All @@ -60,7 +64,7 @@ export default function( HTML ) {
let negativeI = 0;

// Sort the matches and return an array of text pieces and blocks.
return Object.keys( matches ).sort().reduce( ( acc, index ) => {
return Object.keys( matches ).sort( ( a, b ) => a - b ).reduce( ( acc, index ) => {
const match = matches[ index ];

acc = [
Expand Down
21 changes: 19 additions & 2 deletions blocks/api/raw-handling/strip-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { ELEMENT_NODE } = window.Node;
/**
* Internal dependencies
*/
import { isAttributeWhitelisted } from './utils';
import { isAttributeWhitelisted, isClassWhitelisted } from './utils';

export default function( node ) {
if ( node.nodeType !== ELEMENT_NODE ) {
Expand All @@ -20,10 +20,27 @@ export default function( node ) {
const tag = node.nodeName.toLowerCase();

Array.from( node.attributes ).forEach( ( { name } ) => {
if ( isAttributeWhitelisted( tag, name ) ) {
if ( name === 'class' || isAttributeWhitelisted( tag, name ) ) {
return;
}

node.removeAttribute( name );
} );

const oldClasses = node.getAttribute( 'class' );

if ( ! oldClasses ) {
return;
}

const newClasses = oldClasses
.split( ' ' )
.filter( ( name ) => name && isClassWhitelisted( tag, name ) )
.join( ' ' );

if ( newClasses.length ) {
node.setAttribute( 'class', newClasses );
} else {
node.removeAttribute( 'class' );
}
}
32 changes: 27 additions & 5 deletions blocks/api/raw-handling/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { equal, deepEqual } from 'assert';
import rawHandler from '../index';
import { registerBlockType, unregisterBlockType, setUnknownTypeHandlerName } from '../../registration';
import { createBlock } from '../../factory';
import { getBlockContent } from '../../serializer';

describe( 'rawHandler', () => {
it( 'should convert recognised raw content', () => {
Expand Down Expand Up @@ -104,13 +105,34 @@ describe( 'rawHandler', () => {
equal( filtered, '<em>test</em>' );
} );

it( 'should always return blocks', () => {
const blocks = rawHandler( {
HTML: 'test',
mode: 'BLOCKS',
it( 'should parse Markdown', () => {
const filtered = rawHandler( {
HTML: '* one<br>* two<br>* three',
plainText: '* one\n* two\n* three',
mode: 'AUTO',
} ).map( getBlockContent ).join( '' );

equal( filtered, '<ul>\n <li>one</li>\n <li>two</li>\n <li>three</li>\n</ul>' );
} );

it( 'should parse inline Markdown', () => {
const filtered = rawHandler( {
HTML: 'Some **bold** text.',
plainText: 'Some **bold** text.',
mode: 'AUTO',
} );

equal( Array.isArray( blocks ), true );
equal( filtered, 'Some <strong>bold</strong> text.' );
} );

it( 'should parse HTML in plainText', () => {
const filtered = rawHandler( {
HTML: '&lt;p&gt;Some &lt;strong&gt;bold&lt;/strong&gt; text.&lt;/p&gt;',
plainText: '<p>Some <strong>bold</strong> text.</p>',
mode: 'AUTO',
} ).map( getBlockContent ).join( '' );

equal( filtered, '<p>Some <strong>bold</strong> text.</p>' );
} );
} );

Expand Down
4 changes: 4 additions & 0 deletions blocks/api/raw-handling/test/strip-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ describe( 'stripAttributes', () => {
it( 'should keep some attributes', () => {
equal( deepFilterHTML( '<a href="#keep">test</a>', [ stripAttributes ] ), '<a href="#keep">test</a>' );
} );

it( 'should keep some classes', () => {
equal( deepFilterHTML( '<img class="alignright test" src="">', [ stripAttributes ] ), '<img class="alignright" src="">' );
} );
} );
10 changes: 9 additions & 1 deletion blocks/api/raw-handling/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const inlineWrapperWhiteList = {
const whitelist = {
...inlineWhitelist,
...inlineWrapperWhiteList,
img: { attributes: [ 'src', 'alt' ] },
img: { attributes: [ 'src', 'alt' ], classes: [ 'alignleft', 'aligncenter', 'alignright', 'alignnone' ] },
figure: {},
blockquote: {},
hr: {},
Expand Down Expand Up @@ -100,6 +100,14 @@ export function isInline( node, tagName ) {
return !! inlineWhitelist[ nodeName ] || isInlineForTag( nodeName, tagName );
}

export function isClassWhitelisted( tag, name ) {
return (
whitelist[ tag ] &&
whitelist[ tag ].classes &&
whitelist[ tag ].classes.indexOf( name ) !== -1
);
}

export function isInlineWrapper( node ) {
return !! inlineWrapperWhiteList[ node.nodeName.toLowerCase() ];
}
Expand Down
7 changes: 6 additions & 1 deletion blocks/api/test/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ describe( 'block factory', () => {
type: 'boolean',
default: true,
},
includesFalseyDefault: {
type: 'number',
default: 0,
},
},
save: noop,
category: 'common',
Expand All @@ -56,6 +60,7 @@ describe( 'block factory', () => {
expect( block.name ).toEqual( 'core/test-block' );
expect( block.attributes ).toEqual( {
includesDefault: true,
includesFalseyDefault: 0,
align: 'left',
} );
expect( block.isValid ).toBe( true );
Expand Down Expand Up @@ -619,7 +624,7 @@ describe( 'block factory', () => {

expect( createReusableBlock( type, attributes ) ).toMatchObject( {
id: expect.any( Number ),
name: 'Untitled block',
title: 'Untitled block',
type,
attributes,
} );
Expand Down
2 changes: 1 addition & 1 deletion blocks/api/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ export function isEquivalentHTML( actual, expected ) {
}
}

while ( ( expectedToken = getNextNonWhitespaceToken( expectedTokens ) ) ) {
if ( ( expectedToken = getNextNonWhitespaceToken( expectedTokens ) ) ) {
// If any non-whitespace tokens remain in expected token set, this
// indicates inequality
log.warning( 'Expected %o, instead saw end of content.', expectedToken );
Expand Down
27 changes: 21 additions & 6 deletions blocks/block-description/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
/**
* Internal dependencies
*/
import './style.scss';

export default function BlockDescription( { children } ) {
return (
<div className="components-block-description">
{ children }
</div>
);
class BlockDescription extends Component {
constructor() {
super( ...arguments );
// eslint-disable-next-line no-console
console.warn( 'The wp.blocks.BlockDescription component is deprecated. Use the "description" block property instead.' );
}

render() {
const { children } = this.props;
return (
<div className="components-block-description">
{ children }
</div>
);
}
}

export default BlockDescription;
8 changes: 8 additions & 0 deletions blocks/block-description/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,21 @@ import { shallow } from 'enzyme';
*/
import BlockDescription from '../';

/* eslint-disable no-console */
function expectWarning() {
expect( console.warn ).toHaveBeenCalled();
console.warn.mockClear();
}
/* eslint-enable no-console */

describe( 'BlockDescription', () => {
describe( 'basic rendering', () => {
it( 'should render a <p> element with some content', () => {
const blockDescription = shallow( <BlockDescription><p>Hello World</p></BlockDescription> );
expect( blockDescription.hasClass( 'components-block-description' ) ).toBe( true );
expect( blockDescription.type() ).toBe( 'div' );
expect( blockDescription.text() ).toBe( 'Hello World' );
expectWarning();
} );
} );
} );
Loading

0 comments on commit 809daed

Please sign in to comment.