Skip to content

Commit

Permalink
define Ui\Diff::insertSoftbreaks() method
Browse files Browse the repository at this point in the history
  • Loading branch information
ssahara committed Jul 17, 2020
1 parent c29600d commit 2d8dbea
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion inc/Ui/Diff.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public function show()
}

//diff view
print html_insert_softbreaks($diffformatter->format($diff));
print $this->insertSoftbreaks($diffformatter->format($diff));

print '</table>';
print '</div>'. DOKU_LF;
Expand Down Expand Up @@ -474,4 +474,45 @@ protected function diffViewlink($difftype, $linktype, $lrev, $rrev = null)
. '</a>'. DOKU_LF;
}


/**
* Insert soft breaks in diff html
*
* @param string $diffhtml
* @return string
*/
protected function insertSoftbreaks($diffhtml)
{
// search the diff html string for both:
// - html tags, so these can be ignored
// - long strings of characters without breaking characters
return preg_replace_callback('/<[^>]*>|[^<> ]{12,}/', [$this,'softbreak_callback'], $diffhtml);
}

/**
* callback which adds softbreaks
*
* @param array $match array with first the complete match
* @return string the replacement
*/
protected function softbreak_callback($match)
{
// if match is an html tag, return it intact
if ($match[0][0] == '<') return $match[0];

// its a long string without a breaking character,
// make certain characters into breaking characters by inserting a
// word break opportunity (<wbr> tag) in front of them.
$regex = <<< REGEX
(?(?= # start a conditional expression with a positive look ahead ...
&\#?\\w{1,6};) # ... for html entities - we don't want to split them (ok to catch some invalid combinations)
&\#?\\w{1,6}; # yes pattern - a quicker match for the html entity, since we know we have one
|
[?/,&\#;:] # no pattern - any other group of 'special' characters to insert a breaking character after
)+ # end conditional expression
REGEX;

return preg_replace('<'.$regex.'>xu', '\0<wbr>', $match[0]);
}

}

0 comments on commit 2d8dbea

Please sign in to comment.