You are here

private function mimemail_compress::css_to_xpath in Mime Mail 6

Same name and namespace in other branches
  1. 7 modules/mimemail_compress/mimemail_compress.inc \mimemail_compress::css_to_xpath()

Right now we only support CSS 1 selectors, but include CSS2/3 selectors are fully possible.

See also

http://plasmasturm.org/log/444/

1 call to mimemail_compress::css_to_xpath()
mimemail_compress::compress in modules/mimemail_compress/mimemail_compress.inc

File

modules/mimemail_compress/mimemail_compress.inc, line 195
Converts CSS styles into inline style attributes.

Class

mimemail_compress
Compress HTML and CSS into combined message.

Code

private function css_to_xpath($selector) {
  if (drupal_substr($selector, 0, 1) == '/') {

    // Already an XPath expression.
    return $selector;
  }

  // Returns an Xpath selector.
  $search = array(
    '/\\s+>\\s+/',
    // Matches any F element that is a child of an element E.
    '/(\\w+)\\s+\\+\\s+(\\w+)/',
    // Matches any F element that is a child of an element E.
    '/\\s+/',
    // Matches any F element that is a descendant of an E element.
    '/(\\w)\\[(\\w+)\\]/',
    // Matches element with attribute.
    '/(\\w)\\[(\\w+)\\=[\'"]?(\\w+)[\'"]?\\]/',
    // Matches element with EXACT attribute.
    '/(\\w+)?\\#([\\w\\-]+)/e',
    // Matches id attributes.
    '/(\\w+|\\*)?((\\.[\\w\\-]+)+)/e',
  );
  $replace = array(
    '/',
    '\\1/following-sibling::*[1]/self::\\2',
    '//',
    '\\1[@\\2]',
    '\\1[@\\2="\\3"]',
    "(strlen('\\1') ? '\\1' : '*').'[@id=\"\\2\"]'",
    "(strlen('\\1') ? '\\1' : '*').'[contains(concat(\" \",normalize-space(@class),\" \"),concat(\" \",\"'.implode('\",\" \"))][contains(concat(\" \",normalize-space(@class),\" \"),concat(\" \",\"',explode('.',drupal_substr('\\2',1))).'\",\" \"))]'",
  );
  return '//' . preg_replace($search, $replace, trim($selector));
}