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 "RenderIFrame.h"
34
35namespace WebCore {
36
37using namespace HTMLNames;
38
39inline HTMLIFrameElement::HTMLIFrameElement(const QualifiedName& tagName, Document* document)
40    : HTMLFrameElementBase(tagName, document)
41{
42    ASSERT(hasTagName(iframeTag));
43}
44
45PassRefPtr<HTMLIFrameElement> HTMLIFrameElement::create(const QualifiedName& tagName, Document* document)
46{
47    return adoptRef(new HTMLIFrameElement(tagName, document));
48}
49
50bool HTMLIFrameElement::mapToEntry(const QualifiedName& attrName, MappedAttributeEntry& result) const
51{
52    if (attrName == widthAttr || attrName == heightAttr) {
53        result = eUniversal;
54        return false;
55    }
56
57    if (attrName == alignAttr) {
58        result = eReplaced; // Share with <img> since the alignment behavior is the same.
59        return false;
60    }
61
62    if (attrName == frameborderAttr) {
63        result = eReplaced;
64        return false;
65    }
66
67    return HTMLFrameElementBase::mapToEntry(attrName, result);
68}
69
70static SandboxFlags parseSandboxAttribute(Attribute* attribute)
71{
72    if (attribute->isNull())
73        return SandboxNone;
74
75    // Parse the unordered set of unique space-separated tokens.
76    SandboxFlags flags = SandboxAll;
77    const UChar* characters = attribute->value().characters();
78    unsigned length = attribute->value().length();
79    unsigned start = 0;
80    while (true) {
81        while (start < length && isASCIISpace(characters[start]))
82            ++start;
83        if (start >= length)
84            break;
85        unsigned end = start + 1;
86        while (end < length && !isASCIISpace(characters[end]))
87            ++end;
88
89        // Turn off the corresponding sandbox flag if it's set as "allowed".
90        String sandboxToken = String(characters + start, end - start);
91        if (equalIgnoringCase(sandboxToken, "allow-same-origin"))
92            flags &= ~SandboxOrigin;
93        else if (equalIgnoringCase(sandboxToken, "allow-forms"))
94            flags &= ~SandboxForms;
95        else if (equalIgnoringCase(sandboxToken, "allow-scripts"))
96            flags &= ~SandboxScripts;
97        else if (equalIgnoringCase(sandboxToken, "allow-top-navigation"))
98            flags &= ~SandboxTopNavigation;
99
100        start = end + 1;
101    }
102
103    return flags;
104}
105
106void HTMLIFrameElement::parseMappedAttribute(Attribute* attr)
107{
108    if (attr->name() == widthAttr)
109        addCSSLength(attr, CSSPropertyWidth, attr->value());
110    else if (attr->name() == heightAttr)
111        addCSSLength(attr, CSSPropertyHeight, attr->value());
112    else if (attr->name() == alignAttr)
113        addHTMLAlignment(attr);
114    else if (attr->name() == nameAttr) {
115        const AtomicString& newName = attr->value();
116        if (inDocument() && document()->isHTMLDocument()) {
117            HTMLDocument* document = static_cast<HTMLDocument*>(this->document());
118            document->removeExtraNamedItem(m_name);
119            document->addExtraNamedItem(newName);
120        }
121        m_name = newName;
122    } else if (attr->name() == frameborderAttr) {
123        // Frame border doesn't really match the HTML4 spec definition for iframes.  It simply adds
124        // a presentational hint that the border should be off if set to zero.
125        if (!attr->isNull() && !attr->value().toInt())
126            // Add a rule that nulls out our border width.
127            addCSSLength(attr, CSSPropertyBorderWidth, "0");
128    } else if (attr->name() == sandboxAttr)
129        setSandboxFlags(parseSandboxAttribute(attr));
130    else
131        HTMLFrameElementBase::parseMappedAttribute(attr);
132}
133
134bool HTMLIFrameElement::rendererIsNeeded(RenderStyle* style)
135{
136    return isURLAllowed() && style->display() != NONE;
137}
138
139RenderObject* HTMLIFrameElement::createRenderer(RenderArena* arena, RenderStyle*)
140{
141    return new (arena) RenderIFrame(this);
142}
143
144void HTMLIFrameElement::insertedIntoDocument()
145{
146    if (document()->isHTMLDocument())
147        static_cast<HTMLDocument*>(document())->addExtraNamedItem(m_name);
148
149    HTMLFrameElementBase::insertedIntoDocument();
150}
151
152void HTMLIFrameElement::removedFromDocument()
153{
154    if (document()->isHTMLDocument())
155        static_cast<HTMLDocument*>(document())->removeExtraNamedItem(m_name);
156
157    HTMLFrameElementBase::removedFromDocument();
158}
159
160bool HTMLIFrameElement::isURLAttribute(Attribute* attr) const
161{
162    return attr->name() == srcAttr;
163}
164
165}
166