Skip to content
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
13 changes: 12 additions & 1 deletion lib/Sabberworm/CSS/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ private function parseStringValue() {

private function parseCharacter($bIsForIdentifier) {
if ($this->peek() === '\\') {
if ($bIsForIdentifier && $this->oParserSettings->bLenientParsing && ($this->comes('\0') || $this->comes('\9'))) {
// Non-strings can contain \0 or \9 which is an IE hack supported in lenient parsing.
return null;
}
$this->consume('\\');
if ($this->comes('\n') || $this->comes('\r')) {
return '';
Expand Down Expand Up @@ -350,6 +354,13 @@ private function parseRule() {
$this->consume(':');
$oValue = $this->parseValue(self::listDelimiterForRule($oRule->getRule()));
$oRule->setValue($oValue);
if ($this->oParserSettings->bLenientParsing) {
while ($this->comes('\\')) {
$this->consume('\\');
$oRule->addIeHack($this->consume());
$this->consumeWhiteSpace();
}
}
if ($this->comes('!')) {
$this->consume('!');
$this->consumeWhiteSpace();
Expand All @@ -367,7 +378,7 @@ private function parseValue($aListDelimiters) {
$aStack = array();
$this->consumeWhiteSpace();
//Build a list of delimiters and parsed values
while (!($this->comes('}') || $this->comes(';') || $this->comes('!') || $this->comes(')'))) {
while (!($this->comes('}') || $this->comes(';') || $this->comes('!') || $this->comes(')') || $this->comes('\\'))) {
if (count($aStack) > 0) {
$bFoundDelimiter = false;
foreach ($aListDelimiters as $sDelimiter) {
Expand Down
17 changes: 17 additions & 0 deletions lib/Sabberworm/CSS/Rule/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ class Rule implements Renderable {
private $sRule;
private $mValue;
private $bIsImportant;
private $aIeHack;
protected $iLineNo;

public function __construct($sRule, $iLineNo = 0) {
$this->sRule = $sRule;
$this->mValue = null;
$this->bIsImportant = false;
$this->aIeHack = array();
$this->iLineNo = $iLineNo;
}

Expand Down Expand Up @@ -127,6 +129,18 @@ public function addValue($mValue, $sType = ' ') {
}
}

public function addIeHack($iModifier) {
$this->aIeHack[] = $iModifier;
}

public function setIeHack(array $aModifiers) {
$this->aIeHack = $aModifiers;
}

public function getIeHack() {
return $this->aIeHack;
}

public function setIsImportant($bIsImportant) {
$this->bIsImportant = $bIsImportant;
}
Expand All @@ -146,6 +160,9 @@ public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
} else {
$sResult .= $this->mValue;
}
if (!empty($this->aIeHack)) {
$sResult .= ' \\' . implode('\\', $this->aIeHack);
}
if ($this->bIsImportant) {
$sResult .= ' !important';
}
Expand Down
14 changes: 14 additions & 0 deletions tests/Sabberworm/CSS/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -505,4 +505,18 @@ function testUnexpectedTokenExceptionLineNo() {
throw $e;
}
}

/**
* @expectedException Sabberworm\CSS\Parsing\UnexpectedTokenException
*/
function testIeHacksStrictParsing() {
// We can't strictly parse IE hacks.
$this->parsedStructureForFile('ie-hacks', Settings::create()->beStrict());
}

function testIeHacksParsing() {
$oDoc = $this->parsedStructureForFile('ie-hacks', Settings::create()->withLenientParsing(true));
$sExpected = 'p {padding-right: .75rem \9;background-image: none \9;color: red \9\0;background-color: red \9\0;background-color: red \9\0 !important;content: "red \0";content: "red઼";}';
$this->assertEquals($sExpected, $oDoc->render());
}
}
9 changes: 9 additions & 0 deletions tests/files/ie-hacks.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
p {
padding-right: .75rem \9;
background-image: none \9;
color:red\9\0;
background-color:red \9 \0;
background-color:red \9 \0 !important;
content: "red \9\0";
content: "red\0abc";
}