|
1 | | -# ABAP-HTML-Diff |
2 | | -ABAP HTML Diff |
| 1 | +# ABAP HTML Diff |
| 2 | + |
| 3 | +Library to highlight the content difference between two HTML strings (htmldiff). |
| 4 | + |
| 5 | +Made by [Marc Bernard Tools](https://marcbernardtools.com/) giving back to the [SAP Community](https://community.sap.com/) |
| 6 | + |
| 7 | +NO WARRANTIES, [MIT License](LICENSE) |
| 8 | + |
| 9 | +## HTML Diff |
| 10 | + |
| 11 | +This is a diffing library that understands HTML. Best suited for cases when you want to show a diff of user-generated HTML. |
| 12 | + |
| 13 | +- The implementation is a port of JavaScript (https://github.com/alaorneto/htmldiffer, no license defined) |
| 14 | +- which is a port of CoffeeScript (https://github.com/tnwinc/htmldiff.js, MIT license) |
| 15 | +- which is a port of the original Ruby (https://github.com/myobie/htmldiff, MIT license) |
| 16 | + |
| 17 | +An enhancement was made so the code can also produce the diff of two texts (tags are treated like text). |
| 18 | + |
| 19 | +## Prerequisites |
| 20 | + |
| 21 | +SAP Basis 7.4 or higher |
| 22 | + |
| 23 | +## Installation |
| 24 | + |
| 25 | +You can install ABAP Differ using [abapGit](https://github.com/abapGit/abapGit) either creating a new online repository for https://github.com/Marc-Bernard-Tools/ABAP-HTML-Diff or downloading the repository [ZIP file](https://github.com/Marc-Bernard-Tools/ABAP-HTML-Diff/archive/main.zip) and creating a new offline repository. |
| 26 | + |
| 27 | +We recommend using package `$ABAPHTMLDIFF`. |
| 28 | + |
| 29 | +## Usage: HTML Diff |
| 30 | + |
| 31 | +**Diffing HTML** |
| 32 | + |
| 33 | +The following produces the diff of two example HTML snippets: |
| 34 | + |
| 35 | +```abap |
| 36 | +DATA: |
| 37 | + lv_original TYPE string, |
| 38 | + lv_modified TYPE string, |
| 39 | + lv_diff TYPE string, |
| 40 | + li_htmldiff TYPE REF TO zif_htmldiff. |
| 41 | +
|
| 42 | +lv_original = '\n' |
| 43 | + && ' <p>First paragraph.</p>\n' |
| 44 | + && ' <ul>\n' |
| 45 | + && ' <li>Item A</li>\n' |
| 46 | + && ' <li>Item B</li>\n' |
| 47 | + && ' <li>Item C</li>\n' |
| 48 | + && ' </ul>\n' |
| 49 | + && ' <img src="previous.jpg">\n' |
| 50 | + && ' <span>This is some interesting text.</span>\n'. |
| 51 | +
|
| 52 | +lv_modified = '\n' |
| 53 | + && ' <p>First paragraph.</p>\n' |
| 54 | + && ' <ul>\n' |
| 55 | + && ' <li>Item A</li>\n' |
| 56 | + && ' <li>Item B</li>\n' |
| 57 | + && ' <li>Item D</li>\n' |
| 58 | + && ' </ul>\n' |
| 59 | + && ' <img src="next.jpg">\n' |
| 60 | + && ' <span>This is some new text.</span>\n'. |
| 61 | +
|
| 62 | +REPLACE ALL OCCURRENCES OF '\n' IN lv_original WITH cl_abap_char_utilities=>newline. |
| 63 | +REPLACE ALL OCCURRENCES OF '\n' IN lv_modified WITH cl_abap_char_utilities=>newline. |
| 64 | + |
| 65 | +CREATE OBJECT li_differ TYPE zcl_htmldiff. |
| 66 | + |
| 67 | +lv_diff = li_htmldiff->htmldiff( |
| 68 | + iv_before = lv_original |
| 69 | + iv_after = lv_modified |
| 70 | + iv_with_img = abap_false ). |
| 71 | +``` |
| 72 | + |
| 73 | +Result: |
| 74 | + |
| 75 | +```html |
| 76 | + <p>First paragraph.</p> |
| 77 | + <ul> |
| 78 | + <li>Item A</li> |
| 79 | + <li>Item B</li> |
| 80 | + <li>Item <del>C</del><ins>D</ins></li> |
| 81 | + </ul> |
| 82 | + <img src='previous.jpg'><img src='next.jpg'> |
| 83 | + <span>This is some <del>interesting</del><ins>new</ins> text.</span> |
| 84 | +``` |
| 85 | + |
| 86 | +By setting the image parameter to true, you can also mark changed images as deletions or insertions: |
| 87 | + |
| 88 | +```abap |
| 89 | +lv_diff = li_htmldiff->htmldiff( |
| 90 | + iv_before = lv_original |
| 91 | + iv_after = lv_modified |
| 92 | + iv_with_img = abap_true ). |
| 93 | +``` |
| 94 | + |
| 95 | +Result: |
| 96 | + |
| 97 | +```html |
| 98 | + <p>First paragraph.</p> |
| 99 | + <ul> |
| 100 | + <li>Item A</li> |
| 101 | + <li>Item B</li> |
| 102 | + <li>Item <del>C</del><ins>D</ins></li> |
| 103 | + </ul> |
| 104 | + <del><img src='previous.jpg'></del><ins><img src='next.jpg'></ins> |
| 105 | + <span>This is some <del>interesting</del><ins>new</ins> text.</span> |
| 106 | +``` |
| 107 | + |
| 108 | +There are a few other options you can set in the `constructor`: |
| 109 | + |
| 110 | +- `iv_inserts`: Show `<ins>` tags (default: on) |
| 111 | +- `iv_deletes`: Show `<del>` tags (default: on) |
| 112 | +- `iv_css_classes`: Add CSS classes to `<ins>` and `<del>` tags (default: off) |
| 113 | +- `iv_support_chinese`: Treat Chinese characters as individual words (default: off) |
| 114 | + |
| 115 | +Using CSS classes, the result will distinguish between inserts (class `diffins`), deletes (class `diffdel`), and updates (class `diffmod`). |
| 116 | + |
| 117 | +See the [test classes](https://github.com/Marc-Bernard-Tools/ABAP-Differ/blob/main/src/zcl_htmldiff.clas.testclasses.abap) for more examples. |
| 118 | + |
| 119 | +**Diffing Text** |
| 120 | + |
| 121 | +todo |
| 122 | + |
| 123 | +```abap |
| 124 | +lv_diff = li_htmldiff->textdiff( |
| 125 | + iv_before = lv_original |
| 126 | + iv_after = lv_modified ). |
| 127 | +``` |
| 128 | + |
| 129 | +**Styling** |
| 130 | + |
| 131 | +Here's an examle for styling the insertions and deletions using CSS. |
| 132 | + |
| 133 | +```css |
| 134 | +/* CSS for <ins> and <del> tags */ |
| 135 | +ins { background-color: #ddffdd; } |
| 136 | +ins img { border-color: #ddffdd; } |
| 137 | + |
| 138 | +del { background-color: #ffdddd; } |
| 139 | +del img { border-color: #ffdddd; } |
| 140 | +``` |
| 141 | + |
| 142 | +With the CSS class option, use the following: |
| 143 | + |
| 144 | +```css |
| 145 | +/* CSS for insert, delete, and modify classes */ |
| 146 | +.diffins { background-color: #ddffdd; } |
| 147 | +.diffdel { background-color: #ffdddd; } |
| 148 | +.diffmod { background-color: #ffffdd; } |
| 149 | +``` |
| 150 | + |
| 151 | +## Contributions |
| 152 | + |
| 153 | +All contributions are welcome! Just fork this repo and create a pull request. |
| 154 | + |
| 155 | +## About |
| 156 | + |
| 157 | +<p>Made with :heart: in Canada</p> |
| 158 | +<p>Copyright © 2021 <a href="https://marcbernardtools.com/">Marc Bernard Tools</a></p> |
| 159 | +<p>Follow <a href="https://twitter.com/marcfbe">@marcfbe</a> on Twitter</p> |
| 160 | +<p><a href="https://marcbernardtools.com/"><img width="160" height="65" src="https://marcbernardtools.com/info/MBT_Logo_640x250_on_Gray.png" alt="MBT Logo"></a></p> |
| 161 | + |
0 commit comments