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

Add option to keep comments in output #124

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
26 changes: 16 additions & 10 deletions lib/Sabberworm/CSS/CSSList/CSSList.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Sabberworm\CSS\CSSList;

use Sabberworm\CSS\OutputFormat;
use Sabberworm\CSS\Renderable;
use Sabberworm\CSS\RuleSet\DeclarationBlock;
use Sabberworm\CSS\RuleSet\RuleSet;
Expand Down Expand Up @@ -90,24 +91,29 @@ public function removeDeclarationBlockBySelector($mSelector, $bRemoveAll = false
}

public function __toString() {
return $this->render(new \Sabberworm\CSS\OutputFormat());
return $this->render(new OutputFormat());
}

public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
/**
* {@inheritdoc}
*/
public function render(OutputFormat $oOutputFormat) {
$sResult = '';
$bIsFirst = true;
$oNextLevel = $oOutputFormat;
if(!$this->isRootList()) {
if (!$this->isRootList()) {
$oNextLevel = $oOutputFormat->nextLevel();
}
foreach ($this->aContents as $oContent) {
$sRendered = $oOutputFormat->safely(function() use ($oNextLevel, $oContent) {
return $oContent->render($oNextLevel);
});
if($sRendered === null) {
$sRendered = $oOutputFormat->safely(
function () use ($oNextLevel, $oContent) {
return $oContent->render($oNextLevel);
}
);
if ($sRendered === null) {
continue;
}
if($bIsFirst) {
if ($bIsFirst) {
$bIsFirst = false;
$sResult .= $oNextLevel->spaceBeforeBlocks();
} else {
Expand All @@ -116,14 +122,14 @@ public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
$sResult .= $sRendered;
}

if(!$bIsFirst) {
if (!$bIsFirst) {
// Had some output
$sResult .= $oOutputFormat->spaceAfterBlocks();
}

return $sResult;
}

/**
* Return true if the list can not be further outdented. Only important when rendering.
*/
Expand Down
16 changes: 12 additions & 4 deletions lib/Sabberworm/CSS/CSSList/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Sabberworm\CSS\CSSList;

use Sabberworm\CSS\OutputFormat;

/**
* The root CSSList of a parsed file. Contains all top-level css contents, mostly declaration blocks, but also any @-rules encountered.
*/
Expand Down Expand Up @@ -90,10 +92,16 @@ public function createShorthands() {
}
}

// Override render() to make format argument optional
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat = null) {
/**
* {@inheritdoc}
*
* @param OutputFormat|null $oOutputFormat
*
* @return string
*/
public function render(OutputFormat $oOutputFormat = null) {
if($oOutputFormat === null) {
$oOutputFormat = new \Sabberworm\CSS\OutputFormat();
$oOutputFormat = new OutputFormat();
}
return parent::render($oOutputFormat);
}
Expand All @@ -102,4 +110,4 @@ public function isRootList() {
return true;
}

}
}
12 changes: 5 additions & 7 deletions lib/Sabberworm/CSS/Comment/Commentable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@

namespace Sabberworm\CSS\Comment;

interface Commentable {

interface Commentable
{
/**
* @param array $aComments Array of comments.
* @param Comment[] $aComments Array of comments.
*/
public function addComments(array $aComments);

/**
* @return array
* @return Comment[]
*/
public function getComments();

/**
* @param array $aComments Array containing Comment objects.
* @param Comment[] $aComments Array containing Comment objects.
*/
public function setComments(array $aComments);


}
Loading