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

Parser: Add new list of HTML fragments to parse output #11334

Merged
merged 8 commits into from
Nov 7, 2018
Merged
Show file tree
Hide file tree
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
260 changes: 205 additions & 55 deletions lib/parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,20 +259,22 @@ private function peg_f1($pre, $bs, $post) { return peg_join_blocks( $pre, $bs, $
private function peg_f2($blockName, $a) { return $a; }
private function peg_f3($blockName, $attrs) {
return array(
'blockName' => $blockName,
'attrs' => isset( $attrs ) ? $attrs : array(),
'innerBlocks' => array(),
'innerHTML' => '',
'blockName' => $blockName,
'attrs' => isset( $attrs ) ? $attrs : array(),
'innerBlocks' => array(),
'innerHTML' => '',
'innerContent' => array(),
);
}
private function peg_f4($s, $children, $e) {
list( $innerHTML, $innerBlocks ) = peg_array_partition( $children, 'is_string' );
list( $innerHTML, $innerBlocks, $innerContent ) = peg_process_inner_content( $children );

return array(
'blockName' => $s['blockName'],
'attrs' => $s['attrs'],
'innerBlocks' => $innerBlocks,
'innerHTML' => implode( '', $innerHTML ),
'innerHTML' => $innerHTML,
'innerContent' => $innerContent,
);
}
private function peg_f5($blockName, $attrs) {
Expand Down Expand Up @@ -711,36 +713,106 @@ private function peg_parseBlock_Balanced() {
$s3 = $this->peg_parseBlock();
if ($s3 === $this->peg_FAILED) {
$s3 = $this->peg_currPos;
$s4 = $this->peg_currPos;
$s4 = array();
$s5 = $this->peg_currPos;
$s6 = $this->peg_currPos;
$this->peg_silentFails++;
$s6 = $this->peg_parseBlock_End();
$s7 = $this->peg_parseBlock();
$this->peg_silentFails--;
if ($s6 === $this->peg_FAILED) {
$s5 = null;
if ($s7 === $this->peg_FAILED) {
$s6 = null;
} else {
$this->peg_currPos = $s6;
$s6 = $this->peg_FAILED;
}
if ($s6 !== $this->peg_FAILED) {
$s7 = $this->peg_currPos;
$this->peg_silentFails++;
$s8 = $this->peg_parseBlock_End();
$this->peg_silentFails--;
if ($s8 === $this->peg_FAILED) {
$s7 = null;
} else {
$this->peg_currPos = $s7;
$s7 = $this->peg_FAILED;
}
if ($s7 !== $this->peg_FAILED) {
if ($this->input_length > $this->peg_currPos) {
$s8 = $this->input_substr($this->peg_currPos, 1);
$this->peg_currPos++;
} else {
$s8 = $this->peg_FAILED;
if ($this->peg_silentFails === 0) {
$this->peg_fail($this->peg_c0);
}
}
if ($s8 !== $this->peg_FAILED) {
$s6 = array($s6, $s7, $s8);
$s5 = $s6;
} else {
$this->peg_currPos = $s5;
$s5 = $this->peg_FAILED;
}
} else {
$this->peg_currPos = $s5;
$s5 = $this->peg_FAILED;
}
} else {
$this->peg_currPos = $s5;
$s5 = $this->peg_FAILED;
}
if ($s5 !== $this->peg_FAILED) {
if ($this->input_length > $this->peg_currPos) {
$s6 = $this->input_substr($this->peg_currPos, 1);
$this->peg_currPos++;
} else {
$s6 = $this->peg_FAILED;
if ($this->peg_silentFails === 0) {
$this->peg_fail($this->peg_c0);
while ($s5 !== $this->peg_FAILED) {
$s4[] = $s5;
$s5 = $this->peg_currPos;
$s6 = $this->peg_currPos;
$this->peg_silentFails++;
$s7 = $this->peg_parseBlock();
$this->peg_silentFails--;
if ($s7 === $this->peg_FAILED) {
$s6 = null;
} else {
$this->peg_currPos = $s6;
$s6 = $this->peg_FAILED;
}
if ($s6 !== $this->peg_FAILED) {
$s7 = $this->peg_currPos;
$this->peg_silentFails++;
$s8 = $this->peg_parseBlock_End();
$this->peg_silentFails--;
if ($s8 === $this->peg_FAILED) {
$s7 = null;
} else {
$this->peg_currPos = $s7;
$s7 = $this->peg_FAILED;
}
if ($s7 !== $this->peg_FAILED) {
if ($this->input_length > $this->peg_currPos) {
$s8 = $this->input_substr($this->peg_currPos, 1);
$this->peg_currPos++;
} else {
$s8 = $this->peg_FAILED;
if ($this->peg_silentFails === 0) {
$this->peg_fail($this->peg_c0);
}
}
if ($s8 !== $this->peg_FAILED) {
$s6 = array($s6, $s7, $s8);
$s5 = $s6;
} else {
$this->peg_currPos = $s5;
$s5 = $this->peg_FAILED;
}
} else {
$this->peg_currPos = $s5;
$s5 = $this->peg_FAILED;
}
} else {
$this->peg_currPos = $s5;
$s5 = $this->peg_FAILED;
}
}
if ($s6 !== $this->peg_FAILED) {
$s5 = array($s5, $s6);
$s4 = $s5;
} else {
$this->peg_currPos = $s4;
$s4 = $this->peg_FAILED;
}
} else {
$this->peg_currPos = $s4;
$s4 = $this->peg_FAILED;
}
if ($s4 !== $this->peg_FAILED) {
Expand All @@ -754,36 +826,106 @@ private function peg_parseBlock_Balanced() {
$s3 = $this->peg_parseBlock();
if ($s3 === $this->peg_FAILED) {
$s3 = $this->peg_currPos;
$s4 = $this->peg_currPos;
$s4 = array();
$s5 = $this->peg_currPos;
$s6 = $this->peg_currPos;
$this->peg_silentFails++;
$s6 = $this->peg_parseBlock_End();
$s7 = $this->peg_parseBlock();
$this->peg_silentFails--;
if ($s6 === $this->peg_FAILED) {
$s5 = null;
if ($s7 === $this->peg_FAILED) {
$s6 = null;
} else {
$this->peg_currPos = $s6;
$s6 = $this->peg_FAILED;
}
if ($s6 !== $this->peg_FAILED) {
$s7 = $this->peg_currPos;
$this->peg_silentFails++;
$s8 = $this->peg_parseBlock_End();
$this->peg_silentFails--;
if ($s8 === $this->peg_FAILED) {
$s7 = null;
} else {
$this->peg_currPos = $s7;
$s7 = $this->peg_FAILED;
}
if ($s7 !== $this->peg_FAILED) {
if ($this->input_length > $this->peg_currPos) {
$s8 = $this->input_substr($this->peg_currPos, 1);
$this->peg_currPos++;
} else {
$s8 = $this->peg_FAILED;
if ($this->peg_silentFails === 0) {
$this->peg_fail($this->peg_c0);
}
}
if ($s8 !== $this->peg_FAILED) {
$s6 = array($s6, $s7, $s8);
$s5 = $s6;
} else {
$this->peg_currPos = $s5;
$s5 = $this->peg_FAILED;
}
} else {
$this->peg_currPos = $s5;
$s5 = $this->peg_FAILED;
}
} else {
$this->peg_currPos = $s5;
$s5 = $this->peg_FAILED;
}
if ($s5 !== $this->peg_FAILED) {
if ($this->input_length > $this->peg_currPos) {
$s6 = $this->input_substr($this->peg_currPos, 1);
$this->peg_currPos++;
} else {
$s6 = $this->peg_FAILED;
if ($this->peg_silentFails === 0) {
$this->peg_fail($this->peg_c0);
while ($s5 !== $this->peg_FAILED) {
$s4[] = $s5;
$s5 = $this->peg_currPos;
$s6 = $this->peg_currPos;
$this->peg_silentFails++;
$s7 = $this->peg_parseBlock();
$this->peg_silentFails--;
if ($s7 === $this->peg_FAILED) {
$s6 = null;
} else {
$this->peg_currPos = $s6;
$s6 = $this->peg_FAILED;
}
if ($s6 !== $this->peg_FAILED) {
$s7 = $this->peg_currPos;
$this->peg_silentFails++;
$s8 = $this->peg_parseBlock_End();
$this->peg_silentFails--;
if ($s8 === $this->peg_FAILED) {
$s7 = null;
} else {
$this->peg_currPos = $s7;
$s7 = $this->peg_FAILED;
}
if ($s7 !== $this->peg_FAILED) {
if ($this->input_length > $this->peg_currPos) {
$s8 = $this->input_substr($this->peg_currPos, 1);
$this->peg_currPos++;
} else {
$s8 = $this->peg_FAILED;
if ($this->peg_silentFails === 0) {
$this->peg_fail($this->peg_c0);
}
}
if ($s8 !== $this->peg_FAILED) {
$s6 = array($s6, $s7, $s8);
$s5 = $s6;
} else {
$this->peg_currPos = $s5;
$s5 = $this->peg_FAILED;
}
} else {
$this->peg_currPos = $s5;
$s5 = $this->peg_FAILED;
}
} else {
$this->peg_currPos = $s5;
$s5 = $this->peg_FAILED;
}
}
if ($s6 !== $this->peg_FAILED) {
$s5 = array($s5, $s6);
$s4 = $s5;
} else {
$this->peg_currPos = $s4;
$s4 = $this->peg_FAILED;
}
} else {
$this->peg_currPos = $s4;
$s4 = $this->peg_FAILED;
}
if ($s4 !== $this->peg_FAILED) {
Expand Down Expand Up @@ -1441,18 +1583,23 @@ public function parse($input) {
// are the same as `json_decode`

// array arguments are backwards because of PHP
if ( ! function_exists( 'peg_array_partition' ) ) {
function peg_array_partition( $array, $predicate ) {
$truthy = array();
$falsey = array();
if ( ! function_exists( 'peg_process_inner_content' ) ) {
function peg_process_inner_content( $array ) {
$html = '';
$blocks = array();
$content = array();

foreach ( $array as $item ) {
call_user_func( $predicate, $item )
? $truthy[] = $item
: $falsey[] = $item;
if ( is_string( $item ) ) {
$html .= $item;
$content[] = $item;
} else {
$blocks[] = $item;
$content[] = null;
}
}

return array( $truthy, $falsey );
return array( $html, $blocks, $content );
}
}

Expand All @@ -1465,7 +1612,8 @@ function peg_join_blocks( $pre, $tokens, $post ) {
'blockName' => null,
'attrs' => array(),
'innerBlocks' => array(),
'innerHTML' => $pre
'innerHTML' => $pre,
'innerContent' => array( $pre ),
);
}

Expand All @@ -1479,7 +1627,8 @@ function peg_join_blocks( $pre, $tokens, $post ) {
'blockName' => null,
'attrs' => array(),
'innerBlocks' => array(),
'innerHTML' => $html
'innerHTML' => $html,
'innerContent' => array( $html ),
);
}
}
Expand All @@ -1489,7 +1638,8 @@ function peg_join_blocks( $pre, $tokens, $post ) {
'blockName' => null,
'attrs' => array(),
'innerBlocks' => array(),
'innerHTML' => $post
'innerHTML' => $post,
'innerContent' => array( $post ),
);
}

Expand Down
Loading