Skip to content

Commit f7a4adc

Browse files
committed
PB-369: Integrate third party library to work with DOMDocument
- update wrapper classes
1 parent 5a23e0f commit f7a4adc

File tree

7 files changed

+118
-78
lines changed

7 files changed

+118
-78
lines changed

app/code/Magento/PageBuilder/Model/Dom/Adapter/DocumentFragmentInterface.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,25 @@ interface DocumentFragmentInterface
1919
* @return bool
2020
*/
2121
public function appendHTML(string $data): bool;
22+
23+
/**
24+
* Returns the first element matching the specified selector.
25+
*
26+
* @param string $selector
27+
* @return ElementInterface
28+
*/
29+
public function querySelector(string $selector): ElementInterface;
30+
31+
/**
32+
* Returns all elements matching the specified selector.
33+
*
34+
* @param string $selector
35+
* @return HtmlCollectionInterface
36+
*/
37+
public function querySelectorAll(string $selector): HtmlCollectionInterface;
38+
39+
/**
40+
* Returns string content of fragment
41+
*/
42+
public function prop_get_innerText(): string;
2243
}

app/code/Magento/PageBuilder/Model/Dom/Adapter/DocumentInterface.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,29 @@ interface DocumentInterface
2222
*/
2323
public function __toString(): string;
2424

25+
/**
26+
* Create new document fragment
27+
*
28+
* @return DocumentFragmentInterface
29+
*/
30+
public function createDocumentFragment(): DocumentFragmentInterface;
31+
32+
/**
33+
* Returns the first element matching the specified selector.
34+
*
35+
* @param string $selector
36+
* @return ElementInterface
37+
*/
38+
public function querySelector(string $selector): ElementInterface;
39+
40+
/**
41+
* Returns all elements matching the specified selector.
42+
*
43+
* @param string $selector
44+
* @return HtmlCollectionInterface
45+
*/
46+
public function querySelectorAll(string $selector): HtmlCollectionInterface;
47+
2548
/**
2649
* Dumps the internal document into a string using HTML formatting
2750
*

app/code/Magento/PageBuilder/Model/Dom/Adapter/HtmlDocumentInterface.php

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,4 @@ interface HtmlDocumentInterface
1919
* @return HtmlCollectionInterface
2020
*/
2121
public function getElementsByClassName(string $names): HtmlCollectionInterface;
22-
23-
/**
24-
* Returns the first element matching the specified selector.
25-
*
26-
* @param string $selector
27-
* @return HtmlCollectionInterface
28-
*/
29-
public function querySelector(string $selector): ElementInterface;
30-
31-
/**
32-
* Returns all elements matching the specified selector.
33-
*
34-
* @param string $selector
35-
* @return HtmlCollectionInterface
36-
*/
37-
public function querySelectorAll(string $selector): HtmlCollectionInterface;
38-
39-
/**
40-
* Dumps the internal document into a string using HTML formatting
41-
*
42-
* @return string
43-
*/
44-
public function saveHTML(): string;
4522
}

app/code/Magento/PageBuilder/Model/Dom/Document.php

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
use DOMNode;
1111
use Gt\Dom\Document as GtDomDocument;
1212
use Magento\Framework\ObjectManagerInterface;
13+
use Magento\PageBuilder\Model\Dom\Adapter\DocumentFragmentInterface;
1314
use Magento\PageBuilder\Model\Dom\Adapter\DocumentInterface;
15+
use Magento\PageBuilder\Model\Dom\Adapter\ElementInterface;
16+
use Magento\PageBuilder\Model\Dom\Adapter\HtmlCollectionInterface;
1417

1518
/**
1619
* PhpGt DOM Document wrapper.
@@ -20,12 +23,12 @@ class Document implements DocumentInterface
2023
/**
2124
* @var ObjectManagerInterface
2225
*/
23-
private $objectManager;
26+
protected $objectManager;
2427

2528
/**
2629
* @var GtDomDocument
2730
*/
28-
private $document;
31+
protected $document;
2932

3033
/**
3134
* Document constructor.
@@ -38,6 +41,7 @@ public function __construct(
3841
) {
3942
$this->objectManager = $objectManager;
4043
$this->document = $this->objectManager->create(GtDomDocument::class, [ 'document' => $document ]);
44+
$this->document->createDocumentFragment();
4145
}
4246

4347
/**
@@ -48,6 +52,39 @@ public function __toString(): string
4852
return $this->document->__toString();
4953
}
5054

55+
/**
56+
* @inheritDoc
57+
*/
58+
public function createDocumentFragment(): DocumentFragmentInterface
59+
{
60+
return $this->objectManager->create(
61+
DocumentFragmentInterface::class,
62+
[ 'documentFragment' => $this->document->createDocumentFragment() ]
63+
);
64+
}
65+
66+
/**
67+
* @inheritDoc
68+
*/
69+
public function querySelector(string $selector): ElementInterface
70+
{
71+
return $this->objectManager->create(
72+
ElementInterface::class,
73+
[ 'element' => $this->document->querySelector($selector) ]
74+
);
75+
}
76+
77+
/**
78+
* @inheritDoc
79+
*/
80+
public function querySelectorAll(string $selector): HtmlCollectionInterface
81+
{
82+
return $this->objectManager->create(
83+
HtmlCollectionInterface::class,
84+
[ 'collection' => $this->document->querySelectorAll($selector) ]
85+
);
86+
}
87+
5188
/**
5289
* @inheritDoc
5390
*/

