sabberworm / PHP-CSS-Parser

A Parser for CSS Files written in PHP. Allows extraction of CSS files into a data structure, manipulation of said structure and output as (optimized) CSS

Home Page:http://www.sabberworm.com/blog/2010/6/10/php-css-parser

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Full-width space on line before selector declaration wrongly included in selector name

sudofox opened this issue · comments

I was debugging some issues with trying to clean some css rules from a few hundred individual css files (used in an embedded application which only supports one css file at a time). However, for some reason it wasn't removing certain rules. As it turns out, there were zero-width spaces (U+200B / UTF-8 0xE2 0x80 0x8B (e2808b)) being incorrectly pulled into the selector. Interestingly, it also pulled the newline (0x0A) after the full-width space as well.

<?php

$data = <<<CSS

div.thing {
  margin: 0 11px 0 11px;
}
CSS;

$oCssParser = new Sabberworm\CSS\Parser($data);
$oCssDocument = $oCssParser->parse();
$oCssDocument->removeDeclarationBlockBySelector("div.thing");

foreach ($oCssDocument->getAllDeclarationBlocks() as $oBlock) {
	$selectorName = current($oBlock->getSelectors());
	echo "'" . $selectorName . "' => " . bin2hex($selectorName) . PHP_EOL;
}

echo "'" . $oCssDocument->render() . "'";
$ php bugtest.php 
'​
div.thing' => e2808b0a6469762e7468696e67
'​
div.thing {margin: 0 11px 0 11px;}'