PageRenderTime 71ms CodeModel.GetById 36ms 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

Large files files are truncated, but you can click here to view the full file

  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->stat

Large files files are truncated, but you can click here to view the full file