Skip to content

Commit

Permalink
Merge pull request #30 from caxy/issue/28/detecting-link-changes
Browse files Browse the repository at this point in the history
Detect link changes to resolve #28
  • Loading branch information
jschroed91 committed Feb 22, 2016
2 parents a1ab725 + d92a9b3 commit 5c013fa
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 17 deletions.
115 changes: 98 additions & 17 deletions lib/Caxy/HtmlDiff/HtmlDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class HtmlDiff extends AbstractDiff
'sub' => '[[REPLACE_SUB_SCRIPT]]',
'sup' => '[[REPLACE_SUPER_SCRIPT]]',
'dl' => '[[REPLACE_DEFINITION_LIST]]',
'table' => '[[REPLACE_TABLE]]'
'table' => '[[REPLACE_TABLE]]',
'a' => '[[REPLACE_A]]',
);

/**
Expand Down Expand Up @@ -69,7 +70,6 @@ protected function replaceIsolatedDiffTags()
{
$this->oldIsolatedDiffTags = $this->createIsolatedDiffTagPlaceholders($this->oldWords);
$this->newIsolatedDiffTags = $this->createIsolatedDiffTagPlaceholders($this->newWords);

}

protected function createIsolatedDiffTagPlaceholders(&$words)
Expand Down Expand Up @@ -191,6 +191,28 @@ protected function processDeleteOperation($operation, $cssClass)
$this->insertTag( "del", $cssClass, $text );
}

/**
* @param Operation $operation
* @param int $pos
* @param string $placeholder
* @param bool $stripWrappingTags
*
* @return string
*/
protected function diffIsolatedPlaceholder($operation, $pos, $placeholder, $stripWrappingTags = true)
{
$oldText = implode("", $this->findIsolatedDiffTagsInOld($operation, $pos));
$newText = implode("", $this->newIsolatedDiffTags[$pos]);

if ($this->isListPlaceholder($placeholder)) {
return $this->diffList($oldText, $newText);
} elseif ($this->isLinkPlaceholder($placeholder)) {
return $this->diffLinks($oldText, $newText);
}

return $this->diffElements($oldText, $newText, $stripWrappingTags);
}

protected function diffElements($oldText, $newText, $stripWrappingTags = true)
{
$wrapStart = '';
Expand Down Expand Up @@ -221,6 +243,28 @@ protected function diffList($oldText, $newText)
return $diff->build();
}

/**
* @param string $oldText
* @param string $newText
*
* @return string
*/
protected function diffLinks($oldText, $newText)
{
$oldHref = $this->getAttributeFromTag($oldText, 'href');
$newHref = $this->getAttributeFromTag($newText, 'href');

if ($oldHref != $newHref) {
return sprintf(
'%s%s',
$this->wrapText($oldText, 'del', 'diffmod diff-href'),
$this->wrapText($newText, 'ins', 'diffmod diff-href')
);
}

return $this->diffElements($oldText, $newText);
}

protected function processEqualOperation($operation)
{
$result = array();
Expand All @@ -229,14 +273,7 @@ protected function processEqualOperation($operation)
if ($pos >= $operation->startInNew && $pos < $operation->endInNew) {
if (in_array($s, $this->isolatedDiffTags) && isset($this->newIsolatedDiffTags[$pos])) {

$oldText = implode("", $this->findIsolatedDiffTagsInOld($operation, $pos));
$newText = implode("", $this->newIsolatedDiffTags[$pos]);

if ($this->isListPlaceholder($s)) {
$result[] = $this->diffList($oldText, $newText);
} else {
$result[] = $this->diffElements($oldText, $newText);
}
$result[] = $this->diffIsolatedPlaceholder($operation, $pos, $s);
} else {
$result[] = $s;
}
Expand All @@ -245,17 +282,60 @@ protected function processEqualOperation($operation)
$this->content .= implode( "", $result );
}

/**
* @param string $text
* @param string $attribute
*
* @return null|string
*/
protected function getAttributeFromTag($text, $attribute)
{
$matches = array();
if (preg_match(sprintf('/<a\s+[^>]*%s=([\'"])(.*)\1[^>]*>/i', $attribute), $text, $matches)) {
return $matches[2];
}

return null;
}

protected function isListPlaceholder($text)
{
if (in_array($text, array(
$this->isolatedDiffTags['ol'],
$this->isolatedDiffTags['dl'],
$this->isolatedDiffTags['ul']
))) {
return true;
return $this->isPlaceholderType($text, array('ol', 'dl', 'ul'));
}

/**
* @param string $text
*
* @return bool
*/
public function isLinkPlaceholder($text)
{
return $this->isPlaceholderType($text, 'a');
}

/**
* @param string $text
* @param array|string $types
* @param bool $strict
*
* @return bool
*/
protected function isPlaceholderType($text, $types, $strict = true)
{
if (!is_array($types)) {
$types = array($types);
}

return false;
$criteria = array();
foreach ($types as $type) {
if (isset($this->isolatedDiffTags[$type])) {
$criteria[] = $this->isolatedDiffTags[$type];
} else {
$criteria[] = $type;
}
}

return in_array($text, $criteria, $strict);
}

protected function findIsolatedDiffTagsInOld($operation, $posInNew)
Expand Down Expand Up @@ -334,6 +414,7 @@ protected function wrapText($text, $tagName, $cssClass)
protected function extractConsecutiveWords(&$words, $condition)
{
$indexOfFirstTag = null;
$words = array_values($words);
foreach ($words as $i => $word) {
if ( !$this->checkCondition( $word, $condition ) ) {
$indexOfFirstTag = $i;
Expand Down
15 changes: 15 additions & 0 deletions tests/fixtures/HtmlDiff/issue-28-link-changes.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<oldText>
Testing <a href="http://google.com">Link Changes</a>
And when the link <a href="http://samelink.com">stays the same</a>
</oldText>

<newText>
Testing <a href="http://caxy.com">Link Changes</a>
And when the link <a href="http://samelink.com">stays the same</a>
</newText>

<expected>
Testing <del class="diffmod diff-href"><a href="http://google.com">Link Changes</a></del><ins class="diffmod diff-href"><a href="http://caxy.com">Link Changes</a></ins>
And when the link <a href="http://samelink.com">stays the same</a>
</expected>

0 comments on commit 5c013fa

Please sign in to comment.