* Checks for imbalanced braces, brackets, and comments.
* Notifications are rendered when the customizer state is saved.
*
* @todo There are cases where valid CSS can be incorrectly marked as invalid when strings or comments include balancing characters. To fix, CSS tokenization needs to be used.
*
* @since 4.7.0
* @access public
*
* @param string $css The input string.
* @return true|WP_Error True if the input was validated, otherwise WP_Error.
*/
publicfunctionvalidate($css){
$validity=newWP_Error();
if(preg_match('#</?\w+#',$css)){
$validity->add('illegal_markup',__('Markup is not allowed in CSS.'));
}
$imbalanced=false;
// Make sure that there is a closing brace for each opening brace.
$validity->add('imbalanced_curly_brackets',__('Your curly brackets <code>{}</code> are imbalanced. Make sure there is a closing <code>}</code> for every opening <code>{</code>.'));
$validity->add('imbalanced_braces',__('Your brackets <code>[]</code> are imbalanced. Make sure there is a closing <code>]</code> for every opening <code>[</code>.'));
$validity->add('imbalanced_parentheses',__('Your parentheses <code>()</code> are imbalanced. Make sure there is a closing <code>)</code> for every opening <code>(</code>.'));
$imbalanced=true;
}
// Ensure single quotes are equal.
if(!$this->validate_equal_characters('\'',$css)){
$validity->add('unequal_single_quotes',__('Your single quotes <code>\'</code> are uneven. Make sure there is a closing <code>\'</code> for every opening <code>\'</code>.'));
$imbalanced=true;
}
// Ensure single quotes are equal.
if(!$this->validate_equal_characters('"',$css)){
$validity->add('unequal_double_quotes',__('Your double quotes <code>"</code> are uneven. Make sure there is a closing <code>"</code> for every opening <code>"</code>.'));
$imbalanced=true;
}
/*
* Make sure any code comments are closed properly.
*
* The first check could miss stray an unpaired comment closing figure, so if
* The number appears to be balanced, then check for equal numbers
* of opening/closing comment figures.
*
* Although it may initially appear redundant, we use the first method
$validity->add('unclosed_comment',sprintf(_n('There is %s unclosed code comment. Close each comment with <code>*/</code>.','There are %s unclosed code comments. Close each comment with <code>*/</code>.',$unclosed_comment_count),$unclosed_comment_count));
$validity->add('imbalanced_comments',__('There is an extra <code>*/</code>, indicating an end to a comment. Be sure that there is an opening <code>/*</code> for every closing <code>*/</code>.'));
$validity->add('possible_false_positive',__('Imbalanced/unclosed character errors can be caused by <code>content: "";</code> declarations. You may need to remove this or add it to a custom CSS file.'));