Make Emogrifier regular expressions compatibile with PCRE2

In preparation for PHP 7.3, this commit makes the regular expressions used in Emogrifier compatible with PCRE2. It was necessary to escape hyphens used in a few regular expressions. Since the changes were simple and I'm not familiar enough with Emogrifier, I opted to backport the changes that they made in preparation for PHP 7.3 (https://github.com/MyIntervals/emogrifier/pull/588 and https://github.com/MyIntervals/emogrifier/issues/587) instead of updating the library to the yet to be released version 2.1.0.

This change removes the following warning when running WC unit tests using PHP 7.3:

PHP Warning:  preg_replace(): Compilation failed: invalid range in character class at offset 39 in includes/libraries/class-emogrifier.php on line 1504

For more information about the PCRE2 change in PHP 7.3 see the section "PCRE to PCRE2 migration" in the link https://ayesh.me/Upgrade-PHP-7.3
This commit is contained in:
Rodrigo Primo 2018-11-20 15:54:54 -02:00
parent 2e348cda65
commit d29ead0e63
1 changed files with 5 additions and 5 deletions

View File

@ -162,15 +162,15 @@ class Emogrifier
// type and attribute exact value
'/(\\w)\\[(\\w+)\\=[\'"]?([\\w\\s]+)[\'"]?\\]/' => '\\1[@\\2="\\3"]',
// type and attribute value with ~ (one word within a whitespace-separated list of words)
'/([\\w\\*]+)\\[(\\w+)[\\s]*\\~\\=[\\s]*[\'"]?([\\w-_\\/]+)[\'"]?\\]/'
'/([\\w\\*]+)\\[(\\w+)[\\s]*\\~\\=[\\s]*[\'"]?([\\w\\-_\\/]+)[\'"]?\\]/'
=> '\\1[contains(concat(" ", @\\2, " "), concat(" ", "\\3", " "))]',
// type and attribute value with | (either exact value match or prefix followed by a hyphen)
'/([\\w\\*]+)\\[(\\w+)[\\s]*\\|\\=[\\s]*[\'"]?([\\w-_\\s\\/]+)[\'"]?\\]/'
'/([\\w\\*]+)\\[(\\w+)[\\s]*\\|\\=[\\s]*[\'"]?([\\w\\-_\\s\\/]+)[\'"]?\\]/'
=> '\\1[@\\2="\\3" or starts-with(@\\2, concat("\\3", "-"))]',
// type and attribute value with ^ (prefix match)
'/([\\w\\*]+)\\[(\\w+)[\\s]*\\^\\=[\\s]*[\'"]?([\\w-_\\/]+)[\'"]?\\]/' => '\\1[starts-with(@\\2, "\\3")]',
'/([\\w\\*]+)\\[(\\w+)[\\s]*\\^\\=[\\s]*[\'"]?([\\w\\-_\\/]+)[\'"]?\\]/' => '\\1[starts-with(@\\2, "\\3")]',
// type and attribute value with * (substring match)
'/([\\w\\*]+)\\[(\\w+)[\\s]*\\*\\=[\\s]*[\'"]?([\\w-_\\s\\/:;]+)[\'"]?\\]/' => '\\1[contains(@\\2, "\\3")]',
'/([\\w\\*]+)\\[(\\w+)[\\s]*\\*\\=[\\s]*[\'"]?([\\w\\-_\\s\\/:;]+)[\'"]?\\]/' => '\\1[contains(@\\2, "\\3")]',
// adjacent sibling
'/\\s+\\+\\s+/' => '/following-sibling::*[1]/self::',
// child
@ -185,7 +185,7 @@ class Emogrifier
// The following matcher will break things if it is placed before the adjacent matcher.
// So one of the matchers matches either too much or not enough.
// type and attribute value with $ (suffix match)
'/([\\w\\*]+)\\[(\\w+)[\\s]*\\$\\=[\\s]*[\'"]?([\\w-_\\s\\/]+)[\'"]?\\]/'
'/([\\w\\*]+)\\[(\\w+)[\\s]*\\$\\=[\\s]*[\'"]?([\\w\\-_\\s\\/]+)[\'"]?\\]/'
=> '\\1[substring(@\\2, string-length(@\\2) - string-length("\\3") + 1) = "\\3"]',
];