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