@@ -475,12 +475,17 @@ protected ScriptableObject getOwnPropertyDescriptor(final Context cx, final Obje
475475
476476 List <HtmlElement > findElements (final String name ) {
477477 final List <HtmlElement > elements = new ArrayList <>();
478- addElements (name , getHtmlForm ().getHtmlElementDescendants (), elements );
479- addElements (name , getHtmlForm ().getLostChildren (), elements );
478+ final HtmlForm form = (HtmlForm ) getDomNodeOrNull ();
479+ if (form == null ) {
480+ return elements ;
481+ }
482+
483+ addElements (name , form .getHtmlElementDescendants (), elements );
484+ addElements (name , form .getLostChildren (), elements );
480485
481486 // If no form fields are found, browsers are able to find img elements by ID or name.
482487 if (elements .isEmpty ()) {
483- for (final DomNode node : getHtmlForm () .getHtmlElementDescendants ()) {
488+ for (final DomNode node : form .getHtmlElementDescendants ()) {
484489 if (node instanceof HtmlImage ) {
485490 final HtmlImage img = (HtmlImage ) node ;
486491 if (name .equals (img .getId ()) || name .equals (img .getNameAttribute ())) {
@@ -503,20 +508,25 @@ private void addElements(final String name, final Iterable<HtmlElement> nodes,
503508 }
504509
505510 private HtmlElement findFirstElement (final String name ) {
506- for (final HtmlElement node : getHtmlForm ().getHtmlElementDescendants ()) {
511+ final HtmlForm form = (HtmlForm ) getDomNodeOrNull ();
512+ if (form == null ) {
513+ return null ;
514+ }
515+
516+ for (final HtmlElement node : form .getHtmlElementDescendants ()) {
507517 if (isAccessibleByIdOrName (node , name )) {
508518 return node ;
509519 }
510520 }
511521
512- for (final HtmlElement node : getHtmlForm () .getLostChildren ()) {
522+ for (final HtmlElement node : form .getLostChildren ()) {
513523 if (isAccessibleByIdOrName (node , name )) {
514524 return node ;
515525 }
516526 }
517527
518528 // If no form fields are found, browsers are able to find img elements by ID or name.
519- for (final DomNode node : getHtmlForm () .getHtmlElementDescendants ()) {
529+ for (final DomNode node : form .getHtmlElementDescendants ()) {
520530 if (node instanceof HtmlImage ) {
521531 final HtmlImage img = (HtmlImage ) node ;
522532 if (name .equals (img .getId ()) || name .equals (img .getNameAttribute ())) {
0 commit comments