Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions lib/Caxy/HtmlDiff/HtmlDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class HtmlDiff
private $specialCaseOpeningTags = array();
private $specialCaseClosingTags = array();
private $specialCaseTags = array('strong', 'b', 'i', 'big', 'small', 'u', 'sub', 'sup', 'strike', 's', 'p');
private $specialCaseChars = array('.', ',', '(', ')', '\'');
private $groupDiffs = true;

public function __construct($oldText, $newText, $encoding = 'UTF-8', $specialCaseTags = array(), $groupDiffs = true)
Expand All @@ -26,6 +27,31 @@ public function __construct($oldText, $newText, $encoding = 'UTF-8', $specialCas

$this->setSpecialCaseTags($specialCaseTags);
}

public function setSpecialCaseChars(array $chars)
{
$this->specialCaseChars = $chars;
}

public function getSpecialCaseChars()
{
return $this->specialCaseChars;
}

public function addSpecialCaseChar($char)
{
if (!in_array($char, $this->specialCaseChars)) {
$this->specialCaseChars[] = $char;
}
}

public function removeSpecialCaseChar($char)
{
$key = array_search($char, $this->specialCaseChars);
if ($key !== false) {
unset($this->specialCaseChars[$key]);
}
}

public function setSpecialCaseTags(array $tags = array())
{
Expand Down Expand Up @@ -173,13 +199,18 @@ private function splitInputsToWords()
$this->oldWords = $this->convertHtmlToListOfWords( $this->explode( $this->oldText ) );
$this->newWords = $this->convertHtmlToListOfWords( $this->explode( $this->newText ) );
}

private function isPartOfWord($text)
{
return ctype_alnum(str_replace($this->specialCaseChars, '', $text));
}

private function convertHtmlToListOfWords($characterString)
{
$mode = 'character';
$current_word = '';
$words = array();
foreach ($characterString as $character) {
foreach ($characterString as $i => $character) {
switch ($mode) {
case 'character':
if ( $this->isStartOfTag( $character ) ) {
Expand All @@ -195,7 +226,10 @@ private function convertHtmlToListOfWords($characterString)
$current_word = $character;
$mode = 'whitespace';
} else {
if ( ctype_alnum( $character ) && ( strlen($current_word) == 0 || ctype_alnum( $current_word ) ) ) {
if (
(ctype_alnum($character) && (strlen($current_word) == 0 || $this->isPartOfWord($current_word))) ||
(in_array($character, $this->specialCaseChars) && isset($characterString[$i+1]) && $this->isPartOfWord($characterString[$i+1]))
) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a pretty messy if statement, I'll figure out a way to clean it up.

I do have a question on implementation:
As you can see I added the specialCaseChars array for characters that should be considered part of the 'word' when inside it. I only set it to period and comma here.

I'm wondering if I should change it so that any character would be considered part of the word if inside, instead of whitelisting (consider part of the word if not alpha-numeric but the next character in text is anything other than whitespace).

For example, by including all chars, then: this-is-hyphenated would be considered one word, instead of breaking it up like |this|-|is|-|hyphenated|

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, we don't want to end up with tags as parts of a word. I would think
hyphenated words should be diff'd sanely.
On May 30, 2014 5:20 PM, "Josh Schroeder" notifications@github.com wrote:

In lib/Caxy/HtmlDiff/HtmlDiff.php:

@@ -195,7 +201,10 @@ private function convertHtmlToListOfWords($characterString)
$current_word = $character;
$mode = 'whitespace';
} else {

  •                if ( ctype_alnum( $character ) && ( strlen($current_word) == 0 || ctype_alnum( $current_word ) ) ) {
    
  •                if (
    
  •                    (ctype_alnum($character) && (strlen($current_word) == 0 || $this->isSingleWord($current_word))) ||
    
  •                    (in_array($character, $this->specialCaseChars) && isset($characterString[$i+1]) && $this->isSingleWord($characterString[$i+1]))
    
  •                ) {
    

This is a pretty messy if statement, I'll figure out a way to clean it up.

I do have a question on implementation:
As you can see I added the specialCaseChars array for characters that
should be considered part of the 'word' when inside it. I only set it to
period and comma here.

I'm wondering if I should change it so that any character would be
considered part of the word if inside, instead of whitelisting (consider
part of the word if not alpha-numeric but the next character in text is
anything other than whitespace).

For example, by including all chars, then: this-is-hyphenated would be
considered one word, instead of breaking it up like |this|-|is|-|hyphenated|


Reply to this email directly or view it on GitHub
https://github.com/caxy/php-htmldiff/pull/3/files#r13254106.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps ( and ) as should be considered as part of a word as well?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 these are common with table and figure titles

On Fri, May 30, 2014 at 6:07 PM, Brandon Raxter notifications@github.com
wrote:

In lib/Caxy/HtmlDiff/HtmlDiff.php:

@@ -195,7 +201,10 @@ private function convertHtmlToListOfWords($characterString)
$current_word = $character;
$mode = 'whitespace';
} else {

  •                if ( ctype_alnum( $character ) && ( strlen($current_word) == 0 || ctype_alnum( $current_word ) ) ) {
    
  •                if (
    
  •                    (ctype_alnum($character) && (strlen($current_word) == 0 || $this->isSingleWord($current_word))) ||
    
  •                    (in_array($character, $this->specialCaseChars) && isset($characterString[$i+1]) && $this->isSingleWord($characterString[$i+1]))
    
  •                ) {
    

Perhaps ( and ) as should be considered as part of a word as well?


Reply to this email directly or view it on GitHub
https://github.com/caxy/php-htmldiff/pull/3/files#r13255285.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call Brandon - I will add them. Any others that you guys can think of?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Symbols that might be used? #@$% maybe?

$current_word .= $character;
} else {
$words[] = $current_word;
Expand Down