You are here

function _syntaxhighlighter_validate_input in Syntax Highlighter 7

Same name and namespace in other branches
  1. 8 syntaxhighlighter.module \_syntaxhighlighter_validate_input()
  2. 6.2 syntaxhighlighter.module \_syntaxhighlighter_validate_input()
  3. 6 syntaxhighlighter.module \_syntaxhighlighter_validate_input()
  4. 7.2 syntaxhighlighter.module \_syntaxhighlighter_validate_input()

Check for error with syntaxhighlighter input

Parameters

string $field: what input field are we checking? We do form_set_error on this if any error is found

string $text: the input text to check for

2 calls to _syntaxhighlighter_validate_input()
syntaxhighlighter_node_validate in ./syntaxhighlighter.module
Validate on the node input text to be sure there is no bad {syntaxhighlighter} tags
_syntaxhighlighter_comment_validate in ./syntaxhighlighter.module
Validate on comment input text to be sure there is no bad {syntaxhighlighter} tags

File

./syntaxhighlighter.module, line 288
Syntax highlight code using the Syntaxhighlighter javascript library. See http://alexgorbatchev.com/wiki/SyntaxHighlighter

Code

function _syntaxhighlighter_validate_input($field, $text) {
  $errors = array();

  // check for balance open/close tags
  preg_match_all('/\\{ *syntaxhighlighter *[^}]+\\}/', $text, $matches_open, PREG_OFFSET_CAPTURE);
  preg_match_all('/\\{\\/ *syntaxhighlighter *\\}/', $text, $matches_close, PREG_OFFSET_CAPTURE);
  if (count($matches_open[0]) != count($matches_close[0])) {
    $errors[] = format_plural(count($matches_open[0]) <= 1 && count($matches_close[0]) <= 1, '{syntaxhighlighter} tag not balanced: open and close tags must match.', '{syntaxhighlighter} tags not balanced: open and close tags must match.');
  }

  // make sure no nesting
  preg_match_all('/\\{ *syntaxhighlighter *[^}]+\\}.*\\{\\/ *syntaxhighlighter *\\}/sU', $text, $matches_pair, PREG_OFFSET_CAPTURE);
  if (count($matches_pair[0]) != 0 && (count($matches_pair[0]) != count($matches_open[0]) || count($matches_pair[0]) != count($matches_close[0]))) {
    $errors[] = format_plural(count($matches_open[0]) <= 1 && count($matches_close[0]) <= 1, '{syntaxhighlighter} tag cannot be nested. If you need to show the {syntaxhighlighter}/{/syntaxhighlighter} strings in syntax highlighted code, show them in escape form as <code>&amp;#123;syntaxhighlighter OPTIONS&amp;#125;</code> and <code>&amp;#123;/syntaxhighlighter&amp;#125;</code>', '{syntaxhighlighter} tags cannot be nested. If you need to show the {syntaxhighlighter}/{/syntaxhighlighter} strings in syntax highlighted code, show them in escape form as <code>&amp;#123;syntaxhighlighter OPTIONS&amp;#125;</code> and <code>&amp;#123;/syntaxhighlighter&amp;#125;</code>');
  }
  if (!empty($errors)) {
    form_set_error($field, implode('<br />', $errors));
  }
}