示例#1
0
文件: Xml.php 项目: whysasse/kmwiki
 /**
  * Check if a string is a well-formed XML fragment.
  * Wraps fragment in an \<html\> bit and doctype, so it can be a fragment
  * and can use HTML named entities.
  *
  * @param string $text
  * @return bool
  */
 public static function isWellFormedXmlFragment($text)
 {
     $html = Sanitizer::hackDocType() . '<html>' . $text . '</html>';
     return Xml::isWellFormed($html);
 }
示例#2
0
/**
 * Check if a string is a well-formed XML fragment.
 * Wraps fragment in an <html> bit and doctype, so it can be a fragment
 * and can use HTML named entities.
 *
 * @param string $text
 * @return bool
 */
function wfIsWellFormedXmlFragment($text)
{
    $html = Sanitizer::hackDocType() . '<html>' . $text . '</html>';
    return wfIsWellFormedXml($html);
}
示例#3
0
 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);
 }
示例#4
0
 private function wellFormed($text)
 {
     $html = Sanitizer::hackDocType() . '<html>' . $text . '</html>';
     $parser = xml_parser_create("UTF-8");
     # case folding violates XML standard, turn it off
     xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false);
     if (!xml_parse($parser, $html, true)) {
         $err = xml_error_string(xml_get_error_code($parser));
         $position = xml_get_current_byte_index($parser);
         $fragment = $this->extractFragment($html, $position);
         $this->xmlError = "{$err} at byte {$position}:\n{$fragment}";
         xml_parser_free($parser);
         return false;
     }
     xml_parser_free($parser);
     return true;
 }