You are here

function _syntaxhighlighter_validate_input in Syntax Highlighter 6.2

Same name and namespace in other branches
  1. 8 syntaxhighlighter.module \_syntaxhighlighter_validate_input()
  2. 6 syntaxhighlighter.module \_syntaxhighlighter_validate_input()
  3. 7.2 syntaxhighlighter.module \_syntaxhighlighter_validate_input()
  4. 7 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_comment in ./syntaxhighlighter.module
Validate on comment input text to be sure there is no bad {syntaxhighlighter} tags
syntaxhighlighter_nodeapi in ./syntaxhighlighter.module
Validate on the node input text to be sure there is no bad {syntaxhighlighter} tags

File

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

Code

function _syntaxhighlighter_validate_input($field, $text) {
  $errors = array();
  $tag_name = variable_get('syntaxhighlighter_tagname', 'pre');

  // check for balance open/close tags
  preg_match_all("#<{$tag_name}\\s*[^>]*>#", $text, $matches_open);
  preg_match_all("#</\\s*{$tag_name}\\s*>#", $text, $matches_close);
  if (count($matches_open[0]) != count($matches_close[0])) {
    $errors[] = t('Unbalanced !tag tags.', array(
      '!tag' => "&lt;{$tag_name}&gt;",
    ));
  }

  // make sure no nesting
  preg_match_all("#<{$tag_name}\\s*[^>]*>.*</\\s*{$tag_name}\\s*>#sU", $text, $matches_pair);
  if (count($matches_pair[0]) != count($matches_open[0]) || count($matches_pair[0]) != count($matches_close[0])) {
    $errors[] = t('!tag tags cannot be nested.', array(
      '!tag' => "&lt;{$tag_name}&gt;",
    ));
  }
  if (!empty($errors)) {
    form_set_error($field, implode(' ', $errors));
  }
}