Skip to content

Commit b395cbc

Browse files
author
Sven Hagemann
committed
Reworked ListDiffLines to use DOMDocument
Right now TableDiff already uses DOMDocument but this component still used a third party library that extended the old SimpleXML extension from PHP. I ported to the code to use DOMDocument, making the library absolete, and slightly improving performance. Mostly this is preperation for moving the entire library to DOMDocument
1 parent 37a9226 commit b395cbc

File tree

8 files changed

+299
-123
lines changed

8 files changed

+299
-123
lines changed

lib/Caxy/HtmlDiff/ListDiffLines.php

Lines changed: 228 additions & 108 deletions
Large diffs are not rendered by default.

lib/Caxy/HtmlDiff/Operation.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class Operation
66
{
7-
const ADDED = 'a';
7+
const ADDED = 'a';
88
const DELETED = 'd';
99
const CHANGED = 'c';
1010

@@ -14,12 +14,12 @@ class Operation
1414
public $startInNew;
1515
public $endInNew;
1616

17-
public function __construct($action, $startInOld, $endInOld, $startInNew, $endInNew)
17+
public function __construct(string $action, int $startInOld, int $endInOld, int $startInNew, int $endInNew)
1818
{
19-
$this->action = $action;
19+
$this->action = $action;
2020
$this->startInOld = $startInOld;
21-
$this->endInOld = $endInOld;
21+
$this->endInOld = $endInOld;
2222
$this->startInNew = $startInNew;
23-
$this->endInNew = $endInNew;
23+
$this->endInNew = $endInNew;
2424
}
2525
}

