Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

119 lines (91 sloc) 3.482 kb
<?php
/**
*
* Regex management
*
*/
class csscrush_regex {
public static $patt;
// Character classes
public static $class;
public static function init () {
self::$patt = $patt = (object) array();
self::$class = $class = (object) array();
// Character classes
$class->name = '[a-zA-Z0-9_-]+';
$class->notName = '[^a-zA-Z0-9_-]+';
// Patterns
$patt->name = '!^' . $class->name . '$!';
$patt->notName = '!^' . $class->notName . '$!';
$patt->import = '!
@import\s+ # import at-rule
(?:
url\(\s*([^\)]+)\s*\) # url function
| # or
([_s\d]+) # string token
)
\s*([^;]*); # media argument
!xS';
$patt->variables = '!@(?:variables|define)\s*([^\{]*)\{\s*(.*?)\s*\};?!s';
$patt->mixin = '!@mixin\s*([^\{]*)\{\s*(.*?)\s*\};?!s';
$patt->abstract = csscrush_regex::create( '^@abstract\s+(<name>)', 'i' );
$patt->commentAndString = '!
(\'|")(?:\\\\\1|[^\1])*?(?:\1|$) # quoted string (to EOF if unmatched)
|
/\*(?:.*?)(?:\*/|$) # block comment (to EOF if unmatched)
!xsS';
// As an exception we treat some @-rules like standard rule blocks
$patt->rule = '!
(\n(?:[^@{}]+|@(?:font-face|page|abstract)[^{]*)) # The selector
\{([^{}]*)\} # The declaration block
!xS';
// Balanced bracket matching.
$patt->balancedParens = '!\( (?: (?: (?>[^()]+) | (?R) )* ) \)!xS';
$patt->balancedCurlies = '!\{ (?: (?: (?>[^{}]+) | (?R) )* ) \}!xS';
// Tokens
$patt->commentToken = '!___c\d+___!';
$patt->stringToken = '!___s\d+___!';
$patt->ruleToken = '!___r\d+___!';
$patt->parenToken = '!___p\d+___!';
$patt->urlToken = '!___u\d+___!';
$patt->traceToken = '!___t\d+___!';
$patt->argToken = '!___arg(\d+)___!';
// Functions
$patt->varFunction = '!\$\(\s*([a-z0-9_-]+)\s*\)!iS';
$patt->function = '!(^|[^a-z0-9_-])([a-z_-]+)(___p\d+___)!i';
// Specific functions
$patt->argFunction = csscrush_regex::createFunctionMatchPatt( array( 'arg' ) );
$patt->queryFunction = csscrush_regex::createFunctionMatchPatt( array( 'query' ) );
$patt->thisFunction = csscrush_regex::createFunctionMatchPatt( array( 'this' ) );
// Misc.
$patt->vendorPrefix = '!^-([a-z]+)-([a-z-]+)!iS';
$patt->mixinExtend = '!^(?:(@include|mixin)|(@?extends?))[\s\:]+!iS';
$patt->absoluteUrl = '!^https?://!';
$patt->argListSplit = '!\s*[,\s]\s*!S';
$patt->mathBlacklist = '![^\.0-9\*\/\+\-\(\)]!S';
$patt->charset = '!@charset\s+(___s\d+___)\s*;!i';
}
public static function create ( $pattern_template, $flags = '' ) {
// Sugar
$pattern = str_replace(
array( '<name>', '<!name>' ),
array( self::$class->name, self::$class->notName ),
$pattern_template );
return '!' . $pattern . "!$flags";
}
public static function matchAll ( $patt, $subject, $preprocess_patt = false, $offset = 0 ) {
if ( $preprocess_patt ) {
// Assume case-insensitive
$patt = self::create( $patt, 'i' );
}
$count = preg_match_all( $patt, $subject, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER, $offset );
return $count ? $matches : array();
}
public static function createFunctionMatchPatt ( $list, $include_unnamed_function = false ) {
$question = $include_unnamed_function ? '?' : '';
foreach ( $list as &$fn_name ) {
$fn_name = preg_quote( $fn_name );
}
return '!(^|[^a-z0-9_-])(' . implode( '|', $list ) . ')' . $question . '\(!iS';
}
}
Jump to Line
Something went wrong with that request. Please try again.