Skip to content

Commit d285ea6

Browse files
committed
Create AbstractDiff class to be used by new table diff class
1 parent 7846bd7 commit d285ea6

File tree

3 files changed

+181
-171
lines changed

3 files changed

+181
-171
lines changed

demo/index.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use Caxy\HtmlDiff\HtmlDiff;
44

5+
require __DIR__.'/../lib/Caxy/HtmlDiff/AbstractDiff.php';
56
require __DIR__.'/../lib/Caxy/HtmlDiff/HtmlDiff.php';
67
require __DIR__.'/../lib/Caxy/HtmlDiff/Match.php';
78
require __DIR__.'/../lib/Caxy/HtmlDiff/Operation.php';

lib/Caxy/HtmlDiff/AbstractDiff.php

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
<?php
2+
3+
namespace Caxy\HtmlDiff;
4+
5+
abstract class AbstractDiff
6+
{
7+
public static $defaultSpecialCaseTags = array('strong', 'b', 'i', 'big', 'small', 'u', 'sub', 'sup', 'strike', 's', 'p');
8+
public static $defaultSpecialCaseChars = array('.', ',', '(', ')', '\'');
9+
public static $defaultGroupDiffs = true;
10+
11+
protected $content;
12+
protected $oldText;
13+
protected $newText;
14+
protected $encoding;
15+
protected $specialCaseOpeningTags = array();
16+
protected $specialCaseClosingTags = array();
17+
protected $specialCaseTags;
18+
protected $specialCaseChars;
19+
protected $groupDiffs;
20+
21+
public function __construct($oldText, $newText, $encoding = 'UTF-8', $specialCaseTags = null, $groupDiffs = null)
22+
{
23+
if ($specialCaseTags === null) {
24+
$specialCaseTags = static::$defaultSpecialCaseTags;
25+
}
26+
27+
if ($groupDiffs === null) {
28+
$groupDiffs = static::$defaultGroupDiffs;
29+
}
30+
31+
$this->oldText = $this->purifyHtml(trim($oldText));
32+
$this->newText = $this->purifyHtml(trim($newText));
33+
$this->encoding = $encoding;
34+
$this->content = '';
35+
$this->groupDiffs = $groupDiffs;
36+
$this->setSpecialCaseTags($specialCaseTags);
37+
$this->setSpecialCaseChars(static::$defaultSpecialCaseChars);
38+
}
39+
40+
public function setSpecialCaseChars(array $chars)
41+
{
42+
$this->specialCaseChars = $chars;
43+
}
44+
45+
public function getSpecialCaseChars()
46+
{
47+
return $this->specialCaseChars;
48+
}
49+
50+
public function addSpecialCaseChar($char)
51+
{
52+
if (!in_array($char, $this->specialCaseChars)) {
53+
$this->specialCaseChars[] = $char;
54+
}
55+
}
56+
57+
public function removeSpecialCaseChar($char)
58+
{
59+
$key = array_search($char, $this->specialCaseChars);
60+
if ($key !== false) {
61+
unset($this->specialCaseChars[$key]);
62+
}
63+
}
64+
65+
public function setSpecialCaseTags(array $tags = array())
66+
{
67+
$this->specialCaseTags = $tags;
68+
69+
foreach ($this->specialCaseTags as $tag) {
70+
$this->addSpecialCaseTag($tag);
71+
}
72+
}
73+
74+
public function addSpecialCaseTag($tag)
75+
{
76+
if (!in_array($tag, $this->specialCaseTags)) {
77+
$this->specialCaseTags[] = $tag;
78+
}
79+
80+
$opening = $this->getOpeningTag($tag);
81+
$closing = $this->getClosingTag($tag);
82+
83+
if (!in_array($opening, $this->specialCaseOpeningTags)) {
84+
$this->specialCaseOpeningTags[] = $opening;
85+
}
86+
if (!in_array($closing, $this->specialCaseClosingTags)) {
87+
$this->specialCaseClosingTags[] = $closing;
88+
}
89+
}
90+
91+
public function removeSpecialCaseTag($tag)
92+
{
93+
if (($key = array_search($tag, $this->specialCaseTags)) !== false) {
94+
unset($this->specialCaseTags[$key]);
95+
96+
$opening = $this->getOpeningTag($tag);
97+
$closing = $this->getClosingTag($tag);
98+
99+
if (($key = array_search($opening, $this->specialCaseOpeningTags)) !== false) {
100+
unset($this->specialCaseOpeningTags[$key]);
101+
}
102+
if (($key = array_search($closing, $this->specialCaseClosingTags)) !== false) {
103+
unset($this->specialCaseClosingTags[$key]);
104+
}
105+
}
106+
}
107+
108+
public function getSpecialCaseTags()
109+
{
110+
return $this->specialCaseTags;
111+
}
112+
113+
public function getOldHtml()
114+
{
115+
return $this->oldText;
116+
}
117+
118+
public function getNewHtml()
119+
{
120+
return $this->newText;
121+
}
122+
123+
public function getDifference()
124+
{
125+
return $this->content;
126+
}
127+
128+
public function setGroupDiffs($boolean)
129+
{
130+
$this->groupDiffs = $boolean;
131+
}
132+
133+
public function isGroupDiffs()
134+
{
135+
return $this->groupDiffs;
136+
}
137+
138+
protected function getOpeningTag($tag)
139+
{
140+
return "/<".$tag."[^>]*/i";
141+
}
142+
143+
protected function getClosingTag($tag)
144+
{
145+
return "</".$tag.">";
146+
}
147+
148+
protected function getStringBetween($str, $start, $end)
149+
{
150+
$expStr = explode( $start, $str, 2 );
151+
if ( count( $expStr ) > 1 ) {
152+
$expStr = explode( $end, $expStr[ 1 ] );
153+
if ( count( $expStr ) > 1 ) {
154+
array_pop( $expStr );
155+
156+
return implode( $end, $expStr );
157+
}
158+
}
159+
160+
return '';
161+
}
162+
163+
protected function purifyHtml($html, $tags = null)
164+
{
165+
if ( class_exists( 'Tidy' ) && false ) {
166+
$config = array( 'output-xhtml' => true, 'indent' => false );
167+
$tidy = new tidy;
168+
$tidy->parseString( $html, $config, 'utf8' );
169+
$html = (string) $tidy;
170+
171+
return $this->getStringBetween( $html, '<body>' );
172+
}
173+
174+
return $html;
175+
}
176+
}

