@@ -35,7 +35,7 @@ class Xml
3535 protected $ root ;
3636 protected $ parent ;
3737
38- protected $ options ;
38+ protected $ flags ;
3939 protected $ sub ;
4040
4141 /**
@@ -218,9 +218,14 @@ public function addProcessingInstruction(
218218 return $ instr ;
219219 }
220220
221+ public function disableLineBreak (): self
222+ {
223+ return $ this ->setFlag ('lineBreakDisabled ' );
224+ }
225+
221226 public function disableTextIndentation (): self
222227 {
223- return $ this ->setOption ('textIndentationDisabled ' , true );
228+ return $ this ->setFlag ('textIndentationDisabled ' );
224229 }
225230
226231 public function attrib (string $ name , $ value = true ): self
@@ -302,10 +307,10 @@ private function isRoot(): bool
302307 return $ this ->root == $ this ;
303308 }
304309
305- private function setOption (string $ option , bool $ value ): self
310+ private function setFlag (string $ flagName ): self
306311 {
307- $ this ->options = $ this ->options ?? new \ stdClass () ;
308- $ this ->options -> $ option = $ value ;
312+ $ this ->flags = $ this ->flags ?? [] ;
313+ $ this ->flags [ $ flagName ] = true ;
309314 return $ this ;
310315 }
311316
@@ -352,19 +357,16 @@ private function buildContainer(\stdClass $v): string
352357 $ markup .= $ v ->whitespace . $ this ->openingTag ($ v );
353358 }
354359 if ($ this ->cdata ) {
355- $ markup .= $ v ->whitespace . self ::CDATA_START ;
356- }
357- if (!empty ($ v ->content )) {
358- $ markup .= $ v ->whitespace . $ v ->content ;
360+ $ markup .= $ v ->whitespaceCData . self ::CDATA_START ;
359361 }
360362 foreach ($ this ->children as $ child ) {
361363 $ markup .= $ child ->buildMarkup ($ v );
362364 }
363365 if ($ this ->cdata ) {
364- $ markup .= $ v ->whitespace . self ::CDATA_STOP ;
366+ $ markup .= $ v ->whitespaceCData . self ::CDATA_STOP ;
365367 }
366368 if ($ v ->hasTags ) {
367- $ markup .= $ v ->whitespace . $ this ->closingTag ($ v );
369+ $ markup .= $ v ->whitespaceContainerEnd . $ this ->closingTag ($ v );
368370 }
369371 return $ markup ;
370372 }
@@ -409,26 +411,40 @@ private function standaloneTag(\stdClass $v): string
409411 private function calculateVars (\stdClass $ parentVars = null )
410412 {
411413 $ vars = new \stdClass ();
412- $ vars ->hasTags = !empty ($ this ->name );
414+
415+ if (isset ($ this ->flags ['textIndentationDisabled ' ])) {
416+ $ vars ->textIndentDisabled = true ;
417+ }
418+ else if (isset ($ parentVars )) {
419+ $ vars ->textIndentDisabled = $ parentVars ->textIndentDisabled ;
420+ }
421+ else {
422+ $ vars ->textIndentDisabled = false ;
423+ }
413424
414425 if (isset ($ parentVars )) {
415- $ vars ->whitespace = $ parentVars ->whitespace ;
416- if ($ this ->parent ->name != '' && $ vars ->whitespace != '' ) {
417- $ vars ->whitespace .= self ::$ indentation ;
426+ if (isset ($ parentVars ->lineBreakDisabled )) {
427+ $ vars ->whitespace = '' ;
428+ }
429+ else {
430+ $ vars ->whitespace = $ parentVars ->whitespace ;
431+ if ($ this ->parent ->name != '' && $ vars ->whitespace != '' ) {
432+ $ vars ->whitespace .= self ::$ indentation ;
433+ }
418434 }
419435 }
420436 else {
421437 $ vars ->whitespace = self ::$ lineBreak ;
422438 }
423439
424- if (isset ($ this ->options ->textIndentationDisabled )) {
425- $ vars ->textIndentDisabled = true ;
426- }
427- else if (isset ($ parentVars )) {
428- $ vars ->textIndentDisabled = $ parentVars ->textIndentDisabled ;
440+ if (isset ($ this ->flags ['lineBreakDisabled ' ])) {
441+ $ vars ->lineBreakDisabled = true ;
442+ $ vars ->whitespaceContainerEnd = '' ;
443+ $ vars ->whitespaceCData = '' ;
429444 }
430445 else {
431- $ vars ->textIndentDisabled = false ;
446+ $ vars ->whitespaceContainerEnd = $ vars ->whitespace ;
447+ $ vars ->whitespaceCData = $ vars ->whitespace ;
432448 }
433449
434450 if (!empty ($ this ->content ) && $ vars ->whitespace != '' && !$ vars ->textIndentDisabled ) {
@@ -438,12 +454,12 @@ private function calculateVars(\stdClass $parentVars = null)
438454 $ vars ->content = $ this ->content ;
439455 }
440456
457+ $ vars ->hasTags = !empty ($ this ->name );
441458 if ($ vars ->hasTags ) {
442- $ vars ->attributes = $ this ->attributes ->getMarkup (
443- static ::HTML_MODE_ENABLED ,
459+ $ vars ->attributes = $ this ->attributes ->getMarkup (static ::HTML_MODE_ENABLED ,
444460 static ::VERTICAL_ATTRIBUTES_ENABLED && $ vars ->whitespace != ''
445- ? $ vars ->whitespace . self ::$ indentation . self ::$ indentation
446- : ' ' );
461+ ? $ vars ->whitespace . self ::$ indentation . self ::$ indentation
462+ : ' ' );
447463 $ vars ->name = empty (static ::NAMESPACE_PREFIX )
448464 ? $ this ->name
449465 : static ::NAMESPACE_PREFIX . ': ' . $ this ->name ;
0 commit comments