1/*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 *           (C) 1999 Antti Koivisto (koivisto@kde.org)
4 *           (C) 2000 Simon Hausmann (hausmann@kde.org)
5 *           (C) 2001 Dirk Mueller (mueller@kde.org)
6 * Copyright (C) 2004, 2006, 2008, 2009 Apple Inc. All rights reserved.
7 * Copyright (C) 2009 Ericsson AB. All rights reserved.
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * Library General Public License for more details.
18 *
19 * You should have received a copy of the GNU Library General Public License
20 * along with this library; see the file COPYING.LIB.  If not, write to
21 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
23 */
24
25#include "config.h"
26#include "HTMLIFrameElement.h"
27
28#include "Attribute.h"
29#include "CSSPropertyNames.h"
30#include "Frame.h"
31#include "HTMLDocument.h"
32#include "HTMLNames.h"
33#include "NodeRenderingContext.h"
34#include "RenderIFrame.h"
35#include "ScriptableDocumentParser.h"
36#include <wtf/text/TextPosition.h>
37
38namespace WebCore {
39
40using namespace HTMLNames;
41
42inline HTMLIFrameElement::HTMLIFrameElement(const QualifiedName& tagName, Document* document)
43    : HTMLFrameElementBase(tagName, document)
44{
45    ASSERT(hasTagName(iframeTag));
46    setHasCustomStyleCallbacks();
47}
48
49PassRefPtr<HTMLIFrameElement> HTMLIFrameElement::create(const QualifiedName& tagName, Document* document)
50{
51    return adoptRef(new HTMLIFrameElement(tagName, document));
52}
53
54bool HTMLIFrameElement::isPresentationAttribute(const QualifiedName& name) const
55{
56    if (name == widthAttr || name == heightAttr || name == alignAttr || name == frameborderAttr || name == seamlessAttr)
57        return true;
58    return HTMLFrameElementBase::isPresentationAttribute(name);
59}
60
61void HTMLIFrameElement::collectStyleForPresentationAttribute(const QualifiedName& name, const AtomicString& value, MutableStylePropertySet* style)
62{
63    if (name == widthAttr)
64        addHTMLLengthToStyle(style, CSSPropertyWidth, value);
65    else if (name == heightAttr)
66        addHTMLLengthToStyle(style, CSSPropertyHeight, value);
67    else if (name == alignAttr)
68        applyAlignmentAttributeToStyle(value, style);
69    else if (name == frameborderAttr) {
70        // Frame border doesn't really match the HTML4 spec definition for iframes. It simply adds
71        // a presentational hint that the border should be off if set to zero.
72        if (!value.toInt()) {
73            // Add a rule that nulls out our border width.
74            addPropertyToPresentationAttributeStyle(style, CSSPropertyBorderWidth, 0, CSSPrimitiveValue::CSS_PX);
75        }
76    } else
77        HTMLFrameElementBase::collectStyleForPresentationAttribute(name, value, style);
78}
79
80void HTMLIFrameElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
81{
82    if (name == sandboxAttr) {
83        String invalidTokens;
84        setSandboxFlags(value.isNull() ? SandboxNone : SecurityContext::parseSandboxPolicy(value, invalidTokens));
85        if (!invalidTokens.isNull())
86            document()->addConsoleMessage(OtherMessageSource, ErrorMessageLevel, "Error while parsing the 'sandbox' attribute: " + invalidTokens);
87    } else if (name == seamlessAttr) {
88        // If we're adding or removing the seamless attribute, we need to force the content document to recalculate its StyleResolver.
89        if (contentDocument())
90            contentDocument()->styleResolverChanged(DeferRecalcStyle);
91    } else
92        HTMLFrameElementBase::parseAttribute(name, value);
93}
94
95bool HTMLIFrameElement::rendererIsNeeded(const NodeRenderingContext& context)
96{
97    return isURLAllowed() && context.style()->display() != NONE;
98}
99
100RenderObject* HTMLIFrameElement::createRenderer(RenderArena* arena, RenderStyle*)
101{
102    return new (arena) RenderIFrame(this);
103}
104
105bool HTMLIFrameElement::shouldDisplaySeamlessly() const
106{
107    return contentDocument() && contentDocument()->shouldDisplaySeamlesslyWithParent();
108}
109
110void HTMLIFrameElement::didRecalcStyle(StyleChange styleChange)
111{
112    if (!shouldDisplaySeamlessly())
113        return;
114    Document* childDocument = contentDocument();
115    if (styleChange >= Inherit || childDocument->childNeedsStyleRecalc() || childDocument->needsStyleRecalc())
116        contentDocument()->recalcStyle(styleChange);
117}
118
119#if ENABLE(MICRODATA)
120String HTMLIFrameElement::itemValueText() const
121{
122    return getURLAttribute(srcAttr);
123}
124
125void HTMLIFrameElement::setItemValueText(const String& value, ExceptionCode&)
126{
127    setAttribute(srcAttr, value);
128}
129#endif
130
131}
132