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, 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 "HTMLEmbedElement.h"
27 #include "Attribute.h"
28 #include "CSSPropertyNames.h"
29 #include "DocumentLoader.h"
31 #include "HTMLDocument.h"
32 #include "HTMLImageLoader.h"
33 #include "HTMLNames.h"
34 #include "HTMLObjectElement.h"
35 #include "HTMLParserIdioms.h"
36 #include "MainResourceLoader.h"
37 #include "PluginDocument.h"
38 #include "RenderEmbeddedObject.h"
39 #include "RenderImage.h"
40 #include "RenderWidget.h"
45 using namespace HTMLNames;
47 inline HTMLEmbedElement::HTMLEmbedElement(const QualifiedName& tagName, Document* document, bool createdByParser)
48 : HTMLPlugInImageElement(tagName, document, createdByParser, ShouldPreferPlugInsForImages)
50 ASSERT(hasTagName(embedTag));
53 PassRefPtr<HTMLEmbedElement> HTMLEmbedElement::create(const QualifiedName& tagName, Document* document, bool createdByParser)
55 return adoptRef(new HTMLEmbedElement(tagName, document, createdByParser));
58 static inline RenderWidget* findWidgetRenderer(const Node* n)
63 while (n && !n->hasTagName(objectTag));
65 if (n && n->renderer() && n->renderer()->isWidget())
66 return toRenderWidget(n->renderer());
71 RenderWidget* HTMLEmbedElement::renderWidgetForJSBindings()
73 document()->updateLayoutIgnorePendingStylesheets();
74 return findWidgetRenderer(this);
77 bool HTMLEmbedElement::mapToEntry(const QualifiedName& attrName, MappedAttributeEntry& result) const
79 if (attrName == hiddenAttr) {
84 return HTMLPlugInImageElement::mapToEntry(attrName, result);
87 void HTMLEmbedElement::parseMappedAttribute(Attribute* attr)
89 const AtomicString& value = attr->value();
91 if (attr->name() == typeAttr) {
92 m_serviceType = value.string().lower();
93 size_t pos = m_serviceType.find(";");
95 m_serviceType = m_serviceType.left(pos);
96 if (!isImageType() && m_imageLoader)
97 m_imageLoader.clear();
98 } else if (attr->name() == codeAttr)
99 m_url = stripLeadingAndTrailingHTMLSpaces(value.string());
100 else if (attr->name() == srcAttr) {
101 m_url = stripLeadingAndTrailingHTMLSpaces(value.string());
102 if (renderer() && isImageType()) {
104 m_imageLoader = adoptPtr(new HTMLImageLoader(this));
105 m_imageLoader->updateFromElementIgnoringPreviousError();
107 } else if (attr->name() == hiddenAttr) {
108 if (equalIgnoringCase(value.string(), "yes") || equalIgnoringCase(value.string(), "true")) {
109 // FIXME: Not dynamic, since we add this but don't remove it, but it may be OK for now
110 // that this rarely-used attribute won't work properly if you remove it.
111 addCSSLength(attr, CSSPropertyWidth, "0");
112 addCSSLength(attr, CSSPropertyHeight, "0");
114 } else if (attr->name() == nameAttr) {
115 if (inDocument() && document()->isHTMLDocument()) {
116 HTMLDocument* document = static_cast<HTMLDocument*>(this->document());
117 document->removeNamedItem(m_name);
118 document->addNamedItem(value);
122 HTMLPlugInImageElement::parseMappedAttribute(attr);
125 void HTMLEmbedElement::parametersForPlugin(Vector<String>& paramNames, Vector<String>& paramValues)
127 NamedNodeMap* attributes = this->attributes(true);
131 for (unsigned i = 0; i < attributes->length(); ++i) {
132 Attribute* it = attributes->attributeItem(i);
133 paramNames.append(it->localName().string());
134 paramValues.append(it->value().string());
138 // FIXME: This should be unified with HTMLObjectElement::updateWidget and
139 // moved down into HTMLPluginImageElement.cpp
140 void HTMLEmbedElement::updateWidget(PluginCreationOption pluginCreationOption)
142 ASSERT(!renderEmbeddedObject()->pluginCrashedOrWasMissing());
143 // FIXME: We should ASSERT(needsWidgetUpdate()), but currently
144 // FrameView::updateWidget() calls updateWidget(false) without checking if
145 // the widget actually needs updating!
146 setNeedsWidgetUpdate(false);
148 if (m_url.isEmpty() && m_serviceType.isEmpty())
151 // Note these pass m_url and m_serviceType to allow better code sharing with
152 // <object> which modifies url and serviceType before calling these.
153 if (!allowedToLoadFrameURL(m_url))
155 // FIXME: It's sadness that we have this special case here.
156 // See http://trac.webkit.org/changeset/25128 and
157 // plugins/netscape-plugin-setwindow-size.html
158 if (pluginCreationOption == CreateOnlyNonNetscapePlugins && wouldLoadAsNetscapePlugin(m_url, m_serviceType))
161 // FIXME: These should be joined into a PluginParameters class.
162 Vector<String> paramNames;
163 Vector<String> paramValues;
164 parametersForPlugin(paramNames, paramValues);
166 ASSERT(!m_inBeforeLoadEventHandler);
167 m_inBeforeLoadEventHandler = true;
168 bool beforeLoadAllowedLoad = dispatchBeforeLoadEvent(m_url);
169 m_inBeforeLoadEventHandler = false;
171 if (!beforeLoadAllowedLoad) {
172 if (document()->isPluginDocument()) {
173 // Plugins inside plugin documents load differently than other plugins. By the time
174 // we are here in a plugin document, the load of the plugin (which is the plugin document's
175 // main resource) has already started. We need to explicitly cancel the main resource load here.
176 toPluginDocument(document())->cancelManualPluginLoad();
181 RefPtr<HTMLEmbedElement> protect(this); // Loading the plugin might remove us from the document.
182 SubframeLoader* loader = document()->frame()->loader()->subframeLoader();
183 // FIXME: beforeLoad could have detached the renderer! Just like in the <object> case above.
184 loader->requestObject(this, m_url, getAttribute(nameAttr), m_serviceType, paramNames, paramValues);
187 bool HTMLEmbedElement::rendererIsNeeded(const NodeRenderingContext& context)
190 return HTMLPlugInImageElement::rendererIsNeeded(context);
192 Frame* frame = document()->frame();
196 // If my parent is an <object> and is not set to use fallback content, I
197 // should be ignored and not get a renderer.
198 ContainerNode* p = parentNode();
199 if (p && p->hasTagName(objectTag)) {
200 ASSERT(p->renderer());
201 if (!static_cast<HTMLObjectElement*>(p)->useFallbackContent()) {
202 ASSERT(!p->renderer()->isEmbeddedObject());
207 #if ENABLE(DASHBOARD_SUPPORT)
208 // Workaround for <rdar://problem/6642221>.
209 if (Settings* settings = frame->settings()) {
210 if (settings->usesDashboardBackwardCompatibilityMode())
215 return HTMLPlugInImageElement::rendererIsNeeded(context);
218 void HTMLEmbedElement::insertedIntoDocument()
220 HTMLPlugInImageElement::insertedIntoDocument();
224 if (document()->isHTMLDocument())
225 static_cast<HTMLDocument*>(document())->addNamedItem(m_name);
227 String width = getAttribute(widthAttr);
228 String height = getAttribute(heightAttr);
229 if (!width.isEmpty() || !height.isEmpty()) {
230 Node* n = parentNode();
231 while (n && !n->hasTagName(objectTag))
234 if (!width.isEmpty())
235 static_cast<HTMLObjectElement*>(n)->setAttribute(widthAttr, width);
236 if (!height.isEmpty())
237 static_cast<HTMLObjectElement*>(n)->setAttribute(heightAttr, height);
242 void HTMLEmbedElement::removedFromDocument()
244 if (document()->isHTMLDocument())
245 static_cast<HTMLDocument*>(document())->removeNamedItem(m_name);
247 HTMLPlugInImageElement::removedFromDocument();
250 void HTMLEmbedElement::attributeChanged(Attribute* attr, bool preserveDecls)
252 HTMLPlugInImageElement::attributeChanged(attr, preserveDecls);
254 if ((attr->name() == widthAttr || attr->name() == heightAttr) && !attr->isEmpty()) {
255 ContainerNode* n = parentNode();
256 while (n && !n->hasTagName(objectTag))
259 static_cast<HTMLObjectElement*>(n)->setAttribute(attr->name(), attr->value());
263 bool HTMLEmbedElement::isURLAttribute(Attribute* attr) const
265 return attr->name() == srcAttr || HTMLPlugInImageElement::isURLAttribute(attr);
268 const QualifiedName& HTMLEmbedElement::imageSourceAttributeName() const
273 void HTMLEmbedElement::addSubresourceAttributeURLs(ListHashSet<KURL>& urls) const
275 HTMLPlugInImageElement::addSubresourceAttributeURLs(urls);
277 addSubresourceURL(urls, document()->completeURL(getAttribute(srcAttr)));
280 #if ENABLE(MICRODATA)
281 String HTMLEmbedElement::itemValueText() const
283 return getURLAttribute(srcAttr);
286 void HTMLEmbedElement::setItemValueText(const String& value, ExceptionCode& ec)
288 setAttribute(srcAttr, value, ec);