Skip to content

Commit

Permalink
Changes suggested in review
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeQZ committed Apr 17, 2021
1 parent 16b1744 commit bdd6ac0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 33 deletions.
22 changes: 11 additions & 11 deletions src/CssInliner.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,10 @@ public function inlineCss(string $css = ''): self
if ($this->isStyleBlocksParsingEnabled) {
$combinedCss .= $this->getCssFromAllStyleNodes();
}
$cssDocument = new CssDocument($combinedCss);
$parsedCss = new CssDocument($combinedCss);

$excludedNodes = $this->getNodesToExclude();
$cssRules = $this->collateCssRules($cssDocument);
$cssRules = $this->collateCssRules($parsedCss);
$cssSelectorConverter = $this->getCssSelectorConverter();
foreach ($cssRules['inlinable'] as $cssRule) {
try {
Expand Down Expand Up @@ -210,7 +210,7 @@ public function inlineCss(string $css = ''): self
$this->removeImportantAnnotationFromAllInlineStyles();

$this->determineMatchingUninlinableCssRules($cssRules['uninlinable']);
$this->copyUninlinableCssToStyleNode($cssDocument);
$this->copyUninlinableCssToStyleNode($parsedCss);

return $this;
}
Expand Down Expand Up @@ -552,7 +552,7 @@ private function getCssSelectorConverter(): CssSelectorConverter
/**
* Collates the individual rules from a `CssDocument` object.
*
* @param CssDocument $cssDocument
* @param CssDocument $parsedCss
*
* @return array<string, array<array-key, array{
* media: string,
Expand All @@ -573,9 +573,9 @@ private function getCssSelectorConverter(): CssSelectorConverter
* e.g., `color: red; height: 4px;`);
* - "line" (the line number, e.g. 42).
*/
private function collateCssRules(CssDocument $cssDocument): array
private function collateCssRules(CssDocument $parsedCss): array
{
$matches = $cssDocument->getStyleRulesData(\array_keys($this->allowedMediaTypes));
$matches = $parsedCss->getStyleRulesData(\array_keys($this->allowedMediaTypes));

$cssRules = [
'inlinable' => [],
Expand Down Expand Up @@ -1072,15 +1072,15 @@ private function removeUnsupportedOfTypePseudoClasses(string $selectorPart): str
* Applies `$this->matchingUninlinableCssRules` to `$this->domDocument` by placing them as CSS in a `<style>`
* element.
*
* @param CssDocument $cssDocument
* @param CssDocument $parsedCss
* This may contain any `@import` or `@font-face` rules that should precede the CSS placed in the `<style>`
* element. If there are no unlinlinable CSS rules to copy there, a `<style>` element will be created
* containing only the applicable at-rules from `$cssDocument`. If there are none, and there are also no
* element. If there are no uninlinable CSS rules to copy there, a `<style>` element will be created
* containing only the applicable at-rules from `$parsedCss`. If there are none, and there are also no
* unlinlinable CSS rules, an empty `<style>` element will not be created.
*/
private function copyUninlinableCssToStyleNode(CssDocument $cssDocument): void
private function copyUninlinableCssToStyleNode(CssDocument $parsedCss): void
{
$css = $cssDocument->renderNonConditionalAtRules();
$css = $parsedCss->renderNonConditionalAtRules();

// avoid including unneeded class dependency if there are no rules
if ($this->getMatchingUninlinableCssRules() !== []) {
Expand Down
40 changes: 18 additions & 22 deletions src/Utilities/CssDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
*/
class CssDocument
{
/**
* @var string
*/
private const AT_CHARSET_OR_IMPORT_RULE_PATTERN = '/^\\s*+(@((?i)import(?-i)|charset)\\s[^;]++;\\s*+)/';

/**
* This regular expression pattern will match any nested at-rule apart from
* {@link https://developer.mozilla.org/en-US/docs/Web/CSS/At-rule#conditional_group_rules conditional group rules},
Expand All @@ -25,9 +30,14 @@ class CssDocument
*
* @var string
*/
private const NON_CONDITIONAL_AT_RULE_MATCHER
private const NON_CONDITIONAL_AT_RULE_PATTERN
= '/(@(?!media\\b)[\\w\\-]++)[^\\{]*+(\\{[^\\{\\}]*+(?:(?2)[^\\{\\}]*+)*+\\})\\s*+/i';

/**
* @var string
*/
private const MEDIA_RULE_BODY_MATCHER = '[^{]*+{(?:[^{}]*+{.*})?\\s*+}\\s*+';

/**
* Includes regular style rules, and style rules within conditional group rules such as `@media`.
*
Expand Down Expand Up @@ -134,13 +144,7 @@ private function extractImportAndCharsetRules(string $css): array
$possiblyModifiedCss = $css;
$importRules = '';

while (
\preg_match(
'/^\\s*+(@((?i)import(?-i)|charset)\\s[^;]++;\\s*+)/',
$possiblyModifiedCss,
$matches
)
) {
while (\preg_match(self::AT_CHARSET_OR_IMPORT_RULE_PATTERN, $possiblyModifiedCss, $matches)) {
[$fullMatch, $atRuleAndFollowingWhitespace, $atRuleName] = $matches;

if (\strtolower($atRuleName) === 'import') {
Expand Down Expand Up @@ -172,25 +176,19 @@ private function extractImportAndCharsetRules(string $css): array
private function extractNonConditionalAtRules(string $css): array
{
$possiblyModifiedCss = $css;
$atRules = '';
$atRules = [];

while (
\preg_match(
self::NON_CONDITIONAL_AT_RULE_MATCHER,
$possiblyModifiedCss,
$matches
)
) {
while (\preg_match(self::NON_CONDITIONAL_AT_RULE_PATTERN, $possiblyModifiedCss, $matches)) {
[$fullMatch, $atRuleName] = $matches;

if ($this->isValidAtRule($atRuleName, $fullMatch)) {
$atRules .= $fullMatch;
$atRules[] = $fullMatch;
}

$possiblyModifiedCss = \str_replace($fullMatch, '', $possiblyModifiedCss);
}

return [$possiblyModifiedCss, $atRules];
return [$possiblyModifiedCss, \implode('', $atRules)];
}

/**
Expand Down Expand Up @@ -247,10 +245,8 @@ private function splitCssAndMediaQuery(array $allowedMediaTypes): array
$mediaTypesExpression = '|' . \implode('|', $allowedMediaTypes);
}

$mediaRuleBodyMatcher = '[^{]*+{(?:[^{}]*+{.*})?\\s*+}\\s*+';

$cssSplitForAllowedMediaTypes = \preg_split(
'#(@media\\s++(?:only\\s++)?+(?:(?=[{(])' . $mediaTypesExpression . ')' . $mediaRuleBodyMatcher
'#(@media\\s++(?:only\\s++)?+(?:(?=[{(])' . $mediaTypesExpression . ')' . self::MEDIA_RULE_BODY_MATCHER
. ')#misU',
$this->styleRules,
-1,
Expand All @@ -260,7 +256,7 @@ private function splitCssAndMediaQuery(array $allowedMediaTypes): array
// filter the CSS outside/between allowed @media rules
$cssCleaningMatchers = [
'import/charset directives' => '/\\s*+@(?:import|charset)\\s[^;]++;/i',
'remaining media enclosures' => '/\\s*+@media\\s' . $mediaRuleBodyMatcher . '/isU',
'remaining media enclosures' => '/\\s*+@media\\s' . self::MEDIA_RULE_BODY_MATCHER . '/isU',
];

$splitCss = [];
Expand Down

0 comments on commit bdd6ac0

Please sign in to comment.