| 1 |
<?php |
| 2 |
|
| 3 |
namespace Sabberworm\CSS; |
| 4 |
|
| 5 |
use Sabberworm\CSS\CSSList\Document; |
| 6 |
use Sabberworm\CSS\Parsing\ParserState; |
| 7 |
use Sabberworm\CSS\Parsing\SourceException; |
| 8 |
|
| 9 |
/** |
| 10 |
* This class parses CSS from text into a data structure. |
| 11 |
*/ |
| 12 |
class Parser |
| 13 |
{ |
| 14 |
/** |
| 15 |
* @var ParserState |
| 16 |
*/ |
| 17 |
private $oParserState; |
| 18 |
|
| 19 |
/** |
| 20 |
* @param string $sText the complete CSS as text (i.e., usually the contents of a CSS file) |
| 21 |
* @param Settings|null $oParserSettings |
| 22 |
* @param int $iLineNo the line number (starting from 1, not from 0) |
| 23 |
*/ |
| 24 |
public function __construct($sText, $oParserSettings = null, $iLineNo = 1) |
| 25 |
{ |
| 26 |
if ($oParserSettings === null) { |
| 27 |
$oParserSettings = Settings::create(); |
| 28 |
} |
| 29 |
$this->oParserState = new ParserState($sText, $oParserSettings, $iLineNo); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Sets the charset to be used if the CSS does not contain an `@charset` declaration. |
| 34 |
* |
| 35 |
* @param string $sCharset |
| 36 |
* |
| 37 |
* @return void |
| 38 |
* |
| 39 |
* @deprecated since 8.7.0, will be removed in version 9.0.0 with #687 |
| 40 |
*/ |
| 41 |
public function setCharset($sCharset) |
| 42 |
{ |
| 43 |
$this->oParserState->setCharset($sCharset); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Returns the charset that is used if the CSS does not contain an `@charset` declaration. |
| 48 |
* |
| 49 |
* @return void |
| 50 |
* |
| 51 |
* @deprecated since 8.7.0, will be removed in version 9.0.0 with #687 |
| 52 |
*/ |
| 53 |
public function getCharset() |
| 54 |
{ |
| 55 |
// Note: The `return` statement is missing here. This is a bug that needs to be fixed. |
| 56 |
$this->oParserState->getCharset(); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Parses the CSS provided to the constructor and creates a `Document` from it. |
| 61 |
* |
| 62 |
* @return Document |
| 63 |
* |
| 64 |
* @throws SourceException |
| 65 |
*/ |
| 66 |
public function parse() |
| 67 |
{ |
| 68 |
return Document::parse($this->oParserState); |
| 69 |
} |
| 70 |
} |
| 71 |
|