PageRenderTime 64ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/woocommerce/includes/libraries/class-cssmin.php

https://bitbucket.org/theshipswakecreative/psw
PHP | 5087 lines | 3031 code | 67 blank | 1989 comment | 320 complexity | 505456bb6263c93d2ec6ecddedc9afff MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0
  1. <?php
  2. /**
  3. * CssMin - A (simple) css minifier with benefits
  4. *
  5. * --
  6. * Copyright (c) 2011 Joe Scylla <joe.scylla@gmail.com>
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. * --
  26. *
  27. * @package CssMin
  28. * @link http://code.google.com/p/cssmin/
  29. * @author Joe Scylla <joe.scylla@gmail.com>
  30. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  31. * @license http://opensource.org/licenses/mit-license.php MIT License
  32. * @version 3.0.1
  33. */
  34. if ( ! defined( 'ABSPATH' ) ) {
  35. exit; // Exit if accessed directly
  36. }
  37. /**
  38. * Abstract definition of a CSS token class.
  39. *
  40. * Every token has to extend this class.
  41. *
  42. * @package CssMin/Tokens
  43. * @link http://code.google.com/p/cssmin/
  44. * @author Joe Scylla <joe.scylla@gmail.com>
  45. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  46. * @license http://opensource.org/licenses/mit-license.php MIT License
  47. * @version 3.0.1
  48. */
  49. abstract class aCssToken
  50. {
  51. /**
  52. * Returns the token as string.
  53. *
  54. * @return string
  55. */
  56. abstract public function __toString();
  57. }
  58. /**
  59. * Abstract definition of a for a ruleset start token.
  60. *
  61. * @package CssMin/Tokens
  62. * @link http://code.google.com/p/cssmin/
  63. * @author Joe Scylla <joe.scylla@gmail.com>
  64. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  65. * @license http://opensource.org/licenses/mit-license.php MIT License
  66. * @version 3.0.1
  67. */
  68. abstract class aCssRulesetStartToken extends aCssToken
  69. {
  70. }
  71. /**
  72. * Abstract definition of a for ruleset end token.
  73. *
  74. * @package CssMin/Tokens
  75. * @link http://code.google.com/p/cssmin/
  76. * @author Joe Scylla <joe.scylla@gmail.com>
  77. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  78. * @license http://opensource.org/licenses/mit-license.php MIT License
  79. * @version 3.0.1
  80. */
  81. abstract class aCssRulesetEndToken extends aCssToken
  82. {
  83. /**
  84. * Implements {@link aCssToken::__toString()}.
  85. *
  86. * @return string
  87. */
  88. public function __toString()
  89. {
  90. return "}";
  91. }
  92. }
  93. /**
  94. * Abstract definition of a parser plugin.
  95. *
  96. * Every parser plugin have to extend this class. A parser plugin contains the logic to parse one or aspects of a
  97. * stylesheet.
  98. *
  99. * @package CssMin/Parser/Plugins
  100. * @link http://code.google.com/p/cssmin/
  101. * @author Joe Scylla <joe.scylla@gmail.com>
  102. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  103. * @license http://opensource.org/licenses/mit-license.php MIT License
  104. * @version 3.0.1
  105. */
  106. abstract class aCssParserPlugin
  107. {
  108. /**
  109. * Plugin configuration.
  110. *
  111. * @var array
  112. */
  113. protected $configuration = array();
  114. /**
  115. * The CssParser of the plugin.
  116. *
  117. * @var CssParser
  118. */
  119. protected $parser = null;
  120. /**
  121. * Plugin buffer.
  122. *
  123. * @var string
  124. */
  125. protected $buffer = "";
  126. /**
  127. * Constructor.
  128. *
  129. * @param CssParser $parser The CssParser object of this plugin.
  130. * @param array $configuration Plugin configuration [optional]
  131. * @return void
  132. */
  133. public function __construct(CssParser $parser, array $configuration = null)
  134. {
  135. $this->configuration = $configuration;
  136. $this->parser = $parser;
  137. }
  138. /**
  139. * Returns the array of chars triggering the parser plugin.
  140. *
  141. * @return array
  142. */
  143. abstract public function getTriggerChars();
  144. /**
  145. * Returns the array of states triggering the parser plugin or FALSE if every state will trigger the parser plugin.
  146. *
  147. * @return array
  148. */
  149. abstract public function getTriggerStates();
  150. /**
  151. * Parser routine of the plugin.
  152. *
  153. * @param integer $index Current index
  154. * @param string $char Current char
  155. * @param string $previousChar Previous char
  156. * @return mixed TRUE will break the processing; FALSE continue with the next plugin; integer set a new index and break the processing
  157. */
  158. abstract public function parse($index, $char, $previousChar, $state);
  159. }
  160. /**
  161. * Abstract definition of a minifier plugin class.
  162. *
  163. * Minifier plugin process the parsed tokens one by one to apply changes to the token. Every minifier plugin has to
  164. * extend this class.
  165. *
  166. * @package CssMin/Minifier/Plugins
  167. * @link http://code.google.com/p/cssmin/
  168. * @author Joe Scylla <joe.scylla@gmail.com>
  169. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  170. * @license http://opensource.org/licenses/mit-license.php MIT License
  171. * @version 3.0.1
  172. */
  173. abstract class aCssMinifierPlugin
  174. {
  175. /**
  176. * Plugin configuration.
  177. *
  178. * @var array
  179. */
  180. protected $configuration = array();
  181. /**
  182. * The CssMinifier of the plugin.
  183. *
  184. * @var CssMinifier
  185. */
  186. protected $minifier = null;
  187. /**
  188. * Constructor.
  189. *
  190. * @param CssMinifier $minifier The CssMinifier object of this plugin.
  191. * @param array $configuration Plugin configuration [optional]
  192. * @return void
  193. */
  194. public function __construct(CssMinifier $minifier, array $configuration = array())
  195. {
  196. $this->configuration = $configuration;
  197. $this->minifier = $minifier;
  198. }
  199. /**
  200. * Apply the plugin to the token.
  201. *
  202. * @param aCssToken $token Token to process
  203. * @return boolean Return TRUE to break the processing of this token; FALSE to continue
  204. */
  205. abstract public function apply(aCssToken &$token);
  206. /**
  207. * --
  208. *
  209. * @return array
  210. */
  211. abstract public function getTriggerTokens();
  212. }
  213. /**
  214. * Abstract definition of a minifier filter class.
  215. *
  216. * Minifier filters allows a pre-processing of the parsed token to add, edit or delete tokens. Every minifier filter
  217. * has to extend this class.
  218. *
  219. * @package CssMin/Minifier/Filters
  220. * @link http://code.google.com/p/cssmin/
  221. * @author Joe Scylla <joe.scylla@gmail.com>
  222. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  223. * @license http://opensource.org/licenses/mit-license.php MIT License
  224. * @version 3.0.1
  225. */
  226. abstract class aCssMinifierFilter
  227. {
  228. /**
  229. * Filter configuration.
  230. *
  231. * @var array
  232. */
  233. protected $configuration = array();
  234. /**
  235. * The CssMinifier of the filter.
  236. *
  237. * @var CssMinifier
  238. */
  239. protected $minifier = null;
  240. /**
  241. * Constructor.
  242. *
  243. * @param CssMinifier $minifier The CssMinifier object of this plugin.
  244. * @param array $configuration Filter configuration [optional]
  245. * @return void
  246. */
  247. public function __construct(CssMinifier $minifier, array $configuration = array())
  248. {
  249. $this->configuration = $configuration;
  250. $this->minifier = $minifier;
  251. }
  252. /**
  253. * Filter the tokens.
  254. *
  255. * @param array $tokens Array of objects of type aCssToken
  256. * @return integer Count of added, changed or removed tokens; a return value large than 0 will rebuild the array
  257. */
  258. abstract public function apply(array &$tokens);
  259. }
  260. /**
  261. * Abstract formatter definition.
  262. *
  263. * Every formatter have to extend this class.
  264. *
  265. * @package CssMin/Formatter
  266. * @link http://code.google.com/p/cssmin/
  267. * @author Joe Scylla <joe.scylla@gmail.com>
  268. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  269. * @license http://opensource.org/licenses/mit-license.php MIT License
  270. * @version 3.0.1
  271. */
  272. abstract class aCssFormatter
  273. {
  274. /**
  275. * Indent string.
  276. *
  277. * @var string
  278. */
  279. protected $indent = " ";
  280. /**
  281. * Declaration padding.
  282. *
  283. * @var integer
  284. */
  285. protected $padding = 0;
  286. /**
  287. * Tokens.
  288. *
  289. * @var array
  290. */
  291. protected $tokens = array();
  292. /**
  293. * Constructor.
  294. *
  295. * @param array $tokens Array of CssToken
  296. * @param string $indent Indent string [optional]
  297. * @param integer $padding Declaration value padding [optional]
  298. */
  299. public function __construct(array $tokens, $indent = null, $padding = null)
  300. {
  301. $this->tokens = $tokens;
  302. $this->indent = !is_null($indent) ? $indent : $this->indent;
  303. $this->padding = !is_null($padding) ? $padding : $this->padding;
  304. }
  305. /**
  306. * Returns the array of aCssToken as formatted string.
  307. *
  308. * @return string
  309. */
  310. abstract public function __toString();
  311. }
  312. /**
  313. * Abstract definition of a ruleset declaration token.
  314. *
  315. * @package CssMin/Tokens
  316. * @link http://code.google.com/p/cssmin/
  317. * @author Joe Scylla <joe.scylla@gmail.com>
  318. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  319. * @license http://opensource.org/licenses/mit-license.php MIT License
  320. * @version 3.0.1
  321. */
  322. abstract class aCssDeclarationToken extends aCssToken
  323. {
  324. /**
  325. * Is the declaration flagged as important?
  326. *
  327. * @var boolean
  328. */
  329. public $IsImportant = false;
  330. /**
  331. * Is the declaration flagged as last one of the ruleset?
  332. *
  333. * @var boolean
  334. */
  335. public $IsLast = false;
  336. /**
  337. * Property name of the declaration.
  338. *
  339. * @var string
  340. */
  341. public $Property = "";
  342. /**
  343. * Value of the declaration.
  344. *
  345. * @var string
  346. */
  347. public $Value = "";
  348. /**
  349. * Set the properties of the @font-face declaration.
  350. *
  351. * @param string $property Property of the declaration
  352. * @param string $value Value of the declaration
  353. * @param boolean $isImportant Is the !important flag is set?
  354. * @param boolean $IsLast Is the declaration the last one of the block?
  355. * @return void
  356. */
  357. public function __construct($property, $value, $isImportant = false, $isLast = false)
  358. {
  359. $this->Property = $property;
  360. $this->Value = $value;
  361. $this->IsImportant = $isImportant;
  362. $this->IsLast = $isLast;
  363. }
  364. /**
  365. * Implements {@link aCssToken::__toString()}.
  366. *
  367. * @return string
  368. */
  369. public function __toString()
  370. {
  371. return $this->Property . ":" . $this->Value . ($this->IsImportant ? " !important" : "") . ($this->IsLast ? "" : ";");
  372. }
  373. }
  374. /**
  375. * Abstract definition of a for at-rule block start token.
  376. *
  377. * @package CssMin/Tokens
  378. * @link http://code.google.com/p/cssmin/
  379. * @author Joe Scylla <joe.scylla@gmail.com>
  380. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  381. * @license http://opensource.org/licenses/mit-license.php MIT License
  382. * @version 3.0.1
  383. */
  384. abstract class aCssAtBlockStartToken extends aCssToken
  385. {
  386. }
  387. /**
  388. * Abstract definition of a for at-rule block end token.
  389. *
  390. * @package CssMin/Tokens
  391. * @link http://code.google.com/p/cssmin/
  392. * @author Joe Scylla <joe.scylla@gmail.com>
  393. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  394. * @license http://opensource.org/licenses/mit-license.php MIT License
  395. * @version 3.0.1
  396. */
  397. abstract class aCssAtBlockEndToken extends aCssToken
  398. {
  399. /**
  400. * Implements {@link aCssToken::__toString()}.
  401. *
  402. * @return string
  403. */
  404. public function __toString()
  405. {
  406. return "}";
  407. }
  408. }
  409. /**
  410. * {@link aCssFromatter Formatter} returning the CSS source in {@link http://goo.gl/etzLs Whitesmiths indent style}.
  411. *
  412. * @package CssMin/Formatter
  413. * @link http://code.google.com/p/cssmin/
  414. * @author Joe Scylla <joe.scylla@gmail.com>
  415. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  416. * @license http://opensource.org/licenses/mit-license.php MIT License
  417. * @version 3.0.1
  418. */
  419. class CssWhitesmithsFormatter extends aCssFormatter
  420. {
  421. /**
  422. * Implements {@link aCssFormatter::__toString()}.
  423. *
  424. * @return string
  425. */
  426. public function __toString()
  427. {
  428. $r = array();
  429. $level = 0;
  430. for ($i = 0, $l = count($this->tokens); $i < $l; $i++)
  431. {
  432. $token = $this->tokens[$i];
  433. $class = get_class($token);
  434. $indent = str_repeat($this->indent, $level);
  435. if ($class === "CssCommentToken")
  436. {
  437. $lines = array_map("trim", explode("\n", $token->Comment));
  438. for ($ii = 0, $ll = count($lines); $ii < $ll; $ii++)
  439. {
  440. $r[] = $indent . (substr($lines[$ii], 0, 1) == "*" ? " " : "") . $lines[$ii];
  441. }
  442. }
  443. elseif ($class === "CssAtCharsetToken")
  444. {
  445. $r[] = $indent . "@charset " . $token->Charset . ";";
  446. }
  447. elseif ($class === "CssAtFontFaceStartToken")
  448. {
  449. $r[] = $indent . "@font-face";
  450. $r[] = $this->indent . $indent . "{";
  451. $level++;
  452. }
  453. elseif ($class === "CssAtImportToken")
  454. {
  455. $r[] = $indent . "@import " . $token->Import . " " . implode(", ", $token->MediaTypes) . ";";
  456. }
  457. elseif ($class === "CssAtKeyframesStartToken")
  458. {
  459. $r[] = $indent . "@keyframes \"" . $token->Name . "\"";
  460. $r[] = $this->indent . $indent . "{";
  461. $level++;
  462. }
  463. elseif ($class === "CssAtMediaStartToken")
  464. {
  465. $r[] = $indent . "@media " . implode(", ", $token->MediaTypes);
  466. $r[] = $this->indent . $indent . "{";
  467. $level++;
  468. }
  469. elseif ($class === "CssAtPageStartToken")
  470. {
  471. $r[] = $indent . "@page";
  472. $r[] = $this->indent . $indent . "{";
  473. $level++;
  474. }
  475. elseif ($class === "CssAtVariablesStartToken")
  476. {
  477. $r[] = $indent . "@variables " . implode(", ", $token->MediaTypes);
  478. $r[] = $this->indent . $indent . "{";
  479. $level++;
  480. }
  481. elseif ($class === "CssRulesetStartToken" || $class === "CssAtKeyframesRulesetStartToken")
  482. {
  483. $r[] = $indent . implode(", ", $token->Selectors);
  484. $r[] = $this->indent . $indent . "{";
  485. $level++;
  486. }
  487. elseif ($class == "CssAtFontFaceDeclarationToken"
  488. || $class === "CssAtKeyframesRulesetDeclarationToken"
  489. || $class === "CssAtPageDeclarationToken"
  490. || $class == "CssAtVariablesDeclarationToken"
  491. || $class === "CssRulesetDeclarationToken"
  492. )
  493. {
  494. $declaration = $indent . $token->Property . ": ";
  495. if ($this->padding)
  496. {
  497. $declaration = str_pad($declaration, $this->padding, " ", STR_PAD_RIGHT);
  498. }
  499. $r[] = $declaration . $token->Value . ($token->IsImportant ? " !important" : "") . ";";
  500. }
  501. elseif ($class === "CssAtFontFaceEndToken"
  502. || $class === "CssAtMediaEndToken"
  503. || $class === "CssAtKeyframesEndToken"
  504. || $class === "CssAtKeyframesRulesetEndToken"
  505. || $class === "CssAtPageEndToken"
  506. || $class === "CssAtVariablesEndToken"
  507. || $class === "CssRulesetEndToken"
  508. )
  509. {
  510. $r[] = $indent . "}";
  511. $level--;
  512. }
  513. }
  514. return implode("\n", $r);
  515. }
  516. }
  517. /**
  518. * This {@link aCssMinifierPlugin} will process var-statement and sets the declaration value to the variable value.
  519. *
  520. * This plugin only apply the variable values. The variable values itself will get parsed by the
  521. * {@link CssVariablesMinifierFilter}.
  522. *
  523. * Example:
  524. * <code>
  525. * @variables
  526. * {
  527. * defaultColor: black;
  528. * }
  529. * color: var(defaultColor);
  530. * </code>
  531. *
  532. * Will get converted to:
  533. * <code>
  534. * color:black;
  535. * </code>
  536. *
  537. * @package CssMin/Minifier/Plugins
  538. * @link http://code.google.com/p/cssmin/
  539. * @author Joe Scylla <joe.scylla@gmail.com>
  540. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  541. * @license http://opensource.org/licenses/mit-license.php MIT License
  542. * @version 3.0.1
  543. */
  544. class CssVariablesMinifierPlugin extends aCssMinifierPlugin
  545. {
  546. /**
  547. * Regular expression matching a value.
  548. *
  549. * @var string
  550. */
  551. private $reMatch = "/var\((.+)\)/iSU";
  552. /**
  553. * Parsed variables.
  554. *
  555. * @var array
  556. */
  557. private $variables = null;
  558. /**
  559. * Returns the variables.
  560. *
  561. * @return array
  562. */
  563. public function getVariables()
  564. {
  565. return $this->variables;
  566. }
  567. /**
  568. * Implements {@link aCssMinifierPlugin::minify()}.
  569. *
  570. * @param aCssToken $token Token to process
  571. * @return boolean Return TRUE to break the processing of this token; FALSE to continue
  572. */
  573. public function apply(aCssToken &$token)
  574. {
  575. if (stripos($token->Value, "var") !== false && preg_match_all($this->reMatch, $token->Value, $m))
  576. {
  577. $mediaTypes = $token->MediaTypes;
  578. if (!in_array("all", $mediaTypes))
  579. {
  580. $mediaTypes[] = "all";
  581. }
  582. for ($i = 0, $l = count($m[0]); $i < $l; $i++)
  583. {
  584. $variable = trim($m[1][$i]);
  585. foreach ($mediaTypes as $mediaType)
  586. {
  587. if (isset($this->variables[$mediaType], $this->variables[$mediaType][$variable]))
  588. {
  589. // Variable value found => set the declaration value to the variable value and return
  590. $token->Value = str_replace($m[0][$i], $this->variables[$mediaType][$variable], $token->Value);
  591. continue 2;
  592. }
  593. }
  594. // If no value was found trigger an error and replace the token with a CssNullToken
  595. CssMin::triggerError(new CssError(__FILE__, __LINE__, __METHOD__ . ": No value found for variable <code>" . $variable . "</code> in media types <code>" . implode(", ", $mediaTypes) . "</code>", (string) $token));
  596. $token = new CssNullToken();
  597. return true;
  598. }
  599. }
  600. return false;
  601. }
  602. /**
  603. * Implements {@link aMinifierPlugin::getTriggerTokens()}
  604. *
  605. * @return array
  606. */
  607. public function getTriggerTokens()
  608. {
  609. return array
  610. (
  611. "CssAtFontFaceDeclarationToken",
  612. "CssAtPageDeclarationToken",
  613. "CssRulesetDeclarationToken"
  614. );
  615. }
  616. /**
  617. * Sets the variables.
  618. *
  619. * @param array $variables Variables to set
  620. * @return void
  621. */
  622. public function setVariables(array $variables)
  623. {
  624. $this->variables = $variables;
  625. }
  626. }
  627. /**
  628. * This {@link aCssMinifierFilter minifier filter} will parse the variable declarations out of @variables at-rule
  629. * blocks. The variables will get store in the {@link CssVariablesMinifierPlugin} that will apply the variables to
  630. * declaration.
  631. *
  632. * @package CssMin/Minifier/Filters
  633. * @link http://code.google.com/p/cssmin/
  634. * @author Joe Scylla <joe.scylla@gmail.com>
  635. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  636. * @license http://opensource.org/licenses/mit-license.php MIT License
  637. * @version 3.0.1
  638. */
  639. class CssVariablesMinifierFilter extends aCssMinifierFilter
  640. {
  641. /**
  642. * Implements {@link aCssMinifierFilter::filter()}.
  643. *
  644. * @param array $tokens Array of objects of type aCssToken
  645. * @return integer Count of added, changed or removed tokens; a return value large than 0 will rebuild the array
  646. */
  647. public function apply(array &$tokens)
  648. {
  649. $variables = array();
  650. $defaultMediaTypes = array("all");
  651. $mediaTypes = array();
  652. $remove = array();
  653. for($i = 0, $l = count($tokens); $i < $l; $i++)
  654. {
  655. // @variables at-rule block found
  656. if (get_class($tokens[$i]) === "CssAtVariablesStartToken")
  657. {
  658. $remove[] = $i;
  659. $mediaTypes = (count($tokens[$i]->MediaTypes) == 0 ? $defaultMediaTypes : $tokens[$i]->MediaTypes);
  660. foreach ($mediaTypes as $mediaType)
  661. {
  662. if (!isset($variables[$mediaType]))
  663. {
  664. $variables[$mediaType] = array();
  665. }
  666. }
  667. // Read the variable declaration tokens
  668. for($i = $i; $i < $l; $i++)
  669. {
  670. // Found a variable declaration => read the variable values
  671. if (get_class($tokens[$i]) === "CssAtVariablesDeclarationToken")
  672. {
  673. foreach ($mediaTypes as $mediaType)
  674. {
  675. $variables[$mediaType][$tokens[$i]->Property] = $tokens[$i]->Value;
  676. }
  677. $remove[] = $i;
  678. }
  679. // Found the variables end token => break;
  680. elseif (get_class($tokens[$i]) === "CssAtVariablesEndToken")
  681. {
  682. $remove[] = $i;
  683. break;
  684. }
  685. }
  686. }
  687. }
  688. // Variables in @variables at-rule blocks
  689. foreach($variables as $mediaType => $null)
  690. {
  691. foreach($variables[$mediaType] as $variable => $value)
  692. {
  693. // If a var() statement in a variable value found...
  694. if (stripos($value, "var") !== false && preg_match_all("/var\((.+)\)/iSU", $value, $m))
  695. {
  696. // ... then replace the var() statement with the variable values.
  697. for ($i = 0, $l = count($m[0]); $i < $l; $i++)
  698. {
  699. $variables[$mediaType][$variable] = str_replace($m[0][$i], (isset($variables[$mediaType][$m[1][$i]]) ? $variables[$mediaType][$m[1][$i]] : ""), $variables[$mediaType][$variable]);
  700. }
  701. }
  702. }
  703. }
  704. // Remove the complete @variables at-rule block
  705. foreach ($remove as $i)
  706. {
  707. $tokens[$i] = null;
  708. }
  709. if (!($plugin = $this->minifier->getPlugin("CssVariablesMinifierPlugin")))
  710. {
  711. CssMin::triggerError(new CssError(__FILE__, __LINE__, __METHOD__ . ": The plugin <code>CssVariablesMinifierPlugin</code> was not found but is required for <code>" . __CLASS__ . "</code>"));
  712. }
  713. else
  714. {
  715. $plugin->setVariables($variables);
  716. }
  717. return count($remove);
  718. }
  719. }
  720. /**
  721. * {@link aCssParserPlugin Parser plugin} for preserve parsing url() values.
  722. *
  723. * This plugin return no {@link aCssToken CssToken} but ensures that url() values will get parsed properly.
  724. *
  725. * @package CssMin/Parser/Plugins
  726. * @link http://code.google.com/p/cssmin/
  727. * @author Joe Scylla <joe.scylla@gmail.com>
  728. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  729. * @license http://opensource.org/licenses/mit-license.php MIT License
  730. * @version 3.0.1
  731. */
  732. class CssUrlParserPlugin extends aCssParserPlugin
  733. {
  734. /**
  735. * Implements {@link aCssParserPlugin::getTriggerChars()}.
  736. *
  737. * @return array
  738. */
  739. public function getTriggerChars()
  740. {
  741. return array("(", ")");
  742. }
  743. /**
  744. * Implements {@link aCssParserPlugin::getTriggerStates()}.
  745. *
  746. * @return array
  747. */
  748. public function getTriggerStates()
  749. {
  750. return false;
  751. }
  752. /**
  753. * Implements {@link aCssParserPlugin::parse()}.
  754. *
  755. * @param integer $index Current index
  756. * @param string $char Current char
  757. * @param string $previousChar Previous char
  758. * @return mixed TRUE will break the processing; FALSE continue with the next plugin; integer set a new index and break the processing
  759. */
  760. public function parse($index, $char, $previousChar, $state)
  761. {
  762. // Start of string
  763. if ($char === "(" && strtolower(substr($this->parser->getSource(), $index - 3, 4)) === "url(" && $state !== "T_URL")
  764. {
  765. $this->parser->pushState("T_URL");
  766. $this->parser->setExclusive(__CLASS__);
  767. }
  768. // Escaped LF in url => remove escape backslash and LF
  769. elseif ($char === "\n" && $previousChar === "\\" && $state === "T_URL")
  770. {
  771. $this->parser->setBuffer(substr($this->parser->getBuffer(), 0, -2));
  772. }
  773. // Parse error: Unescaped LF in string literal
  774. elseif ($char === "\n" && $previousChar !== "\\" && $state === "T_URL")
  775. {
  776. $line = $this->parser->getBuffer();
  777. $this->parser->setBuffer(substr($this->parser->getBuffer(), 0, -1) . ")"); // Replace the LF with the url string delimiter
  778. $this->parser->popState();
  779. $this->parser->unsetExclusive();
  780. CssMin::triggerError(new CssError(__FILE__, __LINE__, __METHOD__ . ": Unterminated string literal", $line . "_"));
  781. }
  782. // End of string
  783. elseif ($char === ")" && $state === "T_URL")
  784. {
  785. $this->parser->popState();
  786. $this->parser->unsetExclusive();
  787. }
  788. else
  789. {
  790. return false;
  791. }
  792. return true;
  793. }
  794. }
  795. /**
  796. * {@link aCssParserPlugin Parser plugin} for preserve parsing string values.
  797. *
  798. * This plugin return no {@link aCssToken CssToken} but ensures that string values will get parsed properly.
  799. *
  800. * @package CssMin/Parser/Plugins
  801. * @link http://code.google.com/p/cssmin/
  802. * @author Joe Scylla <joe.scylla@gmail.com>
  803. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  804. * @license http://opensource.org/licenses/mit-license.php MIT License
  805. * @version 3.0.1
  806. */
  807. class CssStringParserPlugin extends aCssParserPlugin
  808. {
  809. /**
  810. * Current string delimiter char.
  811. *
  812. * @var string
  813. */
  814. private $delimiterChar = null;
  815. /**
  816. * Implements {@link aCssParserPlugin::getTriggerChars()}.
  817. *
  818. * @return array
  819. */
  820. public function getTriggerChars()
  821. {
  822. return array("\"", "'", "\n");
  823. }
  824. /**
  825. * Implements {@link aCssParserPlugin::getTriggerStates()}.
  826. *
  827. * @return array
  828. */
  829. public function getTriggerStates()
  830. {
  831. return false;
  832. }
  833. /**
  834. * Implements {@link aCssParserPlugin::parse()}.
  835. *
  836. * @param integer $index Current index
  837. * @param string $char Current char
  838. * @param string $previousChar Previous char
  839. * @return mixed TRUE will break the processing; FALSE continue with the next plugin; integer set a new index and break the processing
  840. */
  841. public function parse($index, $char, $previousChar, $state)
  842. {
  843. // Start of string
  844. if (($char === "\"" || $char === "'") && $state !== "T_STRING")
  845. {
  846. $this->delimiterChar = $char;
  847. $this->parser->pushState("T_STRING");
  848. $this->parser->setExclusive(__CLASS__);
  849. }
  850. // Escaped LF in string => remove escape backslash and LF
  851. elseif ($char === "\n" && $previousChar === "\\" && $state === "T_STRING")
  852. {
  853. $this->parser->setBuffer(substr($this->parser->getBuffer(), 0, -2));
  854. }
  855. // Parse error: Unescaped LF in string literal
  856. elseif ($char === "\n" && $previousChar !== "\\" && $state === "T_STRING")
  857. {
  858. $line = $this->parser->getBuffer();
  859. $this->parser->popState();
  860. $this->parser->unsetExclusive();
  861. $this->parser->setBuffer(substr($this->parser->getBuffer(), 0, -1) . $this->delimiterChar); // Replace the LF with the current string char
  862. CssMin::triggerError(new CssError(__FILE__, __LINE__, __METHOD__ . ": Unterminated string literal", $line . "_"));
  863. $this->delimiterChar = null;
  864. }
  865. // End of string
  866. elseif ($char === $this->delimiterChar && $state === "T_STRING")
  867. {
  868. // If the Previous char is a escape char count the amount of the previous escape chars. If the amount of
  869. // escape chars is uneven do not end the string
  870. if ($previousChar == "\\")
  871. {
  872. $source = $this->parser->getSource();
  873. $c = 1;
  874. $i = $index - 2;
  875. while (substr($source, $i, 1) === "\\")
  876. {
  877. $c++; $i--;
  878. }
  879. if ($c % 2)
  880. {
  881. return false;
  882. }
  883. }
  884. $this->parser->popState();
  885. $this->parser->unsetExclusive();
  886. $this->delimiterChar = null;
  887. }
  888. else
  889. {
  890. return false;
  891. }
  892. return true;
  893. }
  894. }
  895. /**
  896. * This {@link aCssMinifierFilter minifier filter} sorts the ruleset declarations of a ruleset by name.
  897. *
  898. * @package CssMin/Minifier/Filters
  899. * @link http://code.google.com/p/cssmin/
  900. * @author Rowan Beentje <http://assanka.net>
  901. * @copyright Rowan Beentje <http://assanka.net>
  902. * @license http://opensource.org/licenses/mit-license.php MIT License
  903. * @version 3.0.1
  904. */
  905. class CssSortRulesetPropertiesMinifierFilter extends aCssMinifierFilter
  906. {
  907. /**
  908. * Implements {@link aCssMinifierFilter::filter()}.
  909. *
  910. * @param array $tokens Array of objects of type aCssToken
  911. * @return integer Count of added, changed or removed tokens; a return value larger than 0 will rebuild the array
  912. */
  913. public function apply(array &$tokens)
  914. {
  915. $r = 0;
  916. for ($i = 0, $l = count($tokens); $i < $l; $i++)
  917. {
  918. // Only look for ruleset start rules
  919. if (get_class($tokens[$i]) !== "CssRulesetStartToken") { continue; }
  920. // Look for the corresponding ruleset end
  921. $endIndex = false;
  922. for ($ii = $i + 1; $ii < $l; $ii++)
  923. {
  924. if (get_class($tokens[$ii]) !== "CssRulesetEndToken") { continue; }
  925. $endIndex = $ii;
  926. break;
  927. }
  928. if (!$endIndex) { break; }
  929. $startIndex = $i;
  930. $i = $endIndex;
  931. // Skip if there's only one token in this ruleset
  932. if ($endIndex - $startIndex <= 2) { continue; }
  933. // Ensure that everything between the start and end is a declaration token, for safety
  934. for ($ii = $startIndex + 1; $ii < $endIndex; $ii++)
  935. {
  936. if (get_class($tokens[$ii]) !== "CssRulesetDeclarationToken") { continue(2); }
  937. }
  938. $declarations = array_slice($tokens, $startIndex + 1, $endIndex - $startIndex - 1);
  939. // Check whether a sort is required
  940. $sortRequired = $lastPropertyName = false;
  941. foreach ($declarations as $declaration)
  942. {
  943. if ($lastPropertyName)
  944. {
  945. if (strcmp($lastPropertyName, $declaration->Property) > 0)
  946. {
  947. $sortRequired = true;
  948. break;
  949. }
  950. }
  951. $lastPropertyName = $declaration->Property;
  952. }
  953. if (!$sortRequired) { continue; }
  954. // Arrange the declarations alphabetically by name
  955. usort($declarations, array(__CLASS__, "userDefinedSort1"));
  956. // Update "IsLast" property
  957. for ($ii = 0, $ll = count($declarations) - 1; $ii <= $ll; $ii++)
  958. {
  959. if ($ii == $ll)
  960. {
  961. $declarations[$ii]->IsLast = true;
  962. }
  963. else
  964. {
  965. $declarations[$ii]->IsLast = false;
  966. }
  967. }
  968. // Splice back into the array.
  969. array_splice($tokens, $startIndex + 1, $endIndex - $startIndex - 1, $declarations);
  970. $r += $endIndex - $startIndex - 1;
  971. }
  972. return $r;
  973. }
  974. /**
  975. * User defined sort function.
  976. *
  977. * @return integer
  978. */
  979. public static function userDefinedSort1($a, $b)
  980. {
  981. return strcmp($a->Property, $b->Property);
  982. }
  983. }
  984. /**
  985. * This {@link aCssToken CSS token} represents the start of a ruleset.
  986. *
  987. * @package CssMin/Tokens
  988. * @link http://code.google.com/p/cssmin/
  989. * @author Joe Scylla <joe.scylla@gmail.com>
  990. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  991. * @license http://opensource.org/licenses/mit-license.php MIT License
  992. * @version 3.0.1
  993. */
  994. class CssRulesetStartToken extends aCssRulesetStartToken
  995. {
  996. /**
  997. * Array of selectors.
  998. *
  999. * @var array
  1000. */
  1001. public $Selectors = array();
  1002. /**
  1003. * Set the properties of a ruleset token.
  1004. *
  1005. * @param array $selectors Selectors of the ruleset
  1006. * @return void
  1007. */
  1008. public function __construct(array $selectors = array())
  1009. {
  1010. $this->Selectors = $selectors;
  1011. }
  1012. /**
  1013. * Implements {@link aCssToken::__toString()}.
  1014. *
  1015. * @return string
  1016. */
  1017. public function __toString()
  1018. {
  1019. return implode(",", $this->Selectors) . "{";
  1020. }
  1021. }
  1022. /**
  1023. * {@link aCssParserPlugin Parser plugin} for parsing ruleset block with including declarations.
  1024. *
  1025. * Found rulesets will add a {@link CssRulesetStartToken} and {@link CssRulesetEndToken} to the
  1026. * parser; including declarations as {@link CssRulesetDeclarationToken}.
  1027. *
  1028. * @package CssMin/Parser/Plugins
  1029. * @link http://code.google.com/p/cssmin/
  1030. * @author Joe Scylla <joe.scylla@gmail.com>
  1031. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  1032. * @license http://opensource.org/licenses/mit-license.php MIT License
  1033. * @version 3.0.1
  1034. */
  1035. class CssRulesetParserPlugin extends aCssParserPlugin
  1036. {
  1037. /**
  1038. * Implements {@link aCssParserPlugin::getTriggerChars()}.
  1039. *
  1040. * @return array
  1041. */
  1042. public function getTriggerChars()
  1043. {
  1044. return array(",", "{", "}", ":", ";");
  1045. }
  1046. /**
  1047. * Implements {@link aCssParserPlugin::getTriggerStates()}.
  1048. *
  1049. * @return array
  1050. */
  1051. public function getTriggerStates()
  1052. {
  1053. return array("T_DOCUMENT", "T_AT_MEDIA", "T_RULESET::SELECTORS", "T_RULESET", "T_RULESET_DECLARATION");
  1054. }
  1055. /**
  1056. * Selectors.
  1057. *
  1058. * @var array
  1059. */
  1060. private $selectors = array();
  1061. /**
  1062. * Implements {@link aCssParserPlugin::parse()}.
  1063. *
  1064. * @param integer $index Current index
  1065. * @param string $char Current char
  1066. * @param string $previousChar Previous char
  1067. * @return mixed TRUE will break the processing; FALSE continue with the next plugin; integer set a new index and break the processing
  1068. */
  1069. public function parse($index, $char, $previousChar, $state)
  1070. {
  1071. // Start of Ruleset and selectors
  1072. if ($char === "," && ($state === "T_DOCUMENT" || $state === "T_AT_MEDIA" || $state === "T_RULESET::SELECTORS"))
  1073. {
  1074. if ($state !== "T_RULESET::SELECTORS")
  1075. {
  1076. $this->parser->pushState("T_RULESET::SELECTORS");
  1077. }
  1078. $this->selectors[] = $this->parser->getAndClearBuffer(",{");
  1079. }
  1080. // End of selectors and start of declarations
  1081. elseif ($char === "{" && ($state === "T_DOCUMENT" || $state === "T_AT_MEDIA" || $state === "T_RULESET::SELECTORS"))
  1082. {
  1083. if ($this->parser->getBuffer() !== "")
  1084. {
  1085. $this->selectors[] = $this->parser->getAndClearBuffer(",{");
  1086. if ($state == "T_RULESET::SELECTORS")
  1087. {
  1088. $this->parser->popState();
  1089. }
  1090. $this->parser->pushState("T_RULESET");
  1091. $this->parser->appendToken(new CssRulesetStartToken($this->selectors));
  1092. $this->selectors = array();
  1093. }
  1094. }
  1095. // Start of declaration
  1096. elseif ($char === ":" && $state === "T_RULESET")
  1097. {
  1098. $this->parser->pushState("T_RULESET_DECLARATION");
  1099. $this->buffer = $this->parser->getAndClearBuffer(":;", true);
  1100. }
  1101. // Unterminated ruleset declaration
  1102. elseif ($char === ":" && $state === "T_RULESET_DECLARATION")
  1103. {
  1104. // Ignore Internet Explorer filter declarations
  1105. if ($this->buffer === "filter")
  1106. {
  1107. return false;
  1108. }
  1109. CssMin::triggerError(new CssError(__FILE__, __LINE__, __METHOD__ . ": Unterminated declaration", $this->buffer . ":" . $this->parser->getBuffer() . "_"));
  1110. }
  1111. // End of declaration
  1112. elseif (($char === ";" || $char === "}") && $state === "T_RULESET_DECLARATION")
  1113. {
  1114. $value = $this->parser->getAndClearBuffer(";}");
  1115. if (strtolower(substr($value, -10, 10)) === "!important")
  1116. {
  1117. $value = trim(substr($value, 0, -10));
  1118. $isImportant = true;
  1119. }
  1120. else
  1121. {
  1122. $isImportant = false;
  1123. }
  1124. $this->parser->popState();
  1125. $this->parser->appendToken(new CssRulesetDeclarationToken($this->buffer, $value, $this->parser->getMediaTypes(), $isImportant));
  1126. // Declaration ends with a right curly brace; so we have to end the ruleset
  1127. if ($char === "}")
  1128. {
  1129. $this->parser->appendToken(new CssRulesetEndToken());
  1130. $this->parser->popState();
  1131. }
  1132. $this->buffer = "";
  1133. }
  1134. // End of ruleset
  1135. elseif ($char === "}" && $state === "T_RULESET")
  1136. {
  1137. $this->parser->popState();
  1138. $this->parser->clearBuffer();
  1139. $this->parser->appendToken(new CssRulesetEndToken());
  1140. $this->buffer = "";
  1141. $this->selectors = array();
  1142. }
  1143. else
  1144. {
  1145. return false;
  1146. }
  1147. return true;
  1148. }
  1149. }
  1150. /**
  1151. * This {@link aCssToken CSS token} represents the end of a ruleset.
  1152. *
  1153. * @package CssMin/Tokens
  1154. * @link http://code.google.com/p/cssmin/
  1155. * @author Joe Scylla <joe.scylla@gmail.com>
  1156. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  1157. * @license http://opensource.org/licenses/mit-license.php MIT License
  1158. * @version 3.0.1
  1159. */
  1160. class CssRulesetEndToken extends aCssRulesetEndToken
  1161. {
  1162. }
  1163. /**
  1164. * This {@link aCssToken CSS token} represents a ruleset declaration.
  1165. *
  1166. * @package CssMin/Tokens
  1167. * @link http://code.google.com/p/cssmin/
  1168. * @author Joe Scylla <joe.scylla@gmail.com>
  1169. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  1170. * @license http://opensource.org/licenses/mit-license.php MIT License
  1171. * @version 3.0.1
  1172. */
  1173. class CssRulesetDeclarationToken extends aCssDeclarationToken
  1174. {
  1175. /**
  1176. * Media types of the declaration.
  1177. *
  1178. * @var array
  1179. */
  1180. public $MediaTypes = array("all");
  1181. /**
  1182. * Set the properties of a ddocument- or at-rule @media level declaration.
  1183. *
  1184. * @param string $property Property of the declaration
  1185. * @param string $value Value of the declaration
  1186. * @param mixed $mediaTypes Media types of the declaration
  1187. * @param boolean $isImportant Is the !important flag is set
  1188. * @param boolean $isLast Is the declaration the last one of the ruleset
  1189. * @return void
  1190. */
  1191. public function __construct($property, $value, $mediaTypes = null, $isImportant = false, $isLast = false)
  1192. {
  1193. parent::__construct($property, $value, $isImportant, $isLast);
  1194. $this->MediaTypes = $mediaTypes ? $mediaTypes : array("all");
  1195. }
  1196. }
  1197. /**
  1198. * This {@link aCssMinifierFilter minifier filter} sets the IsLast property of any last declaration in a ruleset,
  1199. * @font-face at-rule or @page at-rule block. If the property IsLast is TRUE the decrations will get stringified
  1200. * without tailing semicolon.
  1201. *
  1202. * @package CssMin/Minifier/Filters
  1203. * @link http://code.google.com/p/cssmin/
  1204. * @author Joe Scylla <joe.scylla@gmail.com>
  1205. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  1206. * @license http://opensource.org/licenses/mit-license.php MIT License
  1207. * @version 3.0.1
  1208. */
  1209. class CssRemoveLastDelarationSemiColonMinifierFilter extends aCssMinifierFilter
  1210. {
  1211. /**
  1212. * Implements {@link aCssMinifierFilter::filter()}.
  1213. *
  1214. * @param array $tokens Array of objects of type aCssToken
  1215. * @return integer Count of added, changed or removed tokens; a return value large than 0 will rebuild the array
  1216. */
  1217. public function apply(array &$tokens)
  1218. {
  1219. for ($i = 0, $l = count($tokens); $i < $l; $i++)
  1220. {
  1221. $current = get_class($tokens[$i]);
  1222. $next = isset($tokens[$i+1]) ? get_class($tokens[$i+1]) : false;
  1223. if (($current === "CssRulesetDeclarationToken" && $next === "CssRulesetEndToken") ||
  1224. ($current === "CssAtFontFaceDeclarationToken" && $next === "CssAtFontFaceEndToken") ||
  1225. ($current === "CssAtPageDeclarationToken" && $next === "CssAtPageEndToken"))
  1226. {
  1227. $tokens[$i]->IsLast = true;
  1228. }
  1229. }
  1230. return 0;
  1231. }
  1232. }
  1233. /**
  1234. * This {@link aCssMinifierFilter minifier filter} will remove any empty rulesets (including @keyframes at-rule block
  1235. * rulesets).
  1236. *
  1237. * @package CssMin/Minifier/Filters
  1238. * @link http://code.google.com/p/cssmin/
  1239. * @author Joe Scylla <joe.scylla@gmail.com>
  1240. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  1241. * @license http://opensource.org/licenses/mit-license.php MIT License
  1242. * @version 3.0.1
  1243. */
  1244. class CssRemoveEmptyRulesetsMinifierFilter extends aCssMinifierFilter
  1245. {
  1246. /**
  1247. * Implements {@link aCssMinifierFilter::filter()}.
  1248. *
  1249. * @param array $tokens Array of objects of type aCssToken
  1250. * @return integer Count of added, changed or removed tokens; a return value large than 0 will rebuild the array
  1251. */
  1252. public function apply(array &$tokens)
  1253. {
  1254. $r = 0;
  1255. for ($i = 0, $l = count($tokens); $i < $l; $i++)
  1256. {
  1257. $current = get_class($tokens[$i]);
  1258. $next = isset($tokens[$i + 1]) ? get_class($tokens[$i + 1]) : false;
  1259. if (($current === "CssRulesetStartToken" && $next === "CssRulesetEndToken") ||
  1260. ($current === "CssAtKeyframesRulesetStartToken" && $next === "CssAtKeyframesRulesetEndToken" && !array_intersect(array("from", "0%", "to", "100%"), array_map("strtolower", $tokens[$i]->Selectors)))
  1261. )
  1262. {
  1263. $tokens[$i] = null;
  1264. $tokens[$i + 1] = null;
  1265. $i++;
  1266. $r = $r + 2;
  1267. }
  1268. }
  1269. return $r;
  1270. }
  1271. }
  1272. /**
  1273. * This {@link aCssMinifierFilter minifier filter} will remove any empty @font-face, @keyframes, @media and @page
  1274. * at-rule blocks.
  1275. *
  1276. * @package CssMin/Minifier/Filters
  1277. * @link http://code.google.com/p/cssmin/
  1278. * @author Joe Scylla <joe.scylla@gmail.com>
  1279. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  1280. * @license http://opensource.org/licenses/mit-license.php MIT License
  1281. * @version 3.0.1
  1282. */
  1283. class CssRemoveEmptyAtBlocksMinifierFilter extends aCssMinifierFilter
  1284. {
  1285. /**
  1286. * Implements {@link aCssMinifierFilter::filter()}.
  1287. *
  1288. * @param array $tokens Array of objects of type aCssToken
  1289. * @return integer Count of added, changed or removed tokens; a return value large than 0 will rebuild the array
  1290. */
  1291. public function apply(array &$tokens)
  1292. {
  1293. $r = 0;
  1294. for ($i = 0, $l = count($tokens); $i < $l; $i++)
  1295. {
  1296. $current = get_class($tokens[$i]);
  1297. $next = isset($tokens[$i + 1]) ? get_class($tokens[$i + 1]) : false;
  1298. if (($current === "CssAtFontFaceStartToken" && $next === "CssAtFontFaceEndToken") ||
  1299. ($current === "CssAtKeyframesStartToken" && $next === "CssAtKeyframesEndToken") ||
  1300. ($current === "CssAtPageStartToken" && $next === "CssAtPageEndToken") ||
  1301. ($current === "CssAtMediaStartToken" && $next === "CssAtMediaEndToken"))
  1302. {
  1303. $tokens[$i] = null;
  1304. $tokens[$i + 1] = null;
  1305. $i++;
  1306. $r = $r + 2;
  1307. }
  1308. }
  1309. return $r;
  1310. }
  1311. }
  1312. /**
  1313. * This {@link aCssMinifierFilter minifier filter} will remove any comments from the array of parsed tokens.
  1314. *
  1315. * @package CssMin/Minifier/Filters
  1316. * @link http://code.google.com/p/cssmin/
  1317. * @author Joe Scylla <joe.scylla@gmail.com>
  1318. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  1319. * @license http://opensource.org/licenses/mit-license.php MIT License
  1320. * @version 3.0.1
  1321. */
  1322. class CssRemoveCommentsMinifierFilter extends aCssMinifierFilter
  1323. {
  1324. /**
  1325. * Implements {@link aCssMinifierFilter::filter()}.
  1326. *
  1327. * @param array $tokens Array of objects of type aCssToken
  1328. * @return integer Count of added, changed or removed tokens; a return value large than 0 will rebuild the array
  1329. */
  1330. public function apply(array &$tokens)
  1331. {
  1332. $r = 0;
  1333. for ($i = 0, $l = count($tokens); $i < $l; $i++)
  1334. {
  1335. if (get_class($tokens[$i]) === "CssCommentToken")
  1336. {
  1337. $tokens[$i] = null;
  1338. $r++;
  1339. }
  1340. }
  1341. return $r;
  1342. }
  1343. }
  1344. /**
  1345. * CSS Parser.
  1346. *
  1347. * @package CssMin/Parser
  1348. * @link http://code.google.com/p/cssmin/
  1349. * @author Joe Scylla <joe.scylla@gmail.com>
  1350. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  1351. * @license http://opensource.org/licenses/mit-license.php MIT License
  1352. * @version 3.0.1
  1353. */
  1354. class CssParser
  1355. {
  1356. /**
  1357. * Parse buffer.
  1358. *
  1359. * @var string
  1360. */
  1361. private $buffer = "";
  1362. /**
  1363. * {@link aCssParserPlugin Plugins}.
  1364. *
  1365. * @var array
  1366. */
  1367. private $plugins = array();
  1368. /**
  1369. * Source to parse.
  1370. *
  1371. * @var string
  1372. */
  1373. private $source = "";
  1374. /**
  1375. * Current state.
  1376. *
  1377. * @var integer
  1378. */
  1379. private $state = "T_DOCUMENT";
  1380. /**
  1381. * Exclusive state.
  1382. *
  1383. * @var string
  1384. */
  1385. private $stateExclusive = false;
  1386. /**
  1387. * Media types state.
  1388. *
  1389. * @var mixed
  1390. */
  1391. private $stateMediaTypes = false;
  1392. /**
  1393. * State stack.
  1394. *
  1395. * @var array
  1396. */
  1397. private $states = array("T_DOCUMENT");
  1398. /**
  1399. * Parsed tokens.
  1400. *
  1401. * @var array
  1402. */
  1403. private $tokens = array();
  1404. /**
  1405. * Constructer.
  1406. *
  1407. * Create instances of the used {@link aCssParserPlugin plugins}.
  1408. *
  1409. * @param string $source CSS source [optional]
  1410. * @param array $plugins Plugin configuration [optional]
  1411. * @return void
  1412. */
  1413. public function __construct($source = null, array $plugins = null)
  1414. {
  1415. $plugins = array_merge(array
  1416. (
  1417. "Comment" => true,
  1418. "String" => true,
  1419. "Url" => true,
  1420. "Expression" => true,
  1421. "Ruleset" => true,
  1422. "AtCharset" => true,
  1423. "AtFontFace" => true,
  1424. "AtImport" => true,
  1425. "AtKeyframes" => true,
  1426. "AtMedia" => true,
  1427. "AtPage" => true,
  1428. "AtVariables" => true
  1429. ), is_array($plugins) ? $plugins : array());
  1430. // Create plugin instances
  1431. foreach ($plugins as $name => $config)
  1432. {
  1433. if ($config !== false)
  1434. {
  1435. $class = "Css" . $name . "ParserPlugin";
  1436. $config = is_array($config) ? $config : array();
  1437. if (class_exists($class))
  1438. {
  1439. $this->plugins[] = new $class($this, $config);
  1440. }
  1441. else
  1442. {
  1443. CssMin::triggerError(new CssError(__FILE__, __LINE__, __METHOD__ . ": The plugin <code>" . $name . "</code> with the class name <code>" . $class . "</code> was not found"));
  1444. }
  1445. }
  1446. }
  1447. if (!is_null($source))
  1448. {
  1449. $this->parse($source);
  1450. }
  1451. }
  1452. /**
  1453. * Append a token to the array of tokens.
  1454. *
  1455. * @param aCssToken $token Token to append
  1456. * @return void
  1457. */
  1458. public function appendToken(aCssToken $token)
  1459. {
  1460. $this->tokens[] = $token;
  1461. }
  1462. /**
  1463. * Clears the current buffer.
  1464. *
  1465. * @return void
  1466. */
  1467. public function clearBuffer()
  1468. {
  1469. $this->buffer = "";
  1470. }
  1471. /**
  1472. * Returns and clear the current buffer.
  1473. *
  1474. * @param string $trim Chars to use to trim the returned buffer
  1475. * @param boolean $tolower if TRUE the returned buffer will get converted to lower case
  1476. * @return string
  1477. */
  1478. public function getAndClearBuffer($trim = "", $tolower = false)
  1479. {
  1480. $r = $this->getBuffer($trim, $tolower);
  1481. $this->buffer = "";
  1482. return $r;
  1483. }
  1484. /**
  1485. * Returns the current buffer.
  1486. *
  1487. * @param string $trim Chars to use to trim the returned buffer
  1488. * @param boolean $tolower if TRUE the returned buffer will get converted to lower case
  1489. * @return string
  1490. */
  1491. public function getBuffer($trim = "", $tolower = false)
  1492. {
  1493. $r = $this->buffer;
  1494. if ($trim)
  1495. {
  1496. $r = trim($r, " \t\n\r\0\x0B" . $trim);
  1497. }
  1498. if ($tolower)
  1499. {
  1500. $r = strtolower($r);
  1501. }
  1502. return $r;
  1503. }
  1504. /**
  1505. * Returns the current media types state.
  1506. *
  1507. * @return array
  1508. */
  1509. public function getMediaTypes()
  1510. {
  1511. return $this->stateMediaTypes;
  1512. }
  1513. /**
  1514. * Returns the CSS source.
  1515. *
  1516. * @return string
  1517. */
  1518. public function getSource()
  1519. {
  1520. return $this->source;
  1521. }
  1522. /**
  1523. * Returns the current state.
  1524. *
  1525. * @return integer The current state
  1526. */
  1527. public function getState()
  1528. {
  1529. return $this->state;
  1530. }
  1531. /**
  1532. * Returns a plugin by class name.
  1533. *
  1534. * @param string $name Class name of the plugin
  1535. * @return aCssParserPlugin
  1536. */
  1537. public function getPlugin($class)
  1538. {
  1539. static $index = null;
  1540. if (is_null($index))
  1541. {
  1542. $index = array();
  1543. for ($i = 0, $l = count($this->plugins); $i < $l; $i++)
  1544. {
  1545. $index[get_class($this->plugins[$i])] = $i;
  1546. }
  1547. }
  1548. return isset($index[$class]) ? $this->plugins[$index[$class]] : false;
  1549. }
  1550. /**
  1551. * Returns the parsed tokens.
  1552. *
  1553. * @return array
  1554. */
  1555. public function getTokens()
  1556. {
  1557. return $this->tokens;
  1558. }
  1559. /**
  1560. * Returns if the current state equals the passed state.
  1561. *
  1562. * @param integer $state State to compare with the current state
  1563. * @return boolean TRUE is the state equals to the passed state; FALSE if not
  1564. */
  1565. public function isState($state)
  1566. {
  1567. return ($this->state == $state);
  1568. }
  1569. /**
  1570. * Parse the CSS source and return a array with parsed tokens.
  1571. *
  1572. * @param string $source CSS source
  1573. * @return array Array with tokens
  1574. */
  1575. public function parse($source)
  1576. {
  1577. // Reset
  1578. $this->source = "";
  1579. $this->tokens = array();
  1580. // Create a global and plugin lookup table for trigger chars; set array of plugins as local variable and create
  1581. // several helper variables for plugin handling
  1582. $globalTriggerChars = "";
  1583. $plugins = $this->plugins;
  1584. $pluginCount = count($plugins);
  1585. $pluginIndex = array();
  1586. $pluginTriggerStates = array();
  1587. $pluginTriggerChars = array();
  1588. for ($i = 0, $l = count($plugins); $i < $l; $i++)
  1589. {
  1590. $tPluginClassName = get_class($plugins[$i]);
  1591. $pluginTriggerChars[$i] = implode("", $plugins[$i]->getTriggerChars());
  1592. $tPluginTriggerStates = $plugins[$i]->getTriggerStates();
  1593. $pluginTriggerStates[$i] = $tPluginTriggerStates === false ? false : "|" . implode("|", $tPluginTriggerStates) . "|";
  1594. $pluginIndex[$tPluginClassName] = $i;
  1595. for ($ii = 0, $ll = strlen($pluginTriggerChars[$i]); $ii < $ll; $ii++)
  1596. {
  1597. $c = substr($pluginTriggerChars[$i], $ii, 1);
  1598. if (strpos($globalTriggerChars, $c) === false)
  1599. {
  1600. $globalTriggerChars .= $c;
  1601. }
  1602. }
  1603. }
  1604. // Normalise line endings
  1605. $source = str_replace("\r\n", "\n", $source); // Windows to Unix line endings
  1606. $source = str_replace("\r", "\n", $source); // Mac to Unix line endings
  1607. $this->source = $source;
  1608. // Variables
  1609. $buffer = &$this->buffer;
  1610. $exclusive = &$this->stateExclusive;
  1611. $state = &$this->state;
  1612. $c = $p = null;
  1613. // --
  1614. for ($i = 0, $l = strlen($source); $i < $l; $i++)
  1615. {
  1616. // Set the current Char
  1617. $c = $source[$i]; // Is faster than: $c = substr($source, $i, 1);
  1618. // Normalize and filter double whitespace characters
  1619. if ($exclusive === false)
  1620. {
  1621. if ($c === "\n" || $c === "\t")
  1622. {
  1623. $c = " ";
  1624. }
  1625. if ($c === " " && $p === " ")
  1626. {
  1627. continue;
  1628. }
  1629. }
  1630. $buffer .= $c;
  1631. // Extended processing only if the current char is a global trigger char
  1632. if (strpos($globalTriggerChars, $c) !== false)
  1633. {
  1634. // Exclusive state is set; process with the exclusive plugin
  1635. if ($exclusive)
  1636. {
  1637. $tPluginIndex = $pluginIndex[$exclusive];
  1638. if (strpos($pluginTriggerChars[$tPluginIndex], $c) !== false && ($pluginTriggerStates[$tPluginIndex] === false || strpos($pluginTriggerStates[$tPluginIndex], $state) !== false))
  1639. {
  1640. $r = $plugins[$tPluginIndex]->parse($i, $c, $p, $state);
  1641. // Return value is TRUE => continue with next char
  1642. if ($r === true)
  1643. {
  1644. continue;
  1645. }
  1646. // Return value is numeric => set new index and continue with next char
  1647. elseif ($r !== false && $r != $i)
  1648. {
  1649. $i = $r;
  1650. continue;
  1651. }
  1652. }
  1653. }
  1654. // Else iterate through the plugins
  1655. else
  1656. {
  1657. $triggerState = "|" . $state . "|";
  1658. for ($ii = 0, $ll = $pluginCount; $ii < $ll; $ii++)
  1659. {
  1660. // Only process if the current char is one of the plugin trigger chars
  1661. if (strpos($pluginTriggerChars[$ii], $c) !== false && ($pluginTriggerStates[$ii] === false || strpos($pluginTriggerStates[$ii], $triggerState) !== false))
  1662. {
  1663. // Process with the plugin
  1664. $r = $plugins[$ii]->parse($i, $c, $p, $state);
  1665. // Return value is TRUE => break the plugin loop and and continue with next char
  1666. if ($r === true)
  1667. {
  1668. break;
  1669. }
  1670. // Return value is numeric => set new index, break the plugin loop and and continue with next char
  1671. elseif ($r !== false && $r != $i)
  1672. {
  1673. $i = $r;
  1674. break;
  1675. }
  1676. }
  1677. }
  1678. }
  1679. }
  1680. $p = $c; // Set the parent char
  1681. }
  1682. return $this->tokens;
  1683. }
  1684. /**
  1685. * Remove the last state of the state stack and return the removed stack value.
  1686. *
  1687. * @return integer Removed state value
  1688. */
  1689. public function popState()
  1690. {
  1691. $r = array_pop($this->states);
  1692. $this->state = $this->states[count($this->states) - 1];
  1693. return $r;
  1694. }
  1695. /**
  1696. * Adds a new state onto the state stack.
  1697. *
  1698. * @param integer $state State to add onto the state stack.
  1699. * @return integer The index of the added state in the state stacks
  1700. */
  1701. public function pushState($state)
  1702. {
  1703. $r = array_push($this->states, $state);
  1704. $this->state = $this->states[count($this->states) - 1];
  1705. return $r;
  1706. }
  1707. /**
  1708. * Sets/restores the buffer.
  1709. *
  1710. * @param string $buffer Buffer to set
  1711. * @return void
  1712. */
  1713. public function setBuffer($buffer)
  1714. {
  1715. $this->buffer = $buffer;
  1716. }
  1717. /**
  1718. * Set the exclusive state.
  1719. *
  1720. * @param string $exclusive Exclusive state
  1721. * @return void
  1722. */
  1723. public function setExclusive($exclusive)
  1724. {
  1725. $this->stateExclusive = $exclusive;
  1726. }
  1727. /**
  1728. * Set the media types state.
  1729. *
  1730. * @param array $mediaTypes Media types state
  1731. * @return void
  1732. */
  1733. public function setMediaTypes(array $mediaTypes)
  1734. {
  1735. $this->stateMediaTypes = $mediaTypes;
  1736. }
  1737. /**
  1738. * Sets the current state in the state stack; equals to {@link CssParser::popState()} + {@link CssParser::pushState()}.
  1739. *
  1740. * @param integer $state State to set
  1741. * @return integer
  1742. */
  1743. public function setState($state)
  1744. {
  1745. $r = array_pop($this->states);
  1746. array_push($this->states, $state);
  1747. $this->state = $this->states[count($this->states) - 1];
  1748. return $r;
  1749. }
  1750. /**
  1751. * Removes the exclusive state.
  1752. *
  1753. * @return void
  1754. */
  1755. public function unsetExclusive()
  1756. {
  1757. $this->stateExclusive = false;
  1758. }
  1759. /**
  1760. * Removes the media types state.
  1761. *
  1762. * @return void
  1763. */
  1764. public function unsetMediaTypes()
  1765. {
  1766. $this->stateMediaTypes = false;
  1767. }
  1768. }
  1769. /**
  1770. * {@link aCssFromatter Formatter} returning the CSS source in {@link http://goo.gl/j4XdU OTBS indent style} (The One True Brace Style).
  1771. *
  1772. * @package CssMin/Formatter
  1773. * @link http://code.google.com/p/cssmin/
  1774. * @author Joe Scylla <joe.scylla@gmail.com>
  1775. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  1776. * @license http://opensource.org/licenses/mit-license.php MIT License
  1777. * @version 3.0.1
  1778. */
  1779. class CssOtbsFormatter extends aCssFormatter
  1780. {
  1781. /**
  1782. * Implements {@link aCssFormatter::__toString()}.
  1783. *
  1784. * @return string
  1785. */
  1786. public function __toString()
  1787. {
  1788. $r = array();
  1789. $level = 0;
  1790. for ($i = 0, $l = count($this->tokens); $i < $l; $i++)
  1791. {
  1792. $token = $this->tokens[$i];
  1793. $class = get_class($token);
  1794. $indent = str_repeat($this->indent, $level);
  1795. if ($class === "CssCommentToken")
  1796. {
  1797. $lines = array_map("trim", explode("\n", $token->Comment));
  1798. for ($ii = 0, $ll = count($lines); $ii < $ll; $ii++)
  1799. {
  1800. $r[] = $indent . (substr($lines[$ii], 0, 1) == "*" ? " " : "") . $lines[$ii];
  1801. }
  1802. }
  1803. elseif ($class === "CssAtCharsetToken")
  1804. {
  1805. $r[] = $indent . "@charset " . $token->Charset . ";";
  1806. }
  1807. elseif ($class === "CssAtFontFaceStartToken")
  1808. {
  1809. $r[] = $indent . "@font-face {";
  1810. $level++;
  1811. }
  1812. elseif ($class === "CssAtImportToken")
  1813. {
  1814. $r[] = $indent . "@import " . $token->Import . " " . implode(", ", $token->MediaTypes) . ";";
  1815. }
  1816. elseif ($class === "CssAtKeyframesStartToken")
  1817. {
  1818. $r[] = $indent . "@keyframes \"" . $token->Name . "\" {";
  1819. $level++;
  1820. }
  1821. elseif ($class === "CssAtMediaStartToken")
  1822. {
  1823. $r[] = $indent . "@media " . implode(", ", $token->MediaTypes) . " {";
  1824. $level++;
  1825. }
  1826. elseif ($class === "CssAtPageStartToken")
  1827. {
  1828. $r[] = $indent . "@page {";
  1829. $level++;
  1830. }
  1831. elseif ($class === "CssAtVariablesStartToken")
  1832. {
  1833. $r[] = $indent . "@variables " . implode(", ", $token->MediaTypes) . " {";
  1834. $level++;
  1835. }
  1836. elseif ($class === "CssRulesetStartToken" || $class === "CssAtKeyframesRulesetStartToken")
  1837. {
  1838. $r[] = $indent . implode(", ", $token->Selectors) . " {";
  1839. $level++;
  1840. }
  1841. elseif ($class == "CssAtFontFaceDeclarationToken"
  1842. || $class === "CssAtKeyframesRulesetDeclarationToken"
  1843. || $class === "CssAtPageDeclarationToken"
  1844. || $class == "CssAtVariablesDeclarationToken"
  1845. || $class === "CssRulesetDeclarationToken"
  1846. )
  1847. {
  1848. $declaration = $indent . $token->Property . ": ";
  1849. if ($this->padding)
  1850. {
  1851. $declaration = str_pad($declaration, $this->padding, " ", STR_PAD_RIGHT);
  1852. }
  1853. $r[] = $declaration . $token->Value . ($token->IsImportant ? " !important" : "") . ";";
  1854. }
  1855. elseif ($class === "CssAtFontFaceEndToken"
  1856. || $class === "CssAtMediaEndToken"
  1857. || $class === "CssAtKeyframesEndToken"
  1858. || $class === "CssAtKeyframesRulesetEndToken"
  1859. || $class === "CssAtPageEndToken"
  1860. || $class === "CssAtVariablesEndToken"
  1861. || $class === "CssRulesetEndToken"
  1862. )
  1863. {
  1864. $level--;
  1865. $r[] = str_repeat($indent, $level) . "}";
  1866. }
  1867. }
  1868. return implode("\n", $r);
  1869. }
  1870. }
  1871. /**
  1872. * This {@link aCssToken CSS token} is a utility token that extends {@link aNullToken} and returns only a empty string.
  1873. *
  1874. * @package CssMin/Tokens
  1875. * @link http://code.google.com/p/cssmin/
  1876. * @author Joe Scylla <joe.scylla@gmail.com>
  1877. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  1878. * @license http://opensource.org/licenses/mit-license.php MIT License
  1879. * @version 3.0.1
  1880. */
  1881. class CssNullToken extends aCssToken
  1882. {
  1883. /**
  1884. * Implements {@link aCssToken::__toString()}.
  1885. *
  1886. * @return string
  1887. */
  1888. public function __toString()
  1889. {
  1890. return "";
  1891. }
  1892. }
  1893. /**
  1894. * CSS Minifier.
  1895. *
  1896. * @package CssMin/Minifier
  1897. * @link http://code.google.com/p/cssmin/
  1898. * @author Joe Scylla <joe.scylla@gmail.com>
  1899. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  1900. * @license http://opensource.org/licenses/mit-license.php MIT License
  1901. * @version 3.0.1
  1902. */
  1903. class CssMinifier
  1904. {
  1905. /**
  1906. * {@link aCssMinifierFilter Filters}.
  1907. *
  1908. * @var array
  1909. */
  1910. private $filters = array();
  1911. /**
  1912. * {@link aCssMinifierPlugin Plugins}.
  1913. *
  1914. * @var array
  1915. */
  1916. private $plugins = array();
  1917. /**
  1918. * Minified source.
  1919. *
  1920. * @var string
  1921. */
  1922. private $minified = "";
  1923. /**
  1924. * Constructer.
  1925. *
  1926. * Creates instances of {@link aCssMinifierFilter filters} and {@link aCssMinifierPlugin plugins}.
  1927. *
  1928. * @param string $source CSS source [optional]
  1929. * @param array $filters Filter configuration [optional]
  1930. * @param array $plugins Plugin configuration [optional]
  1931. * @return void
  1932. */
  1933. public function __construct($source = null, array $filters = null, array $plugins = null)
  1934. {
  1935. $filters = array_merge(array
  1936. (
  1937. "ImportImports" => false,
  1938. "RemoveComments" => true,
  1939. "RemoveEmptyRulesets" => true,
  1940. "RemoveEmptyAtBlocks" => true,
  1941. "ConvertLevel3Properties" => false,
  1942. "ConvertLevel3AtKeyframes" => false,
  1943. "Variables" => true,
  1944. "RemoveLastDelarationSemiColon" => true
  1945. ), is_array($filters) ? $filters : array());
  1946. $plugins = array_merge(array
  1947. (
  1948. "Variables" => true,
  1949. "ConvertFontWeight" => false,
  1950. "ConvertHslColors" => false,
  1951. "ConvertRgbColors" => false,
  1952. "ConvertNamedColors" => false,
  1953. "CompressColorValues" => false,
  1954. "CompressUnitValues" => false,
  1955. "CompressExpressionValues" => false
  1956. ), is_array($plugins) ? $plugins : array());
  1957. // Filters
  1958. foreach ($filters as $name => $config)
  1959. {
  1960. if ($config !== false)
  1961. {
  1962. $class = "Css" . $name . "MinifierFilter";
  1963. $config = is_array($config) ? $config : array();
  1964. if (class_exists($class))
  1965. {
  1966. $this->filters[] = new $class($this, $config);
  1967. }
  1968. else
  1969. {
  1970. CssMin::triggerError(new CssError(__FILE__, __LINE__, __METHOD__ . ": The filter <code>" . $name . "</code> with the class name <code>" . $class . "</code> was not found"));
  1971. }
  1972. }
  1973. }
  1974. // Plugins
  1975. foreach ($plugins as $name => $config)
  1976. {
  1977. if ($config !== false)
  1978. {
  1979. $class = "Css" . $name . "MinifierPlugin";
  1980. $config = is_array($config) ? $config : array();
  1981. if (class_exists($class))
  1982. {
  1983. $this->plugins[] = new $class($this, $config);
  1984. }
  1985. else
  1986. {
  1987. CssMin::triggerError(new CssError(__FILE__, __LINE__, __METHOD__ . ": The plugin <code>" . $name . "</code> with the class name <code>" . $class . "</code> was not found"));
  1988. }
  1989. }
  1990. }
  1991. // --
  1992. if (!is_null($source))
  1993. {
  1994. $this->minify($source);
  1995. }
  1996. }
  1997. /**
  1998. * Returns the minified Source.
  1999. *
  2000. * @return string
  2001. */
  2002. public function getMinified()
  2003. {
  2004. return $this->minified;
  2005. }
  2006. /**
  2007. * Returns a plugin by class name.
  2008. *
  2009. * @param string $name Class name of the plugin
  2010. * @return aCssMinifierPlugin
  2011. */
  2012. public function getPlugin($class)
  2013. {
  2014. static $index = null;
  2015. if (is_null($index))
  2016. {
  2017. $index = array();
  2018. for ($i = 0, $l = count($this->plugins); $i < $l; $i++)
  2019. {
  2020. $index[get_class($this->plugins[$i])] = $i;
  2021. }
  2022. }
  2023. return isset($index[$class]) ? $this->plugins[$index[$class]] : false;
  2024. }
  2025. /**
  2026. * Minifies the CSS source.
  2027. *
  2028. * @param string $source CSS source
  2029. * @return string
  2030. */
  2031. public function minify($source)
  2032. {
  2033. // Variables
  2034. $r = "";
  2035. $parser = new CssParser($source);
  2036. $tokens = $parser->getTokens();
  2037. $filters = $this->filters;
  2038. $filterCount = count($this->filters);
  2039. $plugins = $this->plugins;
  2040. $pluginCount = count($plugins);
  2041. $pluginIndex = array();
  2042. $pluginTriggerTokens = array();
  2043. $globalTriggerTokens = array();
  2044. for ($i = 0, $l = count($plugins); $i < $l; $i++)
  2045. {
  2046. $tPluginClassName = get_class($plugins[$i]);
  2047. $pluginTriggerTokens[$i] = $plugins[$i]->getTriggerTokens();
  2048. foreach ($pluginTriggerTokens[$i] as $v)
  2049. {
  2050. if (!in_array($v, $globalTriggerTokens))
  2051. {
  2052. $globalTriggerTokens[] = $v;
  2053. }
  2054. }
  2055. $pluginTriggerTokens[$i] = "|" . implode("|", $pluginTriggerTokens[$i]) . "|";
  2056. $pluginIndex[$tPluginClassName] = $i;
  2057. }
  2058. $globalTriggerTokens = "|" . implode("|", $globalTriggerTokens) . "|";
  2059. /*
  2060. * Apply filters
  2061. */
  2062. for($i = 0; $i < $filterCount; $i++)
  2063. {
  2064. // Apply the filter; if the return value is larger than 0...
  2065. if ($filters[$i]->apply($tokens) > 0)
  2066. {
  2067. // ...then filter null values and rebuild the token array
  2068. $tokens = array_values(array_filter($tokens));
  2069. }
  2070. }
  2071. $tokenCount = count($tokens);
  2072. /*
  2073. * Apply plugins
  2074. */
  2075. for($i = 0; $i < $tokenCount; $i++)
  2076. {
  2077. $triggerToken = "|" . get_class($tokens[$i]) . "|";
  2078. if (strpos($globalTriggerTokens, $triggerToken) !== false)
  2079. {
  2080. for($ii = 0; $ii < $pluginCount; $ii++)
  2081. {
  2082. if (strpos($pluginTriggerTokens[$ii], $triggerToken) !== false || $pluginTriggerTokens[$ii] === false)
  2083. {
  2084. // Apply the plugin; if the return value is TRUE continue to the next token
  2085. if ($plugins[$ii]->apply($tokens[$i]) === true)
  2086. {
  2087. continue 2;
  2088. }
  2089. }
  2090. }
  2091. }
  2092. }
  2093. // Stringify the tokens
  2094. for($i = 0; $i < $tokenCount; $i++)
  2095. {
  2096. $r .= (string) $tokens[$i];
  2097. }
  2098. $this->minified = $r;
  2099. return $r;
  2100. }
  2101. }
  2102. /**
  2103. * CssMin - A (simple) css minifier with benefits
  2104. *
  2105. * --
  2106. * Copyright (c) 2011 Joe Scylla <joe.scylla@gmail.com>
  2107. *
  2108. * Permission is hereby granted, free of charge, to any person obtaining a copy
  2109. * of this software and associated documentation files (the "Software"), to deal
  2110. * in the Software without restriction, including without limitation the rights
  2111. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  2112. * copies of the Software, and to permit persons to whom the Software is
  2113. * furnished to do so, subject to the following conditions:
  2114. *
  2115. * The above copyright notice and this permission notice shall be included in
  2116. * all copies or substantial portions of the Software.
  2117. *
  2118. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  2119. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  2120. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  2121. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  2122. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  2123. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  2124. * THE SOFTWARE.
  2125. * --
  2126. *
  2127. * @package CssMin
  2128. * @link http://code.google.com/p/cssmin/
  2129. * @author Joe Scylla <joe.scylla@gmail.com>
  2130. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  2131. * @license http://opensource.org/licenses/mit-license.php MIT License
  2132. * @version 3.0.1
  2133. */
  2134. class CssMin
  2135. {
  2136. /**
  2137. * Index of classes
  2138. *
  2139. * @var array
  2140. */
  2141. private static $classIndex = array();
  2142. /**
  2143. * Parse/minify errors
  2144. *
  2145. * @var array
  2146. */
  2147. private static $errors = array();
  2148. /**
  2149. * Verbose output.
  2150. *
  2151. * @var boolean
  2152. */
  2153. private static $isVerbose = false;
  2154. /**
  2155. * {@link http://goo.gl/JrW54 Autoload} function of CssMin.
  2156. *
  2157. * @param string $class Name of the class
  2158. * @return void
  2159. */
  2160. public static function autoload($class)
  2161. {
  2162. if (isset(self::$classIndex[$class]))
  2163. {
  2164. require(self::$classIndex[$class]);
  2165. }
  2166. }
  2167. /**
  2168. * Return errors
  2169. *
  2170. * @return array of {CssError}.
  2171. */
  2172. public static function getErrors()
  2173. {
  2174. return self::$errors;
  2175. }
  2176. /**
  2177. * Returns if there were errors.
  2178. *
  2179. * @return boolean
  2180. */
  2181. public static function hasErrors()
  2182. {
  2183. return count(self::$errors) > 0;
  2184. }
  2185. /**
  2186. * Initialises CssMin.
  2187. *
  2188. * @return void
  2189. */
  2190. public static function initialise()
  2191. {
  2192. // Create the class index for autoloading or including
  2193. $paths = array(dirname(__FILE__));
  2194. while (list($i, $path) = each($paths))
  2195. {
  2196. $subDirectorys = glob($path . "*", GLOB_MARK | GLOB_ONLYDIR | GLOB_NOSORT);
  2197. if (is_array($subDirectorys))
  2198. {
  2199. foreach ($subDirectorys as $subDirectory)
  2200. {
  2201. $paths[] = $subDirectory;
  2202. }
  2203. }
  2204. $files = glob($path . "*.php", 0);
  2205. if (is_array($files))
  2206. {
  2207. foreach ($files as $file)
  2208. {
  2209. $class = substr(basename($file), 0, -4);
  2210. self::$classIndex[$class] = $file;
  2211. }
  2212. }
  2213. }
  2214. krsort(self::$classIndex);
  2215. // Only use autoloading if spl_autoload_register() is available and no __autoload() is defined (because
  2216. // __autoload() breaks if spl_autoload_register() is used.
  2217. if (function_exists("spl_autoload_register") && !is_callable("__autoload"))
  2218. {
  2219. spl_autoload_register(array(__CLASS__, "autoload"));
  2220. }
  2221. // Otherwise include all class files
  2222. else
  2223. {
  2224. foreach (self::$classIndex as $class => $file)
  2225. {
  2226. if (!class_exists($class))
  2227. {
  2228. require_once($file);
  2229. }
  2230. }
  2231. }
  2232. }
  2233. /**
  2234. * Minifies CSS source.
  2235. *
  2236. * @param string $source CSS source
  2237. * @param array $filters Filter configuration [optional]
  2238. * @param array $plugins Plugin configuration [optional]
  2239. * @return string Minified CSS
  2240. */
  2241. public static function minify($source, array $filters = null, array $plugins = null)
  2242. {
  2243. self::$errors = array();
  2244. $minifier = new CssMinifier($source, $filters, $plugins);
  2245. return $minifier->getMinified();
  2246. }
  2247. /**
  2248. * Parse the CSS source.
  2249. *
  2250. * @param string $source CSS source
  2251. * @param array $plugins Plugin configuration [optional]
  2252. * @return array Array of aCssToken
  2253. */
  2254. public static function parse($source, array $plugins = null)
  2255. {
  2256. self::$errors = array();
  2257. $parser = new CssParser($source, $plugins);
  2258. return $parser->getTokens();
  2259. }
  2260. /**
  2261. * --
  2262. *
  2263. * @param boolean $to
  2264. * @return boolean
  2265. */
  2266. public static function setVerbose($to)
  2267. {
  2268. self::$isVerbose = (boolean) $to;
  2269. return self::$isVerbose;
  2270. }
  2271. /**
  2272. * --
  2273. *
  2274. * @param CssError $error
  2275. * @return void
  2276. */
  2277. public static function triggerError(CssError $error)
  2278. {
  2279. self::$errors[] = $error;
  2280. if (self::$isVerbose)
  2281. {
  2282. trigger_error((string) $error, E_USER_WARNING);
  2283. }
  2284. }
  2285. }
  2286. // Initialises CssMin
  2287. CssMin::initialise();
  2288. /**
  2289. * This {@link aCssMinifierFilter minifier filter} import external css files defined with the @import at-rule into the
  2290. * current stylesheet.
  2291. *
  2292. * @package CssMin/Minifier/Filters
  2293. * @link http://code.google.com/p/cssmin/
  2294. * @author Joe Scylla <joe.scylla@gmail.com>
  2295. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  2296. * @license http://opensource.org/licenses/mit-license.php MIT License
  2297. * @version 3.0.1
  2298. */
  2299. class CssImportImportsMinifierFilter extends aCssMinifierFilter
  2300. {
  2301. /**
  2302. * Array with already imported external stylesheets.
  2303. *
  2304. * @var array
  2305. */
  2306. private $imported = array();
  2307. /**
  2308. * Implements {@link aCssMinifierFilter::filter()}.
  2309. *
  2310. * @param array $tokens Array of objects of type aCssToken
  2311. * @return integer Count of added, changed or removed tokens; a return value large than 0 will rebuild the array
  2312. */
  2313. public function apply(array &$tokens)
  2314. {
  2315. if (!isset($this->configuration["BasePath"]) || !is_dir($this->configuration["BasePath"]))
  2316. {
  2317. CssMin::triggerError(new CssError(__FILE__, __LINE__, __METHOD__ . ": Base path <code>" . ($this->configuration["BasePath"] ? $this->configuration["BasePath"] : "null"). "</code> is not a directory"));
  2318. return 0;
  2319. }
  2320. for ($i = 0, $l = count($tokens); $i < $l; $i++)
  2321. {
  2322. if (get_class($tokens[$i]) === "CssAtImportToken")
  2323. {
  2324. $import = $this->configuration["BasePath"] . "/" . $tokens[$i]->Import;
  2325. // Import file was not found/is not a file
  2326. if (!is_file($import))
  2327. {
  2328. CssMin::triggerError(new CssError(__FILE__, __LINE__, __METHOD__ . ": Import file <code>" . $import. "</code> was not found.", (string) $tokens[$i]));
  2329. }
  2330. // Import file already imported; remove this @import at-rule to prevent recursions
  2331. elseif (in_array($import, $this->imported))
  2332. {
  2333. CssMin::triggerError(new CssError(__FILE__, __LINE__, __METHOD__ . ": Import file <code>" . $import. "</code> was already imported.", (string) $tokens[$i]));
  2334. $tokens[$i] = null;
  2335. }
  2336. else
  2337. {
  2338. $this->imported[] = $import;
  2339. $parser = new CssParser(file_get_contents($import));
  2340. $import = $parser->getTokens();
  2341. // The @import at-rule has media types defined requiring special handling
  2342. if (count($tokens[$i]->MediaTypes) > 0 && !(count($tokens[$i]->MediaTypes) == 1 && $tokens[$i]->MediaTypes[0] == "all"))
  2343. {
  2344. $blocks = array();
  2345. /*
  2346. * Filter or set media types of @import at-rule or remove the @import at-rule if no media type is matching the parent @import at-rule
  2347. */
  2348. for($ii = 0, $ll = count($import); $ii < $ll; $ii++)
  2349. {
  2350. if (get_class($import[$ii]) === "CssAtImportToken")
  2351. {
  2352. // @import at-rule defines no media type or only the "all" media type; set the media types to the one defined in the parent @import at-rule
  2353. if (count($import[$ii]->MediaTypes) == 0 || (count($import[$ii]->MediaTypes) == 1 && $import[$ii]->MediaTypes[0] == "all"))
  2354. {
  2355. $import[$ii]->MediaTypes = $tokens[$i]->MediaTypes;
  2356. }
  2357. // @import at-rule defineds one or more media types; filter out media types not matching with the parent @import at-rule
  2358. elseif (count($import[$ii]->MediaTypes) > 0)
  2359. {
  2360. foreach ($import[$ii]->MediaTypes as $index => $mediaType)
  2361. {
  2362. if (!in_array($mediaType, $tokens[$i]->MediaTypes))
  2363. {
  2364. unset($import[$ii]->MediaTypes[$index]);
  2365. }
  2366. }
  2367. $import[$ii]->MediaTypes = array_values($import[$ii]->MediaTypes);
  2368. // If there are no media types left in the @import at-rule remove the @import at-rule
  2369. if (count($import[$ii]->MediaTypes) == 0)
  2370. {
  2371. $import[$ii] = null;
  2372. }
  2373. }
  2374. }
  2375. }
  2376. /*
  2377. * Remove media types of @media at-rule block not defined in the @import at-rule
  2378. */
  2379. for($ii = 0, $ll = count($import); $ii < $ll; $ii++)
  2380. {
  2381. if (get_class($import[$ii]) === "CssAtMediaStartToken")
  2382. {
  2383. foreach ($import[$ii]->MediaTypes as $index => $mediaType)
  2384. {
  2385. if (!in_array($mediaType, $tokens[$i]->MediaTypes))
  2386. {
  2387. unset($import[$ii]->MediaTypes[$index]);
  2388. }
  2389. $import[$ii]->MediaTypes = array_values($import[$ii]->MediaTypes);
  2390. }
  2391. }
  2392. }
  2393. /*
  2394. * If no media types left of the @media at-rule block remove the complete block
  2395. */
  2396. for($ii = 0, $ll = count($import); $ii < $ll; $ii++)
  2397. {
  2398. if (get_class($import[$ii]) === "CssAtMediaStartToken")
  2399. {
  2400. if (count($import[$ii]->MediaTypes) === 0)
  2401. {
  2402. for ($iii = $ii; $iii < $ll; $iii++)
  2403. {
  2404. if (get_class($import[$iii]) === "CssAtMediaEndToken")
  2405. {
  2406. break;
  2407. }
  2408. }
  2409. if (get_class($import[$iii]) === "CssAtMediaEndToken")
  2410. {
  2411. array_splice($import, $ii, $iii - $ii + 1, array());
  2412. $ll = count($import);
  2413. }
  2414. }
  2415. }
  2416. }
  2417. /*
  2418. * If the media types of the @media at-rule equals the media types defined in the @import
  2419. * at-rule remove the CssAtMediaStartToken and CssAtMediaEndToken token
  2420. */
  2421. for($ii = 0, $ll = count($import); $ii < $ll; $ii++)
  2422. {
  2423. if (get_class($import[$ii]) === "CssAtMediaStartToken" && count(array_diff($tokens[$i]->MediaTypes, $import[$ii]->MediaTypes)) === 0)
  2424. {
  2425. for ($iii = $ii; $iii < $ll; $iii++)
  2426. {
  2427. if (get_class($import[$iii]) == "CssAtMediaEndToken")
  2428. {
  2429. break;
  2430. }
  2431. }
  2432. if (get_class($import[$iii]) == "CssAtMediaEndToken")
  2433. {
  2434. unset($import[$ii]);
  2435. unset($import[$iii]);
  2436. $import = array_values($import);
  2437. $ll = count($import);
  2438. }
  2439. }
  2440. }
  2441. /**
  2442. * Extract CssAtImportToken and CssAtCharsetToken tokens
  2443. */
  2444. for($ii = 0, $ll = count($import); $ii < $ll; $ii++)
  2445. {
  2446. $class = get_class($import[$ii]);
  2447. if ($class === "CssAtImportToken" || $class === "CssAtCharsetToken")
  2448. {
  2449. $blocks = array_merge($blocks, array_splice($import, $ii, 1, array()));
  2450. $ll = count($import);
  2451. }
  2452. }
  2453. /*
  2454. * Extract the @font-face, @media and @page at-rule block
  2455. */
  2456. for($ii = 0, $ll = count($import); $ii < $ll; $ii++)
  2457. {
  2458. $class = get_class($import[$ii]);
  2459. if ($class === "CssAtFontFaceStartToken" || $class === "CssAtMediaStartToken" || $class === "CssAtPageStartToken" || $class === "CssAtVariablesStartToken")
  2460. {
  2461. for ($iii = $ii; $iii < $ll; $iii++)
  2462. {
  2463. $class = get_class($import[$iii]);
  2464. if ($class === "CssAtFontFaceEndToken" || $class === "CssAtMediaEndToken" || $class === "CssAtPageEndToken" || $class === "CssAtVariablesEndToken")
  2465. {
  2466. break;
  2467. }
  2468. }
  2469. $class = get_class($import[$iii]);
  2470. if (isset($import[$iii]) && ($class === "CssAtFontFaceEndToken" || $class === "CssAtMediaEndToken" || $class === "CssAtPageEndToken" || $class === "CssAtVariablesEndToken"))
  2471. {
  2472. $blocks = array_merge($blocks, array_splice($import, $ii, $iii - $ii + 1, array()));
  2473. $ll = count($import);
  2474. }
  2475. }
  2476. }
  2477. // Create the import array with extracted tokens and the rulesets wrapped into a @media at-rule block
  2478. $import = array_merge($blocks, array(new CssAtMediaStartToken($tokens[$i]->MediaTypes)), $import, array(new CssAtMediaEndToken()));
  2479. }
  2480. // Insert the imported tokens
  2481. array_splice($tokens, $i, 1, $import);
  2482. // Modify parameters of the for-loop
  2483. $i--;
  2484. $l = count($tokens);
  2485. }
  2486. }
  2487. }
  2488. }
  2489. }
  2490. /**
  2491. * {@link aCssParserPlugin Parser plugin} for preserve parsing expression() declaration values.
  2492. *
  2493. * This plugin return no {@link aCssToken CssToken} but ensures that expression() declaration values will get parsed
  2494. * properly.
  2495. *
  2496. * @package CssMin/Parser/Plugins
  2497. * @link http://code.google.com/p/cssmin/
  2498. * @author Joe Scylla <joe.scylla@gmail.com>
  2499. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  2500. * @license http://opensource.org/licenses/mit-license.php MIT License
  2501. * @version 3.0.1
  2502. */
  2503. class CssExpressionParserPlugin extends aCssParserPlugin
  2504. {
  2505. /**
  2506. * Count of left braces.
  2507. *
  2508. * @var integer
  2509. */
  2510. private $leftBraces = 0;
  2511. /**
  2512. * Count of right braces.
  2513. *
  2514. * @var integer
  2515. */
  2516. private $rightBraces = 0;
  2517. /**
  2518. * Implements {@link aCssParserPlugin::getTriggerChars()}.
  2519. *
  2520. * @return array
  2521. */
  2522. public function getTriggerChars()
  2523. {
  2524. return array("(", ")", ";", "}");
  2525. }
  2526. /**
  2527. * Implements {@link aCssParserPlugin::getTriggerStates()}.
  2528. *
  2529. * @return array
  2530. */
  2531. public function getTriggerStates()
  2532. {
  2533. return false;
  2534. }
  2535. /**
  2536. * Implements {@link aCssParserPlugin::parse()}.
  2537. *
  2538. * @param integer $index Current index
  2539. * @param string $char Current char
  2540. * @param string $previousChar Previous char
  2541. * @return mixed TRUE will break the processing; FALSE continue with the next plugin; integer set a new index and break the processing
  2542. */
  2543. public function parse($index, $char, $previousChar, $state)
  2544. {
  2545. // Start of expression
  2546. if ($char === "(" && strtolower(substr($this->parser->getSource(), $index - 10, 11)) === "expression(" && $state !== "T_EXPRESSION")
  2547. {
  2548. $this->parser->pushState("T_EXPRESSION");
  2549. $this->leftBraces++;
  2550. }
  2551. // Count left braces
  2552. elseif ($char === "(" && $state === "T_EXPRESSION")
  2553. {
  2554. $this->leftBraces++;
  2555. }
  2556. // Count right braces
  2557. elseif ($char === ")" && $state === "T_EXPRESSION")
  2558. {
  2559. $this->rightBraces++;
  2560. }
  2561. // Possible end of expression; if left and right braces are equal the expressen ends
  2562. elseif (($char === ";" || $char === "}") && $state === "T_EXPRESSION" && $this->leftBraces === $this->rightBraces)
  2563. {
  2564. $this->leftBraces = $this->rightBraces = 0;
  2565. $this->parser->popState();
  2566. return $index - 1;
  2567. }
  2568. else
  2569. {
  2570. return false;
  2571. }
  2572. return true;
  2573. }
  2574. }
  2575. /**
  2576. * CSS Error.
  2577. *
  2578. * @package CssMin
  2579. * @link http://code.google.com/p/cssmin/
  2580. * @author Joe Scylla <joe.scylla@gmail.com>
  2581. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  2582. * @license http://opensource.org/licenses/mit-license.php MIT License
  2583. * @version 3.0.1
  2584. */
  2585. class CssError
  2586. {
  2587. /**
  2588. * File.
  2589. *
  2590. * @var string
  2591. */
  2592. public $File = "";
  2593. /**
  2594. * Line.
  2595. *
  2596. * @var integer
  2597. */
  2598. public $Line = 0;
  2599. /**
  2600. * Error message.
  2601. *
  2602. * @var string
  2603. */
  2604. public $Message = "";
  2605. /**
  2606. * Source.
  2607. *
  2608. * @var string
  2609. */
  2610. public $Source = "";
  2611. /**
  2612. * Constructor triggering the error.
  2613. *
  2614. * @param string $message Error message
  2615. * @param string $source Corresponding line [optional]
  2616. * @return void
  2617. */
  2618. public function __construct($file, $line, $message, $source = "")
  2619. {
  2620. $this->File = $file;
  2621. $this->Line = $line;
  2622. $this->Message = $message;
  2623. $this->Source = $source;
  2624. }
  2625. /**
  2626. * Returns the error as formatted string.
  2627. *
  2628. * @return string
  2629. */
  2630. public function __toString()
  2631. {
  2632. return $this->Message . ($this->Source ? ": <br /><code>" . $this->Source . "</code>": "") . "<br />in file " . $this->File . " at line " . $this->Line;
  2633. }
  2634. }
  2635. /**
  2636. * This {@link aCssMinifierPlugin} will convert a color value in rgb notation to hexadecimal notation.
  2637. *
  2638. * Example:
  2639. * <code>
  2640. * color: rgb(200,60%,5);
  2641. * </code>
  2642. *
  2643. * Will get converted to:
  2644. * <code>
  2645. * color:#c89905;
  2646. * </code>
  2647. *
  2648. * @package CssMin/Minifier/Plugins
  2649. * @link http://code.google.com/p/cssmin/
  2650. * @author Joe Scylla <joe.scylla@gmail.com>
  2651. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  2652. * @license http://opensource.org/licenses/mit-license.php MIT License
  2653. * @version 3.0.1
  2654. */
  2655. class CssConvertRgbColorsMinifierPlugin extends aCssMinifierPlugin
  2656. {
  2657. /**
  2658. * Regular expression matching the value.
  2659. *
  2660. * @var string
  2661. */
  2662. private $reMatch = "/rgb\s*\(\s*([0-9%]+)\s*,\s*([0-9%]+)\s*,\s*([0-9%]+)\s*\)/iS";
  2663. /**
  2664. * Implements {@link aCssMinifierPlugin::minify()}.
  2665. *
  2666. * @param aCssToken $token Token to process
  2667. * @return boolean Return TRUE to break the processing of this token; FALSE to continue
  2668. */
  2669. public function apply(aCssToken &$token)
  2670. {
  2671. if (stripos($token->Value, "rgb") !== false && preg_match($this->reMatch, $token->Value, $m))
  2672. {
  2673. for ($i = 1, $l = count($m); $i < $l; $i++)
  2674. {
  2675. if (strpos("%", $m[$i]) !== false)
  2676. {
  2677. $m[$i] = substr($m[$i], 0, -1);
  2678. $m[$i] = (int) (256 * ($m[$i] / 100));
  2679. }
  2680. $m[$i] = str_pad(dechex($m[$i]), 2, "0", STR_PAD_LEFT);
  2681. }
  2682. $token->Value = str_replace($m[0], "#" . $m[1] . $m[2] . $m[3], $token->Value);
  2683. }
  2684. return false;
  2685. }
  2686. /**
  2687. * Implements {@link aMinifierPlugin::getTriggerTokens()}
  2688. *
  2689. * @return array
  2690. */
  2691. public function getTriggerTokens()
  2692. {
  2693. return array
  2694. (
  2695. "CssAtFontFaceDeclarationToken",
  2696. "CssAtPageDeclarationToken",
  2697. "CssRulesetDeclarationToken"
  2698. );
  2699. }
  2700. }
  2701. /**
  2702. * This {@link aCssMinifierPlugin} will convert named color values to hexadecimal notation.
  2703. *
  2704. * Example:
  2705. * <code>
  2706. * color: black;
  2707. * border: 1px solid indigo;
  2708. * </code>
  2709. *
  2710. * Will get converted to:
  2711. * <code>
  2712. * color:#000;
  2713. * border:1px solid #4b0082;
  2714. * </code>
  2715. *
  2716. * @package CssMin/Minifier/Plugins
  2717. * @link http://code.google.com/p/cssmin/
  2718. * @author Joe Scylla <joe.scylla@gmail.com>
  2719. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  2720. * @license http://opensource.org/licenses/mit-license.php MIT License
  2721. * @version 3.0.1
  2722. */
  2723. class CssConvertNamedColorsMinifierPlugin extends aCssMinifierPlugin
  2724. {
  2725. /**
  2726. * Regular expression matching the value.
  2727. *
  2728. * @var string
  2729. */
  2730. private $reMatch = null;
  2731. /**
  2732. * Regular expression replacing the value.
  2733. *
  2734. * @var string
  2735. */
  2736. private $reReplace = "\"\${1}\" . \$this->transformation[strtolower(\"\${2}\")] . \"\${3}\"";
  2737. /**
  2738. * Transformation table used by the {@link CssConvertNamedColorsMinifierPlugin::$reReplace replace regular expression}.
  2739. *
  2740. * @var array
  2741. */
  2742. private $transformation = array
  2743. (
  2744. "aliceblue" => "#f0f8ff",
  2745. "antiquewhite" => "#faebd7",
  2746. "aqua" => "#0ff",
  2747. "aquamarine" => "#7fffd4",
  2748. "azure" => "#f0ffff",
  2749. "beige" => "#f5f5dc",
  2750. "black" => "#000",
  2751. "blue" => "#00f",
  2752. "blueviolet" => "#8a2be2",
  2753. "brown" => "#a52a2a",
  2754. "burlywood" => "#deb887",
  2755. "cadetblue" => "#5f9ea0",
  2756. "chartreuse" => "#7fff00",
  2757. "chocolate" => "#d2691e",
  2758. "coral" => "#ff7f50",
  2759. "cornflowerblue" => "#6495ed",
  2760. "cornsilk" => "#fff8dc",
  2761. "crimson" => "#dc143c",
  2762. "darkblue" => "#00008b",
  2763. "darkcyan" => "#008b8b",
  2764. "darkgoldenrod" => "#b8860b",
  2765. "darkgray" => "#a9a9a9",
  2766. "darkgreen" => "#006400",
  2767. "darkkhaki" => "#bdb76b",
  2768. "darkmagenta" => "#8b008b",
  2769. "darkolivegreen" => "#556b2f",
  2770. "darkorange" => "#ff8c00",
  2771. "darkorchid" => "#9932cc",
  2772. "darkred" => "#8b0000",
  2773. "darksalmon" => "#e9967a",
  2774. "darkseagreen" => "#8fbc8f",
  2775. "darkslateblue" => "#483d8b",
  2776. "darkslategray" => "#2f4f4f",
  2777. "darkturquoise" => "#00ced1",
  2778. "darkviolet" => "#9400d3",
  2779. "deeppink" => "#ff1493",
  2780. "deepskyblue" => "#00bfff",
  2781. "dimgray" => "#696969",
  2782. "dodgerblue" => "#1e90ff",
  2783. "firebrick" => "#b22222",
  2784. "floralwhite" => "#fffaf0",
  2785. "forestgreen" => "#228b22",
  2786. "fuchsia" => "#f0f",
  2787. "gainsboro" => "#dcdcdc",
  2788. "ghostwhite" => "#f8f8ff",
  2789. "gold" => "#ffd700",
  2790. "goldenrod" => "#daa520",
  2791. "gray" => "#808080",
  2792. "green" => "#008000",
  2793. "greenyellow" => "#adff2f",
  2794. "honeydew" => "#f0fff0",
  2795. "hotpink" => "#ff69b4",
  2796. "indianred" => "#cd5c5c",
  2797. "indigo" => "#4b0082",
  2798. "ivory" => "#fffff0",
  2799. "khaki" => "#f0e68c",
  2800. "lavender" => "#e6e6fa",
  2801. "lavenderblush" => "#fff0f5",
  2802. "lawngreen" => "#7cfc00",
  2803. "lemonchiffon" => "#fffacd",
  2804. "lightblue" => "#add8e6",
  2805. "lightcoral" => "#f08080",
  2806. "lightcyan" => "#e0ffff",
  2807. "lightgoldenrodyellow" => "#fafad2",
  2808. "lightgreen" => "#90ee90",
  2809. "lightgrey" => "#d3d3d3",
  2810. "lightpink" => "#ffb6c1",
  2811. "lightsalmon" => "#ffa07a",
  2812. "lightseagreen" => "#20b2aa",
  2813. "lightskyblue" => "#87cefa",
  2814. "lightslategray" => "#789",
  2815. "lightsteelblue" => "#b0c4de",
  2816. "lightyellow" => "#ffffe0",
  2817. "lime" => "#0f0",
  2818. "limegreen" => "#32cd32",
  2819. "linen" => "#faf0e6",
  2820. "maroon" => "#800000",
  2821. "mediumaquamarine" => "#66cdaa",
  2822. "mediumblue" => "#0000cd",
  2823. "mediumorchid" => "#ba55d3",
  2824. "mediumpurple" => "#9370db",
  2825. "mediumseagreen" => "#3cb371",
  2826. "mediumslateblue" => "#7b68ee",
  2827. "mediumspringgreen" => "#00fa9a",
  2828. "mediumturquoise" => "#48d1cc",
  2829. "mediumvioletred" => "#c71585",
  2830. "midnightblue" => "#191970",
  2831. "mintcream" => "#f5fffa",
  2832. "mistyrose" => "#ffe4e1",
  2833. "moccasin" => "#ffe4b5",
  2834. "navajowhite" => "#ffdead",
  2835. "navy" => "#000080",
  2836. "oldlace" => "#fdf5e6",
  2837. "olive" => "#808000",
  2838. "olivedrab" => "#6b8e23",
  2839. "orange" => "#ffa500",
  2840. "orangered" => "#ff4500",
  2841. "orchid" => "#da70d6",
  2842. "palegoldenrod" => "#eee8aa",
  2843. "palegreen" => "#98fb98",
  2844. "paleturquoise" => "#afeeee",
  2845. "palevioletred" => "#db7093",
  2846. "papayawhip" => "#ffefd5",
  2847. "peachpuff" => "#ffdab9",
  2848. "peru" => "#cd853f",
  2849. "pink" => "#ffc0cb",
  2850. "plum" => "#dda0dd",
  2851. "powderblue" => "#b0e0e6",
  2852. "purple" => "#800080",
  2853. "red" => "#f00",
  2854. "rosybrown" => "#bc8f8f",
  2855. "royalblue" => "#4169e1",
  2856. "saddlebrown" => "#8b4513",
  2857. "salmon" => "#fa8072",
  2858. "sandybrown" => "#f4a460",
  2859. "seagreen" => "#2e8b57",
  2860. "seashell" => "#fff5ee",
  2861. "sienna" => "#a0522d",
  2862. "silver" => "#c0c0c0",
  2863. "skyblue" => "#87ceeb",
  2864. "slateblue" => "#6a5acd",
  2865. "slategray" => "#708090",
  2866. "snow" => "#fffafa",
  2867. "springgreen" => "#00ff7f",
  2868. "steelblue" => "#4682b4",
  2869. "tan" => "#d2b48c",
  2870. "teal" => "#008080",
  2871. "thistle" => "#d8bfd8",
  2872. "tomato" => "#ff6347",
  2873. "turquoise" => "#40e0d0",
  2874. "violet" => "#ee82ee",
  2875. "wheat" => "#f5deb3",
  2876. "white" => "#fff",
  2877. "whitesmoke" => "#f5f5f5",
  2878. "yellow" => "#ff0",
  2879. "yellowgreen" => "#9acd32"
  2880. );
  2881. /**
  2882. * Overwrites {@link aCssMinifierPlugin::__construct()}.
  2883. *
  2884. * The constructor will create the {@link CssConvertNamedColorsMinifierPlugin::$reReplace replace regular expression}
  2885. * based on the {@link CssConvertNamedColorsMinifierPlugin::$transformation transformation table}.
  2886. *
  2887. * @param CssMinifier $minifier The CssMinifier object of this plugin.
  2888. * @param array $configuration Plugin configuration [optional]
  2889. * @return void
  2890. */
  2891. public function __construct(CssMinifier $minifier, array $configuration = array())
  2892. {
  2893. $this->reMatch = "/(^|\s)+(" . implode("|", array_keys($this->transformation)) . ")(\s|$)+/eiS";
  2894. parent::__construct($minifier, $configuration);
  2895. }
  2896. /**
  2897. * Implements {@link aCssMinifierPlugin::minify()}.
  2898. *
  2899. * @param aCssToken $token Token to process
  2900. * @return boolean Return TRUE to break the processing of this token; FALSE to continue
  2901. */
  2902. public function apply(aCssToken &$token)
  2903. {
  2904. $lcValue = strtolower($token->Value);
  2905. // Declaration value equals a value in the transformation table => simple replace
  2906. if (isset($this->transformation[$lcValue]))
  2907. {
  2908. $token->Value = $this->transformation[$lcValue];
  2909. }
  2910. // Declaration value contains a value in the transformation table => regular expression replace
  2911. elseif (preg_match($this->reMatch, $token->Value))
  2912. {
  2913. $token->Value = preg_replace($this->reMatch, $this->reReplace, $token->Value);
  2914. }
  2915. return false;
  2916. }
  2917. /**
  2918. * Implements {@link aMinifierPlugin::getTriggerTokens()}
  2919. *
  2920. * @return array
  2921. */
  2922. public function getTriggerTokens()
  2923. {
  2924. return array
  2925. (
  2926. "CssAtFontFaceDeclarationToken",
  2927. "CssAtPageDeclarationToken",
  2928. "CssRulesetDeclarationToken"
  2929. );
  2930. }
  2931. }
  2932. /**
  2933. * This {@link aCssMinifierFilter minifier filter} triggers on CSS Level 3 properties and will add declaration tokens
  2934. * with browser-specific properties.
  2935. *
  2936. * @package CssMin/Minifier/Filters
  2937. * @link http://code.google.com/p/cssmin/
  2938. * @author Joe Scylla <joe.scylla@gmail.com>
  2939. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  2940. * @license http://opensource.org/licenses/mit-license.php MIT License
  2941. * @version 3.0.1
  2942. */
  2943. class CssConvertLevel3PropertiesMinifierFilter extends aCssMinifierFilter
  2944. {
  2945. /**
  2946. * Css property transformations table. Used to convert CSS3 and proprietary properties to the browser-specific
  2947. * counterparts.
  2948. *
  2949. * @var array
  2950. */
  2951. private $transformations = array
  2952. (
  2953. // Property Array(Mozilla, Webkit, Opera, Internet Explorer); NULL values are placeholders and will get ignored
  2954. "animation" => array(null, "-webkit-animation", null, null),
  2955. "animation-delay" => array(null, "-webkit-animation-delay", null, null),
  2956. "animation-direction" => array(null, "-webkit-animation-direction", null, null),
  2957. "animation-duration" => array(null, "-webkit-animation-duration", null, null),
  2958. "animation-fill-mode" => array(null, "-webkit-animation-fill-mode", null, null),
  2959. "animation-iteration-count" => array(null, "-webkit-animation-iteration-count", null, null),
  2960. "animation-name" => array(null, "-webkit-animation-name", null, null),
  2961. "animation-play-state" => array(null, "-webkit-animation-play-state", null, null),
  2962. "animation-timing-function" => array(null, "-webkit-animation-timing-function", null, null),
  2963. "appearance" => array("-moz-appearance", "-webkit-appearance", null, null),
  2964. "backface-visibility" => array(null, "-webkit-backface-visibility", null, null),
  2965. "background-clip" => array(null, "-webkit-background-clip", null, null),
  2966. "background-composite" => array(null, "-webkit-background-composite", null, null),
  2967. "background-inline-policy" => array("-moz-background-inline-policy", null, null, null),
  2968. "background-origin" => array(null, "-webkit-background-origin", null, null),
  2969. "background-position-x" => array(null, null, null, "-ms-background-position-x"),
  2970. "background-position-y" => array(null, null, null, "-ms-background-position-y"),
  2971. "background-size" => array(null, "-webkit-background-size", null, null),
  2972. "behavior" => array(null, null, null, "-ms-behavior"),
  2973. "binding" => array("-moz-binding", null, null, null),
  2974. "border-after" => array(null, "-webkit-border-after", null, null),
  2975. "border-after-color" => array(null, "-webkit-border-after-color", null, null),
  2976. "border-after-style" => array(null, "-webkit-border-after-style", null, null),
  2977. "border-after-width" => array(null, "-webkit-border-after-width", null, null),
  2978. "border-before" => array(null, "-webkit-border-before", null, null),
  2979. "border-before-color" => array(null, "-webkit-border-before-color", null, null),
  2980. "border-before-style" => array(null, "-webkit-border-before-style", null, null),
  2981. "border-before-width" => array(null, "-webkit-border-before-width", null, null),
  2982. "border-border-bottom-colors" => array("-moz-border-bottom-colors", null, null, null),
  2983. "border-bottom-left-radius" => array("-moz-border-radius-bottomleft", "-webkit-border-bottom-left-radius", null, null),
  2984. "border-bottom-right-radius" => array("-moz-border-radius-bottomright", "-webkit-border-bottom-right-radius", null, null),
  2985. "border-end" => array("-moz-border-end", "-webkit-border-end", null, null),
  2986. "border-end-color" => array("-moz-border-end-color", "-webkit-border-end-color", null, null),
  2987. "border-end-style" => array("-moz-border-end-style", "-webkit-border-end-style", null, null),
  2988. "border-end-width" => array("-moz-border-end-width", "-webkit-border-end-width", null, null),
  2989. "border-fit" => array(null, "-webkit-border-fit", null, null),
  2990. "border-horizontal-spacing" => array(null, "-webkit-border-horizontal-spacing", null, null),
  2991. "border-image" => array("-moz-border-image", "-webkit-border-image", null, null),
  2992. "border-left-colors" => array("-moz-border-left-colors", null, null, null),
  2993. "border-radius" => array("-moz-border-radius", "-webkit-border-radius", null, null),
  2994. "border-border-right-colors" => array("-moz-border-right-colors", null, null, null),
  2995. "border-start" => array("-moz-border-start", "-webkit-border-start", null, null),
  2996. "border-start-color" => array("-moz-border-start-color", "-webkit-border-start-color", null, null),
  2997. "border-start-style" => array("-moz-border-start-style", "-webkit-border-start-style", null, null),
  2998. "border-start-width" => array("-moz-border-start-width", "-webkit-border-start-width", null, null),
  2999. "border-top-colors" => array("-moz-border-top-colors", null, null, null),
  3000. "border-top-left-radius" => array("-moz-border-radius-topleft", "-webkit-border-top-left-radius", null, null),
  3001. "border-top-right-radius" => array("-moz-border-radius-topright", "-webkit-border-top-right-radius", null, null),
  3002. "border-vertical-spacing" => array(null, "-webkit-border-vertical-spacing", null, null),
  3003. "box-align" => array("-moz-box-align", "-webkit-box-align", null, null),
  3004. "box-direction" => array("-moz-box-direction", "-webkit-box-direction", null, null),
  3005. "box-flex" => array("-moz-box-flex", "-webkit-box-flex", null, null),
  3006. "box-flex-group" => array(null, "-webkit-box-flex-group", null, null),
  3007. "box-flex-lines" => array(null, "-webkit-box-flex-lines", null, null),
  3008. "box-ordinal-group" => array("-moz-box-ordinal-group", "-webkit-box-ordinal-group", null, null),
  3009. "box-orient" => array("-moz-box-orient", "-webkit-box-orient", null, null),
  3010. "box-pack" => array("-moz-box-pack", "-webkit-box-pack", null, null),
  3011. "box-reflect" => array(null, "-webkit-box-reflect", null, null),
  3012. "box-shadow" => array("-moz-box-shadow", "-webkit-box-shadow", null, null),
  3013. "box-sizing" => array("-moz-box-sizing", null, null, null),
  3014. "color-correction" => array(null, "-webkit-color-correction", null, null),
  3015. "column-break-after" => array(null, "-webkit-column-break-after", null, null),
  3016. "column-break-before" => array(null, "-webkit-column-break-before", null, null),
  3017. "column-break-inside" => array(null, "-webkit-column-break-inside", null, null),
  3018. "column-count" => array("-moz-column-count", "-webkit-column-count", null, null),
  3019. "column-gap" => array("-moz-column-gap", "-webkit-column-gap", null, null),
  3020. "column-rule" => array("-moz-column-rule", "-webkit-column-rule", null, null),
  3021. "column-rule-color" => array("-moz-column-rule-color", "-webkit-column-rule-color", null, null),
  3022. "column-rule-style" => array("-moz-column-rule-style", "-webkit-column-rule-style", null, null),
  3023. "column-rule-width" => array("-moz-column-rule-width", "-webkit-column-rule-width", null, null),
  3024. "column-span" => array(null, "-webkit-column-span", null, null),
  3025. "column-width" => array("-moz-column-width", "-webkit-column-width", null, null),
  3026. "columns" => array(null, "-webkit-columns", null, null),
  3027. "filter" => array(__CLASS__, "filter"),
  3028. "float-edge" => array("-moz-float-edge", null, null, null),
  3029. "font-feature-settings" => array("-moz-font-feature-settings", null, null, null),
  3030. "font-language-override" => array("-moz-font-language-override", null, null, null),
  3031. "font-size-delta" => array(null, "-webkit-font-size-delta", null, null),
  3032. "font-smoothing" => array(null, "-webkit-font-smoothing", null, null),
  3033. "force-broken-image-icon" => array("-moz-force-broken-image-icon", null, null, null),
  3034. "highlight" => array(null, "-webkit-highlight", null, null),
  3035. "hyphenate-character" => array(null, "-webkit-hyphenate-character", null, null),
  3036. "hyphenate-locale" => array(null, "-webkit-hyphenate-locale", null, null),
  3037. "hyphens" => array(null, "-webkit-hyphens", null, null),
  3038. "force-broken-image-icon" => array("-moz-image-region", null, null, null),
  3039. "ime-mode" => array(null, null, null, "-ms-ime-mode"),
  3040. "interpolation-mode" => array(null, null, null, "-ms-interpolation-mode"),
  3041. "layout-flow" => array(null, null, null, "-ms-layout-flow"),
  3042. "layout-grid" => array(null, null, null, "-ms-layout-grid"),
  3043. "layout-grid-char" => array(null, null, null, "-ms-layout-grid-char"),
  3044. "layout-grid-line" => array(null, null, null, "-ms-layout-grid-line"),
  3045. "layout-grid-mode" => array(null, null, null, "-ms-layout-grid-mode"),
  3046. "layout-grid-type" => array(null, null, null, "-ms-layout-grid-type"),
  3047. "line-break" => array(null, "-webkit-line-break", null, "-ms-line-break"),
  3048. "line-clamp" => array(null, "-webkit-line-clamp", null, null),
  3049. "line-grid-mode" => array(null, null, null, "-ms-line-grid-mode"),
  3050. "logical-height" => array(null, "-webkit-logical-height", null, null),
  3051. "logical-width" => array(null, "-webkit-logical-width", null, null),
  3052. "margin-after" => array(null, "-webkit-margin-after", null, null),
  3053. "margin-after-collapse" => array(null, "-webkit-margin-after-collapse", null, null),
  3054. "margin-before" => array(null, "-webkit-margin-before", null, null),
  3055. "margin-before-collapse" => array(null, "-webkit-margin-before-collapse", null, null),
  3056. "margin-bottom-collapse" => array(null, "-webkit-margin-bottom-collapse", null, null),
  3057. "margin-collapse" => array(null, "-webkit-margin-collapse", null, null),
  3058. "margin-end" => array("-moz-margin-end", "-webkit-margin-end", null, null),
  3059. "margin-start" => array("-moz-margin-start", "-webkit-margin-start", null, null),
  3060. "margin-top-collapse" => array(null, "-webkit-margin-top-collapse", null, null),
  3061. "marquee " => array(null, "-webkit-marquee", null, null),
  3062. "marquee-direction" => array(null, "-webkit-marquee-direction", null, null),
  3063. "marquee-increment" => array(null, "-webkit-marquee-increment", null, null),
  3064. "marquee-repetition" => array(null, "-webkit-marquee-repetition", null, null),
  3065. "marquee-speed" => array(null, "-webkit-marquee-speed", null, null),
  3066. "marquee-style" => array(null, "-webkit-marquee-style", null, null),
  3067. "mask" => array(null, "-webkit-mask", null, null),
  3068. "mask-attachment" => array(null, "-webkit-mask-attachment", null, null),
  3069. "mask-box-image" => array(null, "-webkit-mask-box-image", null, null),
  3070. "mask-clip" => array(null, "-webkit-mask-clip", null, null),
  3071. "mask-composite" => array(null, "-webkit-mask-composite", null, null),
  3072. "mask-image" => array(null, "-webkit-mask-image", null, null),
  3073. "mask-origin" => array(null, "-webkit-mask-origin", null, null),
  3074. "mask-position" => array(null, "-webkit-mask-position", null, null),
  3075. "mask-position-x" => array(null, "-webkit-mask-position-x", null, null),
  3076. "mask-position-y" => array(null, "-webkit-mask-position-y", null, null),
  3077. "mask-repeat" => array(null, "-webkit-mask-repeat", null, null),
  3078. "mask-repeat-x" => array(null, "-webkit-mask-repeat-x", null, null),
  3079. "mask-repeat-y" => array(null, "-webkit-mask-repeat-y", null, null),
  3080. "mask-size" => array(null, "-webkit-mask-size", null, null),
  3081. "match-nearest-mail-blockquote-color" => array(null, "-webkit-match-nearest-mail-blockquote-color", null, null),
  3082. "max-logical-height" => array(null, "-webkit-max-logical-height", null, null),
  3083. "max-logical-width" => array(null, "-webkit-max-logical-width", null, null),
  3084. "min-logical-height" => array(null, "-webkit-min-logical-height", null, null),
  3085. "min-logical-width" => array(null, "-webkit-min-logical-width", null, null),
  3086. "object-fit" => array(null, null, "-o-object-fit", null),
  3087. "object-position" => array(null, null, "-o-object-position", null),
  3088. "opacity" => array(__CLASS__, "opacity"),
  3089. "outline-radius" => array("-moz-outline-radius", null, null, null),
  3090. "outline-bottom-left-radius" => array("-moz-outline-radius-bottomleft", null, null, null),
  3091. "outline-bottom-right-radius" => array("-moz-outline-radius-bottomright", null, null, null),
  3092. "outline-top-left-radius" => array("-moz-outline-radius-topleft", null, null, null),
  3093. "outline-top-right-radius" => array("-moz-outline-radius-topright", null, null, null),
  3094. "padding-after" => array(null, "-webkit-padding-after", null, null),
  3095. "padding-before" => array(null, "-webkit-padding-before", null, null),
  3096. "padding-end" => array("-moz-padding-end", "-webkit-padding-end", null, null),
  3097. "padding-start" => array("-moz-padding-start", "-webkit-padding-start", null, null),
  3098. "perspective" => array(null, "-webkit-perspective", null, null),
  3099. "perspective-origin" => array(null, "-webkit-perspective-origin", null, null),
  3100. "perspective-origin-x" => array(null, "-webkit-perspective-origin-x", null, null),
  3101. "perspective-origin-y" => array(null, "-webkit-perspective-origin-y", null, null),
  3102. "rtl-ordering" => array(null, "-webkit-rtl-ordering", null, null),
  3103. "scrollbar-3dlight-color" => array(null, null, null, "-ms-scrollbar-3dlight-color"),
  3104. "scrollbar-arrow-color" => array(null, null, null, "-ms-scrollbar-arrow-color"),
  3105. "scrollbar-base-color" => array(null, null, null, "-ms-scrollbar-base-color"),
  3106. "scrollbar-darkshadow-color" => array(null, null, null, "-ms-scrollbar-darkshadow-color"),
  3107. "scrollbar-face-color" => array(null, null, null, "-ms-scrollbar-face-color"),
  3108. "scrollbar-highlight-color" => array(null, null, null, "-ms-scrollbar-highlight-color"),
  3109. "scrollbar-shadow-color" => array(null, null, null, "-ms-scrollbar-shadow-color"),
  3110. "scrollbar-track-color" => array(null, null, null, "-ms-scrollbar-track-color"),
  3111. "stack-sizing" => array("-moz-stack-sizing", null, null, null),
  3112. "svg-shadow" => array(null, "-webkit-svg-shadow", null, null),
  3113. "tab-size" => array("-moz-tab-size", null, "-o-tab-size", null),
  3114. "table-baseline" => array(null, null, "-o-table-baseline", null),
  3115. "text-align-last" => array(null, null, null, "-ms-text-align-last"),
  3116. "text-autospace" => array(null, null, null, "-ms-text-autospace"),
  3117. "text-combine" => array(null, "-webkit-text-combine", null, null),
  3118. "text-decorations-in-effect" => array(null, "-webkit-text-decorations-in-effect", null, null),
  3119. "text-emphasis" => array(null, "-webkit-text-emphasis", null, null),
  3120. "text-emphasis-color" => array(null, "-webkit-text-emphasis-color", null, null),
  3121. "text-emphasis-position" => array(null, "-webkit-text-emphasis-position", null, null),
  3122. "text-emphasis-style" => array(null, "-webkit-text-emphasis-style", null, null),
  3123. "text-fill-color" => array(null, "-webkit-text-fill-color", null, null),
  3124. "text-justify" => array(null, null, null, "-ms-text-justify"),
  3125. "text-kashida-space" => array(null, null, null, "-ms-text-kashida-space"),
  3126. "text-overflow" => array(null, null, "-o-text-overflow", "-ms-text-overflow"),
  3127. "text-security" => array(null, "-webkit-text-security", null, null),
  3128. "text-size-adjust" => array(null, "-webkit-text-size-adjust", null, "-ms-text-size-adjust"),
  3129. "text-stroke" => array(null, "-webkit-text-stroke", null, null),
  3130. "text-stroke-color" => array(null, "-webkit-text-stroke-color", null, null),
  3131. "text-stroke-width" => array(null, "-webkit-text-stroke-width", null, null),
  3132. "text-underline-position" => array(null, null, null, "-ms-text-underline-position"),
  3133. "transform" => array("-moz-transform", "-webkit-transform", "-o-transform", null),
  3134. "transform-origin" => array("-moz-transform-origin", "-webkit-transform-origin", "-o-transform-origin", null),
  3135. "transform-origin-x" => array(null, "-webkit-transform-origin-x", null, null),
  3136. "transform-origin-y" => array(null, "-webkit-transform-origin-y", null, null),
  3137. "transform-origin-z" => array(null, "-webkit-transform-origin-z", null, null),
  3138. "transform-style" => array(null, "-webkit-transform-style", null, null),
  3139. "transition" => array("-moz-transition", "-webkit-transition", "-o-transition", null),
  3140. "transition-delay" => array("-moz-transition-delay", "-webkit-transition-delay", "-o-transition-delay", null),
  3141. "transition-duration" => array("-moz-transition-duration", "-webkit-transition-duration", "-o-transition-duration", null),
  3142. "transition-property" => array("-moz-transition-property", "-webkit-transition-property", "-o-transition-property", null),
  3143. "transition-timing-function" => array("-moz-transition-timing-function", "-webkit-transition-timing-function", "-o-transition-timing-function", null),
  3144. "user-drag" => array(null, "-webkit-user-drag", null, null),
  3145. "user-focus" => array("-moz-user-focus", null, null, null),
  3146. "user-input" => array("-moz-user-input", null, null, null),
  3147. "user-modify" => array("-moz-user-modify", "-webkit-user-modify", null, null),
  3148. "user-select" => array("-moz-user-select", "-webkit-user-select", null, null),
  3149. "white-space" => array(__CLASS__, "whiteSpace"),
  3150. "window-shadow" => array("-moz-window-shadow", null, null, null),
  3151. "word-break" => array(null, null, null, "-ms-word-break"),
  3152. "word-wrap" => array(null, null, null, "-ms-word-wrap"),
  3153. "writing-mode" => array(null, "-webkit-writing-mode", null, "-ms-writing-mode"),
  3154. "zoom" => array(null, null, null, "-ms-zoom")
  3155. );
  3156. /**
  3157. * Implements {@link aCssMinifierFilter::filter()}.
  3158. *
  3159. * @param array $tokens Array of objects of type aCssToken
  3160. * @return integer Count of added, changed or removed tokens; a return value large than 0 will rebuild the array
  3161. */
  3162. public function apply(array &$tokens)
  3163. {
  3164. $r = 0;
  3165. $transformations = &$this->transformations;
  3166. for ($i = 0, $l = count($tokens); $i < $l; $i++)
  3167. {
  3168. if (get_class($tokens[$i]) === "CssRulesetDeclarationToken")
  3169. {
  3170. $tProperty = $tokens[$i]->Property;
  3171. if (isset($transformations[$tProperty]))
  3172. {
  3173. $result = array();
  3174. if (is_callable($transformations[$tProperty]))
  3175. {
  3176. $result = call_user_func_array($transformations[$tProperty], array($tokens[$i]));
  3177. if (!is_array($result) && is_object($result))
  3178. {
  3179. $result = array($result);
  3180. }
  3181. }
  3182. else
  3183. {
  3184. $tValue = $tokens[$i]->Value;
  3185. $tMediaTypes = $tokens[$i]->MediaTypes;
  3186. foreach ($transformations[$tProperty] as $property)
  3187. {
  3188. if ($property !== null)
  3189. {
  3190. $result[] = new CssRulesetDeclarationToken($property, $tValue, $tMediaTypes);
  3191. }
  3192. }
  3193. }
  3194. if (count($result) > 0)
  3195. {
  3196. array_splice($tokens, $i + 1, 0, $result);
  3197. $i += count($result);
  3198. $l += count($result);
  3199. }
  3200. }
  3201. }
  3202. }
  3203. return $r;
  3204. }
  3205. /**
  3206. * Transforms the Internet Explorer specific declaration property "filter" to Internet Explorer 8+ compatible
  3207. * declaratiopn property "-ms-filter".
  3208. *
  3209. * @param aCssToken $token
  3210. * @return array
  3211. */
  3212. private static function filter($token)
  3213. {
  3214. $r = array
  3215. (
  3216. new CssRulesetDeclarationToken("-ms-filter", "\"" . $token->Value . "\"", $token->MediaTypes),
  3217. );
  3218. return $r;
  3219. }
  3220. /**
  3221. * Transforms "opacity: {value}" into browser specific counterparts.
  3222. *
  3223. * @param aCssToken $token
  3224. * @return array
  3225. */
  3226. private static function opacity($token)
  3227. {
  3228. // Calculate the value for Internet Explorer filter statement
  3229. $ieValue = (int) ((float) $token->Value * 100);
  3230. $r = array
  3231. (
  3232. // Internet Explorer >= 8
  3233. new CssRulesetDeclarationToken("-ms-filter", "\"alpha(opacity=" . $ieValue . ")\"", $token->MediaTypes),
  3234. // Internet Explorer >= 4 <= 7
  3235. new CssRulesetDeclarationToken("filter", "alpha(opacity=" . $ieValue . ")", $token->MediaTypes),
  3236. new CssRulesetDeclarationToken("zoom", "1", $token->MediaTypes)
  3237. );
  3238. return $r;
  3239. }
  3240. /**
  3241. * Transforms "white-space: pre-wrap" into browser specific counterparts.
  3242. *
  3243. * @param aCssToken $token
  3244. * @return array
  3245. */
  3246. private static function whiteSpace($token)
  3247. {
  3248. if (strtolower($token->Value) === "pre-wrap")
  3249. {
  3250. $r = array
  3251. (
  3252. // Firefox < 3
  3253. new CssRulesetDeclarationToken("white-space", "-moz-pre-wrap", $token->MediaTypes),
  3254. // Webkit
  3255. new CssRulesetDeclarationToken("white-space", "-webkit-pre-wrap", $token->MediaTypes),
  3256. // Opera >= 4 <= 6
  3257. new CssRulesetDeclarationToken("white-space", "-pre-wrap", $token->MediaTypes),
  3258. // Opera >= 7
  3259. new CssRulesetDeclarationToken("white-space", "-o-pre-wrap", $token->MediaTypes),
  3260. // Internet Explorer >= 5.5
  3261. new CssRulesetDeclarationToken("word-wrap", "break-word", $token->MediaTypes)
  3262. );
  3263. return $r;
  3264. }
  3265. else
  3266. {
  3267. return array();
  3268. }
  3269. }
  3270. }
  3271. /**
  3272. * This {@link aCssMinifierFilter minifier filter} will convert @keyframes at-rule block to browser specific counterparts.
  3273. *
  3274. * @package CssMin/Minifier/Filters
  3275. * @link http://code.google.com/p/cssmin/
  3276. * @author Joe Scylla <joe.scylla@gmail.com>
  3277. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  3278. * @license http://opensource.org/licenses/mit-license.php MIT License
  3279. * @version 3.0.1
  3280. */
  3281. class CssConvertLevel3AtKeyframesMinifierFilter extends aCssMinifierFilter
  3282. {
  3283. /**
  3284. * Implements {@link aCssMinifierFilter::filter()}.
  3285. *
  3286. * @param array $tokens Array of objects of type aCssToken
  3287. * @return integer Count of added, changed or removed tokens; a return value larger than 0 will rebuild the array
  3288. */
  3289. public function apply(array &$tokens)
  3290. {
  3291. $r = 0;
  3292. $transformations = array("-moz-keyframes", "-webkit-keyframes");
  3293. for ($i = 0, $l = count($tokens); $i < $l; $i++)
  3294. {
  3295. if (get_class($tokens[$i]) === "CssAtKeyframesStartToken")
  3296. {
  3297. for ($ii = $i; $ii < $l; $ii++)
  3298. {
  3299. if (get_class($tokens[$ii]) === "CssAtKeyframesEndToken")
  3300. {
  3301. break;
  3302. }
  3303. }
  3304. if (get_class($tokens[$ii]) === "CssAtKeyframesEndToken")
  3305. {
  3306. $add = array();
  3307. $source = array();
  3308. for ($iii = $i; $iii <= $ii; $iii++)
  3309. {
  3310. $source[] = clone($tokens[$iii]);
  3311. }
  3312. foreach ($transformations as $transformation)
  3313. {
  3314. $t = array();
  3315. foreach ($source as $token)
  3316. {
  3317. $t[] = clone($token);
  3318. }
  3319. $t[0]->AtRuleName = $transformation;
  3320. $add = array_merge($add, $t);
  3321. }
  3322. if (isset($this->configuration["RemoveSource"]) && $this->configuration["RemoveSource"] === true)
  3323. {
  3324. array_splice($tokens, $i, $ii - $i + 1, $add);
  3325. }
  3326. else
  3327. {
  3328. array_splice($tokens, $ii + 1, 0, $add);
  3329. }
  3330. $l = count($tokens);
  3331. $i = $ii + count($add);
  3332. $r += count($add);
  3333. }
  3334. }
  3335. }
  3336. return $r;
  3337. }
  3338. }
  3339. /**
  3340. * This {@link aCssMinifierPlugin} will convert a color value in hsl notation to hexadecimal notation.
  3341. *
  3342. * Example:
  3343. * <code>
  3344. * color: hsl(232,36%,48%);
  3345. * </code>
  3346. *
  3347. * Will get converted to:
  3348. * <code>
  3349. * color:#4e5aa7;
  3350. * </code>
  3351. *
  3352. * @package CssMin/Minifier/Plugins
  3353. * @link http://code.google.com/p/cssmin/
  3354. * @author Joe Scylla <joe.scylla@gmail.com>
  3355. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  3356. * @license http://opensource.org/licenses/mit-license.php MIT License
  3357. * @version 3.0.1
  3358. */
  3359. class CssConvertHslColorsMinifierPlugin extends aCssMinifierPlugin
  3360. {
  3361. /**
  3362. * Regular expression matching the value.
  3363. *
  3364. * @var string
  3365. */
  3366. private $reMatch = "/^hsl\s*\(\s*([0-9]+)\s*,\s*([0-9]+)\s*%\s*,\s*([0-9]+)\s*%\s*\)/iS";
  3367. /**
  3368. * Implements {@link aCssMinifierPlugin::minify()}.
  3369. *
  3370. * @param aCssToken $token Token to process
  3371. * @return boolean Return TRUE to break the processing of this token; FALSE to continue
  3372. */
  3373. public function apply(aCssToken &$token)
  3374. {
  3375. if (stripos($token->Value, "hsl") !== false && preg_match($this->reMatch, $token->Value, $m))
  3376. {
  3377. $token->Value = str_replace($m[0], $this->hsl2hex($m[1], $m[2], $m[3]), $token->Value);
  3378. }
  3379. return false;
  3380. }
  3381. /**
  3382. * Implements {@link aMinifierPlugin::getTriggerTokens()}
  3383. *
  3384. * @return array
  3385. */
  3386. public function getTriggerTokens()
  3387. {
  3388. return array
  3389. (
  3390. "CssAtFontFaceDeclarationToken",
  3391. "CssAtPageDeclarationToken",
  3392. "CssRulesetDeclarationToken"
  3393. );
  3394. }
  3395. /**
  3396. * Convert a HSL value to hexadecimal notation.
  3397. *
  3398. * Based on: {@link http://www.easyrgb.com/index.php?X=MATH&H=19#text19}.
  3399. *
  3400. * @param integer $hue Hue
  3401. * @param integer $saturation Saturation
  3402. * @param integer $lightness Lightnesss
  3403. * @return string
  3404. */
  3405. private function hsl2hex($hue, $saturation, $lightness)
  3406. {
  3407. $hue = $hue / 360;
  3408. $saturation = $saturation / 100;
  3409. $lightness = $lightness / 100;
  3410. if ($saturation == 0)
  3411. {
  3412. $red = $lightness * 255;
  3413. $green = $lightness * 255;
  3414. $blue = $lightness * 255;
  3415. }
  3416. else
  3417. {
  3418. if ($lightness < 0.5 )
  3419. {
  3420. $v2 = $lightness * (1 + $saturation);
  3421. }
  3422. else
  3423. {
  3424. $v2 = ($lightness + $saturation) - ($saturation * $lightness);
  3425. }
  3426. $v1 = 2 * $lightness - $v2;
  3427. $red = 255 * self::hue2rgb($v1, $v2, $hue + (1 / 3));
  3428. $green = 255 * self::hue2rgb($v1, $v2, $hue);
  3429. $blue = 255 * self::hue2rgb($v1, $v2, $hue - (1 / 3));
  3430. }
  3431. return "#" . str_pad(dechex(round($red)), 2, "0", STR_PAD_LEFT) . str_pad(dechex(round($green)), 2, "0", STR_PAD_LEFT) . str_pad(dechex(round($blue)), 2, "0", STR_PAD_LEFT);
  3432. }
  3433. /**
  3434. * Apply hue to a rgb color value.
  3435. *
  3436. * @param integer $v1 Value 1
  3437. * @param integer $v2 Value 2
  3438. * @param integer $hue Hue
  3439. * @return integer
  3440. */
  3441. private function hue2rgb($v1, $v2, $hue)
  3442. {
  3443. if ($hue < 0)
  3444. {
  3445. $hue += 1;
  3446. }
  3447. if ($hue > 1)
  3448. {
  3449. $hue -= 1;
  3450. }
  3451. if ((6 * $hue) < 1)
  3452. {
  3453. return ($v1 + ($v2 - $v1) * 6 * $hue);
  3454. }
  3455. if ((2 * $hue) < 1)
  3456. {
  3457. return ($v2);
  3458. }
  3459. if ((3 * $hue) < 2)
  3460. {
  3461. return ($v1 + ($v2 - $v1) * (( 2 / 3) - $hue) * 6);
  3462. }
  3463. return $v1;
  3464. }
  3465. }
  3466. /**
  3467. * This {@link aCssMinifierPlugin} will convert the font-weight values normal and bold to their numeric notation.
  3468. *
  3469. * Example:
  3470. * <code>
  3471. * font-weight: normal;
  3472. * font: bold 11px monospace;
  3473. * </code>
  3474. *
  3475. * Will get converted to:
  3476. * <code>
  3477. * font-weight:400;
  3478. * font:700 11px monospace;
  3479. * </code>
  3480. *
  3481. * @package CssMin/Minifier/Pluginsn
  3482. * @link http://code.google.com/p/cssmin/
  3483. * @author Joe Scylla <joe.scylla@gmail.com>
  3484. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  3485. * @license http://opensource.org/licenses/mit-license.php MIT License
  3486. * @version 3.0.1
  3487. */
  3488. class CssConvertFontWeightMinifierPlugin extends aCssMinifierPlugin
  3489. {
  3490. /**
  3491. * Array of included declaration properties this plugin will process; others declaration properties will get
  3492. * ignored.
  3493. *
  3494. * @var array
  3495. */
  3496. private $include = array
  3497. (
  3498. "font",
  3499. "font-weight"
  3500. );
  3501. /**
  3502. * Regular expression matching the value.
  3503. *
  3504. * @var string
  3505. */
  3506. private $reMatch = null;
  3507. /**
  3508. * Regular expression replace the value.
  3509. *
  3510. * @var string
  3511. */
  3512. private $reReplace = "\"\${1}\" . \$this->transformation[\"\${2}\"] . \"\${3}\"";
  3513. /**
  3514. * Transformation table used by the {@link CssConvertFontWeightMinifierPlugin::$reReplace replace regular expression}.
  3515. *
  3516. * @var array
  3517. */
  3518. private $transformation = array
  3519. (
  3520. "normal" => "400",
  3521. "bold" => "700"
  3522. );
  3523. /**
  3524. * Overwrites {@link aCssMinifierPlugin::__construct()}.
  3525. *
  3526. * The constructor will create the {@link CssConvertFontWeightMinifierPlugin::$reReplace replace regular expression}
  3527. * based on the {@link CssConvertFontWeightMinifierPlugin::$transformation transformation table}.
  3528. *
  3529. * @param CssMinifier $minifier The CssMinifier object of this plugin.
  3530. * @return void
  3531. */
  3532. public function __construct(CssMinifier $minifier)
  3533. {
  3534. $this->reMatch = "/(^|\s)+(" . implode("|", array_keys($this->transformation)). ")(\s|$)+/eiS";
  3535. parent::__construct($minifier);
  3536. }
  3537. /**
  3538. * Implements {@link aCssMinifierPlugin::minify()}.
  3539. *
  3540. * @param aCssToken $token Token to process
  3541. * @return boolean Return TRUE to break the processing of this token; FALSE to continue
  3542. */
  3543. public function apply(aCssToken &$token)
  3544. {
  3545. if (in_array($token->Property, $this->include) && preg_match($this->reMatch, $token->Value, $m))
  3546. {
  3547. $token->Value = preg_replace($this->reMatch, $this->reReplace, $token->Value);
  3548. }
  3549. return false;
  3550. }
  3551. /**
  3552. * Implements {@link aMinifierPlugin::getTriggerTokens()}
  3553. *
  3554. * @return array
  3555. */
  3556. public function getTriggerTokens()
  3557. {
  3558. return array
  3559. (
  3560. "CssAtFontFaceDeclarationToken",
  3561. "CssAtPageDeclarationToken",
  3562. "CssRulesetDeclarationToken"
  3563. );
  3564. }
  3565. }
  3566. /**
  3567. * This {@link aCssMinifierPlugin} will compress several unit values to their short notations. Examples:
  3568. *
  3569. * <code>
  3570. * padding: 0.5em;
  3571. * border: 0px;
  3572. * margin: 0 0 0 0;
  3573. * </code>
  3574. *
  3575. * Will get compressed to:
  3576. *
  3577. * <code>
  3578. * padding:.5px;
  3579. * border:0;
  3580. * margin:0;
  3581. * </code>
  3582. *
  3583. * --
  3584. *
  3585. * @package CssMin/Minifier/Plugins
  3586. * @link http://code.google.com/p/cssmin/
  3587. * @author Joe Scylla <joe.scylla@gmail.com>
  3588. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  3589. * @license http://opensource.org/licenses/mit-license.php MIT License
  3590. * @version 3.0.1
  3591. */
  3592. class CssCompressUnitValuesMinifierPlugin extends aCssMinifierPlugin
  3593. {
  3594. /**
  3595. * Regular expression used for matching and replacing unit values.
  3596. *
  3597. * @var array
  3598. */
  3599. private $re = array
  3600. (
  3601. "/(^| |-)0\.([0-9]+?)(0+)?(%|em|ex|px|in|cm|mm|pt|pc)/iS" => "\${1}.\${2}\${4}",
  3602. "/(^| )-?(\.?)0(%|em|ex|px|in|cm|mm|pt|pc)/iS" => "\${1}0",
  3603. "/(^0\s0\s0\s0)|(^0\s0\s0$)|(^0\s0$)/iS" => "0"
  3604. );
  3605. /**
  3606. * Regular expression matching the value.
  3607. *
  3608. * @var string
  3609. */
  3610. private $reMatch = "/(^| |-)0\.([0-9]+?)(0+)?(%|em|ex|px|in|cm|mm|pt|pc)|(^| )-?(\.?)0(%|em|ex|px|in|cm|mm|pt|pc)|(^0\s0\s0\s0$)|(^0\s0\s0$)|(^0\s0$)/iS";
  3611. /**
  3612. * Implements {@link aCssMinifierPlugin::minify()}.
  3613. *
  3614. * @param aCssToken $token Token to process
  3615. * @return boolean Return TRUE to break the processing of this token; FALSE to continue
  3616. */
  3617. public function apply(aCssToken &$token)
  3618. {
  3619. if (preg_match($this->reMatch, $token->Value))
  3620. {
  3621. foreach ($this->re as $reMatch => $reReplace)
  3622. {
  3623. $token->Value = preg_replace($reMatch, $reReplace, $token->Value);
  3624. }
  3625. }
  3626. return false;
  3627. }
  3628. /**
  3629. * Implements {@link aMinifierPlugin::getTriggerTokens()}
  3630. *
  3631. * @return array
  3632. */
  3633. public function getTriggerTokens()
  3634. {
  3635. return array
  3636. (
  3637. "CssAtFontFaceDeclarationToken",
  3638. "CssAtPageDeclarationToken",
  3639. "CssRulesetDeclarationToken"
  3640. );
  3641. }
  3642. }
  3643. /**
  3644. * This {@link aCssMinifierPlugin} compress the content of expresssion() declaration values.
  3645. *
  3646. * For compression of expressions {@link https://github.com/rgrove/jsmin-php/ JSMin} will get used. JSMin have to be
  3647. * already included or loadable via {@link http://goo.gl/JrW54 PHP autoloading}.
  3648. *
  3649. * @package CssMin/Minifier/Plugins
  3650. * @link http://code.google.com/p/cssmin/
  3651. * @author Joe Scylla <joe.scylla@gmail.com>
  3652. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  3653. * @license http://opensource.org/licenses/mit-license.php MIT License
  3654. * @version 3.0.1
  3655. */
  3656. class CssCompressExpressionValuesMinifierPlugin extends aCssMinifierPlugin
  3657. {
  3658. /**
  3659. * Implements {@link aCssMinifierPlugin::minify()}.
  3660. *
  3661. * @param aCssToken $token Token to process
  3662. * @return boolean Return TRUE to break the processing of this token; FALSE to continue
  3663. */
  3664. public function apply(aCssToken &$token)
  3665. {
  3666. if (class_exists("JSMin") && stripos($token->Value, "expression(") !== false)
  3667. {
  3668. $value = $token->Value;
  3669. $value = substr($token->Value, stripos($token->Value, "expression(") + 10);
  3670. $value = trim(JSMin::minify($value));
  3671. $token->Value = "expression(" . $value . ")";
  3672. }
  3673. return false;
  3674. }
  3675. /**
  3676. * Implements {@link aMinifierPlugin::getTriggerTokens()}
  3677. *
  3678. * @return array
  3679. */
  3680. public function getTriggerTokens()
  3681. {
  3682. return array
  3683. (
  3684. "CssAtFontFaceDeclarationToken",
  3685. "CssAtPageDeclarationToken",
  3686. "CssRulesetDeclarationToken"
  3687. );
  3688. }
  3689. }
  3690. /**
  3691. * This {@link aCssMinifierPlugin} will convert hexadecimal color value with 6 chars to their 3 char hexadecimal
  3692. * notation (if possible).
  3693. *
  3694. * Example:
  3695. * <code>
  3696. * color: #aabbcc;
  3697. * </code>
  3698. *
  3699. * Will get converted to:
  3700. * <code>
  3701. * color:#abc;
  3702. * </code>
  3703. *
  3704. * @package CssMin/Minifier/Plugins
  3705. * @link http://code.google.com/p/cssmin/
  3706. * @author Joe Scylla <joe.scylla@gmail.com>
  3707. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  3708. * @license http://opensource.org/licenses/mit-license.php MIT License
  3709. * @version 3.0.1
  3710. */
  3711. class CssCompressColorValuesMinifierPlugin extends aCssMinifierPlugin
  3712. {
  3713. /**
  3714. * Regular expression matching 6 char hexadecimal color values.
  3715. *
  3716. * @var string
  3717. */
  3718. private $reMatch = "/\#([0-9a-f]{6})/iS";
  3719. /**
  3720. * Implements {@link aCssMinifierPlugin::minify()}.
  3721. *
  3722. * @param aCssToken $token Token to process
  3723. * @return boolean Return TRUE to break the processing of this token; FALSE to continue
  3724. */
  3725. public function apply(aCssToken &$token)
  3726. {
  3727. if (strpos($token->Value, "#") !== false && preg_match($this->reMatch, $token->Value, $m))
  3728. {
  3729. $value = strtolower($m[1]);
  3730. if ($value[0] == $value[1] && $value[2] == $value[3] && $value[4] == $value[5])
  3731. {
  3732. $token->Value = str_replace($m[0], "#" . $value[0] . $value[2] . $value[4], $token->Value);
  3733. }
  3734. }
  3735. return false;
  3736. }
  3737. /**
  3738. * Implements {@link aMinifierPlugin::getTriggerTokens()}
  3739. *
  3740. * @return array
  3741. */
  3742. public function getTriggerTokens()
  3743. {
  3744. return array
  3745. (
  3746. "CssAtFontFaceDeclarationToken",
  3747. "CssAtPageDeclarationToken",
  3748. "CssRulesetDeclarationToken"
  3749. );
  3750. }
  3751. }
  3752. /**
  3753. * This {@link aCssToken CSS token} represents a CSS comment.
  3754. *
  3755. * @package CssMin/Tokens
  3756. * @link http://code.google.com/p/cssmin/
  3757. * @author Joe Scylla <joe.scylla@gmail.com>
  3758. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  3759. * @license http://opensource.org/licenses/mit-license.php MIT License
  3760. * @version 3.0.1
  3761. */
  3762. class CssCommentToken extends aCssToken
  3763. {
  3764. /**
  3765. * Comment as Text.
  3766. *
  3767. * @var string
  3768. */
  3769. public $Comment = "";
  3770. /**
  3771. * Set the properties of a comment token.
  3772. *
  3773. * @param string $comment Comment including comment delimiters
  3774. * @return void
  3775. */
  3776. public function __construct($comment)
  3777. {
  3778. $this->Comment = $comment;
  3779. }
  3780. /**
  3781. * Implements {@link aCssToken::__toString()}.
  3782. *
  3783. * @return string
  3784. */
  3785. public function __toString()
  3786. {
  3787. return $this->Comment;
  3788. }
  3789. }
  3790. /**
  3791. * {@link aCssParserPlugin Parser plugin} for parsing comments.
  3792. *
  3793. * Adds a {@link CssCommentToken} to the parser if a comment was found.
  3794. *
  3795. * @package CssMin/Parser/Plugins
  3796. * @link http://code.google.com/p/cssmin/
  3797. * @author Joe Scylla <joe.scylla@gmail.com>
  3798. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  3799. * @license http://opensource.org/licenses/mit-license.php MIT License
  3800. * @version 3.0.1
  3801. */
  3802. class CssCommentParserPlugin extends aCssParserPlugin
  3803. {
  3804. /**
  3805. * Implements {@link aCssParserPlugin::getTriggerChars()}.
  3806. *
  3807. * @return array
  3808. */
  3809. public function getTriggerChars()
  3810. {
  3811. return array("*", "/");
  3812. }
  3813. /**
  3814. * Implements {@link aCssParserPlugin::getTriggerStates()}.
  3815. *
  3816. * @return array
  3817. */
  3818. public function getTriggerStates()
  3819. {
  3820. return false;
  3821. }
  3822. /**
  3823. * Stored buffer for restore.
  3824. *
  3825. * @var string
  3826. */
  3827. private $restoreBuffer = "";
  3828. /**
  3829. * Implements {@link aCssParserPlugin::parse()}.
  3830. *
  3831. * @param integer $index Current index
  3832. * @param string $char Current char
  3833. * @param string $previousChar Previous char
  3834. * @return mixed TRUE will break the processing; FALSE continue with the next plugin; integer set a new index and break the processing
  3835. */
  3836. public function parse($index, $char, $previousChar, $state)
  3837. {
  3838. if ($char === "*" && $previousChar === "/" && $state !== "T_COMMENT")
  3839. {
  3840. $this->parser->pushState("T_COMMENT");
  3841. $this->parser->setExclusive(__CLASS__);
  3842. $this->restoreBuffer = substr($this->parser->getAndClearBuffer(), 0, -2);
  3843. }
  3844. elseif ($char === "/" && $previousChar === "*" && $state === "T_COMMENT")
  3845. {
  3846. $this->parser->popState();
  3847. $this->parser->unsetExclusive();
  3848. $this->parser->appendToken(new CssCommentToken("/*" . $this->parser->getAndClearBuffer()));
  3849. $this->parser->setBuffer($this->restoreBuffer);
  3850. }
  3851. else
  3852. {
  3853. return false;
  3854. }
  3855. return true;
  3856. }
  3857. }
  3858. /**
  3859. * This {@link aCssToken CSS token} represents the start of a @variables at-rule block.
  3860. *
  3861. * @package CssMin/Tokens
  3862. * @link http://code.google.com/p/cssmin/
  3863. * @author Joe Scylla <joe.scylla@gmail.com>
  3864. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  3865. * @license http://opensource.org/licenses/mit-license.php MIT License
  3866. * @version 3.0.1
  3867. */
  3868. class CssAtVariablesStartToken extends aCssAtBlockStartToken
  3869. {
  3870. /**
  3871. * Media types of the @variables at-rule block.
  3872. *
  3873. * @var array
  3874. */
  3875. public $MediaTypes = array();
  3876. /**
  3877. * Set the properties of a @variables at-rule token.
  3878. *
  3879. * @param array $mediaTypes Media types
  3880. * @return void
  3881. */
  3882. public function __construct($mediaTypes = null)
  3883. {
  3884. $this->MediaTypes = $mediaTypes ? $mediaTypes : array("all");
  3885. }
  3886. /**
  3887. * Implements {@link aCssToken::__toString()}.
  3888. *
  3889. * @return string
  3890. */
  3891. public function __toString()
  3892. {
  3893. return "";
  3894. }
  3895. }
  3896. /**
  3897. * {@link aCssParserPlugin Parser plugin} for parsing @variables at-rule block with including declarations.
  3898. *
  3899. * Found @variables at-rule blocks will add a {@link CssAtVariablesStartToken} and {@link CssAtVariablesEndToken} to the
  3900. * parser; including declarations as {@link CssAtVariablesDeclarationToken}.
  3901. *
  3902. * @package CssMin/Parser/Plugins
  3903. * @link http://code.google.com/p/cssmin/
  3904. * @author Joe Scylla <joe.scylla@gmail.com>
  3905. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  3906. * @license http://opensource.org/licenses/mit-license.php MIT License
  3907. * @version 3.0.1
  3908. */
  3909. class CssAtVariablesParserPlugin extends aCssParserPlugin
  3910. {
  3911. /**
  3912. * Implements {@link aCssParserPlugin::getTriggerChars()}.
  3913. *
  3914. * @return array
  3915. */
  3916. public function getTriggerChars()
  3917. {
  3918. return array("@", "{", "}", ":", ";");
  3919. }
  3920. /**
  3921. * Implements {@link aCssParserPlugin::getTriggerStates()}.
  3922. *
  3923. * @return array
  3924. */
  3925. public function getTriggerStates()
  3926. {
  3927. return array("T_DOCUMENT", "T_AT_VARIABLES::PREPARE", "T_AT_VARIABLES", "T_AT_VARIABLES_DECLARATION");
  3928. }
  3929. /**
  3930. * Implements {@link aCssParserPlugin::parse()}.
  3931. *
  3932. * @param integer $index Current index
  3933. * @param string $char Current char
  3934. * @param string $previousChar Previous char
  3935. * @return mixed TRUE will break the processing; FALSE continue with the next plugin; integer set a new index and break the processing
  3936. */
  3937. public function parse($index, $char, $previousChar, $state)
  3938. {
  3939. // Start of @variables at-rule block
  3940. if ($char === "@" && $state === "T_DOCUMENT" && strtolower(substr($this->parser->getSource(), $index, 10)) === "@variables")
  3941. {
  3942. $this->parser->pushState("T_AT_VARIABLES::PREPARE");
  3943. $this->parser->clearBuffer();
  3944. return $index + 10;
  3945. }
  3946. // Start of @variables declarations
  3947. elseif ($char === "{" && $state === "T_AT_VARIABLES::PREPARE")
  3948. {
  3949. $this->parser->setState("T_AT_VARIABLES");
  3950. $mediaTypes = array_filter(array_map("trim", explode(",", $this->parser->getAndClearBuffer("{"))));
  3951. $this->parser->appendToken(new CssAtVariablesStartToken($mediaTypes));
  3952. }
  3953. // Start of @variables declaration
  3954. if ($char === ":" && $state === "T_AT_VARIABLES")
  3955. {
  3956. $this->buffer = $this->parser->getAndClearBuffer(":");
  3957. $this->parser->pushState("T_AT_VARIABLES_DECLARATION");
  3958. }
  3959. // Unterminated @variables declaration
  3960. elseif ($char === ":" && $state === "T_AT_VARIABLES_DECLARATION")
  3961. {
  3962. // Ignore Internet Explorer filter declarations
  3963. if ($this->buffer === "filter")
  3964. {
  3965. return false;
  3966. }
  3967. CssMin::triggerError(new CssError(__FILE__, __LINE__, __METHOD__ . ": Unterminated @variables declaration", $this->buffer . ":" . $this->parser->getBuffer() . "_"));
  3968. }
  3969. // End of @variables declaration
  3970. elseif (($char === ";" || $char === "}") && $state === "T_AT_VARIABLES_DECLARATION")
  3971. {
  3972. $value = $this->parser->getAndClearBuffer(";}");
  3973. if (strtolower(substr($value, -10, 10)) === "!important")
  3974. {
  3975. $value = trim(substr($value, 0, -10));
  3976. $isImportant = true;
  3977. }
  3978. else
  3979. {
  3980. $isImportant = false;
  3981. }
  3982. $this->parser->popState();
  3983. $this->parser->appendToken(new CssAtVariablesDeclarationToken($this->buffer, $value, $isImportant));
  3984. $this->buffer = "";
  3985. }
  3986. // End of @variables at-rule block
  3987. elseif ($char === "}" && $state === "T_AT_VARIABLES")
  3988. {
  3989. $this->parser->popState();
  3990. $this->parser->clearBuffer();
  3991. $this->parser->appendToken(new CssAtVariablesEndToken());
  3992. }
  3993. else
  3994. {
  3995. return false;
  3996. }
  3997. return true;
  3998. }
  3999. }
  4000. /**
  4001. * This {@link aCssToken CSS token} represents the end of a @variables at-rule block.
  4002. *
  4003. * @package CssMin/Tokens
  4004. * @link http://code.google.com/p/cssmin/
  4005. * @author Joe Scylla <joe.scylla@gmail.com>
  4006. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  4007. * @license http://opensource.org/licenses/mit-license.php MIT License
  4008. * @version 3.0.1
  4009. */
  4010. class CssAtVariablesEndToken extends aCssAtBlockEndToken
  4011. {
  4012. /**
  4013. * Implements {@link aCssToken::__toString()}.
  4014. *
  4015. * @return string
  4016. */
  4017. public function __toString()
  4018. {
  4019. return "";
  4020. }
  4021. }
  4022. /**
  4023. * This {@link aCssToken CSS token} represents a declaration of a @variables at-rule block.
  4024. *
  4025. * @package CssMin/Tokens
  4026. * @link http://code.google.com/p/cssmin/
  4027. * @author Joe Scylla <joe.scylla@gmail.com>
  4028. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  4029. * @license http://opensource.org/licenses/mit-license.php MIT License
  4030. * @version 3.0.1
  4031. */
  4032. class CssAtVariablesDeclarationToken extends aCssDeclarationToken
  4033. {
  4034. /**
  4035. * Implements {@link aCssToken::__toString()}.
  4036. *
  4037. * @return string
  4038. */
  4039. public function __toString()
  4040. {
  4041. return "";
  4042. }
  4043. }
  4044. /**
  4045. * This {@link aCssToken CSS token} represents the start of a @page at-rule block.
  4046. *
  4047. * @package CssMin/Tokens
  4048. * @link http://code.google.com/p/cssmin/
  4049. * @author Joe Scylla <joe.scylla@gmail.com>
  4050. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  4051. * @license http://opensource.org/licenses/mit-license.php MIT License
  4052. * @version 3.0.1
  4053. */
  4054. class CssAtPageStartToken extends aCssAtBlockStartToken
  4055. {
  4056. /**
  4057. * Selector.
  4058. *
  4059. * @var string
  4060. */
  4061. public $Selector = "";
  4062. /**
  4063. * Sets the properties of the @page at-rule.
  4064. *
  4065. * @param string $selector Selector
  4066. * @return void
  4067. */
  4068. public function __construct($selector = "")
  4069. {
  4070. $this->Selector = $selector;
  4071. }
  4072. /**
  4073. * Implements {@link aCssToken::__toString()}.
  4074. *
  4075. * @return string
  4076. */
  4077. public function __toString()
  4078. {
  4079. return "@page" . ($this->Selector ? " " . $this->Selector : "") . "{";
  4080. }
  4081. }
  4082. /**
  4083. * {@link aCssParserPlugin Parser plugin} for parsing @page at-rule block with including declarations.
  4084. *
  4085. * Found @page at-rule blocks will add a {@link CssAtPageStartToken} and {@link CssAtPageEndToken} to the
  4086. * parser; including declarations as {@link CssAtPageDeclarationToken}.
  4087. *
  4088. * @package CssMin/Parser/Plugins
  4089. * @link http://code.google.com/p/cssmin/
  4090. * @author Joe Scylla <joe.scylla@gmail.com>
  4091. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  4092. * @license http://opensource.org/licenses/mit-license.php MIT License
  4093. * @version 3.0.1
  4094. */
  4095. class CssAtPageParserPlugin extends aCssParserPlugin
  4096. {
  4097. /**
  4098. * Implements {@link aCssParserPlugin::getTriggerChars()}.
  4099. *
  4100. * @return array
  4101. */
  4102. public function getTriggerChars()
  4103. {
  4104. return array("@", "{", "}", ":", ";");
  4105. }
  4106. /**
  4107. * Implements {@link aCssParserPlugin::getTriggerStates()}.
  4108. *
  4109. * @return array
  4110. */
  4111. public function getTriggerStates()
  4112. {
  4113. return array("T_DOCUMENT", "T_AT_PAGE::SELECTOR", "T_AT_PAGE", "T_AT_PAGE_DECLARATION");
  4114. }
  4115. /**
  4116. * Implements {@link aCssParserPlugin::parse()}.
  4117. *
  4118. * @param integer $index Current index
  4119. * @param string $char Current char
  4120. * @param string $previousChar Previous char
  4121. * @return mixed TRUE will break the processing; FALSE continue with the next plugin; integer set a new index and break the processing
  4122. */
  4123. public function parse($index, $char, $previousChar, $state)
  4124. {
  4125. // Start of @page at-rule block
  4126. if ($char === "@" && $state === "T_DOCUMENT" && strtolower(substr($this->parser->getSource(), $index, 5)) === "@page")
  4127. {
  4128. $this->parser->pushState("T_AT_PAGE::SELECTOR");
  4129. $this->parser->clearBuffer();
  4130. return $index + 5;
  4131. }
  4132. // Start of @page declarations
  4133. elseif ($char === "{" && $state === "T_AT_PAGE::SELECTOR")
  4134. {
  4135. $selector = $this->parser->getAndClearBuffer("{");
  4136. $this->parser->setState("T_AT_PAGE");
  4137. $this->parser->clearBuffer();
  4138. $this->parser->appendToken(new CssAtPageStartToken($selector));
  4139. }
  4140. // Start of @page declaration
  4141. elseif ($char === ":" && $state === "T_AT_PAGE")
  4142. {
  4143. $this->parser->pushState("T_AT_PAGE_DECLARATION");
  4144. $this->buffer = $this->parser->getAndClearBuffer(":", true);
  4145. }
  4146. // Unterminated @font-face declaration
  4147. elseif ($char === ":" && $state === "T_AT_PAGE_DECLARATION")
  4148. {
  4149. // Ignore Internet Explorer filter declarations
  4150. if ($this->buffer === "filter")
  4151. {
  4152. return false;
  4153. }
  4154. CssMin::triggerError(new CssError(__FILE__, __LINE__, __METHOD__ . ": Unterminated @page declaration", $this->buffer . ":" . $this->parser->getBuffer() . "_"));
  4155. }
  4156. // End of @page declaration
  4157. elseif (($char === ";" || $char === "}") && $state == "T_AT_PAGE_DECLARATION")
  4158. {
  4159. $value = $this->parser->getAndClearBuffer(";}");
  4160. if (strtolower(substr($value, -10, 10)) == "!important")
  4161. {
  4162. $value = trim(substr($value, 0, -10));
  4163. $isImportant = true;
  4164. }
  4165. else
  4166. {
  4167. $isImportant = false;
  4168. }
  4169. $this->parser->popState();
  4170. $this->parser->appendToken(new CssAtPageDeclarationToken($this->buffer, $value, $isImportant));
  4171. // --
  4172. if ($char === "}")
  4173. {
  4174. $this->parser->popState();
  4175. $this->parser->appendToken(new CssAtPageEndToken());
  4176. }
  4177. $this->buffer = "";
  4178. }
  4179. // End of @page at-rule block
  4180. elseif ($char === "}" && $state === "T_AT_PAGE")
  4181. {
  4182. $this->parser->popState();
  4183. $this->parser->clearBuffer();
  4184. $this->parser->appendToken(new CssAtPageEndToken());
  4185. }
  4186. else
  4187. {
  4188. return false;
  4189. }
  4190. return true;
  4191. }
  4192. }
  4193. /**
  4194. * This {@link aCssToken CSS token} represents the end of a @page at-rule block.
  4195. *
  4196. * @package CssMin/Tokens
  4197. * @link http://code.google.com/p/cssmin/
  4198. * @author Joe Scylla <joe.scylla@gmail.com>
  4199. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  4200. * @license http://opensource.org/licenses/mit-license.php MIT License
  4201. * @version 3.0.1
  4202. */
  4203. class CssAtPageEndToken extends aCssAtBlockEndToken
  4204. {
  4205. }
  4206. /**
  4207. * This {@link aCssToken CSS token} represents a declaration of a @page at-rule block.
  4208. *
  4209. * @package CssMin/Tokens
  4210. * @link http://code.google.com/p/cssmin/
  4211. * @author Joe Scylla <joe.scylla@gmail.com>
  4212. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  4213. * @license http://opensource.org/licenses/mit-license.php MIT License
  4214. * @version 3.0.1
  4215. */
  4216. class CssAtPageDeclarationToken extends aCssDeclarationToken
  4217. {
  4218. }
  4219. /**
  4220. * This {@link aCssToken CSS token} represents the start of a @media at-rule block.
  4221. *
  4222. * @package CssMin/Tokens
  4223. * @link http://code.google.com/p/cssmin/
  4224. * @author Joe Scylla <joe.scylla@gmail.com>
  4225. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  4226. * @license http://opensource.org/licenses/mit-license.php MIT License
  4227. * @version 3.0.1
  4228. */
  4229. class CssAtMediaStartToken extends aCssAtBlockStartToken
  4230. {
  4231. /**
  4232. * Sets the properties of the @media at-rule.
  4233. *
  4234. * @param array $mediaTypes Media types
  4235. * @return void
  4236. */
  4237. public function __construct(array $mediaTypes = array())
  4238. {
  4239. $this->MediaTypes = $mediaTypes;
  4240. }
  4241. /**
  4242. * Implements {@link aCssToken::__toString()}.
  4243. *
  4244. * @return string
  4245. */
  4246. public function __toString()
  4247. {
  4248. return "@media " . implode(",", $this->MediaTypes) . "{";
  4249. }
  4250. }
  4251. /**
  4252. * {@link aCssParserPlugin Parser plugin} for parsing @media at-rule block.
  4253. *
  4254. * Found @media at-rule blocks will add a {@link CssAtMediaStartToken} and {@link CssAtMediaEndToken} to the parser.
  4255. * This plugin will also set the the current media types using {@link CssParser::setMediaTypes()} and
  4256. * {@link CssParser::unsetMediaTypes()}.
  4257. *
  4258. * @package CssMin/Parser/Plugins
  4259. * @link http://code.google.com/p/cssmin/
  4260. * @author Joe Scylla <joe.scylla@gmail.com>
  4261. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  4262. * @license http://opensource.org/licenses/mit-license.php MIT License
  4263. * @version 3.0.1
  4264. */
  4265. class CssAtMediaParserPlugin extends aCssParserPlugin
  4266. {
  4267. /**
  4268. * Implements {@link aCssParserPlugin::getTriggerChars()}.
  4269. *
  4270. * @return array
  4271. */
  4272. public function getTriggerChars()
  4273. {
  4274. return array("@", "{", "}");
  4275. }
  4276. /**
  4277. * Implements {@link aCssParserPlugin::getTriggerStates()}.
  4278. *
  4279. * @return array
  4280. */
  4281. public function getTriggerStates()
  4282. {
  4283. return array("T_DOCUMENT", "T_AT_MEDIA::PREPARE", "T_AT_MEDIA");
  4284. }
  4285. /**
  4286. * Implements {@link aCssParserPlugin::parse()}.
  4287. *
  4288. * @param integer $index Current index
  4289. * @param string $char Current char
  4290. * @param string $previousChar Previous char
  4291. * @return mixed TRUE will break the processing; FALSE continue with the next plugin; integer set a new index and break the processing
  4292. */
  4293. public function parse($index, $char, $previousChar, $state)
  4294. {
  4295. if ($char === "@" && $state === "T_DOCUMENT" && strtolower(substr($this->parser->getSource(), $index, 6)) === "@media")
  4296. {
  4297. $this->parser->pushState("T_AT_MEDIA::PREPARE");
  4298. $this->parser->clearBuffer();
  4299. return $index + 6;
  4300. }
  4301. elseif ($char === "{" && $state === "T_AT_MEDIA::PREPARE")
  4302. {
  4303. $mediaTypes = array_filter(array_map("trim", explode(",", $this->parser->getAndClearBuffer("{"))));
  4304. $this->parser->setMediaTypes($mediaTypes);
  4305. $this->parser->setState("T_AT_MEDIA");
  4306. $this->parser->appendToken(new CssAtMediaStartToken($mediaTypes));
  4307. }
  4308. elseif ($char === "}" && $state === "T_AT_MEDIA")
  4309. {
  4310. $this->parser->appendToken(new CssAtMediaEndToken());
  4311. $this->parser->clearBuffer();
  4312. $this->parser->unsetMediaTypes();
  4313. $this->parser->popState();
  4314. }
  4315. else
  4316. {
  4317. return false;
  4318. }
  4319. return true;
  4320. }
  4321. }
  4322. /**
  4323. * This {@link aCssToken CSS token} represents the end of a @media at-rule block.
  4324. *
  4325. * @package CssMin/Tokens
  4326. * @link http://code.google.com/p/cssmin/
  4327. * @author Joe Scylla <joe.scylla@gmail.com>
  4328. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  4329. * @license http://opensource.org/licenses/mit-license.php MIT License
  4330. * @version 3.0.1
  4331. */
  4332. class CssAtMediaEndToken extends aCssAtBlockEndToken
  4333. {
  4334. }
  4335. /**
  4336. * This {@link aCssToken CSS token} represents the start of a @keyframes at-rule block.
  4337. *
  4338. * @package CssMin/Tokens
  4339. * @link http://code.google.com/p/cssmin/
  4340. * @author Joe Scylla <joe.scylla@gmail.com>
  4341. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  4342. * @license http://opensource.org/licenses/mit-license.php MIT License
  4343. * @version 3.0.1
  4344. */
  4345. class CssAtKeyframesStartToken extends aCssAtBlockStartToken
  4346. {
  4347. /**
  4348. * Name of the at-rule.
  4349. *
  4350. * @var string
  4351. */
  4352. public $AtRuleName = "keyframes";
  4353. /**
  4354. * Name
  4355. *
  4356. * @var string
  4357. */
  4358. public $Name = "";
  4359. /**
  4360. * Sets the properties of the @page at-rule.
  4361. *
  4362. * @param string $selector Selector
  4363. * @return void
  4364. */
  4365. public function __construct($name, $atRuleName = null)
  4366. {
  4367. $this->Name = $name;
  4368. if (!is_null($atRuleName))
  4369. {
  4370. $this->AtRuleName = $atRuleName;
  4371. }
  4372. }
  4373. /**
  4374. * Implements {@link aCssToken::__toString()}.
  4375. *
  4376. * @return string
  4377. */
  4378. public function __toString()
  4379. {
  4380. return "@" . $this->AtRuleName . " \"" . $this->Name . "\"{";
  4381. }
  4382. }
  4383. /**
  4384. * This {@link aCssToken CSS token} represents the start of a ruleset of a @keyframes at-rule block.
  4385. *
  4386. * @package CssMin/Tokens
  4387. * @link http://code.google.com/p/cssmin/
  4388. * @author Joe Scylla <joe.scylla@gmail.com>
  4389. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  4390. * @license http://opensource.org/licenses/mit-license.php MIT License
  4391. * @version 3.0.1
  4392. */
  4393. class CssAtKeyframesRulesetStartToken extends aCssRulesetStartToken
  4394. {
  4395. /**
  4396. * Array of selectors.
  4397. *
  4398. * @var array
  4399. */
  4400. public $Selectors = array();
  4401. /**
  4402. * Set the properties of a ruleset token.
  4403. *
  4404. * @param array $selectors Selectors of the ruleset
  4405. * @return void
  4406. */
  4407. public function __construct(array $selectors = array())
  4408. {
  4409. $this->Selectors = $selectors;
  4410. }
  4411. /**
  4412. * Implements {@link aCssToken::__toString()}.
  4413. *
  4414. * @return string
  4415. */
  4416. public function __toString()
  4417. {
  4418. return implode(",", $this->Selectors) . "{";
  4419. }
  4420. }
  4421. /**
  4422. * This {@link aCssToken CSS token} represents the end of a ruleset of a @keyframes at-rule block.
  4423. *
  4424. * @package CssMin/Tokens
  4425. * @link http://code.google.com/p/cssmin/
  4426. * @author Joe Scylla <joe.scylla@gmail.com>
  4427. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  4428. * @license http://opensource.org/licenses/mit-license.php MIT License
  4429. * @version 3.0.1
  4430. */
  4431. class CssAtKeyframesRulesetEndToken extends aCssRulesetEndToken
  4432. {
  4433. }
  4434. /**
  4435. * This {@link aCssToken CSS token} represents a ruleset declaration of a @keyframes at-rule block.
  4436. *
  4437. * @package CssMin/Tokens
  4438. * @link http://code.google.com/p/cssmin/
  4439. * @author Joe Scylla <joe.scylla@gmail.com>
  4440. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  4441. * @license http://opensource.org/licenses/mit-license.php MIT License
  4442. * @version 3.0.1
  4443. */
  4444. class CssAtKeyframesRulesetDeclarationToken extends aCssDeclarationToken
  4445. {
  4446. }
  4447. /**
  4448. * {@link aCssParserPlugin Parser plugin} for parsing @keyframes at-rule blocks, rulesets and declarations.
  4449. *
  4450. * @package CssMin/Parser/Plugins
  4451. * @link http://code.google.com/p/cssmin/
  4452. * @author Joe Scylla <joe.scylla@gmail.com>
  4453. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  4454. * @license http://opensource.org/licenses/mit-license.php MIT License
  4455. * @version 3.0.1
  4456. */
  4457. class CssAtKeyframesParserPlugin extends aCssParserPlugin
  4458. {
  4459. /**
  4460. * @var string Keyword
  4461. */
  4462. private $atRuleName = "";
  4463. /**
  4464. * Selectors.
  4465. *
  4466. * @var array
  4467. */
  4468. private $selectors = array();
  4469. /**
  4470. * Implements {@link aCssParserPlugin::getTriggerChars()}.
  4471. *
  4472. * @return array
  4473. */
  4474. public function getTriggerChars()
  4475. {
  4476. return array("@", "{", "}", ":", ",", ";");
  4477. }
  4478. /**
  4479. * Implements {@link aCssParserPlugin::getTriggerStates()}.
  4480. *
  4481. * @return array
  4482. */
  4483. public function getTriggerStates()
  4484. {
  4485. return array("T_DOCUMENT", "T_AT_KEYFRAMES::NAME", "T_AT_KEYFRAMES", "T_AT_KEYFRAMES_RULESETS", "T_AT_KEYFRAMES_RULESET", "T_AT_KEYFRAMES_RULESET_DECLARATION");
  4486. }
  4487. /**
  4488. * Implements {@link aCssParserPlugin::parse()}.
  4489. *
  4490. * @param integer $index Current index
  4491. * @param string $char Current char
  4492. * @param string $previousChar Previous char
  4493. * @return mixed TRUE will break the processing; FALSE continue with the next plugin; integer set a new index and break the processing
  4494. */
  4495. public function parse($index, $char, $previousChar, $state)
  4496. {
  4497. // Start of @keyframes at-rule block
  4498. if ($char === "@" && $state === "T_DOCUMENT" && strtolower(substr($this->parser->getSource(), $index, 10)) === "@keyframes")
  4499. {
  4500. $this->atRuleName = "keyframes";
  4501. $this->parser->pushState("T_AT_KEYFRAMES::NAME");
  4502. $this->parser->clearBuffer();
  4503. return $index + 10;
  4504. }
  4505. // Start of @keyframes at-rule block (@-moz-keyframes)
  4506. elseif ($char === "@" && $state === "T_DOCUMENT" && strtolower(substr($this->parser->getSource(), $index, 15)) === "@-moz-keyframes")
  4507. {
  4508. $this->atRuleName = "-moz-keyframes";
  4509. $this->parser->pushState("T_AT_KEYFRAMES::NAME");
  4510. $this->parser->clearBuffer();
  4511. return $index + 15;
  4512. }
  4513. // Start of @keyframes at-rule block (@-webkit-keyframes)
  4514. elseif ($char === "@" && $state === "T_DOCUMENT" && strtolower(substr($this->parser->getSource(), $index, 18)) === "@-webkit-keyframes")
  4515. {
  4516. $this->atRuleName = "-webkit-keyframes";
  4517. $this->parser->pushState("T_AT_KEYFRAMES::NAME");
  4518. $this->parser->clearBuffer();
  4519. return $index + 18;
  4520. }
  4521. // Start of @keyframes rulesets
  4522. elseif ($char === "{" && $state === "T_AT_KEYFRAMES::NAME")
  4523. {
  4524. $name = $this->parser->getAndClearBuffer("{\"'");
  4525. $this->parser->setState("T_AT_KEYFRAMES_RULESETS");
  4526. $this->parser->clearBuffer();
  4527. $this->parser->appendToken(new CssAtKeyframesStartToken($name, $this->atRuleName));
  4528. }
  4529. // Start of @keyframe ruleset and selectors
  4530. if ($char === "," && $state === "T_AT_KEYFRAMES_RULESETS")
  4531. {
  4532. $this->selectors[] = $this->parser->getAndClearBuffer(",{");
  4533. }
  4534. // Start of a @keyframes ruleset
  4535. elseif ($char === "{" && $state === "T_AT_KEYFRAMES_RULESETS")
  4536. {
  4537. if ($this->parser->getBuffer() !== "")
  4538. {
  4539. $this->selectors[] = $this->parser->getAndClearBuffer(",{");
  4540. $this->parser->pushState("T_AT_KEYFRAMES_RULESET");
  4541. $this->parser->appendToken(new CssAtKeyframesRulesetStartToken($this->selectors));
  4542. $this->selectors = array();
  4543. }
  4544. }
  4545. // Start of @keyframes ruleset declaration
  4546. elseif ($char === ":" && $state === "T_AT_KEYFRAMES_RULESET")
  4547. {
  4548. $this->parser->pushState("T_AT_KEYFRAMES_RULESET_DECLARATION");
  4549. $this->buffer = $this->parser->getAndClearBuffer(":;", true);
  4550. }
  4551. // Unterminated @keyframes ruleset declaration
  4552. elseif ($char === ":" && $state === "T_AT_KEYFRAMES_RULESET_DECLARATION")
  4553. {
  4554. // Ignore Internet Explorer filter declarations
  4555. if ($this->buffer === "filter")
  4556. {
  4557. return false;
  4558. }
  4559. CssMin::triggerError(new CssError(__FILE__, __LINE__, __METHOD__ . ": Unterminated @keyframes ruleset declaration", $this->buffer . ":" . $this->parser->getBuffer() . "_"));
  4560. }
  4561. // End of declaration
  4562. elseif (($char === ";" || $char === "}") && $state === "T_AT_KEYFRAMES_RULESET_DECLARATION")
  4563. {
  4564. $value = $this->parser->getAndClearBuffer(";}");
  4565. if (strtolower(substr($value, -10, 10)) === "!important")
  4566. {
  4567. $value = trim(substr($value, 0, -10));
  4568. $isImportant = true;
  4569. }
  4570. else
  4571. {
  4572. $isImportant = false;
  4573. }
  4574. $this->parser->popState();
  4575. $this->parser->appendToken(new CssAtKeyframesRulesetDeclarationToken($this->buffer, $value, $isImportant));
  4576. // Declaration ends with a right curly brace; so we have to end the ruleset
  4577. if ($char === "}")
  4578. {
  4579. $this->parser->appendToken(new CssAtKeyframesRulesetEndToken());
  4580. $this->parser->popState();
  4581. }
  4582. $this->buffer = "";
  4583. }
  4584. // End of @keyframes ruleset
  4585. elseif ($char === "}" && $state === "T_AT_KEYFRAMES_RULESET")
  4586. {
  4587. $this->parser->clearBuffer();
  4588. $this->parser->popState();
  4589. $this->parser->appendToken(new CssAtKeyframesRulesetEndToken());
  4590. }
  4591. // End of @keyframes rulesets
  4592. elseif ($char === "}" && $state === "T_AT_KEYFRAMES_RULESETS")
  4593. {
  4594. $this->parser->clearBuffer();
  4595. $this->parser->popState();
  4596. $this->parser->appendToken(new CssAtKeyframesEndToken());
  4597. }
  4598. else
  4599. {
  4600. return false;
  4601. }
  4602. return true;
  4603. }
  4604. }
  4605. /**
  4606. * This {@link aCssToken CSS token} represents the end of a @keyframes at-rule block.
  4607. *
  4608. * @package CssMin/Tokens
  4609. * @link http://code.google.com/p/cssmin/
  4610. * @author Joe Scylla <joe.scylla@gmail.com>
  4611. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  4612. * @license http://opensource.org/licenses/mit-license.php MIT License
  4613. * @version 3.0.1
  4614. */
  4615. class CssAtKeyframesEndToken extends aCssAtBlockEndToken
  4616. {
  4617. }
  4618. /**
  4619. * This {@link aCssToken CSS token} represents a @import at-rule.
  4620. *
  4621. * @package CssMin/Tokens
  4622. * @link http://code.google.com/p/cssmin/
  4623. * @author Joe Scylla <joe.scylla@gmail.com>
  4624. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  4625. * @license http://opensource.org/licenses/mit-license.php MIT License
  4626. * @version 3.0.1.b1 (2001-02-22)
  4627. */
  4628. class CssAtImportToken extends aCssToken
  4629. {
  4630. /**
  4631. * Import path of the @import at-rule.
  4632. *
  4633. * @var string
  4634. */
  4635. public $Import = "";
  4636. /**
  4637. * Media types of the @import at-rule.
  4638. *
  4639. * @var array
  4640. */
  4641. public $MediaTypes = array();
  4642. /**
  4643. * Set the properties of a @import at-rule token.
  4644. *
  4645. * @param string $import Import path
  4646. * @param array $mediaTypes Media types
  4647. * @return void
  4648. */
  4649. public function __construct($import, $mediaTypes)
  4650. {
  4651. $this->Import = $import;
  4652. $this->MediaTypes = $mediaTypes ? $mediaTypes : array();
  4653. }
  4654. /**
  4655. * Implements {@link aCssToken::__toString()}.
  4656. *
  4657. * @return string
  4658. */
  4659. public function __toString()
  4660. {
  4661. return "@import \"" . $this->Import . "\"" . (count($this->MediaTypes) > 0 ? " " . implode(",", $this->MediaTypes) : ""). ";";
  4662. }
  4663. }
  4664. /**
  4665. * {@link aCssParserPlugin Parser plugin} for parsing @import at-rule.
  4666. *
  4667. * If a @import at-rule was found this plugin will add a {@link CssAtImportToken} to the parser.
  4668. *
  4669. * @package CssMin/Parser/Plugins
  4670. * @link http://code.google.com/p/cssmin/
  4671. * @author Joe Scylla <joe.scylla@gmail.com>
  4672. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  4673. * @license http://opensource.org/licenses/mit-license.php MIT License
  4674. * @version 3.0.1
  4675. */
  4676. class CssAtImportParserPlugin extends aCssParserPlugin
  4677. {
  4678. /**
  4679. * Implements {@link aCssParserPlugin::getTriggerChars()}.
  4680. *
  4681. * @return array
  4682. */
  4683. public function getTriggerChars()
  4684. {
  4685. return array("@", ";", ",", "\n");
  4686. }
  4687. /**
  4688. * Implements {@link aCssParserPlugin::getTriggerStates()}.
  4689. *
  4690. * @return array
  4691. */
  4692. public function getTriggerStates()
  4693. {
  4694. return array("T_DOCUMENT", "T_AT_IMPORT");
  4695. }
  4696. /**
  4697. * Implements {@link aCssParserPlugin::parse()}.
  4698. *
  4699. * @param integer $index Current index
  4700. * @param string $char Current char
  4701. * @param string $previousChar Previous char
  4702. * @return mixed TRUE will break the processing; FALSE continue with the next plugin; integer set a new index and break the processing
  4703. */
  4704. public function parse($index, $char, $previousChar, $state)
  4705. {
  4706. if ($char === "@" && $state === "T_DOCUMENT" && strtolower(substr($this->parser->getSource(), $index, 7)) === "@import")
  4707. {
  4708. $this->parser->pushState("T_AT_IMPORT");
  4709. $this->parser->clearBuffer();
  4710. return $index + 7;
  4711. }
  4712. elseif (($char === ";" || $char === "\n") && $state === "T_AT_IMPORT")
  4713. {
  4714. $this->buffer = $this->parser->getAndClearBuffer(";");
  4715. $pos = false;
  4716. foreach (array(")", "\"", "'") as $needle)
  4717. {
  4718. if (($pos = strrpos($this->buffer, $needle)) !== false)
  4719. {
  4720. break;
  4721. }
  4722. }
  4723. $import = substr($this->buffer, 0, $pos + 1);
  4724. if (stripos($import, "url(") === 0)
  4725. {
  4726. $import = substr($import, 4, -1);
  4727. }
  4728. $import = trim($import, " \t\n\r\0\x0B'\"");
  4729. $mediaTypes = array_filter(array_map("trim", explode(",", trim(substr($this->buffer, $pos + 1), " \t\n\r\0\x0B{"))));
  4730. if ($pos)
  4731. {
  4732. $this->parser->appendToken(new CssAtImportToken($import, $mediaTypes));
  4733. }
  4734. else
  4735. {
  4736. CssMin::triggerError(new CssError(__FILE__, __LINE__, __METHOD__ . ": Invalid @import at-rule syntax", $this->parser->buffer));
  4737. }
  4738. $this->parser->popState();
  4739. }
  4740. else
  4741. {
  4742. return false;
  4743. }
  4744. return true;
  4745. }
  4746. }
  4747. /**
  4748. * This {@link aCssToken CSS token} represents the start of a @font-face at-rule block.
  4749. *
  4750. * @package CssMin/Tokens
  4751. * @link http://code.google.com/p/cssmin/
  4752. * @author Joe Scylla <joe.scylla@gmail.com>
  4753. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  4754. * @license http://opensource.org/licenses/mit-license.php MIT License
  4755. * @version 3.0.1
  4756. */
  4757. class CssAtFontFaceStartToken extends aCssAtBlockStartToken
  4758. {
  4759. /**
  4760. * Implements {@link aCssToken::__toString()}.
  4761. *
  4762. * @return string
  4763. */
  4764. public function __toString()
  4765. {
  4766. return "@font-face{";
  4767. }
  4768. }
  4769. /**
  4770. * {@link aCssParserPlugin Parser plugin} for parsing @font-face at-rule block with including declarations.
  4771. *
  4772. * Found @font-face at-rule blocks will add a {@link CssAtFontFaceStartToken} and {@link CssAtFontFaceEndToken} to the
  4773. * parser; including declarations as {@link CssAtFontFaceDeclarationToken}.
  4774. *
  4775. * @package CssMin/Parser/Plugins
  4776. * @link http://code.google.com/p/cssmin/
  4777. * @author Joe Scylla <joe.scylla@gmail.com>
  4778. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  4779. * @license http://opensource.org/licenses/mit-license.php MIT License
  4780. * @version 3.0.1
  4781. */
  4782. class CssAtFontFaceParserPlugin extends aCssParserPlugin
  4783. {
  4784. /**
  4785. * Implements {@link aCssParserPlugin::getTriggerChars()}.
  4786. *
  4787. * @return array
  4788. */
  4789. public function getTriggerChars()
  4790. {
  4791. return array("@", "{", "}", ":", ";");
  4792. }
  4793. /**
  4794. * Implements {@link aCssParserPlugin::getTriggerStates()}.
  4795. *
  4796. * @return array
  4797. */
  4798. public function getTriggerStates()
  4799. {
  4800. return array("T_DOCUMENT", "T_AT_FONT_FACE::PREPARE", "T_AT_FONT_FACE", "T_AT_FONT_FACE_DECLARATION");
  4801. }
  4802. /**
  4803. * Implements {@link aCssParserPlugin::parse()}.
  4804. *
  4805. * @param integer $index Current index
  4806. * @param string $char Current char
  4807. * @param string $previousChar Previous char
  4808. * @return mixed TRUE will break the processing; FALSE continue with the next plugin; integer set a new index and break the processing
  4809. */
  4810. public function parse($index, $char, $previousChar, $state)
  4811. {
  4812. // Start of @font-face at-rule block
  4813. if ($char === "@" && $state === "T_DOCUMENT" && strtolower(substr($this->parser->getSource(), $index, 10)) === "@font-face")
  4814. {
  4815. $this->parser->pushState("T_AT_FONT_FACE::PREPARE");
  4816. $this->parser->clearBuffer();
  4817. return $index + 10;
  4818. }
  4819. // Start of @font-face declarations
  4820. elseif ($char === "{" && $state === "T_AT_FONT_FACE::PREPARE")
  4821. {
  4822. $this->parser->setState("T_AT_FONT_FACE");
  4823. $this->parser->clearBuffer();
  4824. $this->parser->appendToken(new CssAtFontFaceStartToken());
  4825. }
  4826. // Start of @font-face declaration
  4827. elseif ($char === ":" && $state === "T_AT_FONT_FACE")
  4828. {
  4829. $this->parser->pushState("T_AT_FONT_FACE_DECLARATION");
  4830. $this->buffer = $this->parser->getAndClearBuffer(":", true);
  4831. }
  4832. // Unterminated @font-face declaration
  4833. elseif ($char === ":" && $state === "T_AT_FONT_FACE_DECLARATION")
  4834. {
  4835. // Ignore Internet Explorer filter declarations
  4836. if ($this->buffer === "filter")
  4837. {
  4838. return false;
  4839. }
  4840. CssMin::triggerError(new CssError(__FILE__, __LINE__, __METHOD__ . ": Unterminated @font-face declaration", $this->buffer . ":" . $this->parser->getBuffer() . "_"));
  4841. }
  4842. // End of @font-face declaration
  4843. elseif (($char === ";" || $char === "}") && $state === "T_AT_FONT_FACE_DECLARATION")
  4844. {
  4845. $value = $this->parser->getAndClearBuffer(";}");
  4846. if (strtolower(substr($value, -10, 10)) === "!important")
  4847. {
  4848. $value = trim(substr($value, 0, -10));
  4849. $isImportant = true;
  4850. }
  4851. else
  4852. {
  4853. $isImportant = false;
  4854. }
  4855. $this->parser->popState();
  4856. $this->parser->appendToken(new CssAtFontFaceDeclarationToken($this->buffer, $value, $isImportant));
  4857. $this->buffer = "";
  4858. // --
  4859. if ($char === "}")
  4860. {
  4861. $this->parser->appendToken(new CssAtFontFaceEndToken());
  4862. $this->parser->popState();
  4863. }
  4864. }
  4865. // End of @font-face at-rule block
  4866. elseif ($char === "}" && $state === "T_AT_FONT_FACE")
  4867. {
  4868. $this->parser->appendToken(new CssAtFontFaceEndToken());
  4869. $this->parser->clearBuffer();
  4870. $this->parser->popState();
  4871. }
  4872. else
  4873. {
  4874. return false;
  4875. }
  4876. return true;
  4877. }
  4878. }
  4879. /**
  4880. * This {@link aCssToken CSS token} represents the end of a @font-face at-rule block.
  4881. *
  4882. * @package CssMin/Tokens
  4883. * @link http://code.google.com/p/cssmin/
  4884. * @author Joe Scylla <joe.scylla@gmail.com>
  4885. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  4886. * @license http://opensource.org/licenses/mit-license.php MIT License
  4887. * @version 3.0.1
  4888. */
  4889. class CssAtFontFaceEndToken extends aCssAtBlockEndToken
  4890. {
  4891. }
  4892. /**
  4893. * This {@link aCssToken CSS token} represents a declaration of a @font-face at-rule block.
  4894. *
  4895. * @package CssMin/Tokens
  4896. * @link http://code.google.com/p/cssmin/
  4897. * @author Joe Scylla <joe.scylla@gmail.com>
  4898. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  4899. * @license http://opensource.org/licenses/mit-license.php MIT License
  4900. * @version 3.0.1
  4901. */
  4902. class CssAtFontFaceDeclarationToken extends aCssDeclarationToken
  4903. {
  4904. }
  4905. /**
  4906. * This {@link aCssToken CSS token} represents a @charset at-rule.
  4907. *
  4908. * @package CssMin/Tokens
  4909. * @link http://code.google.com/p/cssmin/
  4910. * @author Joe Scylla <joe.scylla@gmail.com>
  4911. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  4912. * @license http://opensource.org/licenses/mit-license.php MIT License
  4913. * @version 3.0.1
  4914. */
  4915. class CssAtCharsetToken extends aCssToken
  4916. {
  4917. /**
  4918. * Charset of the @charset at-rule.
  4919. *
  4920. * @var string
  4921. */
  4922. public $Charset = "";
  4923. /**
  4924. * Set the properties of @charset at-rule token.
  4925. *
  4926. * @param string $charset Charset of the @charset at-rule token
  4927. * @return void
  4928. */
  4929. public function __construct($charset)
  4930. {
  4931. $this->Charset = $charset;
  4932. }
  4933. /**
  4934. * Implements {@link aCssToken::__toString()}.
  4935. *
  4936. * @return string
  4937. */
  4938. public function __toString()
  4939. {
  4940. return "@charset " . $this->Charset . ";";
  4941. }
  4942. }
  4943. /**
  4944. * {@link aCssParserPlugin Parser plugin} for parsing @charset at-rule.
  4945. *
  4946. * If a @charset at-rule was found this plugin will add a {@link CssAtCharsetToken} to the parser.
  4947. *
  4948. * @package CssMin/Parser/Plugins
  4949. * @link http://code.google.com/p/cssmin/
  4950. * @author Joe Scylla <joe.scylla@gmail.com>
  4951. * @copyright 2008 - 2011 Joe Scylla <joe.scylla@gmail.com>
  4952. * @license http://opensource.org/licenses/mit-license.php MIT License
  4953. * @version 3.0.1
  4954. */
  4955. class CssAtCharsetParserPlugin extends aCssParserPlugin
  4956. {
  4957. /**
  4958. * Implements {@link aCssParserPlugin::getTriggerChars()}.
  4959. *
  4960. * @return array
  4961. */
  4962. public function getTriggerChars()
  4963. {
  4964. return array("@", ";", "\n");
  4965. }
  4966. /**
  4967. * Implements {@link aCssParserPlugin::getTriggerStates()}.
  4968. *
  4969. * @return array
  4970. */
  4971. public function getTriggerStates()
  4972. {
  4973. return array("T_DOCUMENT", "T_AT_CHARSET");
  4974. }
  4975. /**
  4976. * Implements {@link aCssParserPlugin::parse()}.
  4977. *
  4978. * @param integer $index Current index
  4979. * @param string $char Current char
  4980. * @param string $previousChar Previous char
  4981. * @return mixed TRUE will break the processing; FALSE continue with the next plugin; integer set a new index and break the processing
  4982. */
  4983. public function parse($index, $char, $previousChar, $state)
  4984. {
  4985. if ($char === "@" && $state === "T_DOCUMENT" && strtolower(substr($this->parser->getSource(), $index, 8)) === "@charset")
  4986. {
  4987. $this->parser->pushState("T_AT_CHARSET");
  4988. $this->parser->clearBuffer();
  4989. return $index + 8;
  4990. }
  4991. elseif (($char === ";" || $char === "\n") && $state === "T_AT_CHARSET")
  4992. {
  4993. $charset = $this->parser->getAndClearBuffer(";");
  4994. $this->parser->popState();
  4995. $this->parser->appendToken(new CssAtCharsetToken($charset));
  4996. }
  4997. else
  4998. {
  4999. return false;
  5000. }
  5001. return true;
  5002. }
  5003. }
  5004. ?>