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