Skip to content

Instantly share code, notes, and snippets.

@matthieuprat
Last active December 30, 2015 06:49
Show Gist options
  • Save matthieuprat/7792280 to your computer and use it in GitHub Desktop.
Save matthieuprat/7792280 to your computer and use it in GitHub Desktop.
<?php
$pattern = '/(?>\.no-touch\s+)?((?>[^,{;}\s][^,{;}]*?:hover)(?>[^,{;}]+)?)(?=[{,])/i';
$replacement = ".no-touch $1";
$subject = <<<CSS
a
{
color: blue;
}
a:hover
{
color: blue;
}
.no-touch a:hover
{
color: blue;
}
.no-touch a:hover, b
{
color: blue;
}
a, b:hover, c
{
color: blue;
}
a,
b:hover,
c
{
color: blue;
}
a
b:hover
c
{}
a{}b:hover,c d:hover e {}
CSS;
$expected = <<<CSS
a
{
color: blue;
}
.no-touch a:hover
{
color: blue;
}
.no-touch a:hover
{
color: blue;
}
.no-touch a:hover, b
{
color: blue;
}
a, .no-touch b:hover, c
{
color: blue;
}
a,
.no-touch b:hover,
c
{
color: blue;
}
.no-touch a
b:hover
c
{}
a{}.no-touch b:hover,.no-touch c d:hover e {}
CSS;
$result = preg_replace($pattern, $replacement, $subject);
if ($result === $expected) {
echo "<h2>Test passes.</h2>";
} else {
echo "<h2>Test fails.</h2>";
}
echo "<h4>Result:</h4>";
echo "<pre>$result</pre>";
echo "<hr/>";
echo "<h4>Expected:</h4>";
echo "<pre>$expected</pre>";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment