Pelago\Emogrifier
This class provides functions for converting CSS styles into inline style attributes in your HTML code.
For more information, please see the README.md file.
- Author: Cameron Brooks
Synopsis
class Emogrifier
{
- // constants
- const ENCODING = 'UTF-8';
- const CACHE_KEY_CSS = 0;
- const CACHE_KEY_SELECTOR = 1;
- const CACHE_KEY_XPATH = 2;
- const INDEX = 0;
- const MULTIPLIER = 1;
- const ID_ATTRIBUTE_MATCHER = '/(\\w+)?\\#([\\w\\-]+)/';
- const CLASS_ATTRIBUTE_MATCHER = '/(\\w+|[\\*\\]])?((\\.[\\w\\-]+)+)/';
- // members
- private string $html = '';
- private string $css = '';
- private array<string> $unprocessableHtmlTags = ;
- private array<array> $caches = ;
- private DOMNode> $visitedNodes = ;
- private array<array><string> $styleAttributesForNodes = ;
- public boolean $preserveEncoding = false;
- // methods
- public void __construct()
- public void __destruct()
- public void setHtml()
- public void setCss()
- private void clearAllCaches()
- private void clearCache()
- private void purgeVisitedNodes()
- public void addUnprocessableHtmlTag()
- public void removeUnprocessableHtmlTag()
- public string emogrify()
- public void copyCssWithMediaToStyleNode()
- private string getCssFromAllStyleNodes()
- private void addStyleElementToDocument()
- private DOMNode getOrCreateHeadElement()
- private array splitCssAndMediaQuery()
- private DOMDocument createXmlDocument()
- private string getUnifiedHtml()
- private integer sortBySelectorPrecedence()
- private integer getCssSelectorPrecedence()
- private string translateCssToXpath()
- private string matchIdAttributes()
- private string matchClassAttributes()
- private string translateNthChild()
- private string translateNthOfType()
- private array parseNth()
- private array parseCssDeclarationBlock()
Constants
Name | Value |
---|---|
ENCODING | 'UTF-8' |
CACHE_KEY_CSS | 0 |
CACHE_KEY_SELECTOR | 1 |
CACHE_KEY_XPATH | 2 |
INDEX | 0 |
MULTIPLIER | 1 |
ID_ATTRIBUTE_MATCHER | '/(\\w+)?\\#([\\w\\-]+)/' |
CLASS_ATTRIBUTE_MATCHER | '/(\\w+|[\\*\\]])?((\\.[\\w\\-]+)+)/' |
Members
private
- $caches — Pelago\array<array>
- $css — string
- $html — string
- $styleAttributesForNodes
—
Pelago\array<array><string>
the styles to apply to the nodes with the XPath paths as array keys for the outer array and the attribute names/values as key/value pairs for the inner array - $unprocessableHtmlTags — Pelago\array<string>
- $visitedNodes
—
Pelago\array<\DOMNode>
the visited nodes with the XPath paths as array keys
public
- $preserveEncoding
—
boolean
This attribute applies to the case where you want to preserve your original text encoding.
Methods
private
- addStyleElementToDocument() — Adds a style element with $css to $document.
- clearAllCaches() — Clears all caches.
- clearCache() — Clears a single cache by key.
- createXmlDocument() — Creates a DOMDocument instance with the current HTML.
- getCssFromAllStyleNodes() — Returns CSS content.
- getCssSelectorPrecedence()
- getOrCreateHeadElement() — Returns the existing or creates a new head element in $document.
- getUnifiedHtml() — Returns the HTML with the non-ASCII characters converts into HTML entities and the unprocessable HTML tags removed.
- matchClassAttributes()
- matchIdAttributes()
- parseCssDeclarationBlock() — Parses a CSS declaration block into property name/value pairs.
- parseNth()
- purgeVisitedNodes() — Purges the visited nodes.
- sortBySelectorPrecedence()
- splitCssAndMediaQuery() — Splits input CSS code to an array where:
- translateCssToXpath() — Right now, we support all CSS 1 selectors and most CSS2/3 selectors.
- translateNthChild()
- translateNthOfType()
public
- __construct() — The constructor.
- __destruct() — The destructor.
- addUnprocessableHtmlTag() — Marks a tag for removal.
- copyCssWithMediaToStyleNode() — Copies the media part from CSS array parts to $xmlDocument.
- emogrify() — Applies the CSS you submit to the HTML you submit.
- removeUnprocessableHtmlTag() — Drops a tag from the removal list.
- setCss() — Sets the CSS to merge with the HTML.
- setHtml() — Sets the HTML to emogrify.