2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2000 Stefan Schimanski (1Stein@gmx.de)
5 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2011 Apple Inc. All rights reserved.
6 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
25 #include "HTMLObjectElement.h"
27 #include "Attribute.h"
28 #include "CSSValueKeywords.h"
29 #include "EventNames.h"
30 #include "ExceptionCode.h"
31 #include "FormDataList.h"
33 #include "HTMLDocument.h"
34 #include "HTMLFormElement.h"
35 #include "HTMLImageLoader.h"
36 #include "HTMLMetaElement.h"
37 #include "HTMLNames.h"
38 #include "HTMLParamElement.h"
39 #include "HTMLParserIdioms.h"
40 #include "MIMETypeRegistry.h"
43 #include "PluginViewBase.h"
44 #include "RenderEmbeddedObject.h"
45 #include "RenderImage.h"
46 #include "RenderWidget.h"
47 #include "ScriptEventListener.h"
54 using namespace HTMLNames;
56 inline HTMLObjectElement::HTMLObjectElement(const QualifiedName& tagName, Document* document, HTMLFormElement* form, bool createdByParser)
57 : HTMLPlugInImageElement(tagName, document, createdByParser, ShouldNotPreferPlugInsForImages)
58 , FormAssociatedElement(form)
59 , m_docNamedItem(true)
60 , m_useFallbackContent(false)
62 ASSERT(hasTagName(objectTag));
64 setForm(findFormAncestor());
66 this->form()->registerFormElement(this);
69 inline HTMLObjectElement::~HTMLObjectElement()
72 form()->removeFormElement(this);
75 PassRefPtr<HTMLObjectElement> HTMLObjectElement::create(const QualifiedName& tagName, Document* document, HTMLFormElement* form, bool createdByParser)
77 return adoptRef(new HTMLObjectElement(tagName, document, form, createdByParser));
80 RenderWidget* HTMLObjectElement::renderWidgetForJSBindings()
82 document()->updateLayoutIgnorePendingStylesheets();
83 return renderPart(); // This will return 0 if the renderer is not a RenderPart.
86 void HTMLObjectElement::parseMappedAttribute(Attribute* attr)
88 if (attr->name() == formAttr)
89 formAttributeChanged();
90 else if (attr->name() == typeAttr) {
91 m_serviceType = attr->value().lower();
92 size_t pos = m_serviceType.find(";");
94 m_serviceType = m_serviceType.left(pos);
96 setNeedsWidgetUpdate(true);
97 if (!isImageType() && m_imageLoader)
98 m_imageLoader.clear();
99 } else if (attr->name() == dataAttr) {
100 m_url = stripLeadingAndTrailingHTMLSpaces(attr->value());
102 setNeedsWidgetUpdate(true);
105 m_imageLoader = adoptPtr(new HTMLImageLoader(this));
106 m_imageLoader->updateFromElementIgnoringPreviousError();
109 } else if (attr->name() == classidAttr) {
110 m_classId = attr->value();
112 setNeedsWidgetUpdate(true);
113 } else if (attr->name() == onloadAttr)
114 setAttributeEventListener(eventNames().loadEvent, createAttributeEventListener(this, attr));
115 else if (attr->name() == onbeforeloadAttr)
116 setAttributeEventListener(eventNames().beforeloadEvent, createAttributeEventListener(this, attr));
117 else if (attr->name() == nameAttr) {
118 const AtomicString& newName = attr->value();
119 if (isDocNamedItem() && inDocument() && document()->isHTMLDocument()) {
120 HTMLDocument* document = static_cast<HTMLDocument*>(this->document());
121 document->removeNamedItem(m_name);
122 document->addNamedItem(newName);
125 } else if (attr->name() == borderAttr)
126 applyBorderAttribute(attr);
127 else if (isIdAttributeName(attr->name())) {
128 const AtomicString& newId = attr->value();
129 if (isDocNamedItem() && inDocument() && document()->isHTMLDocument()) {
130 HTMLDocument* document = static_cast<HTMLDocument*>(this->document());
131 document->removeExtraNamedItem(m_id);
132 document->addExtraNamedItem(newId);
135 // also call superclass
136 HTMLPlugInImageElement::parseMappedAttribute(attr);
138 HTMLPlugInImageElement::parseMappedAttribute(attr);
141 static void mapDataParamToSrc(Vector<String>* paramNames, Vector<String>* paramValues)
143 // Some plugins don't understand the "data" attribute of the OBJECT tag (i.e. Real and WMP
144 // require "src" attribute).
145 int srcIndex = -1, dataIndex = -1;
146 for (unsigned int i = 0; i < paramNames->size(); ++i) {
147 if (equalIgnoringCase((*paramNames)[i], "src"))
149 else if (equalIgnoringCase((*paramNames)[i], "data"))
153 if (srcIndex == -1 && dataIndex != -1) {
154 paramNames->append("src");
155 paramValues->append((*paramValues)[dataIndex]);
159 // FIXME: This function should not deal with url or serviceType!
160 void HTMLObjectElement::parametersForPlugin(Vector<String>& paramNames, Vector<String>& paramValues, String& url, String& serviceType)
162 HashSet<StringImpl*, CaseFoldingHash> uniqueParamNames;
165 // Scan the PARAM children and store their name/value pairs.
166 // Get the URL and type from the params if we don't already have them.
167 for (Node* child = firstChild(); child; child = child->nextSibling()) {
168 if (!child->hasTagName(paramTag))
171 HTMLParamElement* p = static_cast<HTMLParamElement*>(child);
172 String name = p->name();
176 uniqueParamNames.add(name.impl());
177 paramNames.append(p->name());
178 paramValues.append(p->value());
180 // FIXME: url adjustment does not belong in this function.
181 if (url.isEmpty() && urlParameter.isEmpty() && (equalIgnoringCase(name, "src") || equalIgnoringCase(name, "movie") || equalIgnoringCase(name, "code") || equalIgnoringCase(name, "url")))
182 urlParameter = stripLeadingAndTrailingHTMLSpaces(p->value());
183 // FIXME: serviceType calculation does not belong in this function.
184 if (serviceType.isEmpty() && equalIgnoringCase(name, "type")) {
185 serviceType = p->value();
186 size_t pos = serviceType.find(";");
188 serviceType = serviceType.left(pos);
192 // When OBJECT is used for an applet via Sun's Java plugin, the CODEBASE attribute in the tag
193 // points to the Java plugin itself (an ActiveX component) while the actual applet CODEBASE is
194 // in a PARAM tag. See <http://java.sun.com/products/plugin/1.2/docs/tags.html>. This means
195 // we have to explicitly suppress the tag's CODEBASE attribute if there is none in a PARAM,
196 // else our Java plugin will misinterpret it. [4004531]
198 if (MIMETypeRegistry::isJavaAppletMIMEType(serviceType)) {
199 codebase = "codebase";
200 uniqueParamNames.add(codebase.impl()); // pretend we found it in a PARAM already
203 // Turn the attributes of the <object> element into arrays, but don't override <param> values.
204 NamedNodeMap* attributes = this->attributes(true);
206 for (unsigned i = 0; i < attributes->length(); ++i) {
207 Attribute* it = attributes->attributeItem(i);
208 const AtomicString& name = it->name().localName();
209 if (!uniqueParamNames.contains(name.impl())) {
210 paramNames.append(name.string());
211 paramValues.append(it->value().string());
216 mapDataParamToSrc(¶mNames, ¶mValues);
218 // HTML5 says that an object resource's URL is specified by the object's data
219 // attribute, not by a param element. However, for compatibility, allow the
220 // resource's URL to be given by a param named "src", "movie", "code" or "url"
221 // if we know that resource points to a plug-in.
222 if (url.isEmpty() && !urlParameter.isEmpty()) {
223 SubframeLoader* loader = document()->frame()->loader()->subframeLoader();
224 if (loader->resourceWillUsePlugin(urlParameter, serviceType, shouldPreferPlugInsForImages()))
230 bool HTMLObjectElement::hasFallbackContent() const
232 for (Node* child = firstChild(); child; child = child->nextSibling()) {
233 // Ignore whitespace-only text, and <param> tags, any other content is fallback content.
234 if (child->isTextNode()) {
235 if (!static_cast<Text*>(child)->containsOnlyWhitespace())
237 } else if (!child->hasTagName(paramTag))
243 bool HTMLObjectElement::shouldAllowQuickTimeClassIdQuirk()
245 // This site-specific hack maintains compatibility with Mac OS X Wiki Server,
246 // which embeds QuickTime movies using an object tag containing QuickTime's
247 // ActiveX classid. Treat this classid as valid only if OS X Server's unique
248 // 'generator' meta tag is present. Only apply this quirk if there is no
249 // fallback content, which ensures the quirk will disable itself if Wiki
250 // Server is updated to generate an alternate embed tag as fallback content.
251 if (!document()->page()
252 || !document()->page()->settings()->needsSiteSpecificQuirks()
253 || hasFallbackContent()
254 || !equalIgnoringCase(classId(), "clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"))
257 RefPtr<NodeList> metaElements = document()->getElementsByTagName(HTMLNames::metaTag.localName());
258 unsigned length = metaElements->length();
259 for (unsigned i = 0; i < length; ++i) {
260 ASSERT(metaElements->item(i)->isHTMLElement());
261 HTMLMetaElement* metaElement = static_cast<HTMLMetaElement*>(metaElements->item(i));
262 if (equalIgnoringCase(metaElement->name(), "generator") && metaElement->content().startsWith("Mac OS X Server Web Services Server", false))
269 bool HTMLObjectElement::hasValidClassId()
272 if (equalIgnoringCase(serviceType(), "application/x-qt-plugin") || equalIgnoringCase(serviceType(), "application/x-qt-styled-widget"))
276 if (MIMETypeRegistry::isJavaAppletMIMEType(serviceType()) && classId().startsWith("java:", false))
279 if (shouldAllowQuickTimeClassIdQuirk())
282 // HTML5 says that fallback content should be rendered if a non-empty
283 // classid is specified for which the UA can't find a suitable plug-in.
284 return classId().isEmpty();
287 // FIXME: This should be unified with HTMLEmbedElement::updateWidget and
288 // moved down into HTMLPluginImageElement.cpp
289 void HTMLObjectElement::updateWidget(PluginCreationOption pluginCreationOption)
291 ASSERT(!renderEmbeddedObject()->pluginCrashedOrWasMissing());
292 // FIXME: We should ASSERT(needsWidgetUpdate()), but currently
293 // FrameView::updateWidget() calls updateWidget(false) without checking if
294 // the widget actually needs updating!
295 setNeedsWidgetUpdate(false);
296 // FIXME: This should ASSERT isFinishedParsingChildren() instead.
297 if (!isFinishedParsingChildren())
300 String url = this->url();
301 String serviceType = this->serviceType();
303 // FIXME: These should be joined into a PluginParameters class.
304 Vector<String> paramNames;
305 Vector<String> paramValues;
306 parametersForPlugin(paramNames, paramValues, url, serviceType);
308 // Note: url is modified above by parametersForPlugin.
309 if (!allowedToLoadFrameURL(url))
312 bool fallbackContent = hasFallbackContent();
313 renderEmbeddedObject()->setHasFallbackContent(fallbackContent);
315 if (pluginCreationOption == CreateOnlyNonNetscapePlugins && wouldLoadAsNetscapePlugin(url, serviceType))
318 ASSERT(!m_inBeforeLoadEventHandler);
319 m_inBeforeLoadEventHandler = true;
320 bool beforeLoadAllowedLoad = dispatchBeforeLoadEvent(url);
321 m_inBeforeLoadEventHandler = false;
323 // beforeload events can modify the DOM, potentially causing
324 // RenderWidget::destroy() to be called. Ensure we haven't been
325 // destroyed before continuing.
326 // FIXME: Should this render fallback content?
330 SubframeLoader* loader = document()->frame()->loader()->subframeLoader();
331 bool success = beforeLoadAllowedLoad && hasValidClassId() && loader->requestObject(this, url, getAttribute(nameAttr), serviceType, paramNames, paramValues);
333 if (!success && fallbackContent)
334 renderFallbackContent();
337 bool HTMLObjectElement::rendererIsNeeded(const NodeRenderingContext& context)
339 // FIXME: This check should not be needed, detached documents never render!
340 Frame* frame = document()->frame();
344 return HTMLPlugInImageElement::rendererIsNeeded(context);
347 void HTMLObjectElement::insertedIntoDocument()
349 HTMLPlugInImageElement::insertedIntoDocument();
353 if (isDocNamedItem() && document()->isHTMLDocument()) {
354 HTMLDocument* document = static_cast<HTMLDocument*>(this->document());
355 document->addNamedItem(m_name);
356 document->addExtraNamedItem(m_id);
359 FormAssociatedElement::insertedIntoDocument();
362 void HTMLObjectElement::removedFromDocument()
364 if (isDocNamedItem() && document()->isHTMLDocument()) {
365 HTMLDocument* document = static_cast<HTMLDocument*>(this->document());
366 document->removeNamedItem(m_name);
367 document->removeExtraNamedItem(m_id);
370 HTMLPlugInImageElement::removedFromDocument();
371 FormAssociatedElement::removedFromDocument();
374 void HTMLObjectElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
376 updateDocNamedItem();
377 if (inDocument() && !useFallbackContent()) {
378 setNeedsWidgetUpdate(true);
379 setNeedsStyleRecalc();
381 HTMLPlugInImageElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
384 bool HTMLObjectElement::isURLAttribute(Attribute *attr) const
386 return (attr->name() == dataAttr || (attr->name() == usemapAttr && attr->value().string()[0] != '#'));
389 const QualifiedName& HTMLObjectElement::imageSourceAttributeName() const
394 void HTMLObjectElement::renderFallbackContent()
396 if (useFallbackContent())
402 // Before we give up and use fallback content, check to see if this is a MIME type issue.
403 if (m_imageLoader && m_imageLoader->image() && m_imageLoader->image()->status() != CachedResource::LoadError) {
404 m_serviceType = m_imageLoader->image()->response().mimeType();
405 if (!isImageType()) {
406 // If we don't think we have an image type anymore, then clear the image from the loader.
407 m_imageLoader->setImage(0);
413 m_useFallbackContent = true;
415 // FIXME: Style gets recalculated which is suboptimal.
420 // FIXME: This should be removed, all callers are almost certainly wrong.
421 static bool isRecognizedTagName(const QualifiedName& tagName)
423 DEFINE_STATIC_LOCAL(HashSet<AtomicStringImpl*>, tagList, ());
424 if (tagList.isEmpty()) {
426 QualifiedName** tags = HTMLNames::getHTMLTags(&tagCount);
427 for (size_t i = 0; i < tagCount; i++) {
428 if (*tags[i] == bgsoundTag
429 || *tags[i] == commandTag
430 || *tags[i] == detailsTag
431 || *tags[i] == figcaptionTag
432 || *tags[i] == figureTag
433 || *tags[i] == summaryTag
434 || *tags[i] == trackTag) {
435 // Even though we have atoms for these tags, we don't want to
436 // treat them as "recognized tags" for the purpose of parsing
437 // because that changes how we parse documents.
440 tagList.add(tags[i]->localName().impl());
443 return tagList.contains(tagName.localName().impl());
446 void HTMLObjectElement::updateDocNamedItem()
448 // The rule is "<object> elements with no children other than
449 // <param> elements, unknown elements and whitespace can be
450 // found by name in a document, and other <object> elements cannot."
451 bool wasNamedItem = m_docNamedItem;
452 bool isNamedItem = true;
453 Node* child = firstChild();
454 while (child && isNamedItem) {
455 if (child->isElementNode()) {
456 Element* element = static_cast<Element*>(child);
457 // FIXME: Use of isRecognizedTagName is almost certainly wrong here.
458 if (isRecognizedTagName(element->tagQName()) && !element->hasTagName(paramTag))
460 } else if (child->isTextNode()) {
461 if (!static_cast<Text*>(child)->containsOnlyWhitespace())
465 child = child->nextSibling();
467 if (isNamedItem != wasNamedItem && document()->isHTMLDocument()) {
468 HTMLDocument* document = static_cast<HTMLDocument*>(this->document());
470 document->addNamedItem(m_name);
471 document->addExtraNamedItem(m_id);
473 document->removeNamedItem(m_name);
474 document->removeExtraNamedItem(m_id);
477 m_docNamedItem = isNamedItem;
480 bool HTMLObjectElement::containsJavaApplet() const
482 if (MIMETypeRegistry::isJavaAppletMIMEType(getAttribute(typeAttr)))
485 for (Element* child = firstElementChild(); child; child = child->nextElementSibling()) {
486 if (child->hasTagName(paramTag)
487 && equalIgnoringCase(child->getAttribute(nameAttr), "type")
488 && MIMETypeRegistry::isJavaAppletMIMEType(child->getAttribute(valueAttr).string()))
490 if (child->hasTagName(objectTag)
491 && static_cast<HTMLObjectElement*>(child)->containsJavaApplet())
493 if (child->hasTagName(appletTag))
500 void HTMLObjectElement::addSubresourceAttributeURLs(ListHashSet<KURL>& urls) const
502 HTMLPlugInImageElement::addSubresourceAttributeURLs(urls);
504 addSubresourceURL(urls, document()->completeURL(getAttribute(dataAttr)));
506 // FIXME: Passing a string that starts with "#" to the completeURL function does
507 // not seem like it would work. The image element has similar but not identical code.
508 const AtomicString& useMap = getAttribute(usemapAttr);
509 if (useMap.startsWith("#"))
510 addSubresourceURL(urls, document()->completeURL(useMap));
513 void HTMLObjectElement::willMoveToNewOwnerDocument()
515 FormAssociatedElement::willMoveToNewOwnerDocument();
516 HTMLPlugInImageElement::willMoveToNewOwnerDocument();
519 void HTMLObjectElement::insertedIntoTree(bool deep)
521 FormAssociatedElement::insertedIntoTree();
522 HTMLPlugInImageElement::insertedIntoTree(deep);
525 void HTMLObjectElement::removedFromTree(bool deep)
527 FormAssociatedElement::removedFromTree();
528 HTMLPlugInImageElement::removedFromTree(deep);
531 bool HTMLObjectElement::appendFormData(FormDataList& encoding, bool)
533 if (name().isEmpty())
536 Widget* widget = pluginWidget();
537 if (!widget || !widget->isPluginViewBase())
540 if (!static_cast<PluginViewBase*>(widget)->getFormValue(value))
542 encoding.appendData(name(), value);
546 const AtomicString& HTMLObjectElement::formControlName() const
548 return m_name.isNull() ? emptyAtom : m_name;
551 HTMLFormElement* HTMLObjectElement::virtualForm() const
553 return FormAssociatedElement::form();