Skip to content

fixes #26 & #27, added testuite.xml #28

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

Closed
wants to merge 4 commits into from
Closed
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
17 changes: 14 additions & 3 deletions CSSParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,17 @@ private function parseAtRule() {
$this->consume(';');
$this->setCharset($sCharset->getString());
return new CSSCharset($sCharset);
} else {
} else if($sIdentifier === 'namespace') {
if($this->comes('"') || $this->comes("'") || $this->comes(('url'))) {
$oNamespace = new CSSNamespace($this->parseURLValue());
} else {
$sPrefix = $this->parseIdentifier();
$oNamespace = new CSSNamespace($this->parseURLValue(), $sPrefix);
}
$this->consumeWhiteSpace();
$this->consume(';');
return $oNamespace;
} else {
//Unknown other at rule (font-face or such)
$this->consume('{');
$this->consumeWhiteSpace();
Expand Down Expand Up @@ -210,7 +220,8 @@ private function parseRuleSet($oRuleSet) {
private function parseRule() {
$oRule = new CSSRule($this->parseIdentifier());
$this->consumeWhiteSpace();
$this->consume(':');
$this->consume(':');
$sRule = $oRule->getrule();
$oValue = $this->parseValue(self::listDelimiterForRule($oRule->getRule()));
$oRule->setValue($oValue);
if($this->comes('!')) {
Expand All @@ -226,7 +237,7 @@ private function parseRule() {
$this->consume(';');
}
return $oRule;
}
}

private function parseValue($aListDelimiters) {
$aStack = array();
Expand Down
38 changes: 38 additions & 0 deletions lib/CSSProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,44 @@ public function __toString() {
}
}

/**
* Class representing an @namespace rule.
* The following restrictions apply:
* <ul>
* <li>May not be found in any CSSList other than the CSSDocument.</li>
* <li>May only appear after all @import rules and before other @rules.</li>
* </ul>
*/
class CSSNamespace {
private $sPrefix;
private $sURI;

public function __construct($sURI, $sPrefix=null) {
$this->sURI = $sURI;
$this->sPrefix = $sPrefix;
}

public function getURI() {
return $this->sURI;
}
public function setURI($sURI) {
$this->sURI = $sURI;
}

public function getPrefix() {
return $this->sPrefix;
}
public function setPrefix($sPrefix) {
$this->sPrefix = $sPrefix;
}

public function __toString() {
$sPrefix = $this->sPrefix ? ' '.$this->sPrefix : '';
return "@namespace" . $sPrefix . ' ' . $this->sURI . ';';
}

}

/**
* Class representing a single CSS selector. Selectors have to be split by the comma prior to being passed into this class.
*/
Expand Down
31 changes: 31 additions & 0 deletions tests/bugs/GH27_Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
require_once __DIR__.'/../../CSSParser.php';
/**
* Test case for GH27
* https://github.com/sabberworm/PHP-CSS-Parser/issues/27
*/
class GH27_Test extends PHPUnit_Framework_TestCase
{
/**
* @dataProvider testNamespacesProvider
**/
public function testNamespaces($sCss, $sExpected)
{
$oParser = new CSSParser($sCss);
$oDoc = $oParser->parse();
$this->assertEquals((string)$oDoc, $sExpected);
}
public function testNamespacesProvider()
{
return array(
array(
'@namespace "http://www.w3.org/1999/xhtml";',
'@namespace url("http://www.w3.org/1999/xhtml");',
),
array(
'@namespace svg "http://www.w3.org/2000/svg";',
'@namespace svg url("http://www.w3.org/2000/svg");',
)
);
}
}
11 changes: 11 additions & 0 deletions testsuite.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<phpunit>
<testsuites>
<testsuite name="PHP-CSS-Parser Tests">
<file>tests/CSSDeclarationBlockTest.php</file>
<file>tests/CSSParserTests.php</file>
</testsuite>
<testsuite name="PHP-CSS-Parser Bugs">
<directory>tests/bugs</directory>
</testsuite>
</testsuites>
</phpunit>