lib/Caxy/HtmlDiff/HtmlDiff.php

Lines changed: 4 additions & 171 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,15 @@
22

33
namespace Caxy\HtmlDiff;
44

5-
class HtmlDiff
6-
{
7-
public static $defaultSpecialCaseTags = array('strong', 'b', 'i', 'big', 'small', 'u', 'sub', 'sup', 'strike', 's', 'p');
8-
public static $defaultSpecialCaseChars = array('.', ',', '(', ')', '\'');
9-
public static $defaultGroupDiffs = true;
10-
11-
protected $content;
12-
protected $oldText;
13-
protected $newText;
5+
class HtmlDiff extends AbstractDiff
6+
{
147
protected $oldWords = array();
158
protected $newWords = array();
169
protected $wordIndices;
17-
protected $encoding;
18-
protected $specialCaseOpeningTags = array();
19-
protected $specialCaseClosingTags = array();
20-
protected $specialCaseTags;
21-
protected $specialCaseChars;
22-
protected $groupDiffs;
10+
protected $oldTables;
11+
protected $newTables;
2312
protected $insertSpaceInReplace = false;
2413

25-
public function __construct($oldText, $newText, $encoding = 'UTF-8', $specialCaseTags = null, $groupDiffs = null)
26-
{
27-
if ($specialCaseTags === null) {
28-
$specialCaseTags = static::$defaultSpecialCaseTags;
29-
}
30-
31-
if ($groupDiffs === null) {
32-
$groupDiffs = static::$defaultGroupDiffs;
33-
}
34-
35-
$this->oldText = $this->purifyHtml(trim($oldText));
36-
$this->newText = $this->purifyHtml(trim($newText));
37-
$this->encoding = $encoding;
38-
$this->content = '';
39-
$this->groupDiffs = $groupDiffs;
40-
$this->setSpecialCaseTags($specialCaseTags);
41-
$this->setSpecialCaseChars(static::$defaultSpecialCaseChars);
42-
}
43-
4414
/**
4515
* @param boolean $boolean
4616
* @return HtmlDiff
@@ -59,143 +29,6 @@ public function getInsertSpaceInReplace()
5929
{
6030
return $this->insertSpaceInReplace;
6131
}
62-
63-
public function setSpecialCaseChars(array $chars)
64-
{
65-
$this->specialCaseChars = $chars;
66-
}
67-
68-
public function getSpecialCaseChars()
69-
{
70-
return $this->specialCaseChars;
71-
}
72-
73-
public function addSpecialCaseChar($char)
74-
{
75-
if (!in_array($char, $this->specialCaseChars)) {
76-
$this->specialCaseChars[] = $char;
77-
}
78-
}
79-
80-
public function removeSpecialCaseChar($char)
81-
{
82-
$key = array_search($char, $this->specialCaseChars);
83-
if ($key !== false) {
84-
unset($this->specialCaseChars[$key]);
85-
}
86-
}
87-
88-
public function setSpecialCaseTags(array $tags = array())
89-
{
90-
$this->specialCaseTags = $tags;
91-
92-
foreach ($this->specialCaseTags as $tag) {
93-
$this->addSpecialCaseTag($tag);
94-
}
95-
}
96-
97-
public function addSpecialCaseTag($tag)
98-
{
99-
if (!in_array($tag, $this->specialCaseTags)) {
100-
$this->specialCaseTags[] = $tag;
101-
}
102-
103-
$opening = $this->getOpeningTag($tag);
104-
$closing = $this->getClosingTag($tag);
105-
106-
if (!in_array($opening, $this->specialCaseOpeningTags)) {
107-
$this->specialCaseOpeningTags[] = $opening;
108-
}
109-
if (!in_array($closing, $this->specialCaseClosingTags)) {
110-
$this->specialCaseClosingTags[] = $closing;
111-
}
112-
}
113-
114-
public function removeSpecialCaseTag($tag)
115-
{
116-
if (($key = array_search($tag, $this->specialCaseTags)) !== false) {
117-
unset($this->specialCaseTags[$key]);
118-
119-
$opening = $this->getOpeningTag($tag);
120-
$closing = $this->getClosingTag($tag);
121-
122-
if (($key = array_search($opening, $this->specialCaseOpeningTags)) !== false) {
123-
unset($this->specialCaseOpeningTags[$key]);
124-
}
125-
if (($key = array_search($closing, $this->specialCaseClosingTags)) !== false) {
126-
unset($this->specialCaseClosingTags[$key]);
127-
}
128-
}
129-
}
130-
131-
public function getSpecialCaseTags()
132-
{
133-
return $this->specialCaseTags;
134-
}
135-
136-
public function getOldHtml()
137-
{
138-
return $this->oldText;
139-
}
140-
141-
public function getNewHtml()
142-
{
143-
return $this->newText;
144-
}
145-
146-
public function getDifference()
147-
{
148-
return $this->content;
149-
}
150-
151-
public function setGroupDiffs($boolean)
152-
{
153-
$this->groupDiffs = $boolean;
154-
}
155-
156-
public function isGroupDiffs()
157-
{
158-
return $this->groupDiffs;
159-
}
160-
161-
protected function getOpeningTag($tag)
162-
{
163-
return "/<".$tag."[^>]*/i";
164-
}
165-
166-
protected function getClosingTag($tag)
167-
{
168-
return "</".$tag.">";
169-
}
170-
171-
protected function getStringBetween($str, $start, $end)
172-
{
173-
$expStr = explode( $start, $str, 2 );
174-
if ( count( $expStr ) > 1 ) {
175-
$expStr = explode( $end, $expStr[ 1 ] );
176-
if ( count( $expStr ) > 1 ) {
177-
array_pop( $expStr );
178-
179-
return implode( $end, $expStr );
180-
}
181-
}
182-
183-
return '';
184-
}
185-
186-
protected function purifyHtml($html, $tags = null)
187-
{
188-
if ( class_exists( 'Tidy' ) && false ) {
189-
$config = array( 'output-xhtml' => true, 'indent' => false );
190-
$tidy = new tidy;
191-
$tidy->parseString( $html, $config, 'utf8' );
192-
$html = (string) $tidy;
193-
194-
return $this->getStringBetween( $html, '<body>' );
195-
}
196-
197-
return $html;
198-
}
19932

20033
public function build()
20134
{

0 commit comments

Comments
 (0)