tests/Caxy/Tests/AbstractTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
abstract class AbstractTest extends TestCase
88
{
9-
protected function stripExtraWhitespaceAndNewLines($text)
9+
protected function stripExtraWhitespaceAndNewLines(string $text)
1010
{
1111
return trim(
1212
preg_replace(

tests/Caxy/Tests/HtmlDiff/Functional/HtmlDiffFunctionalTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ public function testHtmlDiff($oldText, $newText, $expected)
2020
$diff = new HtmlDiff(trim($oldText), trim($newText), 'UTF-8', array());
2121
$output = $diff->build();
2222

23-
static::assertEquals($this->stripExtraWhitespaceAndNewLines($expected), $this->stripExtraWhitespaceAndNewLines($output));
23+
static::assertEquals(
24+
$this->stripExtraWhitespaceAndNewLines($expected),
25+
$this->stripExtraWhitespaceAndNewLines($output)
26+
);
2427
}
2528

2629
public function diffContentProvider()

tests/Caxy/Tests/HtmlDiff/Performance/PerformanceTest.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,26 @@
77

88
class PerformanceTest extends AbstractTest
99
{
10+
private const FIXTURE_PATH = __DIR__ . '/../../../../fixtures/Performance/';
11+
1012
/**
1113
* @group performance
1214
*/
1315
public function testParagraphPerformance()
1416
{
15-
$fixturesPath = __DIR__ . '/../../../../fixtures/Performance/';
16-
17-
$expected = file_get_contents($fixturesPath . 'paragraphs_expected.html');
17+
$expected = file_get_contents(self::FIXTURE_PATH . 'paragraphs_expected.html');
1818

1919
$diff = new HtmlDiff(
20-
file_get_contents($fixturesPath . 'paragraphs.html'),
21-
file_get_contents($fixturesPath . 'paragraphs_changed.html'),
20+
file_get_contents(self::FIXTURE_PATH . 'paragraphs.html'),
21+
file_get_contents(self::FIXTURE_PATH . 'paragraphs_changed.html'),
2222
'UTF-8', array()
2323
);
2424

2525
$output = $diff->build();
2626

27-
$this->assertSame($this->stripExtraWhitespaceAndNewLines($output), $this->stripExtraWhitespaceAndNewLines($expected));
27+
self::assertSame(
28+
$this->stripExtraWhitespaceAndNewLines($expected),
29+
$this->stripExtraWhitespaceAndNewLines($output)
30+
);
2831
}
2932
}

tests/fixtures/HtmlDiff/first-and-last.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@
3535
</newText>
3636

3737
<expected>
38-
<ol class="diff-list"><li class="removed"><del> Etiam feugiat lorem non metus. Nullam accumsan lorem in dui. </del></li><li class="removed"><del> Praesent ac sem eget est egestas volutpat. Nulla neque dolor, sagittis eget, iaculis quis, molestie non, velit. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Integer ante arcu, accumsan a, consectetuer eget, posuere ut, mauris. Aenean massa. </del></li><li class="removed"><del> Vestibulum suscipit nulla quis orci. Integer ante arcu, accumsan a, consectetuer eget, posuere ut, mauris. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. </del></li><li class="replacement"> Suspendisse non nisl sit amet velit hendrerit rutrum. Aenean massa. Curabitur ullamcorper ultricies nisi. Praesent vestibulum dapibus nibh. In dui magna, posuere eget, vestibulum et, tempor auctor, justo. </li><li class="normal new"><ins> Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. </ins></li><li class="normal new"><ins> Duis lobortis massa imperdiet quam. Donec sodales sagittis magna. Nam ipsum risus, rutrum vitae, vestibulum eu, molestie vel, lacus. Suspendisse eu ligula. Morbi nec metus. </ins></li></ol>
38+
<ol class="diff-list"><li class="removed"><del> Etiam feugiat lorem non metus. Nullam accumsan lorem in dui. </del></li><li class="removed"><del> Praesent ac sem eget est egestas volutpat. Nulla neque dolor, sagittis eget, iaculis quis, molestie non, velit. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Integer ante arcu, accumsan a, consectetuer eget, posuere ut, mauris. Aenean massa. </del></li><li class="removed"><del> Vestibulum suscipit nulla quis orci. Integer ante arcu, accumsan a, consectetuer eget, posuere ut, mauris. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. </del></li><li class="replacement">Suspendisse non nisl sit amet velit hendrerit rutrum. Aenean massa. Curabitur ullamcorper ultricies nisi. Praesent vestibulum dapibus nibh. In dui magna, posuere eget, vestibulum et, tempor auctor, justo.</li><li class="normal new"><ins> Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. </ins></li><li class="normal new"><ins> Duis lobortis massa imperdiet quam. Donec sodales sagittis magna. Nam ipsum risus, rutrum vitae, vestibulum eu, molestie vel, lacus. Suspendisse eu ligula. Morbi nec metus. </ins></li></ol>
3939
</expected>

tests/fixtures/HtmlDiff/override-5.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@
2727
</newText>
2828

2929
<expected>
30-
<p>In addition to the <em>means of egress</em> required by this chapter, <del class="diffmod">provisions </del><em class="diffmod"><ins class="diffmod">emergency escape and rescue openings </ins></em>shall be <del class="diffmod">made for </del><em class="diffmod"><del class="diffmod">emergency escape and rescue openings</del></em><ins class="diffmod">provided</ins> in<ins class="diffins"> the following occupancies:</ins></p><ins class="diffins"> </ins><div style="padding-left:30px;" class="diffmod"><ins class="diffins">1.</ins> Group R<ins class="diffins">-1 and R-2</ins> occupancies.<del class="diffmod"> </del></div><ins class="diffmod"> </ins><div style="padding-left:30px;" class="diffmod"><ins class="diffmod">2. Group R-1 and R-2 occupancies located on stories with one exit or access to one exit where permitted by Tables 1006.3.2(1) and 1006.3.2(2). </ins></div><ins class="diffmod"> </ins><div style="padding-left:30px;" class="diffmod"><ins class="diffmod">3. Group R-3 occupancies.</ins></div><ins class="diffmod"> </ins><p class="diffmod"><br /><em>Basements</em> and sleeping rooms below the fourth story above <em>grade plane</em> shall have at least one exterior <em>emergency escape and rescue opening</em> in accordance with this section. Where <em>basements</em> contain one or more sleeping rooms, <em>emergency escape and rescue openings</em> shall be required in each sleeping room, but shall not be required in adjoining areas of the <em>basement</em>. Such openings shall open directly into a <em>public way</em> or to a <em>yard </em>or<em> court</em> that opens to a <em>public way</em>.</p> <ul class="exception diff-list"><li class="normal"><strong>Exceptions:</strong><br class="diffmod" /><ol class="diff-list"><li class="removed"><del><em>Groups R-1 and R-2 occupancies are not required to provide emergency and escape openings where they comply with all of the following:</em><ol><li><em>Each story has access to two or more means of egress.<br /></em></li> <li><em>The building is constructed of Type I, Type II, Type IIIA or Type IV construction.</em></li> <li><em>The building is equipped throughout with an approved automatic sprinkler system in accordance with Sections 903.3.1.1 or 903.3.3.2.</em></li> </ol></del></li><li class="removed"><del><em>The emergency escape and rescue opening is permitted to open onto a balcony within an atrium in accordance with the requirements of Section 404, provided the balcony provides access to an exit and the dwelling unit or sleeping unit has a means of egress that is not open to the atrium.</em></del></li><li class="replacement"><ins><em>Emergency escape and rescue opening</em> are not required from Group R-1 and R-2 occupancies where each story has access to at least two exits or access to exits and the building is equipped throughout with an automatic sprinkler system in accordance with Section 903.3.1.1.</ins></li><li class="normal"><em>Basements</em> with a ceiling height of less than 80 inches (2032 mm) shall not be required to have <em>emergency escape and rescue openings</em>.</li><li class="normal"><em>Emergency escape and rescue openings</em> are not required from <em>basements</em> or sleeping rooms that have an <em>exit</em> door or <em>exit access</em> door that opens directly into a <em>public way</em> or to a <em>yard</em>, <em>court</em> or exterior exit balcony that opens to a <em>public way</em>.</li><li class="normal"><em>Basements</em> without <em>habitable spaces</em> and having not more than 200 square feet (18.6 m<sup>2</sup>) in floor area shall not be required to have <em>emergency escape and rescue openings</em>.</li></ol></li></ul>
30+
<p>In addition to the <em>means of egress</em> required by this chapter, <del class="diffmod">provisions </del><em class="diffmod"><ins class="diffmod">emergency escape and rescue openings </ins></em>shall be <del class="diffmod">made for </del><em class="diffmod"><del class="diffmod">emergency escape and rescue openings</del></em><ins class="diffmod">provided</ins> in<ins class="diffins"> the following occupancies:</ins></p><ins class="diffins"> </ins><div style="padding-left:30px;" class="diffmod"><ins class="diffins">1.</ins> Group R<ins class="diffins">-1 and R-2</ins> occupancies.<del class="diffmod"> </del></div><ins class="diffmod"> </ins><div style="padding-left:30px;" class="diffmod"><ins class="diffmod">2. Group R-1 and R-2 occupancies located on stories with one exit or access to one exit where permitted by Tables 1006.3.2(1) and 1006.3.2(2). </ins></div><ins class="diffmod"> </ins><div style="padding-left:30px;" class="diffmod"><ins class="diffmod">3. Group R-3 occupancies.</ins></div><ins class="diffmod"> </ins><p class="diffmod"><br /><em>Basements</em> and sleeping rooms below the fourth story above <em>grade plane</em> shall have at least one exterior <em>emergency escape and rescue opening</em> in accordance with this section. Where <em>basements</em> contain one or more sleeping rooms, <em>emergency escape and rescue openings</em> shall be required in each sleeping room, but shall not be required in adjoining areas of the <em>basement</em>. Such openings shall open directly into a <em>public way</em> or to a <em>yard </em>or<em> court</em> that opens to a <em>public way</em>.</p> <ul class="exception diff-list"><li class="normal"><strong>Exceptions:</strong><br class="diffmod"><ol class="diff-list"><li class="removed"><del><em>Groups R-1 and R-2 occupancies are not required to provide emergency and escape openings where they comply with all of the following:</em><ol><li><em>Each story has access to two or more means of egress.<br></em></li> <li><em>The building is constructed of Type I, Type II, Type IIIA or Type IV construction.</em></li> <li><em>The building is equipped throughout with an approved automatic sprinkler system in accordance with Sections 903.3.1.1 or 903.3.3.2.</em></li> </ol></del></li><li class="removed"><del><em>The emergency escape and rescue opening is permitted to open onto a balcony within an atrium in accordance with the requirements of Section 404, provided the balcony provides access to an exit and the dwelling unit or sleeping unit has a means of egress that is not open to the atrium.</em></del></li><li class="replacement"><ins><em>Emergency escape and rescue opening</em> are not required from Group R-1 and R-2 occupancies where each story has access to at least two exits or access to exits and the building is equipped throughout with an automatic sprinkler system in accordance with Section 903.3.1.1.</ins></li><li class="normal"><em>Basements</em> with a ceiling height of less than 80 inches (2032 mm) shall not be required to have <em>emergency escape and rescue openings</em>.</li><li class="normal"><em>Emergency escape and rescue openings</em> are not required from <em>basements</em> or sleeping rooms that have an <em>exit</em> door or <em>exit access</em> door that opens directly into a <em>public way</em> or to a <em>yard</em>, <em>court</em> or exterior exit balcony that opens to a <em>public way</em>.</li><li class="normal"><em>Basements</em> without <em>habitable spaces</em> and having not more than 200 square feet (18.6 m<sup>2</sup>) in floor area shall not be required to have <em>emergency escape and rescue openings</em>.</li></ol></li></ul>
3131
</expected>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<oldText>
2+
<ol>
3+
<li>List item one</li>
4+
<li>List item two with subitems:
5+
<ul>
6+
<li>Subitem 1</li>
7+
<li>Subitem 2</li>
8+
<li>
9+
<p>
10+
Hi there<br />
11+
<ul>
12+
<li>A</li>
13+
<li>B</li>
14+
</ul>
15+
</p>
16+
</li>
17+
<li>
18+
<dl><li>Deleted list</li></dl>
19+
</li>
20+
</ul>
21+
</li>
22+
<li>Final list item</li>
23+
</ol>
24+
</oldText>
25+
26+
<newText>
27+
<ol>
28+
<li>List item one</li>
29+
<li>List item tw with subitems:
30+
<ul>
31+
<li>Subitem 1</li>
32+
<li>Subitem 2</li>
33+
<li>
34+
<p>
35+
*.* Hi there<br />
36+
<ul>
37+
<li>A</li>
38+
<li>C</li>
39+
</ul>
40+
</p>
41+
</li>
42+
</ul>
43+
</li>
44+
<li>Final list item</li>
45+
</ol>
46+
</newText>
47+
48+
<expected>
49+
<ol class="diff-list"><li class="normal">List item one</li><li class="normal">List item <del class="diffmod">two</del><ins class="diffmod">tw</ins> with subitems: <ul class="diff-list"><li class="normal">Subitem 1</li><li class="normal">Subitem 2</li><li class="removed"><del><p> Hi there<br></p><ul><li>A</li><li>B</li></ul></del></li><li class="replacement"><p class="diffmod"><ins class="diffins"> *.* Hi there</ins><br class="diffmod"><ins class="diffins"></ins></p><ul><ins class="diffins"></ins><li class="diffmod"><ins class="diffins">A</ins></li><ins class="diffins"></ins><li class="diffmod"><ins class="diffins">C</ins></li><ins class="diffins"></ins></ul></li><li class="removed"><del>Deleted list</del></li></ul></li><li class="normal">Final list item</li></ol>
50+
</expected>

0 commit comments

Comments
 (0)