Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Parser: improve HTML attributes parsing
  • Loading branch information
youknowriad committed May 4, 2017
commit ef3e4e13e59d42841bbeeb6011d02c3cf6b90fe3
8 changes: 4 additions & 4 deletions blocks/api/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export function parseWithTinyMCE( content ) {
// In : <!-- wp:core/embed url:youtube.com/xxx& -->
// Out : <wp-block slug="core/embed" attributes="url:youtube.com/xxx&amp;">
content = content.replace(
/<!--\s*(\/?)wp:([a-z0-9/-]+)((?:\s+[a-z0-9_-]+="[^"]*")*)\s*-->/g,
/<!--\s*(\/?)wp:([a-z0-9/-]+)((?:\s+[a-z0-9_-]+(="[^"]*")?)*)\s*-->/g,
function( match, closingSlash, slug, attributes ) {
if ( closingSlash ) {
return '</wp-block>';
Expand Down Expand Up @@ -166,13 +166,13 @@ export function parseWithTinyMCE( content ) {
}, {} );

// Retrieve the block attributes from the original delimiters
const attributesMatcher = /([a-z0-9_-]+)="([^"]*)"/g;
const attributesMatcher = /([a-z0-9_-]+)(="([^"]*)")?/g;
const blockAttributes = {};
let match;
do {
match = attributesMatcher.exec( unescape( nodeAttributes.attributes || '' ) );
if ( match ) {
blockAttributes[ match[ 1 ] ] = match[ 2 ];
blockAttributes[ match[ 1 ] ] = !! match[ 2 ] ? match[ 3 ] : true;
}
} while ( !! match );

Expand Down Expand Up @@ -221,4 +221,4 @@ export function parseWithGrammar( content ) {
}, [] );
}

export default parseWithGrammar;
export default parseWithTinyMCE;
43 changes: 26 additions & 17 deletions blocks/api/post.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ WP_Block_Html
}

WP_Block_Start
= "<!--" __ "wp:" blockType:WP_Block_Type attrs:WP_Block_Attribute_List _? "-->"
= "<!--" __ "wp:" blockType:WP_Block_Type attrs:HTML_Attribute_List _? "-->"
{ return {
blockType: blockType,
attrs: attrs
Expand All @@ -41,22 +41,34 @@ WP_Block_End
WP_Block_Type
= $(ASCII_Letter WP_Block_Type_Char*)

WP_Block_Attribute_List
= as:(_+ attr:WP_Block_Attribute { return attr })*
{ return as.reduce( function( attrs, pair ) {
attrs[ pair.name ] = pair.value;
return attrs;
}, {} ) }
HTML_Attribute_List
= as:(_+ a:HTML_Attribute_Item { return a })*
{ return as.reduce( ( attrs, [ name, value ] ) => Object.assign(
attrs,
{ [ name ]: value }
), {} ) }

WP_Block_Attribute
= name:WP_Block_Attribute_Name '="' value:WP_Block_Attribute_Value '"'
{ return { name: name, value: value }; }
HTML_Attribute_Item
= HTML_Attribute_Quoted
/ HTML_Attribute_Unquoted
/ HTML_Attribute_Empty

WP_Block_Attribute_Name
= $(ASCII_Letter ASCII_AlphaNumeric*)
HTML_Attribute_Empty
= name:HTML_Attribute_Name
{ return [ name, true ] }

WP_Block_Attribute_Value
= $(ASCII_Letter WP_Block_Attribute_Value_Char*)
HTML_Attribute_Unquoted
= name:HTML_Attribute_Name _* "=" _* value:$([a-zA-Z0-9]+)
{ return [ name, value ] }

HTML_Attribute_Quoted
= name:HTML_Attribute_Name _* "=" _* '"' value:$((!'"' .)*) '"'
{ return [ name, value ] }
/ name:HTML_Attribute_Name _* "=" _* "'" value:$((!"'" .)*) "'"
{ return [ name, value ] }

HTML_Attribute_Name
= $([a-zA-Z0-9:.]+)

WP_Block_Type_Char
= ASCII_AlphaNumeric
Expand All @@ -67,9 +79,6 @@ ASCII_AlphaNumeric
/ ASCII_Digit
/ Special_Chars

WP_Block_Attribute_Value_Char
= [^"]

ASCII_Letter
= [a-zA-Z]

Expand Down