Emogrifier.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. <?php
  2. /**
  3. * This class provides functions for converting CSS styles into inline style attributes in your HTML code.
  4. *
  5. * For more information, please see the README.md file.
  6. *
  7. * @author Cameron Brooks
  8. * @author Jaime Prado
  9. * @author Roman Ožana <ozana@omdesign.cz>
  10. *
  11. * @deprecated Use composer to include Emogrifier instead.
  12. */
  13. class Emogrifier {
  14. /**
  15. * @var string
  16. */
  17. const ENCODING = 'UTF-8';
  18. /**
  19. * @var integer
  20. */
  21. const CACHE_KEY_CSS = 0;
  22. /**
  23. * @var integer
  24. */
  25. const CACHE_KEY_SELECTOR = 1;
  26. /**
  27. * @var integer
  28. */
  29. const CACHE_KEY_XPATH = 2;
  30. /**
  31. * @var integer
  32. */
  33. const CACHE_KEY_CSS_DECLARATION_BLOCK = 3;
  34. /**
  35. * for calculating nth-of-type and nth-child selectors
  36. *
  37. * @var integer
  38. */
  39. const INDEX = 0;
  40. /**
  41. * for calculating nth-of-type and nth-child selectors
  42. *
  43. * @var integer
  44. */
  45. const MULTIPLIER = 1;
  46. /**
  47. * @var string
  48. */
  49. const ID_ATTRIBUTE_MATCHER = '/(\\w+)?\\#([\\w\\-]+)/';
  50. /**
  51. * @var string
  52. */
  53. const CLASS_ATTRIBUTE_MATCHER = '/(\\w+|[\\*\\]])?((\\.[\\w\\-]+)+)/';
  54. /**
  55. * @var string
  56. */
  57. private $html = '';
  58. /**
  59. * @var string
  60. */
  61. private $css = '';
  62. /**
  63. * @var array<string>
  64. */
  65. private $unprocessableHtmlTags = ['wbr'];
  66. /**
  67. * @var array<array>
  68. */
  69. private $caches = [
  70. self::CACHE_KEY_CSS => [],
  71. self::CACHE_KEY_SELECTOR => [],
  72. self::CACHE_KEY_XPATH => [],
  73. self::CACHE_KEY_CSS_DECLARATION_BLOCK => [],
  74. ];
  75. /**
  76. * the visited nodes with the XPath paths as array keys
  77. *
  78. * @var array<\DOMNode>
  79. */
  80. private $visitedNodes = [];
  81. /**
  82. * the styles to apply to the nodes with the XPath paths as array keys for the outer array and the attribute names/values
  83. * as key/value pairs for the inner array
  84. *
  85. * @var array<array><string>
  86. */
  87. private $styleAttributesForNodes = [];
  88. /**
  89. * This attribute applies to the case where you want to preserve your original text encoding.
  90. *
  91. * By default, emogrifier translates your text into HTML entities for two reasons:
  92. *
  93. * 1. Because of client incompatibilities, it is better practice to send out HTML entities rather than unicode over email.
  94. *
  95. * 2. It translates any illegal XML characters that DOMDocument cannot work with.
  96. *
  97. * If you would like to preserve your original encoding, set this attribute to TRUE.
  98. *
  99. * @var boolean
  100. */
  101. public $preserveEncoding = FALSE;
  102. /**
  103. * The constructor.
  104. *
  105. * @param string $html the HTML to emogrify, must be UTF-8-encoded
  106. * @param string $css the CSS to merge, must be UTF-8-encoded
  107. */
  108. public function __construct($html = '', $css = '') {
  109. $this->setHtml($html);
  110. $this->setCss($css);
  111. }
  112. /**
  113. * The destructor.
  114. */
  115. public function __destruct() {
  116. $this->purgeVisitedNodes();
  117. }
  118. /**
  119. * Sets the HTML to emogrify.
  120. *
  121. * @param string $html the HTML to emogrify, must be UTF-8-encoded
  122. *
  123. * @return void
  124. */
  125. public function setHtml($html = '') {
  126. $this->html = $html;
  127. }
  128. /**
  129. * Sets the CSS to merge with the HTML.
  130. *
  131. * @param string $css the CSS to merge, must be UTF-8-encoded
  132. *
  133. * @return void
  134. */
  135. public function setCss($css = '') {
  136. $this->css = $css;
  137. }
  138. /**
  139. * Clears all caches.
  140. *
  141. * @return void
  142. */
  143. private function clearAllCaches() {
  144. $this->clearCache(self::CACHE_KEY_CSS);
  145. $this->clearCache(self::CACHE_KEY_SELECTOR);
  146. $this->clearCache(self::CACHE_KEY_XPATH);
  147. $this->clearCache(self::CACHE_KEY_CSS_DECLARATION_BLOCK);
  148. }
  149. /**
  150. * Clears a single cache by key.
  151. *
  152. * @param integer $key the cache key, must be CACHE_KEY_CSS, CACHE_KEY_SELECTOR, CACHE_KEY_XPATH or CACHE_KEY_CSS_DECLARATION_BLOCK
  153. *
  154. * @return void
  155. *
  156. * @throws \InvalidArgumentException
  157. */
  158. private function clearCache($key) {
  159. $allowedCacheKeys = [self::CACHE_KEY_CSS, self::CACHE_KEY_SELECTOR, self::CACHE_KEY_XPATH, self::CACHE_KEY_CSS_DECLARATION_BLOCK];
  160. if (!in_array($key, $allowedCacheKeys, TRUE)) {
  161. throw new \InvalidArgumentException('Invalid cache key: ' . $key, 1391822035);
  162. }
  163. $this->caches[$key] = [];
  164. }
  165. /**
  166. * Purges the visited nodes.
  167. *
  168. * @return void
  169. */
  170. private function purgeVisitedNodes() {
  171. $this->visitedNodes = [];
  172. $this->styleAttributesForNodes = [];
  173. }
  174. /**
  175. * Marks a tag for removal.
  176. *
  177. * There are some HTML tags that DOMDocument cannot process, and it will throw an error if it encounters them.
  178. * In particular, DOMDocument will complain if you try to use HTML5 tags in an XHTML document.
  179. *
  180. * Note: The tags will not be removed if they have any content.
  181. *
  182. * @param string $tagName the tag name, e.g., "p"
  183. *
  184. * @return void
  185. */
  186. public function addUnprocessableHtmlTag($tagName) {
  187. $this->unprocessableHtmlTags[] = $tagName;
  188. }
  189. /**
  190. * Drops a tag from the removal list.
  191. *
  192. * @param string $tagName the tag name, e.g., "p"
  193. *
  194. * @return void
  195. */
  196. public function removeUnprocessableHtmlTag($tagName) {
  197. $key = array_search($tagName, $this->unprocessableHtmlTags, TRUE);
  198. if ($key !== FALSE) {
  199. unset($this->unprocessableHtmlTags[$key]);
  200. }
  201. }
  202. /**
  203. * Applies the CSS you submit to the HTML you submit.
  204. *
  205. * This method places the CSS inline.
  206. *
  207. * @return string
  208. *
  209. * @throws \BadMethodCallException
  210. */
  211. public function emogrify() {
  212. if ($this->html === '') {
  213. throw new \BadMethodCallException('Please set some HTML first before calling emogrify.', 1390393096);
  214. }
  215. $xmlDocument = $this->createXmlDocument();
  216. $xpath = new \DOMXPath($xmlDocument);
  217. $this->clearAllCaches();
  218. // before be begin processing the CSS file, parse the document and normalize all existing CSS attributes (changes 'DISPLAY: none' to 'display: none');
  219. // we wouldn't have to do this if DOMXPath supported XPath 2.0.
  220. // also store a reference of nodes with existing inline styles so we don't overwrite them
  221. $this->purgeVisitedNodes();
  222. $nodesWithStyleAttributes = $xpath->query('//*[@style]');
  223. if ($nodesWithStyleAttributes !== FALSE) {
  224. /** @var $nodeWithStyleAttribute \DOMNode */
  225. foreach ($nodesWithStyleAttributes as $node) {
  226. $normalizedOriginalStyle = preg_replace_callback(
  227. '/[A-z\\-]+(?=\\:)/S',
  228. function (array $m) {
  229. return strtolower($m[0]);
  230. },
  231. $node->getAttribute('style')
  232. );
  233. // in order to not overwrite existing style attributes in the HTML, we have to save the original HTML styles
  234. $nodePath = $node->getNodePath();
  235. if (!isset($this->styleAttributesForNodes[$nodePath])) {
  236. $this->styleAttributesForNodes[$nodePath] = $this->parseCssDeclarationBlock($normalizedOriginalStyle);
  237. $this->visitedNodes[$nodePath] = $node;
  238. }
  239. $node->setAttribute('style', $normalizedOriginalStyle);
  240. }
  241. }
  242. // grab any existing style blocks from the html and append them to the existing CSS
  243. // (these blocks should be appended so as to have precedence over conflicting styles in the existing CSS)
  244. $allCss = $this->css;
  245. $allCss .= $this->getCssFromAllStyleNodes($xpath);
  246. $cssParts = $this->splitCssAndMediaQuery($allCss);
  247. $cssKey = md5($cssParts['css']);
  248. if (!isset($this->caches[self::CACHE_KEY_CSS][$cssKey])) {
  249. // process the CSS file for selectors and definitions
  250. preg_match_all('/(?:^|[\\s^{}]*)([^{]+){([^}]*)}/mis', $cssParts['css'], $matches, PREG_SET_ORDER);
  251. $allSelectors = [];
  252. foreach ($matches as $key => $selectorString) {
  253. // if there is a blank definition, skip
  254. if (!strlen(trim($selectorString[2]))) {
  255. continue;
  256. }
  257. // else split by commas and duplicate attributes so we can sort by selector precedence
  258. $selectors = explode(',', $selectorString[1]);
  259. foreach ($selectors as $selector) {
  260. // don't process pseudo-elements and behavioral (dynamic) pseudo-classes; ONLY allow structural pseudo-classes
  261. if (strpos($selector, ':') !== FALSE && !preg_match('/:\\S+\\-(child|type)\\(/i', $selector)) {
  262. continue;
  263. }
  264. $allSelectors[] = ['selector' => trim($selector),
  265. 'attributes' => trim($selectorString[2]),
  266. // keep track of where it appears in the file, since order is important
  267. 'line' => $key,
  268. ];
  269. }
  270. }
  271. // now sort the selectors by precedence
  272. usort($allSelectors, [$this,'sortBySelectorPrecedence']);
  273. $this->caches[self::CACHE_KEY_CSS][$cssKey] = $allSelectors;
  274. }
  275. foreach ($this->caches[self::CACHE_KEY_CSS][$cssKey] as $value) {
  276. // query the body for the xpath selector
  277. $nodesMatchingCssSelectors = $xpath->query($this->translateCssToXpath(trim($value['selector'])));
  278. /** @var $node \DOMNode */
  279. foreach ($nodesMatchingCssSelectors as $node) {
  280. // if it has a style attribute, get it, process it, and append (overwrite) new stuff
  281. if ($node->hasAttribute('style')) {
  282. // break it up into an associative array
  283. $oldStyleDeclarations = $this->parseCssDeclarationBlock($node->getAttribute('style'));
  284. $newStyleDeclarations = $this->parseCssDeclarationBlock($value['attributes']);
  285. // new styles overwrite the old styles (not technically accurate, but close enough)
  286. $combinedArray = array_merge($oldStyleDeclarations, $newStyleDeclarations);
  287. $style = '';
  288. foreach ($combinedArray as $attributeName => $attributeValue) {
  289. $style .= (strtolower($attributeName) . ':' . $attributeValue . ';');
  290. }
  291. } else {
  292. // otherwise create a new style
  293. $style = trim($value['attributes']);
  294. }
  295. $node->setAttribute('style', $style);
  296. }
  297. }
  298. // now iterate through the nodes that contained inline styles in the original HTML
  299. foreach ($this->styleAttributesForNodes as $nodePath => $styleAttributesForNode) {
  300. $node = $this->visitedNodes[$nodePath];
  301. $currentStyleAttributes = $this->parseCssDeclarationBlock($node->getAttribute('style'));
  302. $combinedArray = array_merge($currentStyleAttributes, $styleAttributesForNode);
  303. $style = '';
  304. foreach ($combinedArray as $attributeName => $attributeValue) {
  305. $style .= (strtolower($attributeName) . ':' . $attributeValue . ';');
  306. }
  307. $node->setAttribute('style', $style);
  308. }
  309. // This removes styles from your email that contain display:none.
  310. // We need to look for display:none, but we need to do a case-insensitive search. Since DOMDocument only supports XPath 1.0,
  311. // lower-case() isn't available to us. We've thus far only set attributes to lowercase, not attribute values. Consequently, we need
  312. // to translate() the letters that would be in 'NONE' ("NOE") to lowercase.
  313. $nodesWithStyleDisplayNone = $xpath->query('//*[contains(translate(translate(@style," ",""),"NOE","noe"),"display:none")]');
  314. // The checks on parentNode and is_callable below ensure that if we've deleted the parent node,
  315. // we don't try to call removeChild on a nonexistent child node
  316. if ($nodesWithStyleDisplayNone->length > 0) {
  317. /** @var $node \DOMNode */
  318. foreach ($nodesWithStyleDisplayNone as $node) {
  319. if ($node->parentNode && is_callable([$node->parentNode,'removeChild'])) {
  320. $node->parentNode->removeChild($node);
  321. }
  322. }
  323. }
  324. $this->copyCssWithMediaToStyleNode($cssParts, $xmlDocument);
  325. if ($this->preserveEncoding) {
  326. return mb_convert_encoding($xmlDocument->saveHTML(), self::ENCODING, 'HTML-ENTITIES');
  327. } else {
  328. return $xmlDocument->saveHTML();
  329. }
  330. }
  331. /**
  332. * Copies the media part from CSS array parts to $xmlDocument.
  333. *
  334. * @param array $cssParts
  335. * @param \DOMDocument $xmlDocument
  336. * @return void
  337. */
  338. public function copyCssWithMediaToStyleNode(array $cssParts, \DOMDocument $xmlDocument) {
  339. if (isset($cssParts['media']) && $cssParts['media'] !== '') {
  340. $this->addStyleElementToDocument($xmlDocument, $cssParts['media']);
  341. }
  342. }
  343. /**
  344. * Returns CSS content.
  345. *
  346. * @param \DOMXPath $xpath
  347. * @return string
  348. */
  349. private function getCssFromAllStyleNodes(\DOMXPath $xpath) {
  350. $styleNodes = $xpath->query('//style');
  351. if ($styleNodes === FALSE) {
  352. return '';
  353. }
  354. $css = '';
  355. /** @var $styleNode \DOMNode */
  356. foreach ($styleNodes as $styleNode) {
  357. $css .= "\n\n" . $styleNode->nodeValue;
  358. $styleNode->parentNode->removeChild($styleNode);
  359. }
  360. return $css;
  361. }
  362. /**
  363. * Adds a style element with $css to $document.
  364. *
  365. * @param \DOMDocument $document
  366. * @param string $css
  367. * @return void
  368. */
  369. private function addStyleElementToDocument(\DOMDocument $document, $css) {
  370. $styleElement = $document->createElement('style', $css);
  371. $styleAttribute = $document->createAttribute('type');
  372. $styleAttribute->value = 'text/css';
  373. $styleElement->appendChild($styleAttribute);
  374. $head = $this->getOrCreateHeadElement($document);
  375. $head->appendChild($styleElement);
  376. }
  377. /**
  378. * Returns the existing or creates a new head element in $document.
  379. *
  380. * @param \DOMDocument $document
  381. * @return \DOMNode the head element
  382. */
  383. private function getOrCreateHeadElement(\DOMDocument $document) {
  384. $head = $document->getElementsByTagName('head')->item(0);
  385. if ($head === NULL) {
  386. $head = $document->createElement('head');
  387. $html = $document->getElementsByTagName('html')->item(0);
  388. $html->insertBefore($head, $document->getElementsByTagName('body')->item(0));
  389. }
  390. return $head;
  391. }
  392. /**
  393. * Splits input CSS code to an array where:
  394. *
  395. * - key "css" will be contains clean CSS code
  396. * - key "media" will be contains all valuable media queries
  397. *
  398. * Example:
  399. *
  400. * The CSS code
  401. *
  402. * "@import "file.css"; h1 { color:red; } @media { h1 {}} @media tv { h1 {}}"
  403. *
  404. * will be parsed into the following array:
  405. *
  406. * "css" => "h1 { color:red; }"
  407. * "media" => "@media { h1 {}}"
  408. *
  409. * @param string $css
  410. * @return array
  411. */
  412. private function splitCssAndMediaQuery($css) {
  413. $media = '';
  414. $css = preg_replace_callback(
  415. '#@media\\s+(?:only\\s)?(?:[\\s{\(]|screen|all)\\s?[^{]+{.*}\\s*}\\s*#misU',
  416. function($matches) use (&$media) {
  417. $media .= $matches[0];
  418. }, $css
  419. );
  420. // filter the CSS
  421. $search = [
  422. // get rid of css comment code
  423. '/\\/\\*.*\\*\\//sU',
  424. // strip out any import directives
  425. '/^\\s*@import\\s[^;]+;/misU',
  426. // strip remains media enclosures
  427. '/^\\s*@media\\s[^{]+{(.*)}\\s*}\\s/misU',
  428. ];
  429. $replace = [
  430. '',
  431. '',
  432. '',
  433. ];
  434. // clean CSS before output
  435. $css = preg_replace($search, $replace, $css);
  436. return ['css' => $css, 'media' => $media];
  437. }
  438. /**
  439. * Creates a DOMDocument instance with the current HTML.
  440. *
  441. * @return \DOMDocument
  442. */
  443. private function createXmlDocument() {
  444. $xmlDocument = new \DOMDocument;
  445. $xmlDocument->encoding = self::ENCODING;
  446. $xmlDocument->strictErrorChecking = FALSE;
  447. $xmlDocument->formatOutput = TRUE;
  448. $libXmlState = libxml_use_internal_errors(TRUE);
  449. $xmlDocument->loadHTML($this->getUnifiedHtml());
  450. libxml_clear_errors();
  451. libxml_use_internal_errors($libXmlState);
  452. $xmlDocument->normalizeDocument();
  453. return $xmlDocument;
  454. }
  455. /**
  456. * Returns the HTML with the non-ASCII characters converts into HTML entities and the unprocessable HTML tags removed.
  457. *
  458. * @return string the unified HTML
  459. *
  460. * @throws \BadMethodCallException
  461. */
  462. private function getUnifiedHtml() {
  463. if (!empty($this->unprocessableHtmlTags)) {
  464. $unprocessableHtmlTags = implode('|', $this->unprocessableHtmlTags);
  465. $bodyWithoutUnprocessableTags = preg_replace('/<\\/?(' . $unprocessableHtmlTags . ')[^>]*>/i', '', $this->html);
  466. } else {
  467. $bodyWithoutUnprocessableTags = $this->html;
  468. }
  469. return mb_convert_encoding($bodyWithoutUnprocessableTags, 'HTML-ENTITIES', self::ENCODING);
  470. }
  471. /**
  472. * @param array $a
  473. * @param array $b
  474. *
  475. * @return integer
  476. */
  477. private function sortBySelectorPrecedence(array $a, array $b) {
  478. $precedenceA = $this->getCssSelectorPrecedence($a['selector']);
  479. $precedenceB = $this->getCssSelectorPrecedence($b['selector']);
  480. // We want these sorted in ascending order so selectors with lesser precedence get processed first and
  481. // selectors with greater precedence get sorted last.
  482. // The parenthesis around the -1 are necessary to avoid a PHP_CodeSniffer warning about missing spaces around
  483. // arithmetic operators.
  484. // @see http://forge.typo3.org/issues/55605
  485. $precedenceForEquals = ($a['line'] < $b['line'] ? (-1) : 1);
  486. $precedenceForNotEquals = ($precedenceA < $precedenceB ? (-1) : 1);
  487. return ($precedenceA === $precedenceB) ? $precedenceForEquals : $precedenceForNotEquals;
  488. }
  489. /**
  490. * @param string $selector
  491. *
  492. * @return integer
  493. */
  494. private function getCssSelectorPrecedence($selector) {
  495. $selectorKey = md5($selector);
  496. if (!isset($this->caches[self::CACHE_KEY_SELECTOR][$selectorKey])) {
  497. $precedence = 0;
  498. $value = 100;
  499. // ids: worth 100, classes: worth 10, elements: worth 1
  500. $search = ['\\#','\\.',''];
  501. foreach ($search as $s) {
  502. if (trim($selector == '')) {
  503. break;
  504. }
  505. $number = 0;
  506. $selector = preg_replace('/' . $s . '\\w+/', '', $selector, -1, $number);
  507. $precedence += ($value * $number);
  508. $value /= 10;
  509. }
  510. $this->caches[self::CACHE_KEY_SELECTOR][$selectorKey] = $precedence;
  511. }
  512. return $this->caches[self::CACHE_KEY_SELECTOR][$selectorKey];
  513. }
  514. /**
  515. * Right now, we support all CSS 1 selectors and most CSS2/3 selectors.
  516. *
  517. * @see http://plasmasturm.org/log/444/
  518. *
  519. * @param string $cssSelector
  520. *
  521. * @return string
  522. */
  523. private function translateCssToXpath($cssSelector) {
  524. $cssSelector = trim($cssSelector);
  525. $xpathKey = md5($cssSelector);
  526. if (!isset($this->caches[self::CACHE_KEY_XPATH][$xpathKey])) {
  527. // returns an Xpath selector
  528. $search = [
  529. // Matches any element that is a child of parent.
  530. '/\\s+>\\s+/',
  531. // Matches any element that is an adjacent sibling.
  532. '/\\s+\\+\\s+/',
  533. // Matches any element that is a descendant of an parent element element.
  534. '/\\s+/',
  535. // first-child pseudo-selector
  536. '/([^\\/]+):first-child/i',
  537. // last-child pseudo-selector
  538. '/([^\\/]+):last-child/i',
  539. // Matches attribute only selector
  540. '/^\\[(\\w+)\\]/',
  541. // Matches element with attribute
  542. '/(\\w)\\[(\\w+)\\]/',
  543. // Matches element with EXACT attribute
  544. '/(\\w)\\[(\\w+)\\=[\'"]?(\\w+)[\'"]?\\]/',
  545. ];
  546. $replace = [
  547. '/',
  548. '/following-sibling::*[1]/self::',
  549. '//',
  550. '*[1]/self::\\1',
  551. '*[last()]/self::\\1',
  552. '*[@\\1]',
  553. '\\1[@\\2]',
  554. '\\1[@\\2="\\3"]',
  555. ];
  556. $cssSelector = '//' . preg_replace($search, $replace, $cssSelector);
  557. $cssSelector = preg_replace_callback(self::ID_ATTRIBUTE_MATCHER, [$this, 'matchIdAttributes'], $cssSelector);
  558. $cssSelector = preg_replace_callback(self::CLASS_ATTRIBUTE_MATCHER, [$this, 'matchClassAttributes'], $cssSelector);
  559. // Advanced selectors are going to require a bit more advanced emogrification.
  560. // When we required PHP 5.3, we could do this with closures.
  561. $cssSelector = preg_replace_callback(
  562. '/([^\\/]+):nth-child\\(\s*(odd|even|[+\-]?\\d|[+\\-]?\\d?n(\\s*[+\\-]\\s*\\d)?)\\s*\\)/i',
  563. [$this, 'translateNthChild'], $cssSelector
  564. );
  565. $cssSelector = preg_replace_callback(
  566. '/([^\\/]+):nth-of-type\\(\s*(odd|even|[+\-]?\\d|[+\\-]?\\d?n(\\s*[+\\-]\\s*\\d)?)\\s*\\)/i',
  567. [$this, 'translateNthOfType'], $cssSelector
  568. );
  569. $this->caches[self::CACHE_KEY_SELECTOR][$xpathKey] = $cssSelector;
  570. }
  571. return $this->caches[self::CACHE_KEY_SELECTOR][$xpathKey];
  572. }
  573. /**
  574. * @param array $match
  575. *
  576. * @return string
  577. */
  578. private function matchIdAttributes(array $match) {
  579. return (strlen($match[1]) ? $match[1] : '*') . '[@id="' . $match[2] . '"]';
  580. }
  581. /**
  582. * @param array $match
  583. *
  584. * @return string
  585. */
  586. private function matchClassAttributes(array $match) {
  587. return (strlen($match[1]) ? $match[1] : '*') . '[contains(concat(" ",@class," "),concat(" ","' .
  588. implode(
  589. '"," "))][contains(concat(" ",@class," "),concat(" ","',
  590. explode('.', substr($match[2], 1))
  591. ) . '"," "))]';
  592. }
  593. /**
  594. * @param array $match
  595. *
  596. * @return string
  597. */
  598. private function translateNthChild(array $match) {
  599. $result = $this->parseNth($match);
  600. if (isset($result[self::MULTIPLIER])) {
  601. if ($result[self::MULTIPLIER] < 0) {
  602. $result[self::MULTIPLIER] = abs($result[self::MULTIPLIER]);
  603. return sprintf('*[(last() - position()) mod %u = %u]/self::%s', $result[self::MULTIPLIER], $result[self::INDEX], $match[1]);
  604. } else {
  605. return sprintf('*[position() mod %u = %u]/self::%s', $result[self::MULTIPLIER], $result[self::INDEX], $match[1]);
  606. }
  607. } else {
  608. return sprintf('*[%u]/self::%s', $result[self::INDEX], $match[1]);
  609. }
  610. }
  611. /**
  612. * @param array $match
  613. *
  614. * @return string
  615. */
  616. private function translateNthOfType(array $match) {
  617. $result = $this->parseNth($match);
  618. if (isset($result[self::MULTIPLIER])) {
  619. if ($result[self::MULTIPLIER] < 0) {
  620. $result[self::MULTIPLIER] = abs($result[self::MULTIPLIER]);
  621. return sprintf('%s[(last() - position()) mod %u = %u]', $match[1], $result[self::MULTIPLIER], $result[self::INDEX]);
  622. } else {
  623. return sprintf('%s[position() mod %u = %u]', $match[1], $result[self::MULTIPLIER], $result[self::INDEX]);
  624. }
  625. } else {
  626. return sprintf('%s[%u]', $match[1], $result[self::INDEX]);
  627. }
  628. }
  629. /**
  630. * @param array $match
  631. *
  632. * @return array
  633. */
  634. private function parseNth(array $match) {
  635. if (in_array(strtolower($match[2]), ['even','odd'])) {
  636. $index = strtolower($match[2]) == 'even' ? 0 : 1;
  637. return [self::MULTIPLIER => 2, self::INDEX => $index];
  638. } elseif (stripos($match[2], 'n') === FALSE) {
  639. // if there is a multiplier
  640. $index = intval(str_replace(' ', '', $match[2]));
  641. return [self::INDEX => $index];
  642. } else {
  643. if (isset($match[3])) {
  644. $multipleTerm = str_replace($match[3], '', $match[2]);
  645. $index = intval(str_replace(' ', '', $match[3]));
  646. } else {
  647. $multipleTerm = $match[2];
  648. $index = 0;
  649. }
  650. $multiplier = str_ireplace('n', '', $multipleTerm);
  651. if (!strlen($multiplier)) {
  652. $multiplier = 1;
  653. } elseif ($multiplier == 0) {
  654. return [self::INDEX => $index];
  655. } else {
  656. $multiplier = intval($multiplier);
  657. }
  658. while ($index < 0) {
  659. $index += abs($multiplier);
  660. }
  661. return [self::MULTIPLIER => $multiplier, self::INDEX => $index];
  662. }
  663. }
  664. /**
  665. * Parses a CSS declaration block into property name/value pairs.
  666. *
  667. * Example:
  668. *
  669. * The declaration block
  670. *
  671. * "color: #000; font-weight: bold;"
  672. *
  673. * will be parsed into the following array:
  674. *
  675. * "color" => "#000"
  676. * "font-weight" => "bold"
  677. *
  678. * @param string $cssDeclarationBlock the CSS declaration block without the curly braces, may be empty
  679. *
  680. * @return array the CSS declarations with the property names as array keys and the property values as array values
  681. */
  682. private function parseCssDeclarationBlock($cssDeclarationBlock) {
  683. if (isset($this->caches[self::CACHE_KEY_CSS_DECLARATION_BLOCK][$cssDeclarationBlock])) {
  684. return $this->caches[self::CACHE_KEY_CSS_DECLARATION_BLOCK][$cssDeclarationBlock];
  685. }
  686. $properties = [];
  687. $declarations = explode(';', $cssDeclarationBlock);
  688. foreach ($declarations as $declaration) {
  689. $matches = [];
  690. if (!preg_match('/ *([a-z\\-]+) *: *([^;]+) */', $declaration, $matches)) {
  691. continue;
  692. }
  693. $propertyName = $matches[1];
  694. $propertyValue = $matches[2];
  695. $properties[$propertyName] = $propertyValue;
  696. }
  697. $this->caches[self::CACHE_KEY_CSS_DECLARATION_BLOCK][$cssDeclarationBlock] = $properties;
  698. return $properties;
  699. }
  700. }