Nenhuma Descrição

class.csstidy.php 36KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249
  1. <?php
  2. /**
  3. * CSSTidy - CSS Parser and Optimiser
  4. *
  5. * CSS Parser class
  6. *
  7. * Copyright 2005, 2006, 2007 Florian Schmitz
  8. *
  9. * This file is part of CSSTidy.
  10. *
  11. * CSSTidy is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU Lesser General Public License as published by
  13. * the Free Software Foundation; either version 2.1 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * CSSTidy is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public License
  22. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  23. *
  24. * @license https://opensource.org/licenses/lgpl-license.php GNU Lesser General Public License
  25. * @package csstidy
  26. * @author Florian Schmitz (floele at gmail dot com) 2005-2007
  27. * @author Brett Zamir (brettz9 at yahoo dot com) 2007
  28. * @author Nikolay Matsievsky (speed at webo dot name) 2009-2010
  29. * @author Cedric Morin (cedric at yterium dot com) 2010
  30. */
  31. /**
  32. * Defines ctype functions if required
  33. *
  34. * @version 1.0
  35. */
  36. require_once( dirname( __FILE__ ) . '/class.csstidy_ctype.php' );
  37. /**
  38. * Various CSS data needed for correct optimisations etc.
  39. *
  40. * @version 1.3
  41. */
  42. require( dirname( __FILE__ ) . '/data.inc.php' );
  43. /**
  44. * Contains a class for printing CSS code
  45. *
  46. * @version 1.0
  47. */
  48. require( dirname( __FILE__ ) . '/class.csstidy_print.php' );
  49. /**
  50. * Contains a class for optimising CSS code
  51. *
  52. * @version 1.0
  53. */
  54. require( dirname( __FILE__ ) . '/class.csstidy_optimise.php' );
  55. /**
  56. * CSS Parser class
  57. *
  58. * This class represents a CSS parser which reads CSS code and saves it in an array.
  59. * In opposite to most other CSS parsers, it does not use regular expressions and
  60. * thus has full CSS2 support and a higher reliability.
  61. * Additional to that it applies some optimisations and fixes to the CSS code.
  62. * An online version should be available here: https://cdburnerxp.se/cssparse/css_optimiser.php
  63. * @package csstidy
  64. * @author Florian Schmitz (floele at gmail dot com) 2005-2006
  65. * @version 1.3.1
  66. */
  67. class csstidy {
  68. /**
  69. * Saves the parsed CSS. This array is empty if preserve_css is on.
  70. * @var array
  71. * @access public
  72. */
  73. public $css = array();
  74. /**
  75. * Saves the parsed CSS (raw)
  76. * @var array
  77. * @access private
  78. */
  79. public $tokens = array();
  80. /**
  81. * Printer class
  82. * @see csstidy_print
  83. * @var object
  84. * @access public
  85. */
  86. public $print;
  87. /**
  88. * Optimiser class
  89. * @see csstidy_optimise
  90. * @var object
  91. * @access private
  92. */
  93. public $optimise;
  94. /**
  95. * Saves the CSS charset (@charset)
  96. * @var string
  97. * @access private
  98. */
  99. public $charset = '';
  100. /**
  101. * Saves all @import URLs
  102. * @var array
  103. * @access private
  104. */
  105. public $import = array();
  106. /**
  107. * Saves the namespace
  108. * @var string
  109. * @access private
  110. */
  111. public $namespace = '';
  112. /**
  113. * Contains the version of csstidy
  114. * @var string
  115. * @access private
  116. */
  117. public $version = '1.3';
  118. /**
  119. * Stores the settings
  120. * @var array
  121. * @access private
  122. */
  123. public $settings = array();
  124. /**
  125. * Saves the parser-status.
  126. *
  127. * Possible values:
  128. * - is = in selector
  129. * - ip = in property
  130. * - iv = in value
  131. * - instr = in string (started at " or ' or ( )
  132. * - ic = in comment (ignore everything)
  133. * - at = in @-block
  134. *
  135. * @var string
  136. * @access private
  137. */
  138. public $status = 'is';
  139. /**
  140. * Saves the current at rule (@media)
  141. * @var string
  142. * @access private
  143. */
  144. public $at = '';
  145. /**
  146. * Saves the current selector
  147. * @var string
  148. * @access private
  149. */
  150. public $selector = '';
  151. /**
  152. * Saves the current property
  153. * @var string
  154. * @access private
  155. */
  156. public $property = '';
  157. /**
  158. * Saves the position of , in selectors
  159. * @var array
  160. * @access private
  161. */
  162. public $sel_separate = array();
  163. /**
  164. * Saves the current value
  165. * @var string
  166. * @access private
  167. */
  168. public $value = '';
  169. /**
  170. * Saves the current sub-value
  171. *
  172. * Example for a subvalue:
  173. * background:url(foo.png) red no-repeat;
  174. * "url(foo.png)", "red", and "no-repeat" are subvalues,
  175. * separated by whitespace
  176. * @var string
  177. * @access private
  178. */
  179. public $sub_value = '';
  180. /**
  181. * Array which saves all subvalues for a property.
  182. * @var array
  183. * @see sub_value
  184. * @access private
  185. */
  186. public $sub_value_arr = array();
  187. /**
  188. * Saves the stack of characters that opened the current strings
  189. * @var array
  190. * @access private
  191. */
  192. public $str_char = array();
  193. public $cur_string = array();
  194. /**
  195. * Status from which the parser switched to ic or instr
  196. * @var array
  197. * @access private
  198. */
  199. public $from = array();
  200. /**
  201. /**
  202. * =true if in invalid at-rule
  203. * @var bool
  204. * @access private
  205. */
  206. public $invalid_at = false;
  207. /**
  208. * =true if something has been added to the current selector
  209. * @var bool
  210. * @access private
  211. */
  212. public $added = false;
  213. /**
  214. * Array which saves the message log
  215. * @var array
  216. * @access private
  217. */
  218. public $log = array();
  219. /**
  220. * Saves the line number
  221. * @var integer
  222. * @access private
  223. */
  224. public $line = 1;
  225. /**
  226. * Marks if we need to leave quotes for a string
  227. * @var array
  228. * @access private
  229. */
  230. public $quoted_string = array();
  231. /**
  232. * List of tokens
  233. * @var string
  234. */
  235. public $tokens_list = "";
  236. /**
  237. * Loads standard template and sets default settings
  238. * @access private
  239. * @version 1.3
  240. */
  241. function __construct() {
  242. $this->settings['remove_bslash'] = true;
  243. $this->settings['compress_colors'] = true;
  244. $this->settings['compress_font-weight'] = true;
  245. $this->settings['lowercase_s'] = false;
  246. /*
  247. 1 common shorthands optimization
  248. 2 + font property optimization
  249. 3 + background property optimization
  250. */
  251. $this->settings['optimise_shorthands'] = 1;
  252. $this->settings['remove_last_;'] = true;
  253. /* rewrite all properties with low case, better for later gzip OK, safe*/
  254. $this->settings['case_properties'] = 1;
  255. /* sort properties in alpabetic order, better for later gzip
  256. * but can cause trouble in case of overiding same propertie or using hack
  257. */
  258. $this->settings['sort_properties'] = false;
  259. /*
  260. 1, 3, 5, etc -- enable sorting selectors inside @media: a{}b{}c{}
  261. 2, 5, 8, etc -- enable sorting selectors inside one CSS declaration: a,b,c{}
  262. preserve order by default cause it can break functionnality
  263. */
  264. $this->settings['sort_selectors'] = 0;
  265. /* is dangeroues to be used: CSS is broken sometimes */
  266. $this->settings['merge_selectors'] = 0;
  267. /* preserve or not browser hacks */
  268. $this->settings['discard_invalid_selectors'] = false;
  269. $this->settings['discard_invalid_properties'] = false;
  270. $this->settings['css_level'] = 'CSS2.1';
  271. $this->settings['preserve_css'] = false;
  272. $this->settings['timestamp'] = false;
  273. $this->settings['template'] = ''; // say that propertie exist
  274. $this->set_cfg('template','default'); // call load_template
  275. /* Tells csstidy_optimise to keep leading zeros on decimal numbers, e.g., 0.7 */
  276. $this->settings['preserve_leading_zeros'] = false;
  277. $this->optimise = new csstidy_optimise($this);
  278. $this->tokens_list = & $GLOBALS['csstidy']['tokens'];
  279. }
  280. function csstidy() {
  281. $this->__construct();
  282. }
  283. /**
  284. * Get the value of a setting.
  285. * @param string $setting
  286. * @access public
  287. * @return mixed
  288. * @version 1.0
  289. */
  290. function get_cfg($setting) {
  291. if (isset($this->settings[$setting])) {
  292. return $this->settings[$setting];
  293. }
  294. return false;
  295. }
  296. /**
  297. * Load a template
  298. * @param string $template used by set_cfg to load a template via a configuration setting
  299. * @access private
  300. * @version 1.4
  301. */
  302. function _load_template($template) {
  303. switch ($template) {
  304. case 'default':
  305. $this->load_template('default');
  306. break;
  307. case 'highest':
  308. $this->load_template('highest_compression');
  309. break;
  310. case 'high':
  311. $this->load_template('high_compression');
  312. break;
  313. case 'low':
  314. $this->load_template('low_compression');
  315. break;
  316. default:
  317. $this->load_template($template);
  318. break;
  319. }
  320. }
  321. /**
  322. * Set the value of a setting.
  323. * @param string $setting
  324. * @param mixed $value
  325. * @access public
  326. * @return bool
  327. * @version 1.0
  328. */
  329. function set_cfg($setting, $value=null) {
  330. if (is_array($setting) && $value === null) {
  331. foreach ($setting as $setprop => $setval) {
  332. $this->settings[$setprop] = $setval;
  333. }
  334. if (array_key_exists('template', $setting)) {
  335. $this->_load_template($this->settings['template']);
  336. }
  337. return true;
  338. } else if (isset($this->settings[$setting]) && $value !== '') {
  339. $this->settings[$setting] = $value;
  340. if ($setting === 'template') {
  341. $this->_load_template($this->settings['template']);
  342. }
  343. return true;
  344. }
  345. return false;
  346. }
  347. /**
  348. * Adds a token to $this->tokens
  349. * @param mixed $type
  350. * @param string $data
  351. * @param bool $do add a token even if preserve_css is off
  352. * @access private
  353. * @version 1.0
  354. */
  355. function _add_token($type, $data, $do = false) {
  356. if ($this->get_cfg('preserve_css') || $do) {
  357. $this->tokens[] = array($type, ($type == COMMENT) ? $data : trim($data));
  358. }
  359. }
  360. /**
  361. * Add a message to the message log
  362. * @param string $message
  363. * @param string $type
  364. * @param integer $line
  365. * @access private
  366. * @version 1.0
  367. */
  368. function log($message, $type, $line = -1) {
  369. if ($line === -1) {
  370. $line = $this->line;
  371. }
  372. $line = (int) $line;
  373. $add = array('m' => $message, 't' => $type);
  374. if (!isset($this->log[$line]) || !in_array($add, $this->log[$line])) {
  375. $this->log[$line][] = $add;
  376. }
  377. }
  378. /**
  379. * Parse unicode notations and find a replacement character
  380. * @param string $string
  381. * @param integer $i
  382. * @access private
  383. * @return string
  384. * @version 1.2
  385. */
  386. function _unicode(&$string, &$i) {
  387. ++$i;
  388. $add = '';
  389. $replaced = false;
  390. while ($i < strlen($string) && (ctype_xdigit($string[$i]) || ctype_space($string[$i])) && strlen($add) < 6) {
  391. $add .= $string[$i];
  392. if (ctype_space($string[$i])) {
  393. break;
  394. }
  395. $i++;
  396. }
  397. if (hexdec($add) > 47 && hexdec($add) < 58 || hexdec($add) > 64 && hexdec($add) < 91 || hexdec($add) > 96 && hexdec($add) < 123) {
  398. $this->log('Replaced unicode notation: Changed \\' . $add . ' to ' . chr(hexdec($add)), 'Information');
  399. $add = chr(hexdec($add));
  400. $replaced = true;
  401. } else {
  402. $add = trim('\\' . $add);
  403. }
  404. if (@ctype_xdigit($string[$i + 1]) && ctype_space($string[$i])
  405. && !$replaced || !ctype_space($string[$i])) {
  406. $i--;
  407. }
  408. if ($add !== '\\' || !$this->get_cfg('remove_bslash') || strpos($this->tokens_list, $string[$i + 1]) !== false) {
  409. return $add;
  410. }
  411. if ($add === '\\') {
  412. $this->log('Removed unnecessary backslash', 'Information');
  413. }
  414. return '';
  415. }
  416. /**
  417. * Write formatted output to a file
  418. * @param string $filename
  419. * @param string $doctype when printing formatted, is a shorthand for the document type
  420. * @param bool $externalcss when printing formatted, indicates whether styles to be attached internally or as an external stylesheet
  421. * @param string $title when printing formatted, is the title to be added in the head of the document
  422. * @param string $lang when printing formatted, gives a two-letter language code to be added to the output
  423. * @access public
  424. * @version 1.4
  425. */
  426. function write_page($filename, $doctype='xhtml1.1', $externalcss=true, $title='', $lang='en') {
  427. $this->write($filename, true);
  428. }
  429. /**
  430. * Write plain output to a file
  431. * @param string $filename
  432. * @param bool $formatted whether to print formatted or not
  433. * @param string $doctype when printing formatted, is a shorthand for the document type
  434. * @param bool $externalcss when printing formatted, indicates whether styles to be attached internally or as an external stylesheet
  435. * @param string $title when printing formatted, is the title to be added in the head of the document
  436. * @param string $lang when printing formatted, gives a two-letter language code to be added to the output
  437. * @param bool $pre_code whether to add pre and code tags around the code (for light HTML formatted templates)
  438. * @access public
  439. * @version 1.4
  440. */
  441. function write($filename, $formatted=false, $doctype='xhtml1.1', $externalcss=true, $title='', $lang='en', $pre_code=true) {
  442. $filename .= ( $formatted) ? '.xhtml' : '.css';
  443. if (!is_dir('temp')) {
  444. $madedir = mkdir('temp');
  445. if (!$madedir) {
  446. print 'Could not make directory "temp" in ' . dirname(__FILE__);
  447. exit;
  448. }
  449. }
  450. $handle = fopen('temp/' . $filename, 'w');
  451. if ($handle) {
  452. if (!$formatted) {
  453. fwrite($handle, $this->print->plain());
  454. } else {
  455. fwrite($handle, $this->print->formatted_page($doctype, $externalcss, $title, $lang, $pre_code));
  456. }
  457. }
  458. fclose($handle);
  459. }
  460. /**
  461. * Loads a new template
  462. * @param string $content either filename (if $from_file == true), content of a template file, "high_compression", "highest_compression", "low_compression", or "default"
  463. * @param bool $from_file uses $content as filename if true
  464. * @access public
  465. * @version 1.1
  466. * @see http://csstidy.sourceforge.net/templates.php
  467. */
  468. function load_template($content, $from_file=true) {
  469. $predefined_templates = & $GLOBALS['csstidy']['predefined_templates'];
  470. if ($content === 'high_compression' || $content === 'default' || $content === 'highest_compression' || $content === 'low_compression') {
  471. $this->template = $predefined_templates[$content];
  472. return;
  473. }
  474. if ($from_file) {
  475. $content = strip_tags(file_get_contents($content), '<span>');
  476. }
  477. $content = str_replace("\r\n", "\n", $content); // Unify newlines (because the output also only uses \n)
  478. $template = explode('|', $content);
  479. for ($i = 0; $i < count($template); $i++) {
  480. $this->template[$i] = $template[$i];
  481. }
  482. }
  483. /**
  484. * Starts parsing from URL
  485. * @param string $url
  486. * @access public
  487. * @version 1.0
  488. */
  489. function parse_from_url($url) {
  490. return $this->parse(@file_get_contents($url));
  491. }
  492. /**
  493. * Checks if there is a token at the current position
  494. * @param string $string
  495. * @param integer $i
  496. * @access public
  497. * @version 1.11
  498. */
  499. function is_token(&$string, $i) {
  500. return (strpos($this->tokens_list, $string[$i]) !== false && !csstidy::escaped($string, $i));
  501. }
  502. /**
  503. * Parses CSS in $string. The code is saved as array in $this->css
  504. * @param string $string the CSS code
  505. * @access public
  506. * @return bool
  507. * @version 1.1
  508. */
  509. function parse($string) {
  510. // Temporarily set locale to en_US in order to handle floats properly
  511. $old = @setlocale(LC_ALL, 0);
  512. @setlocale(LC_ALL, 'C');
  513. // PHP bug? Settings need to be refreshed in PHP4
  514. $this->print = new csstidy_print($this);
  515. //$this->optimise = new csstidy_optimise($this);
  516. $all_properties = & $GLOBALS['csstidy']['all_properties'];
  517. $at_rules = & $GLOBALS['csstidy']['at_rules'];
  518. $quoted_string_properties = & $GLOBALS['csstidy']['quoted_string_properties'];
  519. $this->css = array();
  520. $this->print->input_css = $string;
  521. $string = str_replace("\r\n", "\n", $string) . ' ';
  522. $cur_comment = '';
  523. for ($i = 0, $size = strlen($string); $i < $size; $i++) {
  524. if ($string[$i] === "\n" || $string[$i] === "\r") {
  525. ++$this->line;
  526. }
  527. switch ($this->status) {
  528. /* Case in at-block */
  529. case 'at':
  530. if (csstidy::is_token($string, $i)) {
  531. if ($string[$i] === '/' && @$string[$i + 1] === '*') {
  532. $this->status = 'ic';
  533. ++$i;
  534. $this->from[] = 'at';
  535. } elseif ($string[$i] === '{') {
  536. $this->status = 'is';
  537. $this->at = $this->css_new_media_section($this->at);
  538. $this->_add_token(AT_START, $this->at);
  539. } elseif ($string[$i] === ',') {
  540. $this->at = trim($this->at) . ',';
  541. } elseif ($string[$i] === '\\') {
  542. $this->at .= $this->_unicode($string, $i);
  543. }
  544. // fix for complicated media, i.e @media screen and (-webkit-min-device-pixel-ratio:1.5)
  545. // '/' is included for ratios in Opera: (-o-min-device-pixel-ratio: 3/2)
  546. elseif (in_array($string[$i], array('(', ')', ':', '.', '/'))) {
  547. $this->at .= $string[$i];
  548. }
  549. } else {
  550. $lastpos = strlen($this->at) - 1;
  551. if (!( (ctype_space($this->at[$lastpos]) || csstidy::is_token($this->at, $lastpos) && $this->at[$lastpos] === ',') && ctype_space($string[$i]))) {
  552. $this->at .= $string[$i];
  553. }
  554. }
  555. break;
  556. /* Case in-selector */
  557. case 'is':
  558. if (csstidy::is_token($string, $i)) {
  559. if ($string[$i] === '/' && @$string[$i + 1] === '*' && trim($this->selector) == '') {
  560. $this->status = 'ic';
  561. ++$i;
  562. $this->from[] = 'is';
  563. } elseif ($string[$i] === '@' && trim($this->selector) == '') {
  564. // Check for at-rule
  565. $this->invalid_at = true;
  566. foreach ($at_rules as $name => $type) {
  567. if (!strcasecmp(substr($string, $i + 1, strlen($name)), $name)) {
  568. ($type === 'at') ? $this->at = '@' . $name : $this->selector = '@' . $name;
  569. $this->status = $type;
  570. $i += strlen($name);
  571. $this->invalid_at = false;
  572. }
  573. }
  574. if ($this->invalid_at) {
  575. $this->selector = '@';
  576. $invalid_at_name = '';
  577. for ($j = $i + 1; $j < $size; ++$j) {
  578. if (!ctype_alpha($string[$j])) {
  579. break;
  580. }
  581. $invalid_at_name .= $string[$j];
  582. }
  583. $this->log('Invalid @-rule: ' . $invalid_at_name . ' (removed)', 'Warning');
  584. }
  585. } elseif (($string[$i] === '"' || $string[$i] === "'")) {
  586. $this->cur_string[] = $string[$i];
  587. $this->status = 'instr';
  588. $this->str_char[] = $string[$i];
  589. $this->from[] = 'is';
  590. /* fixing CSS3 attribute selectors, i.e. a[href$=".mp3" */
  591. $this->quoted_string[] = ($string[$i - 1] == '=' );
  592. } elseif ($this->invalid_at && $string[$i] === ';') {
  593. $this->invalid_at = false;
  594. $this->status = 'is';
  595. } elseif ($string[$i] === '{') {
  596. $this->status = 'ip';
  597. if($this->at == '') {
  598. $this->at = $this->css_new_media_section(DEFAULT_AT);
  599. }
  600. $this->selector = $this->css_new_selector($this->at,$this->selector);
  601. $this->_add_token(SEL_START, $this->selector);
  602. $this->added = false;
  603. } elseif ($string[$i] === '}') {
  604. $this->_add_token(AT_END, $this->at);
  605. $this->at = '';
  606. $this->selector = '';
  607. $this->sel_separate = array();
  608. } elseif ($string[$i] === ',') {
  609. $this->selector = trim($this->selector) . ',';
  610. $this->sel_separate[] = strlen($this->selector);
  611. } elseif ($string[$i] === '\\') {
  612. $this->selector .= $this->_unicode($string, $i);
  613. } elseif ($string[$i] === '*' && @in_array($string[$i + 1], array('.', '#', '[', ':'))) {
  614. // remove unnecessary universal selector, FS#147
  615. } else {
  616. $this->selector .= $string[$i];
  617. }
  618. } else {
  619. $lastpos = strlen($this->selector) - 1;
  620. if ($lastpos == -1 || !( (ctype_space($this->selector[$lastpos]) || csstidy::is_token($this->selector, $lastpos) && $this->selector[$lastpos] === ',') && ctype_space($string[$i]))) {
  621. $this->selector .= $string[$i];
  622. }
  623. else if (ctype_space($string[$i]) && $this->get_cfg('preserve_css') && !$this->get_cfg('merge_selectors')) {
  624. $this->selector .= $string[$i];
  625. }
  626. }
  627. break;
  628. /* Case in-property */
  629. case 'ip':
  630. if (csstidy::is_token($string, $i)) {
  631. if (($string[$i] === ':' || $string[$i] === '=') && $this->property != '') {
  632. $this->status = 'iv';
  633. if (!$this->get_cfg('discard_invalid_properties') || csstidy::property_is_valid($this->property)) {
  634. $this->property = $this->css_new_property($this->at,$this->selector,$this->property);
  635. $this->_add_token(PROPERTY, $this->property);
  636. }
  637. } elseif ($string[$i] === '/' && @$string[$i + 1] === '*' && $this->property == '') {
  638. $this->status = 'ic';
  639. ++$i;
  640. $this->from[] = 'ip';
  641. } elseif ($string[$i] === '}') {
  642. $this->explode_selectors();
  643. $this->status = 'is';
  644. $this->invalid_at = false;
  645. $this->_add_token(SEL_END, $this->selector);
  646. $this->selector = '';
  647. $this->property = '';
  648. } elseif ($string[$i] === ';') {
  649. $this->property = '';
  650. } elseif ($string[$i] === '\\') {
  651. $this->property .= $this->_unicode($string, $i);
  652. }
  653. // else this is dumb IE a hack, keep it
  654. elseif ($this->property=='' AND !ctype_space($string[$i])) {
  655. $this->property .= $string[$i];
  656. }
  657. }
  658. elseif (!ctype_space($string[$i])) {
  659. $this->property .= $string[$i];
  660. }
  661. break;
  662. /* Case in-value */
  663. case 'iv':
  664. $pn = (($string[$i] === "\n" || $string[$i] === "\r") && $this->property_is_next($string, $i + 1) || $i == strlen($string) - 1);
  665. if ((csstidy::is_token($string, $i) || $pn) && (!($string[$i] == ',' && !ctype_space($string[$i+1])))) {
  666. if ($string[$i] === '/' && @$string[$i + 1] === '*') {
  667. $this->status = 'ic';
  668. ++$i;
  669. $this->from[] = 'iv';
  670. } elseif (($string[$i] === '"' || $string[$i] === "'" || $string[$i] === '(')) {
  671. $this->cur_string[] = $string[$i];
  672. $this->str_char[] = ($string[$i] === '(') ? ')' : $string[$i];
  673. $this->status = 'instr';
  674. $this->from[] = 'iv';
  675. $this->quoted_string[] = in_array(strtolower($this->property), $quoted_string_properties);
  676. } elseif ($string[$i] === ',') {
  677. $this->sub_value = trim($this->sub_value) . ',';
  678. } elseif ($string[$i] === '\\') {
  679. $this->sub_value .= $this->_unicode($string, $i);
  680. } elseif ($string[$i] === ';' || $pn) {
  681. if ($this->selector[0] === '@' && isset($at_rules[substr($this->selector, 1)]) && $at_rules[substr($this->selector, 1)] === 'iv') {
  682. $this->status = 'is';
  683. switch ($this->selector) {
  684. case '@charset':
  685. /* Add quotes to charset */
  686. $this->sub_value_arr[] = '"' . trim($this->sub_value) . '"';
  687. $this->charset = $this->sub_value_arr[0];
  688. break;
  689. case '@namespace':
  690. /* Add quotes to namespace */
  691. $this->sub_value_arr[] = '"' . trim($this->sub_value) . '"';
  692. $this->namespace = implode(' ', $this->sub_value_arr);
  693. break;
  694. case '@import':
  695. $this->sub_value = trim($this->sub_value);
  696. if (empty($this->sub_value_arr)) {
  697. // Quote URLs in imports only if they're not already inside url() and not already quoted.
  698. if (substr($this->sub_value, 0, 4) != 'url(') {
  699. if (!($this->sub_value[0] == substr($this->sub_value, -1) && in_array($this->sub_value[0], array("'", '"')))) {
  700. $this->sub_value = '"' . $this->sub_value . '"';
  701. }
  702. }
  703. }
  704. $this->sub_value_arr[] = $this->sub_value;
  705. $this->import[] = implode(' ', $this->sub_value_arr);
  706. break;
  707. }
  708. $this->sub_value_arr = array();
  709. $this->sub_value = '';
  710. $this->selector = '';
  711. $this->sel_separate = array();
  712. } else {
  713. $this->status = 'ip';
  714. }
  715. } elseif ($string[$i] !== '}') {
  716. $this->sub_value .= $string[$i];
  717. }
  718. if (($string[$i] === '}' || $string[$i] === ';' || $pn) && !empty($this->selector)) {
  719. if ($this->at == '') {
  720. $this->at = $this->css_new_media_section(DEFAULT_AT);
  721. }
  722. // case settings
  723. if ($this->get_cfg('lowercase_s')) {
  724. $this->selector = strtolower($this->selector);
  725. }
  726. $this->property = strtolower($this->property);
  727. $this->optimise->subvalue();
  728. if ($this->sub_value != '') {
  729. if (substr($this->sub_value, 0, 6) == 'format') {
  730. $format_strings = csstidy::parse_string_list(substr($this->sub_value, 7, -1));
  731. if (!$format_strings) {
  732. $this->sub_value = "";
  733. }
  734. else {
  735. $this->sub_value = "format(";
  736. foreach ($format_strings as $format_string) {
  737. $this->sub_value .= '"' . str_replace('"', '\\"', $format_string) . '",';
  738. }
  739. $this->sub_value = substr($this->sub_value, 0, -1) . ")";
  740. }
  741. }
  742. if ($this->sub_value != '') {
  743. $this->sub_value_arr[] = $this->sub_value;
  744. }
  745. $this->sub_value = '';
  746. }
  747. $this->value = array_shift($this->sub_value_arr);
  748. while(count($this->sub_value_arr)){
  749. //$this->value .= (substr($this->value,-1,1)==','?'':' ').array_shift($this->sub_value_arr);
  750. $this->value .= ' '.array_shift($this->sub_value_arr);
  751. }
  752. $this->optimise->value();
  753. $valid = csstidy::property_is_valid($this->property);
  754. if ((!$this->invalid_at || $this->get_cfg('preserve_css')) && (!$this->get_cfg('discard_invalid_properties') || $valid)) {
  755. $this->css_add_property($this->at, $this->selector, $this->property, $this->value);
  756. $this->_add_token(VALUE, $this->value);
  757. $this->optimise->shorthands();
  758. }
  759. if (!$valid) {
  760. if ($this->get_cfg('discard_invalid_properties')) {
  761. $this->log('Removed invalid property: ' . $this->property, 'Warning');
  762. } else {
  763. $this->log('Invalid property in ' . strtoupper($this->get_cfg('css_level')) . ': ' . $this->property, 'Warning');
  764. }
  765. }
  766. $this->property = '';
  767. $this->sub_value_arr = array();
  768. $this->value = '';
  769. }
  770. if ($string[$i] === '}') {
  771. $this->explode_selectors();
  772. $this->_add_token(SEL_END, $this->selector);
  773. $this->status = 'is';
  774. $this->invalid_at = false;
  775. $this->selector = '';
  776. }
  777. } elseif (!$pn) {
  778. $this->sub_value .= $string[$i];
  779. if (ctype_space($string[$i]) || $string[$i] == ',') {
  780. $this->optimise->subvalue();
  781. if ($this->sub_value != '') {
  782. $this->sub_value_arr[] = $this->sub_value;
  783. $this->sub_value = '';
  784. }
  785. }
  786. }
  787. break;
  788. /* Case in string */
  789. case 'instr':
  790. $_str_char = $this->str_char[count($this->str_char)-1];
  791. $_cur_string = $this->cur_string[count($this->cur_string)-1];
  792. $temp_add = $string[$i];
  793. // Add another string to the stack. Strings can't be nested inside of quotes, only parentheses, but
  794. // parentheticals can be nested more than once.
  795. if ($_str_char === ")" && ($string[$i] === "(" || $string[$i] === '"' || $string[$i] === '\'') && !csstidy::escaped($string, $i)) {
  796. $this->cur_string[] = $string[$i];
  797. $this->str_char[] = $string[$i] == "(" ? ")" : $string[$i];
  798. $this->from[] = 'instr';
  799. $this->quoted_string[] = !($string[$i] === "(");
  800. continue 2;
  801. }
  802. if ($_str_char !== ")" && ($string[$i] === "\n" || $string[$i] === "\r") && !($string[$i - 1] === '\\' && !csstidy::escaped($string, $i - 1))) {
  803. $temp_add = "\\A";
  804. $this->log('Fixed incorrect newline in string', 'Warning');
  805. }
  806. $_cur_string .= $temp_add;
  807. if ($string[$i] === $_str_char && !csstidy::escaped($string, $i)) {
  808. $_quoted_string = array_pop($this->quoted_string);
  809. $this->status = array_pop($this->from);
  810. if (!preg_match('|[' . implode('', $GLOBALS['csstidy']['whitespace']) . ']|uis', $_cur_string) && $this->property !== 'content') {
  811. if (!$_quoted_string) {
  812. if ($_str_char !== ')') {
  813. // Convert properties like
  814. // font-family: 'Arial';
  815. // to
  816. // font-family: Arial;
  817. // or
  818. // url("abc")
  819. // to
  820. // url(abc)
  821. $_cur_string = substr($_cur_string, 1, -1);
  822. }
  823. } else {
  824. $_quoted_string = false;
  825. }
  826. }
  827. array_pop($this->cur_string);
  828. array_pop($this->str_char);
  829. if ($_str_char === ")") {
  830. $_cur_string = "(" . trim(substr($_cur_string, 1, -1)) . ")";
  831. }
  832. if ($this->status === 'iv') {
  833. if (!$_quoted_string){
  834. if (strpos($_cur_string,',')!==false)
  835. // we can on only remove space next to ','
  836. $_cur_string = implode(',',array_map('trim',explode(',',$_cur_string)));
  837. // and multiple spaces (too expensive)
  838. if (strpos($_cur_string,' ')!==false)
  839. $_cur_string = preg_replace(",\s+,"," ",$_cur_string);
  840. }
  841. $this->sub_value .= $_cur_string;
  842. } elseif ($this->status === 'is') {
  843. $this->selector .= $_cur_string;
  844. } elseif ($this->status === 'instr') {
  845. $this->cur_string[count($this->cur_string)-1] .= $_cur_string;
  846. }
  847. }
  848. else {
  849. $this->cur_string[count($this->cur_string)-1] = $_cur_string;
  850. }
  851. break;
  852. /* Case in-comment */
  853. case 'ic':
  854. if ($string[$i] === '*' && $string[$i + 1] === '/') {
  855. $this->status = array_pop($this->from);
  856. $i++;
  857. $this->_add_token(COMMENT, $cur_comment);
  858. $cur_comment = '';
  859. } else {
  860. $cur_comment .= $string[$i];
  861. }
  862. break;
  863. }
  864. }
  865. $this->optimise->postparse();
  866. $this->print->_reset();
  867. @setlocale(LC_ALL, $old); // Set locale back to original setting
  868. return!(empty($this->css) && empty($this->import) && empty($this->charset) && empty($this->tokens) && empty($this->namespace));
  869. }
  870. /**
  871. * Explodes selectors
  872. * @access private
  873. * @version 1.0
  874. */
  875. function explode_selectors() {
  876. // Explode multiple selectors
  877. if ($this->get_cfg('merge_selectors') === 1) {
  878. $new_sels = array();
  879. $lastpos = 0;
  880. $this->sel_separate[] = strlen($this->selector);
  881. foreach ($this->sel_separate as $num => $pos) {
  882. if ($num == count($this->sel_separate) - 1) {
  883. $pos += 1;
  884. }
  885. $new_sels[] = substr($this->selector, $lastpos, $pos - $lastpos - 1);
  886. $lastpos = $pos;
  887. }
  888. if (count($new_sels) > 1) {
  889. foreach ($new_sels as $selector) {
  890. if (isset($this->css[$this->at][$this->selector])) {
  891. $this->merge_css_blocks($this->at, $selector, $this->css[$this->at][$this->selector]);
  892. }
  893. }
  894. unset($this->css[$this->at][$this->selector]);
  895. }
  896. }
  897. $this->sel_separate = array();
  898. }
  899. /**
  900. * Checks if a character is escaped (and returns true if it is)
  901. * @param string $string
  902. * @param integer $pos
  903. * @access public
  904. * @return bool
  905. * @version 1.02
  906. */
  907. static function escaped(&$string, $pos) {
  908. return!(@($string[$pos - 1] !== '\\') || csstidy::escaped($string, $pos - 1));
  909. }
  910. /**
  911. * Adds a property with value to the existing CSS code
  912. * @param string $media
  913. * @param string $selector
  914. * @param string $property
  915. * @param string $new_val
  916. * @access private
  917. * @version 1.2
  918. */
  919. function css_add_property($media, $selector, $property, $new_val) {
  920. if ($this->get_cfg('preserve_css') || trim($new_val) == '') {
  921. return;
  922. }
  923. $this->added = true;
  924. if (isset($this->css[$media][$selector][$property])) {
  925. if ((csstidy::is_important($this->css[$media][$selector][$property]) && csstidy::is_important($new_val)) || !csstidy::is_important($this->css[$media][$selector][$property])) {
  926. $this->css[$media][$selector][$property] = trim($new_val);
  927. }
  928. } else {
  929. $this->css[$media][$selector][$property] = trim($new_val);
  930. }
  931. }
  932. /**
  933. * Start a new media section.
  934. * Check if the media is not already known,
  935. * else rename it with extra spaces
  936. * to avoid merging
  937. *
  938. * @param string $media
  939. * @return string
  940. */
  941. function css_new_media_section($media){
  942. if($this->get_cfg('preserve_css')) {
  943. return $media;
  944. }
  945. // if the last @media is the same as this
  946. // keep it
  947. if (!$this->css OR !is_array($this->css) OR empty($this->css)){
  948. return $media;
  949. }
  950. end($this->css);
  951. $at = current( $this->css );
  952. if ($at == $media){
  953. return $media;
  954. }
  955. while (isset($this->css[$media]))
  956. if (is_numeric($media))
  957. $media++;
  958. else
  959. $media .= " ";
  960. return $media;
  961. }
  962. /**
  963. * Start a new selector.
  964. * If already referenced in this media section,
  965. * rename it with extra space to avoid merging
  966. * except if merging is required,
  967. * or last selector is the same (merge siblings)
  968. *
  969. * never merge @font-face
  970. *
  971. * @param string $media
  972. * @param string $selector
  973. * @return string
  974. */
  975. function css_new_selector($media,$selector){
  976. if($this->get_cfg('preserve_css')) {
  977. return $selector;
  978. }
  979. $selector = trim($selector);
  980. if (strncmp($selector,"@font-face",10)!=0){
  981. if ($this->settings['merge_selectors'] != false)
  982. return $selector;
  983. if (!$this->css OR !isset($this->css[$media]) OR !$this->css[$media])
  984. return $selector;
  985. // if last is the same, keep it
  986. end($this->css[$media]);
  987. $sel = current( $this->css[$media] );
  988. if ($sel == $selector){
  989. return $selector;
  990. }
  991. }
  992. while (isset($this->css[$media][$selector]))
  993. $selector .= " ";
  994. return $selector;
  995. }
  996. /**
  997. * Start a new propertie.
  998. * If already references in this selector,
  999. * rename it with extra space to avoid override
  1000. *
  1001. * @param string $media
  1002. * @param string $selector
  1003. * @param string $property
  1004. * @return string
  1005. */
  1006. function css_new_property($media, $selector, $property){
  1007. if($this->get_cfg('preserve_css')) {
  1008. return $property;
  1009. }
  1010. if (!$this->css OR !isset($this->css[$media][$selector]) OR !$this->css[$media][$selector])
  1011. return $property;
  1012. while (isset($this->css[$media][$selector][$property]))
  1013. $property .= " ";
  1014. return $property;
  1015. }
  1016. /**
  1017. * Adds CSS to an existing media/selector
  1018. * @param string $media
  1019. * @param string $selector
  1020. * @param array $css_add
  1021. * @access private
  1022. * @version 1.1
  1023. */
  1024. function merge_css_blocks($media, $selector, $css_add) {
  1025. foreach ($css_add as $property => $value) {
  1026. $this->css_add_property($media, $selector, $property, $value, false);
  1027. }
  1028. }
  1029. /**
  1030. * Checks if $value is !important.
  1031. * @param string $value
  1032. * @return bool
  1033. * @access public
  1034. * @version 1.0
  1035. */
  1036. static function is_important(&$value) {
  1037. return (!strcasecmp(substr(str_replace($GLOBALS['csstidy']['whitespace'], '', $value), -10, 10), '!important'));
  1038. }
  1039. /**
  1040. * Returns a value without !important
  1041. * @param string $value
  1042. * @return string
  1043. * @access public
  1044. * @version 1.0
  1045. */
  1046. static function gvw_important($value) {
  1047. if (csstidy::is_important($value)) {
  1048. $value = trim($value);
  1049. $value = substr($value, 0, -9);
  1050. $value = trim($value);
  1051. $value = substr($value, 0, -1);
  1052. $value = trim($value);
  1053. return $value;
  1054. }
  1055. return $value;
  1056. }
  1057. /**
  1058. * Checks if the next word in a string from pos is a CSS property
  1059. * @param string $istring
  1060. * @param integer $pos
  1061. * @return bool
  1062. * @access private
  1063. * @version 1.2
  1064. */
  1065. function property_is_next($istring, $pos) {
  1066. $all_properties = & $GLOBALS['csstidy']['all_properties'];
  1067. $istring = substr($istring, $pos, strlen($istring) - $pos);
  1068. $pos = strpos($istring, ':');
  1069. if ($pos === false) {
  1070. return false;
  1071. }
  1072. $istring = strtolower(trim(substr($istring, 0, $pos)));
  1073. if (isset($all_properties[$istring])) {
  1074. $this->log('Added semicolon to the end of declaration', 'Warning');
  1075. return true;
  1076. }
  1077. return false;
  1078. }
  1079. /**
  1080. * Checks if a property is valid
  1081. * @param string $property
  1082. * @return bool;
  1083. * @access public
  1084. * @version 1.0
  1085. */
  1086. function property_is_valid($property) {
  1087. $property = strtolower($property);
  1088. if (in_array(trim($property), $GLOBALS['csstidy']['multiple_properties'])) $property = trim($property);
  1089. $all_properties = & $GLOBALS['csstidy']['all_properties'];
  1090. return (isset($all_properties[$property]) && strpos($all_properties[$property], strtoupper($this->get_cfg('css_level'))) !== false );
  1091. }
  1092. /**
  1093. * Accepts a list of strings (e.g., the argument to format() in a @font-face src property)
  1094. * and returns a list of the strings. Converts things like:
  1095. *
  1096. * format(abc) => format("abc")
  1097. * format(abc def) => format("abc","def")
  1098. * format(abc "def") => format("abc","def")
  1099. * format(abc, def, ghi) => format("abc","def","ghi")
  1100. * format("abc",'def') => format("abc","def")
  1101. * format("abc, def, ghi") => format("abc, def, ghi")
  1102. *
  1103. * @param string
  1104. * @return array
  1105. */
  1106. function parse_string_list($value) {
  1107. $value = trim($value);
  1108. // Case: empty
  1109. if (!$value) return array();
  1110. $strings = array();
  1111. $in_str = false;
  1112. $current_string = "";
  1113. for ($i = 0, $_len = strlen($value); $i < $_len; $i++) {
  1114. if (($value[$i] == "," || $value[$i] === " ") && $in_str === true) {
  1115. $in_str = false;
  1116. $strings[] = $current_string;
  1117. $current_string = "";
  1118. }
  1119. else if ($value[$i] == '"' || $value[$i] == "'"){
  1120. if ($in_str === $value[$i]) {
  1121. $strings[] = $current_string;
  1122. $in_str = false;
  1123. $current_string = "";
  1124. continue;
  1125. }
  1126. else if (!$in_str) {
  1127. $in_str = $value[$i];
  1128. }
  1129. }
  1130. else {
  1131. if ($in_str){
  1132. $current_string .= $value[$i];
  1133. }
  1134. else {
  1135. if (!preg_match("/[\s,]/", $value[$i])) {
  1136. $in_str = true;
  1137. $current_string = $value[$i];
  1138. }
  1139. }
  1140. }
  1141. }
  1142. if ($current_string) {
  1143. $strings[] = $current_string;
  1144. }
  1145. return $strings;
  1146. }
  1147. }