Skip to content

Commit

Permalink
Merge pull request #3411 from WordPress/update/raw-content-inner-html
Browse files Browse the repository at this point in the history
Framework: Rename rawContent references as innerHTML
  • Loading branch information
aduth authored Nov 9, 2017
2 parents 475cd88 + f2ec60a commit 05f8169
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 45 deletions.
42 changes: 21 additions & 21 deletions blocks/api/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ export function isValidSource( source ) {
/**
* Returns the block attributes parsed from raw content.
*
* @param {String} rawContent Raw block content
* @param {Object} schema Block attribute schema
* @return {Object} Block attribute values
* @param {String} innerHTML Raw block content
* @param {Object} schema Block attribute schema
* @return {Object} Block attribute values
*/
export function getSourcedAttributes( rawContent, schema ) {
export function getSourcedAttributes( innerHTML, schema ) {
const sources = mapValues(
// Parse only sources with source defined
pickBy( schema, ( attributeSchema ) => isValidSource( attributeSchema.source ) ),
Expand All @@ -46,7 +46,7 @@ export function getSourcedAttributes( rawContent, schema ) {
( attributeSchema ) => attributeSchema.source
);

return hpqParse( rawContent, sources );
return hpqParse( innerHTML, sources );
}

/**
Expand Down Expand Up @@ -90,15 +90,15 @@ export function asType( value, type ) {
/**
* Returns the block attributes of a registered block node given its type.
*
* @param {?Object} blockType Block type
* @param {string} rawContent Raw block content
* @param {?Object} attributes Known block attributes (from delimiters)
* @return {Object} All block attributes
* @param {?Object} blockType Block type
* @param {string} innerHTML Raw block content
* @param {?Object} attributes Known block attributes (from delimiters)
* @return {Object} All block attributes
*/
export function getBlockAttributes( blockType, rawContent, attributes ) {
export function getBlockAttributes( blockType, innerHTML, attributes ) {
// Retrieve additional attributes sourced from content
const sourcedAttributes = getSourcedAttributes(
rawContent,
innerHTML,
blockType.attributes
);

Expand Down Expand Up @@ -150,7 +150,7 @@ export function getBlockAttributes( blockType, rawContent, attributes ) {

// If the block supports anchor, parse the id
if ( blockType.supportAnchor ) {
blockAttributes.anchor = hpqParse( rawContent, attr( '*', 'id' ) );
blockAttributes.anchor = hpqParse( innerHTML, attr( '*', 'id' ) );
}

// If the block supports a custom className parse it
Expand All @@ -165,11 +165,11 @@ export function getBlockAttributes( blockType, rawContent, attributes ) {
* Creates a block with fallback to the unknown type handler.
*
* @param {?String} name Block type name
* @param {String} rawContent Raw block content
* @param {String} innerHTML Raw block content
* @param {?Object} attributes Attributes obtained from block delimiters
* @return {?Object} An initialized block object (if possible)
*/
export function createBlockWithFallback( name, rawContent, attributes ) {
export function createBlockWithFallback( name, innerHTML, attributes ) {
// Use type from block content, otherwise find unknown handler.
name = name || getUnknownTypeHandlerName();

Expand All @@ -186,7 +186,7 @@ export function createBlockWithFallback( name, rawContent, attributes ) {
// If detected as a block which is not registered, preserve comment
// delimiters in content of unknown type handler.
if ( name ) {
rawContent = getCommentDelimitedContent( name, attributes, rawContent );
innerHTML = getCommentDelimitedContent( name, attributes, innerHTML );
}

name = fallbackBlock;
Expand All @@ -195,23 +195,23 @@ export function createBlockWithFallback( name, rawContent, attributes ) {

// Include in set only if type were determined.
// TODO do we ever expect there to not be an unknown type handler?
if ( blockType && ( rawContent || name !== fallbackBlock ) ) {
if ( blockType && ( innerHTML || name !== fallbackBlock ) ) {
// TODO allow blocks to opt-in to receiving a tree instead of a string.
// Gradually convert all blocks to this new format, then remove the
// string serialization.
const block = createBlock(
name,
getBlockAttributes( blockType, rawContent, attributes )
getBlockAttributes( blockType, innerHTML, attributes )
);

// Validate that the parsed block is valid, meaning that if we were to
// reserialize it given the assumed attributes, the markup matches the
// original value.
block.isValid = isValidBlock( rawContent, blockType, block.attributes );
block.isValid = isValidBlock( innerHTML, blockType, block.attributes );

// Preserve original content for future use in case the block is parsed
// as invalid, or future serialization attempt results in an error
block.originalContent = rawContent;
block.originalContent = innerHTML;

return block;
}
Expand All @@ -225,8 +225,8 @@ export function createBlockWithFallback( name, rawContent, attributes ) {
*/
export function parseWithGrammar( content ) {
return grammarParse( content ).reduce( ( memo, blockNode ) => {
const { blockName, innerHTML: rawContent, attrs } = blockNode;
const block = createBlockWithFallback( blockName, rawContent.trim(), attrs );
const { blockName, innerHTML, attrs } = blockNode;
const block = createBlockWithFallback( blockName, innerHTML.trim(), attrs );
if ( block ) {
memo.push( block );
}
Expand Down
18 changes: 9 additions & 9 deletions blocks/api/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,22 @@ export function getBlockDefaultClassname( blockName ) {
* Given a block type containg a save render implementation and attributes, returns the
* static markup to be saved.
*
* @param {Object} blockType Block type
* @param {Object} attributes Block attributes
* @return {string} Save content
* @param {Object} blockType Block type
* @param {Object} attributes Block attributes
* @return {string} Save content
*/
export function getSaveContent( blockType, attributes ) {
const { save, className = getBlockDefaultClassname( blockType.name ) } = blockType;
let rawContent;
let saveContent;

if ( save.prototype instanceof Component ) {
rawContent = createElement( save, { attributes } );
saveContent = createElement( save, { attributes } );
} else {
rawContent = save( { attributes } );
saveContent = save( { attributes } );

// Special-case function render implementation to allow raw HTML return
if ( 'string' === typeof rawContent ) {
return rawContent;
if ( 'string' === typeof saveContent ) {
return saveContent;
}
}

Expand All @@ -71,7 +71,7 @@ export function getSaveContent( blockType, attributes ) {

return cloneElement( element, extraProps );
};
const contentWithClassname = Children.map( rawContent, addAdvancedAttributes );
const contentWithClassname = Children.map( saveContent, addAdvancedAttributes );

// Otherwise, infer as element
return renderToString( contentWithClassname );
Expand Down
24 changes: 12 additions & 12 deletions blocks/api/test/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,18 @@ describe( 'block parser', () => {
},
};

const rawContent = '<span>Ribs <strong>& Chicken</strong></span>';
const innerHTML = '<span>Ribs <strong>& Chicken</strong></span>';

expect( getSourcedAttributes( rawContent, sources ) ).toEqual( {
expect( getSourcedAttributes( innerHTML, sources ) ).toEqual( {
emphasis: '& Chicken',
} );
} );

it( 'should return an empty object if no sources defined', () => {
const sources = {};
const rawContent = '<span>Ribs <strong>& Chicken</strong></span>';
const innerHTML = '<span>Ribs <strong>& Chicken</strong></span>';

expect( getSourcedAttributes( rawContent, sources ) ).toEqual( {} );
expect( getSourcedAttributes( innerHTML, sources ) ).toEqual( {} );
} );
} );

Expand Down Expand Up @@ -146,10 +146,10 @@ describe( 'block parser', () => {
},
};

const rawContent = '<div data-number="10">Ribs</div>';
const innerHTML = '<div data-number="10">Ribs</div>';
const attrs = { align: 'left', invalid: true };

expect( getBlockAttributes( blockType, rawContent, attrs ) ).toEqual( {
expect( getBlockAttributes( blockType, innerHTML, attrs ) ).toEqual( {
content: 'Ribs',
number: 10,
align: 'left',
Expand All @@ -168,10 +168,10 @@ describe( 'block parser', () => {
supportAnchor: true,
};

const rawContent = '<div id="chicken">Ribs</div>';
const innerHTML = '<div id="chicken">Ribs</div>';
const attrs = {};

expect( getBlockAttributes( blockType, rawContent, attrs ) ).toEqual( {
expect( getBlockAttributes( blockType, innerHTML, attrs ) ).toEqual( {
content: 'Ribs',
anchor: 'chicken',
} );
Expand All @@ -182,10 +182,10 @@ describe( 'block parser', () => {
attributes: {},
};

const rawContent = '<div class="chicken">Ribs</div>';
const innerHTML = '<div class="chicken">Ribs</div>';
const attrs = { className: 'chicken' };

expect( getBlockAttributes( blockType, rawContent, attrs ) ).toEqual( {
expect( getBlockAttributes( blockType, innerHTML, attrs ) ).toEqual( {
className: 'chicken',
} );
} );
Expand All @@ -196,10 +196,10 @@ describe( 'block parser', () => {
className: false,
};

const rawContent = '<div class="chicken">Ribs</div>';
const innerHTML = '<div class="chicken">Ribs</div>';
const attrs = { className: 'chicken' };

expect( getBlockAttributes( blockType, rawContent, attrs ) ).toEqual( {} );
expect( getBlockAttributes( blockType, innerHTML, attrs ) ).toEqual( {} );
} );
} );

Expand Down
6 changes: 3 additions & 3 deletions blocks/api/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,18 +374,18 @@ export function isEquivalentHTML( a, b ) {
*
* Logs to console in development environments when invalid.
*
* @param {String} rawContent Original block content
* @param {String} innerHTML Original block content
* @param {String} blockType Block type
* @param {Object} attributes Parsed block attributes
* @return {Boolean} Whether block is valid
*/
export function isValidBlock( rawContent, blockType, attributes ) {
export function isValidBlock( innerHTML, blockType, attributes ) {
let saveContent;
try {
saveContent = getSaveContent( blockType, attributes );
} catch ( error ) {
return false;
}

return isEquivalentHTML( rawContent, saveContent );
return isEquivalentHTML( innerHTML, saveContent );
}

0 comments on commit 05f8169

Please sign in to comment.