caxy / php-htmldiff

A library for comparing two HTML files/snippets and highlighting the differences using simple HTML. Includes support for comparing complex lists and tables
http://php-htmldiff.caxy.com
GNU General Public License v2.0
202 stars 51 forks source link

How to ignore changes in an attribute? #99

Closed Ambient-Impact closed 3 years ago

Ambient-Impact commented 3 years ago

Specifically, I have links whose href attributes change, but for the purposes of what we're doing, it's irrelevant to the user that will be seeing the diff. If you're wondering exactly why one would need this, we're basically faking a future Wikipedia clone for a narrative project. Here's an example of the diff in the current Drupal 7 site that uses an older diffing, and we're working on porting the site to Drupal 8 with this library for the diffing.

SavageTiger commented 3 years ago

Hi @Ambient-Impact,

The library explictly parses the href tag to detect if it changed. However this can be disabled.

Its a bit clunky but if you use a custom config object like this, it should work;

        $config = new HtmlDiffConfig();
        $config->setIsolatedDiffTags([
            'ol' => '[[REPLACE_ORDERED_LIST]]',
            'ul' => '[[REPLACE_UNORDERED_LIST]]',
            'sub' => '[[REPLACE_SUB_SCRIPT]]',
            'sup' => '[[REPLACE_SUPER_SCRIPT]]',
            'dl' => '[[REPLACE_DEFINITION_LIST]]',
            'table' => '[[REPLACE_TABLE]]',
            'strong' => '[[REPLACE_STRONG]]',
            'b' => '[[REPLACE_STRONG]]',
            'em' => '[[REPLACE_EM]]',
            'i' => '[[REPLACE_EM]]',
            'img' => '[[REPLACE_IMG]]',
            'pre' => '[[REPLACE_PRE]]',
        ]);

        $diff = HtmlDiff::create(
            $originalText,
            $changedText,
            $config
        );

Note how I removed the 'a' key from the isolatedDiffTags config option

Ambient-Impact commented 3 years ago

That seems to work, thanks! My slightly less clunky version was to retrieve the isolated diff tags and just remove the 'a' key, and saving it to the config again:


/** @var \Caxy\HtmlDiff\HtmlDiffConfig */
$htmlDiffConfig = $this->htmlDiff->getConfig();

/** @var array */
$isolatedDiffElements = $htmlDiffConfig->getIsolatedDiffTags();

if (isset($isolatedDiffElements['a'])) {
  unset($isolatedDiffElements['a']);
}

$htmlDiffConfig->setIsolatedDiffTags($isolatedDiffElements);
SavageTiger commented 3 years ago

That seems to work, thanks! My slightly less clunky version was to retrieve the isolated diff tags and just remove the 'a' key, and saving it to the config again:

/** @var \Caxy\HtmlDiff\HtmlDiffConfig */
$htmlDiffConfig = $this->htmlDiff->getConfig();

/** @var array */
$isolatedDiffElements = $htmlDiffConfig->getIsolatedDiffTags();

if (isset($isolatedDiffElements['a'])) {
  unset($isolatedDiffElements['a']);
}

$htmlDiffConfig->setIsolatedDiffTags($isolatedDiffElements);

This is a better solution indeed 👍