Skip to content

Commit

Permalink
Merge pull request #1 from totara/TL-34457-totara-8.4.0
Browse files Browse the repository at this point in the history
Tl 34457 totara 8.4.0
  • Loading branch information
codyfinegan authored Jul 21, 2022
2 parents e41d214 + 2141f50 commit 3b81efd
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 3 deletions.
16 changes: 15 additions & 1 deletion src/CSSList/CSSList.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public static function parseList(ParserState $oParserState, CSSList $oList)
$oListItem->setComments($comments);
$oList->append($oListItem);
}
$oParserState->consumeWhiteSpace();
$oParserState->consumeWhiteSpace(false);
}
if (!$bIsRoot && !$bLenientParsing) {
throw new SourceException("Unexpected end of document", $oParserState->currentLine());
Expand Down Expand Up @@ -297,6 +297,20 @@ public function splice($iOffset, $iLength = null, $mReplacement = null)
array_splice($this->aContents, $iOffset, $iLength, $mReplacement);
}

/**
* Insert an item before its sibling.
*
* @param mixed $oItem The item.
* @param mixed $oSibling The sibling.
*/
public function insert($oItem, $oSibling) {
$iIndex = array_search($oSibling, $this->aContents);
if ($iIndex === false) {
return $this->append($oItem);
}
array_splice($this->aContents, $iIndex, 0, array($oItem));
}

/**
* Removes an item from the CSS list.
*
Expand Down
6 changes: 5 additions & 1 deletion src/Parsing/ParserState.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,18 +197,22 @@ public function parseCharacter($bIsForIdentifier)
}

/**
* @param bool $consumeComments
* @return array<int, Comment>|void
*
* @throws UnexpectedEOFException
* @throws UnexpectedTokenException
*/
public function consumeWhiteSpace()
public function consumeWhiteSpace($consumeComments = true)
{
$comments = [];
do {
while (preg_match('/\\s/isSu', $this->peek()) === 1) {
$this->consume(1);
}
if (!$consumeComments) {
return;
}
if ($this->oParserSettings->bLenientParsing) {
try {
$oComment = $this->consumeComment();
Expand Down
2 changes: 1 addition & 1 deletion src/Rule/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static function parse(ParserState $oParserState)
while ($oParserState->comes(';')) {
$oParserState->consume(';');
}
$oParserState->consumeWhiteSpace();
$oParserState->consumeWhiteSpace(false);

return $oRule;
}
Expand Down
33 changes: 33 additions & 0 deletions tests/CSSList/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PHPUnit\Framework\TestCase;
use Sabberworm\CSS\CSSList\Document;
use Sabberworm\CSS\RuleSet\DeclarationBlock;
use Sabberworm\CSS\Parser;

/**
* @covers \Sabberworm\CSS\CSSList\Document
Expand Down Expand Up @@ -67,4 +68,36 @@ public function setContentsReplacesContentsSetInPreviousCall()

self::assertSame($contents2, $this->subject->getContents());
}

public function testInsertContent() {
$sCss = '.thing { left: 10px; } .stuff { margin: 1px; } ';
$oParser = new Parser($sCss);
$oDoc = $oParser->parse();
$aContents = $oDoc->getContents();
$this->assertCount(2, $aContents);

$oThing = $aContents[0];
$oStuff = $aContents[1];

$oFirst = new DeclarationBlock();
$oFirst->setSelectors('.first');
$oBetween = new DeclarationBlock();
$oBetween->setSelectors('.between');
$oOrphan = new DeclarationBlock();
$oOrphan->setSelectors('.forever-alone');
$oNotFound = new DeclarationBlock();
$oNotFound->setSelectors('.not-found');

$oDoc->insert($oFirst, $oThing);
$oDoc->insert($oBetween, $oStuff);
$oDoc->insert($oOrphan, $oNotFound);

$aContents = $oDoc->getContents();
$this->assertCount(5, $aContents);
$this->assertSame($oFirst, $aContents[0]);
$this->assertSame($oThing, $aContents[1]);
$this->assertSame($oBetween, $aContents[2]);
$this->assertSame($oStuff, $aContents[3]);
$this->assertSame($oOrphan, $aContents[4]);
}
}
13 changes: 13 additions & 0 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1120,6 +1120,19 @@ public function flatCommentExtracting()
self::assertSame("Find Me!", $comments[0]->getComment());
}

/**
* @test
*/
public function testInnerCommentExtracting() {
$parser = new Parser('div {left:10px;/*Find Me!*/text-align:left;}');
$doc = $parser->parse();
$contents = $doc->getContents();
$divRules = $contents[0]->getRules();
$comments = $divRules[1]->getComments();
$this->assertCount(1, $comments);
$this->assertEquals("Find Me!", $comments[0]->getComment());
}

/**
* @test
*/
Expand Down

0 comments on commit 3b81efd

Please sign in to comment.