function htmlDiff($from, $to) { // Create an XML parser $xml_parser = xml_parser_create(''); $domfrom = new DomTreeBuilder(); // Set the functions to handle opening and closing tags xml_set_element_handler($xml_parser, array($domfrom, "startElement"), array($domfrom, "endElement")); // Set the function to handle blocks of character data xml_set_character_data_handler($xml_parser, array($domfrom, "characters")); HTMLDiffer::diffDebug("Parsing " . strlen($from) . " characters worth of HTML\n"); if (!xml_parse($xml_parser, '<?xml version="1.0" encoding="UTF-8"?>' . Sanitizer::hackDocType() . '<body>', false) || !xml_parse($xml_parser, $from, false) || !xml_parse($xml_parser, '</body>', true)) { $error = xml_error_string(xml_get_error_code($xml_parser)); $line = xml_get_current_line_number($xml_parser); $col = xml_get_current_column_number($xml_parser); HTMLDiffer::diffDebug("XML error: {$error} at line {$line} and column {$col}\n"); } xml_parser_free($xml_parser); unset($from); $xml_parser = xml_parser_create(''); $domto = new DomTreeBuilder(); // Set the functions to handle opening and closing tags xml_set_element_handler($xml_parser, array($domto, "startElement"), array($domto, "endElement")); // Set the function to handle blocks of character data xml_set_character_data_handler($xml_parser, array($domto, "characters")); HTMLDiffer::diffDebug("Parsing " . strlen($to) . " characters worth of HTML\n"); if (!xml_parse($xml_parser, '<?xml version="1.0" encoding="UTF-8"?>' . Sanitizer::hackDocType() . '<body>', false) || !xml_parse($xml_parser, $to, false) || !xml_parse($xml_parser, '</body>', true)) { $error = xml_error_string(xml_get_error_code($xml_parser)); $line = xml_get_current_line_number($xml_parser); $col = xml_get_current_column_number($xml_parser); HTMLDiffer::diffDebug("XML error: {$error} at line {$line} and column {$col}\n"); } xml_parser_free($xml_parser); unset($to); $diffengine = new WikiDiff3(); $differences = $this->preProcess($diffengine->diff_range($domfrom->getDiffLines(), $domto->getDiffLines())); unset($xml_parser, $diffengine); $textNodeDiffer = new TextNodeDiffer($domto, $domfrom); $currentIndexLeft = 0; $currentIndexRight = 0; foreach ($differences as &$d) { if ($d->leftstart > $currentIndexLeft) { $textNodeDiffer->handlePossibleChangedPart($currentIndexLeft, $d->leftstart, $currentIndexRight, $d->rightstart); } if ($d->leftlength > 0) { $textNodeDiffer->markAsDeleted($d->leftstart, $d->leftend, $d->rightstart); } $textNodeDiffer->markAsNew($d->rightstart, $d->rightend); $currentIndexLeft = $d->leftend; $currentIndexRight = $d->rightend; } $oldLength = $textNodeDiffer->lengthOld(); if ($currentIndexLeft < $oldLength) { $textNodeDiffer->handlePossibleChangedPart($currentIndexLeft, $oldLength, $currentIndexRight, $textNodeDiffer->lengthNew()); } $textNodeDiffer->expandWhiteSpace(); $output = new HTMLOutput('htmldiff'); return $output->parse($textNodeDiffer->bodyNode); }