app/code/Magento/PageBuilder/Model/Dom/DocumentFragment.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use Gt\Dom\DocumentFragment as GtDomDocumentFragment;
1111
use Magento\Framework\ObjectManagerInterface;
1212
use Magento\PageBuilder\Model\Dom\Adapter\DocumentFragmentInterface;
13+
use Magento\PageBuilder\Model\Dom\Adapter\ElementInterface;
14+
use Magento\PageBuilder\Model\Dom\Adapter\HtmlCollectionInterface;
1315

1416
/**
1517
* PhpGt DOM DocumentFragment wrapper.
@@ -44,6 +46,36 @@ public function __construct(
4446
*/
4547
public function appendHTML(string $data): bool
4648
{
47-
return $this->documentFragment->appendXML($data);
49+
return $this->documentFragment->appendHTML($data);
50+
}
51+
52+
/**
53+
* @inheritDoc
54+
*/
55+
public function querySelector(string $selector): ElementInterface
56+
{
57+
return $this->objectManager->create(
58+
ElementInterface::class,
59+
[ 'element' => $this->documentFragment->querySelector($selector) ]
60+
);
61+
}
62+
63+
/**
64+
* @inheritDoc
65+
*/
66+
public function querySelectorAll(string $selector): HtmlCollectionInterface
67+
{
68+
return $this->objectManager->create(
69+
HtmlCollectionInterface::class,
70+
[ 'collection' => $this->documentFragment->querySelectorAll($selector) ]
71+
);
72+
}
73+
74+
/**
75+
* @inheritDoc
76+
*/
77+
public function prop_get_innerText(): string
78+
{
79+
return $this->documentFragment->prop_get_innerText();
4880
}
4981
}

app/code/Magento/PageBuilder/Model/Dom/HtmlDocument.php

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,8 @@
1616
/**
1717
* PhpGt DOM HTMLDocument wrapper.
1818
*/
19-
class HtmlDocument implements HtmlDocumentInterface
19+
class HtmlDocument extends Document implements HtmlDocumentInterface
2020
{
21-
/**
22-
* @var ObjectManagerInterface
23-
*/
24-
private $objectManager;
25-
26-
/**
27-
* @var GtDomHTMLDocument
28-
*/
29-
private $document;
30-
3121
/**
3222
* HtmlDocument constructor.
3323
* @param ObjectManagerInterface $objectManager
@@ -41,36 +31,6 @@ public function __construct(
4131
$this->document = $this->objectManager->create(GtDomHTMLDocument::class, [ 'document' => $document ]);
4232
}
4333

44-
/**
45-
* @inheritDoc
46-
*/
47-
public function querySelector(string $selector): ElementInterface
48-
{
49-
return $this->objectManager->create(
50-
ElementInterface::class,
51-
[ 'element' => $this->document->querySelector($selector) ]
52-
);
53-
}
54-
55-
/**
56-
* @inheritDoc
57-
*/
58-
public function querySelectorAll(string $selector): HtmlCollectionInterface
59-
{
60-
return $this->objectManager->create(
61-
HtmlCollectionInterface::class,
62-
[ 'collection' => $this->document->querySelectorAll($selector) ]
63-
);
64-
}
65-
66-
/**
67-
* @inheritDoc
68-
*/
69-
public function saveHTML(): string
70-
{
71-
return $this->document->saveHTML();
72-
}
73-
7434
/**
7535
* @inheritDoc
7636
*/

app/code/Magento/PageBuilder/Model/Dom/XmlDocument.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,8 @@
1414
/**
1515
* PhpGt DOM XmlDocument wrapper.
1616
*/
17-
class XmlDocument implements XmlDocumentInterface
17+
class XmlDocument extends Document implements XmlDocumentInterface
1818
{
19-
/**
20-
* @var ObjectManagerInterface
21-
*/
22-
private $objectManager;
23-
24-
/**
25-
* @var GtDomXMLDocument
26-
*/
27-
private $document;
28-
2919
/**
3020
* XmlDocument constructor.
3121
*

0 commit comments

Comments
 (0)