How to ignore changes in an attribute?
Ambient-Impact opened this issue ยท 3 comments
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.
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
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);
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 ๐