diff --git a/content/html/document/src/nsHTMLDocument.cpp b/content/html/document/src/nsHTMLDocument.cpp
index e51b33f7360..2215b50e93b 100644
--- a/content/html/document/src/nsHTMLDocument.cpp
+++ b/content/html/document/src/nsHTMLDocument.cpp
@@ -3724,10 +3724,11 @@ nsHTMLDocument::ResolveName(const nsAString& aName,
if (e && e != ID_NOT_IN_DOCUMENT && e->IsContentOfType(nsIContent::eHTML)) {
nsIAtom *tag = e->Tag();
- if (tag == nsHTMLAtoms::embed ||
- tag == nsHTMLAtoms::img ||
- tag == nsHTMLAtoms::object ||
- tag == nsHTMLAtoms::applet) {
+ if ((tag == nsHTMLAtoms::embed ||
+ tag == nsHTMLAtoms::img ||
+ tag == nsHTMLAtoms::object ||
+ tag == nsHTMLAtoms::applet) &&
+ (!aForm || nsContentUtils::BelongsInForm(aForm, e))) {
NS_ADDREF(*aResult = e);
}
}