class.csstidy.php 36 KB

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