You are here

function htmltidy_fragment in HTML Tidy 5

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

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

Parameters

$input: html string to be tidied

$errors: an array to be filled with error info

$warnings: an array to be filled with warning info

Return value

the tidied string

2 calls to htmltidy_fragment()
htmltidy_filter in ./htmltidy.module
Implimentation of hook_filter()
htmltidy_nodeapi in ./htmltidy.module

File

./htmltidy.module, line 251

Code

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

    // Pretend it's a full document. This declaration just suppresses one of
    // the warnings.
    $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 .= $input;
    $html .= '</body></html>';
    $output = htmltidy_string($html, $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;
  }
}