You are here

function htmltidy_fragment in HTML Tidy 7

Same name and namespace in other branches
  1. 5 htmltidy.module \htmltidy_fragment()
  2. 6 htmltidy.module \htmltidy_fragment()

Tidies an incomplete fragment of HTML by passing it through htmltidy full, then stripping back down to the 'body'.

Parameters

$text: HTML string to be tidied

$input: FALSE if text is for output; TRUE if text is for input

$settings: Filter settings for the fragment

$errors: An array to be filled with error info

$warnings: An array to be filled with warning info

Return value

The tidied string

3 calls to htmltidy_fragment()
htmltidy_filter_htmltidy_process in ./htmltidy.filter.inc
Implementation of hook_filter_FILTER_process().
htmltidy_node_prepare in ./htmltidy.node.inc
Implementation of hook_node_prepare().
htmltidy_node_validate in ./htmltidy.node.inc
Implementation of hook_node_validate().

File

./htmltidy.module, line 222
The htmltidy module uses Tidy (http://tidy.sf.net) to properly format HTML for saving and display.

Code

function htmltidy_fragment($text, $input, $settings, &$errors, &$warnings) {
  if ($text) {

    // Pretend it's a full document. This declaration just suppresses one of
    // the warnings.
    if (!strstr($text, '<html') && !strstr($text, '<HTML')) {
      $html = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';

      // Put a new line after the fake headers so our content starts at the
      // begining of a line. this way we can get correct line/column info by just
      // subtracting one from the line number
      $html .= "<html><head><title></title></head><body>\n";
      $html .= $text;
      $html .= '</body></html>';
    }
    else {
      $html = $text;
    }
    $output = htmltidy_string($html, $input, $settings, $errors, $warnings);

    // Remove the html wrapper
    if (preg_match('|<body[^>]*>([\\s\\S]*)</body>|', $output, $matches)) {
      $output = $matches[1];
    }

    // fix the line numbers on both errors and warnings arrays (subtract 1 from each)
    htmltidy_fix_linenums($errors, -1);
    htmltidy_fix_linenums($warnings, -1);
    return $output;
  }
}