@@ -30,6 +30,9 @@ public function __construct($version = null, $encoding = null){
3030 $ this ->registerNodeClass ('DOMText ' , Text::class);
3131 $ this ->registerNodeClass ('DOMCharacterData ' , CharacterData::class);
3232 $ this ->registerNodeClass ('DOMDocumentFragment ' , DocumentFragment::class);
33+ $ this ->registerNodeClass ('DOMDocumentType ' , DocumentType::class);
34+ $ this ->registerNodeClass ('DOMComment ' , Comment::class);
35+ $ this ->registerNodeClass ('DOMAttr ' , Attr::class);
3336 }
3437
3538 /**
@@ -46,20 +49,20 @@ public function selector2xpath(string $selector, string $axis = '//'):string{
4649 * @param string $xpath
4750 * @param \DOMNode|null $contextNode
4851 *
49- * @return \DOMNodeList
52+ * @return \chillerlan\PrototypeDOM\NodeList
5053 */
51- public function query (string $ xpath , DOMNode $ contextNode = null ):DOMNodeList {
52- return ( new DOMXPath ($ this ))->query ($ xpath , $ contextNode );
54+ public function query (string $ xpath , DOMNode $ contextNode = null ):NodeList {
55+ return new NodeList (( new DOMXPath ($ this ))->query ($ xpath , $ contextNode) );
5356 }
5457
5558 /**
5659 * @param string $selector
5760 * @param \DOMNode|null $contextNode
5861 * @param string $axis
5962 *
60- * @return \DOMNodeList
63+ * @return \chillerlan\PrototypeDOM\NodeList
6164 */
62- public function getElementsBySelector (string $ selector , DOMNode $ contextNode = null , string $ axis = 'descendant-or-self:: ' ):DOMNodeList {
65+ public function querySelectorAll (string $ selector , DOMNode $ contextNode = null , string $ axis = 'descendant-or-self:: ' ):NodeList {
6366 return $ this ->query ($ this ->selector2xpath ($ selector , $ axis ), $ contextNode );
6467 }
6568
@@ -69,9 +72,9 @@ public function getElementsBySelector(string $selector, DOMNode $contextNode = n
6972 * @param string $axis
7073 * @param int $nodeType
7174 *
72- * @return \DOMNode[]
75+ * @return \chillerlan\PrototypeDOM\NodeList
7376 */
74- public function select ($ selectors = null , DOMNode $ contextNode = null , string $ axis = 'descendant-or-self:: ' , int $ nodeType = XML_ELEMENT_NODE ):array {
77+ public function select ($ selectors = null , DOMNode $ contextNode = null , string $ axis = 'descendant-or-self:: ' , int $ nodeType = XML_ELEMENT_NODE ):NodeList {
7578
7679 if (is_string ($ selectors )){
7780 $ selectors = [trim ($ selectors )];
@@ -81,11 +84,11 @@ public function select($selectors = null, DOMNode $contextNode = null, string $a
8184 $ selectors = ['* ' ];
8285 }
8386
84- $ elements = [] ;
87+ $ elements = new NodeList ;
8588
8689 foreach ($ selectors as $ selector ){
8790
88- foreach ($ this ->getElementsBySelector ($ selector , $ contextNode , $ axis ) as $ element ){
91+ foreach ($ this ->querySelectorAll ($ selector , $ contextNode , $ axis ) as $ element ){
8992
9093 if ($ element ->nodeType === $ nodeType ){
9194 $ elements [] = $ element ;
@@ -98,6 +101,19 @@ public function select($selectors = null, DOMNode $contextNode = null, string $a
98101 return $ elements ;
99102 }
100103
104+ public function _loadHTMLFragment (string $ content ):NodeList {
105+ $ document = new Document ;
106+ $ document ->loadHTML ('<html><body id="-import-content"> ' .$ content .'</body></html> ' );
107+
108+ return new NodeList ($ document ->getElementById ('-import-content ' )->childNodes );
109+
110+ /*
111+ $document->loadHTML('<!DOCTYPE html>' .$content);
112+ return $document->getElementsByTagName('head')[0]->childNodes
113+ ?? $document->getElementsByTagName('body')[0]->childNodes;
114+ */
115+ }
116+
101117 /**
102118 * @param \DOMNode|null $context
103119 * @param bool $xml
@@ -132,32 +148,26 @@ public function removeElementsBySelector($selectors, DOMNode $contextNode = null
132148 }
133149
134150 /**
135- * @param string|\DOMNode|\DOMNodeList $content
151+ * @param string|\DOMNode|\DOMNodeList|\chillerlan\PrototypeDOM\NodeList $content
136152 *
137- * @return \DOMNodeList
153+ * @return \chillerlan\PrototypeDOM\NodeList
138154 * @throws \Exception
139155 */
140- public function _toDOMNodeList ($ content ):DOMNodeList {
156+ public function _toNodeList ($ content ):NodeList {
141157
142- if ($ content instanceof DOMNodeList){
143- return $ content ;
158+ if ($ content instanceof NodeList || $ content instanceof DOMNodeList || is_array ( $ content ) ){
159+ return new NodeList ( $ content) ;
144160 }
145-
146- $ document = new Document ;
147-
148- if ($ content instanceof DOMNode){
149- $ document ->loadHTML ('<html><body id="content"></body></html> ' );
150-
151- $ document ->getElementById ('content ' )->appendChild ($ document ->importNode ($ content , true ));
161+ elseif ($ content instanceof DOMNode){
162+ return new NodeList ([$ content ]);
152163 }
153164 elseif (is_string ($ content )){
154- $ document -> loadHTML ( ' <html><body id="content"> ' . $ content. ' </body></html> ' );
165+ return $ this -> _loadHTMLFragment ( $ content );
155166 }
156167 else {
157168 throw new \Exception ('invalid content ' ); // @codeCoverageIgnore
158169 }
159170
160- return $ document ->getElementById ('content ' )->childNodes ;
161171 }
162172
163173 /**
@@ -166,10 +176,10 @@ public function _toDOMNodeList($content):DOMNodeList{
166176 * @param int $maxLength
167177 * @param int $nodeType
168178 *
169- * @return \DOMNode[]
179+ * @return \chillerlan\PrototypeDOM\NodeList
170180 */
171- public function recursivelyCollect (DOMNode $ element , string $ property , int $ maxLength = -1 , int $ nodeType = XML_ELEMENT_NODE ):array {
172- $ nodes = [] ;
181+ public function recursivelyCollect (DOMNode $ element , string $ property , int $ maxLength = -1 , int $ nodeType = XML_ELEMENT_NODE ):NodeList {
182+ $ nodes = new NodeList ;
173183
174184 if (in_array ($ property , ['parentNode ' , 'previousSibling ' , 'nextSibling ' ])){
175185
0 commit comments