|
1 | | -[](https://scrutinizer-ci.com/g/caxy/php-htmldiff/?branch=master) |
2 | | -[](https://scrutinizer-ci.com/g/caxy/php-htmldiff/build-status/master) |
3 | | -[](https://scrutinizer-ci.com/g/caxy/php-htmldiff/?branch=master) |
4 | | -[](https://packagist.org/packages/caxy/php-htmldiff) |
5 | | -[](http://isitmaintained.com/project/caxy/php-htmldiff "Average time to resolve an issue") |
6 | | -[](http://isitmaintained.com/project/caxy/php-htmldiff "Percentage of issues still open") |
7 | | - |
8 | 1 | php-htmldiff |
9 | | -======= |
10 | | -*A library for comparing two HTML files/snippets and highlighting the differences using simple HTML.* |
| 2 | +============ |
| 3 | + |
| 4 | +[][badge_score] |
| 5 | +[][badge_status] |
| 6 | +[][badge_coverage] |
| 7 | +[][badge_packagist] |
| 8 | +[][badge_resolve] |
| 9 | +[][badge_issues] |
| 10 | + |
| 11 | +php-htmldiff is a library for comparing two HTML files/snippets and highlighting the differences using simple HTML. |
| 12 | + |
| 13 | +This HTML Diff implementation was forked from [rashid2538/php-htmldiff][upstream] and has been modified with new features, |
| 14 | +bug fixes, and enhancements to the original code. |
| 15 | + |
| 16 | +For more information on these modifications, read the [differences from rashid2538/php-htmldiff][differences] or view the [CHANGELOG][changelog]. |
| 17 | + |
| 18 | +## Installation |
| 19 | + |
| 20 | +The recommended way to install php-htmldiff is through [Composer][composer]. |
| 21 | +Require the [caxy/php-htmldiff][badge_packagist] package by running following command: |
| 22 | + |
| 23 | +```sh |
| 24 | +composer require caxy/php-htmldiff |
| 25 | +``` |
| 26 | + |
| 27 | +This will resolve the latest stable version. |
| 28 | + |
| 29 | +Otherwise, install the library and setup the autoloader yourself. |
| 30 | + |
| 31 | +### Working with Symfony |
| 32 | + |
| 33 | +If you are using Symfony, you can use the [caxy/HtmlDiffBundle][htmldiffbundle] to make life easy! |
11 | 34 |
|
12 | | -This HTML Diff implementation is a PHP port of the ruby implementation found at https://github.com/myobie/htmldiff. |
| 35 | +## Usage |
13 | 36 |
|
14 | | -This is also available in C# at https://github.com/Rohland/htmldiff.net. |
| 37 | +```php |
| 38 | +use Caxy\HtmlDiff\HtmlDiff; |
15 | 39 |
|
16 | | -License |
17 | | -------- |
18 | | -php-htmldiff is available under [GNU General Public License, version 2] (http://www.gnu.org/licenses/gpl-2.0.html). |
| 40 | +$htmlDiff = new HtmlDiff($oldHtml, $newHtml); |
| 41 | +$content = $htmlDiff->build(); |
| 42 | +``` |
19 | 43 |
|
20 | | -Differences from rashid2538/php-htmldiff |
21 | | -======================================== |
22 | | -## Code Styling and Clean-up |
23 | | -* Added namespaces, split up classes to their own files, some code styling changes |
| 44 | +## Configuration |
24 | 45 |
|
25 | | -## Enhancements |
26 | | -* Allow the specialCaseOpeningTags and specialCaseClosingTags properties to be modified by passing an array into the constructor or using set/add/remove functions |
27 | | -* Updated the demo to accept input and diff via AJAX |
28 | | -* Added static properties for the default config variables |
| 46 | +The configuration for HtmlDiff is contained in the `Caxy\HtmlDiff\HtmlDiffConfig` class. |
29 | 47 |
|
30 | | -## Bug Fixes |
31 | | -* Fixed an index out of range bug (may have been fixed on the original repo since): https://github.com/caxy/php-htmldiff/commit/c9ba1fab6777cd47427477f8d747293bb01ef1e8 |
32 | | -* Check for empty oldText or newText before processing del or ins in processReplaceOperation function |
| 48 | +There are two ways to set the configuration: |
| 49 | +1. [Configure an Existing HtmlDiff Object](#configure-an-existing-htmldiff-object) |
| 50 | +2. [Create and Use an HtmlDiffConfig Object](#create-and-use-an-htmldiffconfig-object) |
33 | 51 |
|
34 | | -## New Features |
35 | | -#### Isolated Diffing of certain HTML elements |
36 | | -This is the one of the largest changes from the original repository. |
| 52 | +#### Configure an Existing HtmlDiff Object |
37 | 53 |
|
38 | | -See the release notes for tag 0.0.6 for more information: https://github.com/caxy/php-htmldiff/releases/tag/0.0.6 |
| 54 | +When a new `HtmlDiff` object is created, it creates a `HtmlDiffConfig` object with the default configuration. |
| 55 | +You can change the configuration using setters on the object: |
39 | 56 |
|
40 | | -#### List Diffing |
41 | | -This is the latest new feature (as of last week-ish), and may need some tweaks still. It is similar to the Isolated Diffing feature, but specifically for HTML lists. |
| 57 | +```php |
| 58 | +use Caxy\HtmlDiff\HtmlDiff; |
42 | 59 |
|
43 | | -More information is to come on this, and there will definitely be some tweaks and configuration options added for this feature. Currently there is no easy way to enable/disable the feature, so if you're having issues with it I suggest using the 0.0.6 or earlier release. |
| 60 | +// ... |
44 | 61 |
|
45 | | -#### New option to group together diffed words by not matching on whitespace-only. Option is enabled by default. |
46 | | -This was a specific requirement for an application we use this library for. The original library would replace single words at a time, but enabling this feature will group replacements instead. See example below. |
| 62 | +$htmlDiff = new HtmlDiff($oldHtml, $newHtml); |
47 | 63 |
|
48 | | -Old Text |
49 | | -> testing some text here and there |
| 64 | +// Set some of the configuration options. |
| 65 | +$htmlDiff->getConfig() |
| 66 | + ->setMatchThreshold(80) |
| 67 | + ->setInsertSpaceInReplace(true) |
| 68 | +; |
50 | 69 |
|
51 | | -New Text |
52 | | -> testing other words here and there |
| 70 | +// Calculate the differences using the configuration and get the html diff. |
| 71 | +$content = $htmlDiff->build(); |
53 | 72 |
|
54 | | -With $groupDiffs = false (original functionality) |
55 | | -> testing <del>some</del><ins>other</ins> <del>text</del><ins>words</ins> here and there |
| 73 | +// ... |
56 | 74 |
|
57 | | -With $groupDiffs = true (new feature) |
58 | | -> testing <del>some text</del><ins>other words</ins> here and there |
| 75 | +``` |
59 | 76 |
|
60 | | -#### Change diffing to strike through entire words/numbers if they contain periods or commas within the word |
61 | | -This change introduced a new property `$specialCaseChars`, which defaults to the following characters: `.` `,` `(` `)` `'` |
| 77 | +#### Create and Use an HtmlDiffConfig Object |
62 | 78 |
|
63 | | -This feature can be "disabled" by simply setting the $specialCaseChars to an empty array i.e. `$diff->setSpecialCaseChars(array())` |
| 79 | +You can also set the configuration by creating an instance of |
| 80 | +`Caxy\HtmlDiff\HtmlDiffConfig` and using it when creating a new `HtmlDiff` |
| 81 | +object using `HtmlDiff::create`. |
64 | 82 |
|
65 | | -In the original library, special characters are treated as their own "words" even if they are in the middle of a word. This causes weird things to happen when diffing numbers that have a comma or a period in the middle of the number. |
| 83 | +This is useful when creating more than one instance of `HtmlDiff`: |
66 | 84 |
|
67 | | -For example, diffing `10,000.50` against `11,100.75` gives you: |
| 85 | +```php |
| 86 | +use Caxy\HtmlDiff\HtmlDiff; |
| 87 | +use Caxy\HtmlDiff\HtmlDiffConfig; |
68 | 88 |
|
69 | | -Original Functionality: |
70 | | -> <del class="diffmod">10</del><ins class="diffmod">11</ins>,<del class="diffmod">000</del><ins class="diffmod">100</ins>.<del class="diffmod">50</del><ins class="diffmod">75</ins> |
| 89 | +// ... |
71 | 90 |
|
72 | | -This is very difficult to read, so the new feature allows you to add `.` and `,` to the $specialCaseChars array in order to get output that looks like: |
73 | | -> <del class="diffmod">10,000.50</del><ins class="diffmod">11,100.75</ins> |
| 91 | +$config = new HtmlDiffConfig(); |
| 92 | +$config |
| 93 | + ->setMatchThreshold(95) |
| 94 | + ->setInsertSpaceInReplace(true) |
| 95 | +; |
74 | 96 |
|
75 | | -Note: It will *not* treat the specialCaseChars as part of the word if it is at the beginning or end of the word, so normal periods or commas at the end of words will still be diffed like the original. |
| 97 | +// Create an HtmlDiff object with the custom configuration. |
| 98 | +$firstHtmlDiff = HtmlDiff::create($oldHtml, $newHtml, $config); |
| 99 | +$firstContent = $firstHtmlDiff->build(); |
76 | 100 |
|
77 | | -#### Added option to insert a space between `<del>` and `<ins>` tags. Disabled by default. |
78 | | -This was a requirement for one our applications that uses this library. |
| 101 | +$secondHtmlDiff = HtmlDiff::create($oldHtml2, $newHtml2, $config); |
| 102 | +$secondHtmlDiff->getConfig()->setMatchThreshold(50); |
79 | 103 |
|
80 | | -New property `$insertSpaceInReplace` was added, and setting it to true will simply add a space between the `<del>` and `<ins>` tags in replace operations, which was requested for easier reading. |
| 104 | +$secondContent = $secondHtmlDiff->build(); |
81 | 105 |
|
82 | | -Enable it by calling `$diff->setInsertSpaceInReplace(true);` |
| 106 | +// ... |
| 107 | +``` |
83 | 108 |
|
84 | | -Original Functionality |
85 | | -> <del>Old</del><ins>New</ins> |
| 109 | +#### Full Configuration with Defaults: |
86 | 110 |
|
87 | | -New Functionality |
88 | | -> <del>Old</del> <ins>New</ins> |
| 111 | +```php |
89 | 112 |
|
90 | | -## Upcoming Features (someday) |
91 | | -* Table Diffing (similar to the list diffing updates) - this feature was started a while back, but put on hold. |
| 113 | +$config = new HtmlDiffConfig(); |
| 114 | +$config |
| 115 | + // Percentage required for list items to be considered a match. |
| 116 | + ->setMatchThreshold(80) |
| 117 | + |
| 118 | + // Set the encoding of the text to be diffed. |
| 119 | + ->setEncoding('UTF-8') |
| 120 | + |
| 121 | + // If true, a space will be added between the <del> and <ins> tags of text that was replaced. |
| 122 | + ->setInsertSpaceInReplace(false) |
| 123 | + |
| 124 | + // Option to disable the new Table Diffing feature and treat tables as regular text. |
| 125 | + ->setUseTableDiffing(true) |
| 126 | + |
| 127 | + // Pass an instance of \Doctrine\Common\Cache\Cache to cache the calculated diffs. |
| 128 | + ->setCacheProvider(null) |
| 129 | + |
| 130 | + // Group consecutive deletions and insertions instead of showing a deletion and insertion for each word individually. |
| 131 | + ->setGroupDiffs(true) |
| 132 | + |
| 133 | + // List of characters to consider part of a single word when in the middle of text. |
| 134 | + ->setSpecialCaseChars(array('.', ',', '(', ')', '\'')) |
| 135 | + |
| 136 | + // List of tags to treat as special case tags. |
| 137 | + ->setSpecialCaseTags(array('strong', 'b', 'i', 'big', 'small', 'u', 'sub', 'sup', 'strike', 's', 'p')) |
| 138 | + |
| 139 | + // List of tags (and their replacement strings) to be diffed in isolation. |
| 140 | + ->setIsolatedDiffTags(array( |
| 141 | + 'ol' => '[[REPLACE_ORDERED_LIST]]', |
| 142 | + 'ul' => '[[REPLACE_UNORDERED_LIST]]', |
| 143 | + 'sub' => '[[REPLACE_SUB_SCRIPT]]', |
| 144 | + 'sup' => '[[REPLACE_SUPER_SCRIPT]]', |
| 145 | + 'dl' => '[[REPLACE_DEFINITION_LIST]]', |
| 146 | + 'table' => '[[REPLACE_TABLE]]', |
| 147 | + 'strong' => '[[REPLACE_STRONG]]', |
| 148 | + 'b' => '[[REPLACE_B]]', |
| 149 | + 'em' => '[[REPLACE_EM]]', |
| 150 | + 'i' => '[[REPLACE_I]]', |
| 151 | + 'a' => '[[REPLACE_A]]', |
| 152 | + )) |
| 153 | +; |
| 154 | + |
| 155 | +``` |
| 156 | + |
| 157 | +## Contributing |
| 158 | + |
| 159 | +See [CONTRIBUTING][contributing] file. |
| 160 | + |
| 161 | +## Contributor Code of Conduct |
| 162 | + |
| 163 | +Please note that this project is released with a [Contributor Code of |
| 164 | +Conduct][contributor_covenant]. By participating in this project |
| 165 | +you agree to abide by its terms. See [CODE_OF_CONDUCT][code_of_conduct] file. |
| 166 | + |
| 167 | +## Credits |
| 168 | + |
| 169 | +* [rashid2538][] for the port to PHP and the base for our project: [rashid2538/php-htmldiff][upstream] |
| 170 | +* [willdurand][] for an excellent post on [open sourcing libraries][]. |
| 171 | +Much of this documentation is based off of the examples in the post. |
| 172 | + |
| 173 | +Did we miss anyone? If we did, let us know or put in a pull request! |
| 174 | + |
| 175 | +## License |
| 176 | + |
| 177 | +php-htmldiff is available under [GNU General Public License, version 2][gnu]. See the [LICENSE][license] file for details. |
| 178 | + |
| 179 | +[badge_score]: https://scrutinizer-ci.com/g/caxy/php-htmldiff/?branch=master |
| 180 | +[badge_status]: https://scrutinizer-ci.com/g/caxy/php-htmldiff/build-status/master |
| 181 | +[badge_coverage]: https://scrutinizer-ci.com/g/caxy/php-htmldiff/?branch=master |
| 182 | +[badge_packagist]: https://packagist.org/packages/caxy/php-htmldiff |
| 183 | +[badge_resolve]: http://isitmaintained.com/project/caxy/php-htmldiff "Average time to resolve an issue" |
| 184 | +[badge_issues]: http://isitmaintained.com/project/caxy/php-htmldiff "Percentage of issues still open" |
| 185 | +[upstream]: https://github.com/rashid2538/php-htmldiff |
| 186 | +[htmldiffbundle]: https://github.com/caxy/HtmlDiffBundle |
| 187 | +[differences]: https://github.com/caxy/php-htmldiff/blob/master/doc/differences.rst |
| 188 | +[changelog]: https://github.com/caxy/php-htmldiff/blob/master/CHANGELOG.md |
| 189 | +[contributing]: https://github.com/caxy/php-htmldiff/blob/master/CONTRIBUTING.md |
| 190 | +[gnu]: http://www.gnu.org/licenses/gpl-2.0.html |
| 191 | +[license]: https://github.com/caxy/php-htmldiff/blob/master/LICENSE |
| 192 | +[code_of_conduct]: https://github.com/caxy/php-htmldiff/blob/master/CODE_OF_CONDUCT.md |
| 193 | +[composer]: http://getcomposer.org/ |
| 194 | +[contributor_covenant]: http://contributor-covenant.org/ |
| 195 | +[rashid2538]: https://github.com/rashid2538 |
| 196 | +[willdurand]: https://github.com/willdurand |
| 197 | +[open sourcing libraries]: http://williamdurand.fr/2013/07/04/on-open-sourcing-libraries/ |
0 commit comments