@@ -468,13 +468,13 @@ private function parseBlockScalar($style, $chomping = '', $indentation = 0)
468468 }
469469
470470$ isCurrentLineBlank =$ this ->isCurrentLineBlank ();
471- $ text ='' ;
471+ $ blockLines =array () ;
472472
473473// leading blank lines are consumed before determining indentation
474474while ($ notEOF &&$ isCurrentLineBlank ) {
475475// newline only if not EOF
476476if ($ notEOF =$ this ->moveToNextLine ()) {
477- $ text .= "\n" ;
477+ $ blockLines [] = '' ;
478478$ isCurrentLineBlank =$ this ->isCurrentLineBlank ();
479479 }
480480 }
@@ -495,37 +495,59 @@ private function parseBlockScalar($style, $chomping = '', $indentation = 0)
495495preg_match ($ pattern ,$ this ->currentLine ,$ matches )
496496 )
497497 ) {
498- if ($ isCurrentLineBlank ) {
499- $ text .=substr ($ this ->currentLine ,$ indentation );
498+ if ($ isCurrentLineBlank &&strlen ($ this ->currentLine ) >$ indentation ) {
499+ $ blockLines [] =substr ($ this ->currentLine ,$ indentation );
500+ }elseif ($ isCurrentLineBlank ) {
501+ $ blockLines [] ='' ;
500502 }else {
501- $ text . =$ matches [1 ];
503+ $ blockLines [] =$ matches [1 ];
502504 }
503505
504506// newline only if not EOF
505507if ($ notEOF =$ this ->moveToNextLine ()) {
506- $ text .="\n" ;
507508$ isCurrentLineBlank =$ this ->isCurrentLineBlank ();
508509 }
509510 }
510511 }elseif ($ notEOF ) {
511- $ text .= "\n" ;
512+ $ blockLines [] = '' ;
512513 }
513514
514515if ($ notEOF ) {
516+ $ blockLines [] ='' ;
515517$ this ->moveToPreviousLine ();
516518 }
517519
518520// folded style
519521if ('> ' ===$ style ) {
520- // folded lines
521- // replace all non-leading/non-trailing single newlines with spaces
522- preg_match ('/(\n*)$/ ' ,$ text ,$ matches );
523- $ text =preg_replace ('/(?<!\n|^)\n(?!\n)/ ' ,' ' ,rtrim ($ text ,"\n" ));
524- $ text .=$ matches [1 ];
525-
526- // empty separation lines
527- // remove one newline from each group of non-leading/non-trailing newlines
528- $ text =preg_replace ('/[^\n]\n+\K\n(?=[^\n])/ ' ,'' ,$ text );
522+ $ text ='' ;
523+ $ previousLineIndented =false ;
524+ $ previousLineBlank =false ;
525+
526+ for ($ i =0 ;$ i <count ($ blockLines );$ i ++) {
527+ if ('' ===$ blockLines [$ i ]) {
528+ $ text .="\n" ;
529+ $ previousLineIndented =false ;
530+ $ previousLineBlank =true ;
531+ }elseif (' ' ===$ blockLines [$ i ][0 ]) {
532+ $ text .="\n" .$ blockLines [$ i ];
533+ $ previousLineIndented =true ;
534+ $ previousLineBlank =false ;
535+ }elseif ($ previousLineIndented ) {
536+ $ text .="\n" .$ blockLines [$ i ];
537+ $ previousLineIndented =false ;
538+ $ previousLineBlank =false ;
539+ }elseif ($ previousLineBlank ||0 ===$ i ) {
540+ $ text .=$ blockLines [$ i ];
541+ $ previousLineIndented =false ;
542+ $ previousLineBlank =false ;
543+ }else {
544+ $ text .=' ' .$ blockLines [$ i ];
545+ $ previousLineIndented =false ;
546+ $ previousLineBlank =false ;
547+ }
548+ }
549+ }else {
550+ $ text =implode ("\n" ,$ blockLines );
529551 }
530552
531553// deal with trailing newlines