rashid2538 / php-htmldiff

A library for comparing two HTML files/snippets and highlighting the differences using simple HTML.
GNU General Public License v2.0
69 stars 48 forks source link

Detect new links #7

Closed RadekHavelka closed 10 years ago

RadekHavelka commented 11 years ago

Hello, first of all, thank you for this library ! I tested some of the script, and it seems not to be able to detect the link addition, like

$old_text = ''Some text without link" $new_text = "Some text with link"

it marks the change on WITHOUT to WITH, but not the new anchor tag to LINK word.

Would it be possible to detect new tags added ? maybe just add some special class to them not any INS and DELs, but simply add "class='diff' " or whatever to mark that this exact element is new ? That would be great help.

Thank you

RadekHavelka commented 11 years ago

here is a code modification that works for me :

method InsertTag

almost end of the code, this

$this->content .= implode( "", $this->ExtractConsecutiveWords( $words, 'tag' ) ) . $specialCaseTagInjection;

needs to be replaces like this

                $workTag = $this->ExtractConsecutiveWords( $words, 'tag' );,

                if ($this->IsOpeningTag($workTag[0]) && !$this->IsClosingTag($workTag[0]))
                {
                    //i expect the tag to be first in the consecutiwe words array result, and I am looking only for opening tag to inject the class
                    if (strpos($workTag[0], 'class='))
                    {
                        //tag already contains class parameter, use it and add new class diffmod to it (two variant of apostrophes)
                        $workTag[0] = str_replace('class="', 'class="diffmod ', $workTag[0]);
                        $workTag[0] = str_replace("class='", "class='diffmod ", $workTag[0]);
                    }
                    else
                    {
                        //no class parameter, add before end of the tag >
                        $workTag[0] = str_replace(">", " class='diffmod'>", $workTag[0]);
                    }
                }
                //implode back together
                $this->content .= implode( "", $workTag ) . $specialCaseTagInjection;

this code produces something like < a href = 'something' class='diffmod' ... for the added tags (works for IMG tag as well)

This code may be extended in many ways, it cannot detect if the tag was added or changed, it always uses the diffmod class, but this is just a start for someone who knows better this code, than I :)