@@ -378,9 +378,7 @@ private function initialize()
378
378
{
379
379
$ this ->fields = new FormFieldRegistry ();
380
380
381
- $ document = new \DOMDocument ('1.0 ' , 'UTF-8 ' );
382
- $ xpath = new \DOMXPath ($ document );
383
- $ root = $ document ->appendChild ($ document ->createElement ('_root ' ));
381
+ $ xpath = new \DOMXPath ($ this ->node ->ownerDocument );
384
382
385
383
// add submitted button if it has a valid name
386
384
if ('form ' !== $ this ->button ->nodeName && $ this ->button ->hasAttribute ('name ' ) && $ this ->button ->getAttribute ('name ' )) {
@@ -390,39 +388,33 @@ private function initialize()
390
388
391
389
// temporarily change the name of the input node for the x coordinate
392
390
$ this ->button ->setAttribute ('name ' , $ name .'.x ' );
393
- $ this ->set (new Field \InputFormField ($ document -> importNode ( $ this ->button , true ) ));
391
+ $ this ->set (new Field \InputFormField ($ this ->button ));
394
392
395
393
// temporarily change the name of the input node for the y coordinate
396
394
$ this ->button ->setAttribute ('name ' , $ name .'.y ' );
397
- $ this ->set (new Field \InputFormField ($ document -> importNode ( $ this ->button , true ) ));
395
+ $ this ->set (new Field \InputFormField ($ this ->button ));
398
396
399
397
// restore the original name of the input node
400
398
$ this ->button ->setAttribute ('name ' , $ name );
401
- }
402
- else {
403
- $ this ->set (new Field \InputFormField ($ document ->importNode ($ this ->button , true )));
399
+ } else {
400
+ $ this ->set (new Field \InputFormField ($ this ->button ));
404
401
}
405
402
}
406
403
407
404
// find form elements corresponding to the current form
408
405
if ($ this ->node ->hasAttribute ('id ' )) {
409
- // traverse through the whole document
410
- $ node = $ document ->importNode ($ this ->node ->ownerDocument ->documentElement , true );
411
- $ root ->appendChild ($ node );
412
-
413
406
// corresponding elements are either descendants or have a matching HTML5 form attribute
414
407
$ formId = Crawler::xpathLiteral ($ this ->node ->getAttribute ('id ' ));
415
- $ fieldNodes = $ xpath ->query (sprintf ('descendant::input[@form=%s] | descendant::button[@form=%s] | descendant::textarea[@form=%s] | descendant::select[@form=%s] | //form[@id=%s]//input[not(@form)] | //form[@id=%s]//button[not(@form)] | //form[@id=%s]//textarea[not(@form)] | //form[@id=%s]//select[not(@form)] ' , $ formId , $ formId , $ formId , $ formId , $ formId , $ formId , $ formId , $ formId ), $ root );
408
+
409
+ // do the xpath query without $this->node as the context node (i.e. traverse through the whole document)
410
+ $ fieldNodes = $ xpath ->query (sprintf ('descendant::input[@form=%s] | descendant::button[@form=%s] | descendant::textarea[@form=%s] | descendant::select[@form=%s] | //form[@id=%s]//input[not(@form)] | //form[@id=%s]//button[not(@form)] | //form[@id=%s]//textarea[not(@form)] | //form[@id=%s]//select[not(@form)] ' , $ formId , $ formId , $ formId , $ formId , $ formId , $ formId , $ formId , $ formId ));
416
411
foreach ($ fieldNodes as $ node ) {
417
412
$ this ->addField ($ node );
418
413
}
419
414
} else {
420
- // parent form has no id, add descendant elements only
421
- $ node = $ document ->importNode ($ this ->node , true );
422
- $ root ->appendChild ($ node );
423
-
424
- // descendant elements with form attribute are not part of this form
425
- $ fieldNodes = $ xpath ->query ('descendant::input[not(@form)] | descendant::button[not(@form)] | descendant::textarea[not(@form)] | descendant::select[not(@form)] ' , $ root );
415
+ // do the xpath query with $this->node as the context node, to only find descendant elements
416
+ // however, descendant elements with form attribute are not part of this form
417
+ $ fieldNodes = $ xpath ->query ('descendant::input[not(@form)] | descendant::button[not(@form)] | descendant::textarea[not(@form)] | descendant::select[not(@form)] ' , $ this ->node );
426
418
foreach ($ fieldNodes as $ node ) {
427
419
$ this ->addField ($ node );
428
420
}
0 commit comments