Home | History | Annotate | Download | only in html
      1 /*
      2  * Copyright (C) 1999 Lars Knoll (knoll (at) kde.org)
      3  *           (C) 1999 Antti Koivisto (koivisto (at) kde.org)
      4  *           (C) 2000 Simon Hausmann (hausmann (at) kde.org)
      5  *           (C) 2001 Dirk Mueller (mueller (at) kde.org)
      6  * Copyright (C) 2004, 2006, 2008, 2009 Apple Inc. All rights reserved.
      7  *
      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.
     12  *
     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.
     17  *
     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.
     22  */
     23 
     24 #include "config.h"
     25 #include "core/html/HTMLFrameElementBase.h"
     26 
     27 #include "HTMLNames.h"
     28 #include "bindings/v8/ScriptController.h"
     29 #include "bindings/v8/ScriptEventListener.h"
     30 #include "core/dom/Attribute.h"
     31 #include "core/dom/Document.h"
     32 #include "core/dom/EventNames.h"
     33 #include "core/html/parser/HTMLParserIdioms.h"
     34 #include "core/loader/FrameLoader.h"
     35 #include "core/page/FocusController.h"
     36 #include "core/page/Frame.h"
     37 #include "core/page/FrameView.h"
     38 #include "core/page/Page.h"
     39 #include "core/rendering/RenderPart.h"
     40 
     41 namespace WebCore {
     42 
     43 using namespace HTMLNames;
     44 
     45 HTMLFrameElementBase::HTMLFrameElementBase(const QualifiedName& tagName, Document* document)
     46     : HTMLFrameOwnerElement(tagName, document)
     47     , m_scrolling(ScrollbarAuto)
     48     , m_marginWidth(-1)
     49     , m_marginHeight(-1)
     50 {
     51 }
     52 
     53 bool HTMLFrameElementBase::isURLAllowed() const
     54 {
     55     if (m_URL.isEmpty())
     56         return true;
     57 
     58     const KURL& completeURL = document()->completeURL(m_URL);
     59 
     60     if (protocolIsJavaScript(completeURL)) {
     61         Document* contentDoc = this->contentDocument();
     62         if (contentDoc && !ScriptController::canAccessFromCurrentOrigin(contentDoc->frame()))
     63             return false;
     64     }
     65 
     66     Frame* parentFrame = document()->frame();
     67     if (parentFrame)
     68         return parentFrame->isURLAllowed(completeURL);
     69 
     70     return true;
     71 }
     72 
     73 void HTMLFrameElementBase::openURL(bool lockBackForwardList)
     74 {
     75     if (!isURLAllowed())
     76         return;
     77 
     78     if (m_URL.isEmpty())
     79         m_URL = blankURL().string();
     80 
     81     Frame* parentFrame = document()->frame();
     82     if (!parentFrame)
     83         return;
     84 
     85     // Support for <frame src="javascript:string">
     86     KURL scriptURL;
     87     KURL url = document()->completeURL(m_URL);
     88     if (protocolIsJavaScript(m_URL)) {
     89         scriptURL = url;
     90         url = blankURL();
     91     }
     92 
     93     if (!loadOrRedirectSubframe(url, m_frameName, lockBackForwardList))
     94         return;
     95     if (!contentFrame() || scriptURL.isEmpty())
     96         return;
     97     contentFrame()->script()->executeScriptIfJavaScriptURL(scriptURL);
     98 }
     99 
    100 void HTMLFrameElementBase::parseAttribute(const QualifiedName& name, const AtomicString& value)
    101 {
    102     if (name == srcdocAttr)
    103         setLocation("about:srcdoc");
    104     else if (name == srcAttr && !fastHasAttribute(srcdocAttr))
    105         setLocation(stripLeadingAndTrailingHTMLSpaces(value));
    106     else if (isIdAttributeName(name)) {
    107         // Important to call through to base for the id attribute so the hasID bit gets set.
    108         HTMLFrameOwnerElement::parseAttribute(name, value);
    109         m_frameName = value;
    110     } else if (name == nameAttr) {
    111         m_frameName = value;
    112         // FIXME: If we are already attached, this doesn't actually change the frame's name.
    113         // FIXME: If we are already attached, this doesn't check for frame name
    114         // conflicts and generate a unique frame name.
    115     } else if (name == marginwidthAttr) {
    116         m_marginWidth = value.toInt();
    117         // FIXME: If we are already attached, this has no effect.
    118     } else if (name == marginheightAttr) {
    119         m_marginHeight = value.toInt();
    120         // FIXME: If we are already attached, this has no effect.
    121     } else if (name == scrollingAttr) {
    122         // Auto and yes both simply mean "allow scrolling." No means "don't allow scrolling."
    123         if (equalIgnoringCase(value, "auto") || equalIgnoringCase(value, "yes"))
    124             m_scrolling = ScrollbarAuto;
    125         else if (equalIgnoringCase(value, "no"))
    126             m_scrolling = ScrollbarAlwaysOff;
    127         // FIXME: If we are already attached, this has no effect.
    128     } else if (name == onbeforeloadAttr)
    129         setAttributeEventListener(eventNames().beforeloadEvent, createAttributeEventListener(this, name, value));
    130     else if (name == onbeforeunloadAttr) {
    131         // FIXME: should <frame> elements have beforeunload handlers?
    132         setAttributeEventListener(eventNames().beforeunloadEvent, createAttributeEventListener(this, name, value));
    133     } else
    134         HTMLFrameOwnerElement::parseAttribute(name, value);
    135 }
    136 
    137 void HTMLFrameElementBase::setNameAndOpenURL()
    138 {
    139     m_frameName = getNameAttribute();
    140     if (m_frameName.isNull())
    141         m_frameName = getIdAttribute();
    142     openURL();
    143 }
    144 
    145 Node::InsertionNotificationRequest HTMLFrameElementBase::insertedInto(ContainerNode* insertionPoint)
    146 {
    147     HTMLFrameOwnerElement::insertedInto(insertionPoint);
    148     if (insertionPoint->inDocument())
    149         return InsertionShouldCallDidNotifySubtreeInsertions;
    150     return InsertionDone;
    151 }
    152 
    153 void HTMLFrameElementBase::didNotifySubtreeInsertions(ContainerNode*)
    154 {
    155     if (!inDocument())
    156         return;
    157 
    158     // DocumentFragments don't kick of any loads.
    159     if (!document()->frame())
    160         return;
    161 
    162     if (!SubframeLoadingDisabler::canLoadFrame(this))
    163         return;
    164 
    165     // JavaScript in src=javascript: and beforeonload can access the renderer
    166     // during attribute parsing *before* the normal parser machinery would
    167     // attach the element. To support this, we lazyAttach here, but only
    168     // if we don't already have a renderer (if we're inserted
    169     // as part of a DocumentFragment, insertedInto from an earlier element
    170     // could have forced a style resolve and already attached us).
    171     if (!renderer())
    172         lazyAttach(DoNotSetAttached);
    173     setNameAndOpenURL();
    174 }
    175 
    176 void HTMLFrameElementBase::attach(const AttachContext& context)
    177 {
    178     HTMLFrameOwnerElement::attach(context);
    179 
    180     if (RenderPart* part = renderPart()) {
    181         if (Frame* frame = contentFrame())
    182             part->setWidget(frame->view());
    183     }
    184 }
    185 
    186 KURL HTMLFrameElementBase::location() const
    187 {
    188     if (fastHasAttribute(srcdocAttr))
    189         return KURL(ParsedURLString, "about:srcdoc");
    190     return document()->completeURL(getAttribute(srcAttr));
    191 }
    192 
    193 void HTMLFrameElementBase::setLocation(const String& str)
    194 {
    195     m_URL = AtomicString(str);
    196 
    197     if (inDocument())
    198         openURL(false);
    199 }
    200 
    201 bool HTMLFrameElementBase::supportsFocus() const
    202 {
    203     return true;
    204 }
    205 
    206 void HTMLFrameElementBase::setFocus(bool received)
    207 {
    208     HTMLFrameOwnerElement::setFocus(received);
    209     if (Page* page = document()->page()) {
    210         if (received)
    211             page->focusController().setFocusedFrame(contentFrame());
    212         else if (page->focusController().focusedFrame() == contentFrame()) // Focus may have already been given to another frame, don't take it away.
    213             page->focusController().setFocusedFrame(0);
    214     }
    215 }
    216 
    217 bool HTMLFrameElementBase::isURLAttribute(const Attribute& attribute) const
    218 {
    219     return attribute.name() == srcAttr || HTMLFrameOwnerElement::isURLAttribute(attribute);
    220 }
    221 
    222 bool HTMLFrameElementBase::isHTMLContentAttribute(const Attribute& attribute) const
    223 {
    224     return attribute.name() == srcdocAttr || HTMLFrameOwnerElement::isHTMLContentAttribute(attribute);
    225 }
    226 
    227 int HTMLFrameElementBase::width()
    228 {
    229     document()->updateLayoutIgnorePendingStylesheets();
    230     if (!renderBox())
    231         return 0;
    232     return renderBox()->width();
    233 }
    234 
    235 int HTMLFrameElementBase::height()
    236 {
    237     document()->updateLayoutIgnorePendingStylesheets();
    238     if (!renderBox())
    239         return 0;
    240     return renderBox()->height();
    241 }
    242 
    243 } // namespace WebCore
    244