HTMLFrameElement.cpp 11.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
 * This file is part of the DOM implementation for KDE.
 *
 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
 *           (C) 1999 Antti Koivisto (koivisto@kde.org)
 *           (C) 2000 Simon Hausmann (hausmann@kde.org)
 *           (C) 2001 Dirk Mueller (mueller@kde.org)
 * Copyright (C) 2004, 2006 Apple Computer, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
#include "config.h"
#include "HTMLFrameElement.h"

#include "csshelper.h"
#include "Document.h"
#include "EventNames.h"
#include "Frame.h"
#include "FrameTree.h"
#include "FrameView.h"
#include "HTMLNames.h"
#include "HTMLFrameSetElement.h"
#include "Page.h"
#include "RenderFrame.h"

namespace WebCore {

using namespace EventNames;
using namespace HTMLNames;

HTMLFrameElement::HTMLFrameElement(Document *doc)
    : HTMLElement(frameTag, doc)
{
    init();
}

HTMLFrameElement::HTMLFrameElement(const QualifiedName& tagName, Document *doc)
    : HTMLElement(tagName, doc)
{
    init();
}

void HTMLFrameElement::init()
{
    m_frameBorder = true;
    m_frameBorderSet = false;
    m_marginWidth = -1;
    m_marginHeight = -1;
    m_scrolling = ScrollBarAuto;
    m_noResize = false;
64
    m_viewSource = false;
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
}

HTMLFrameElement::~HTMLFrameElement()
{
}

bool HTMLFrameElement::isURLAllowed(const AtomicString &URLString) const
{
    if (URLString.isEmpty())
        return true;
    
    FrameView* w = document()->view();
    if (!w)
        return false;

    KURL newURL(document()->completeURL(URLString.deprecatedString()));
    newURL.setRef(DeprecatedString::null);

    // Don't allow more than 1000 total frames in a set. This seems
    // like a reasonable upper bound, and otherwise mutually recursive
    // frameset pages can quickly bring the program to its knees with
    // exponential growth in the number of frames.

    // FIXME: This limit could be higher, but WebKit has some
    // algorithms that happen while loading which appear to be N^2 or
    // worse in the number of frames
    if (w->frame()->page()->frameCount() >= 200)
        return false;

    // We allow one level of self-reference because some sites depend on that.
    // But we don't allow more than one.
    bool foundSelfReference = false;
    for (Frame *frame = w->frame(); frame; frame = frame->tree()->parent()) {
        KURL frameURL = frame->url();
        frameURL.setRef(DeprecatedString::null);
        if (frameURL == newURL) {
            if (foundSelfReference)
                return false;
            foundSelfReference = true;
        }
    }
    
    return true;
}

void HTMLFrameElement::openURL()
{
    FrameView *w = document()->view();
    if (!w)
        return;
    
    AtomicString relativeURL = m_URL;
    if (relativeURL.isEmpty())
        relativeURL = "about:blank";

    // Load the frame contents.
    Frame* parentFrame = w->frame();
    if (Frame* childFrame = parentFrame->tree()->child(m_name))
        childFrame->openURL(document()->completeURL(relativeURL.deprecatedString()));
    else
andersca's avatar
andersca committed
125
        parentFrame->requestFrame(this, relativeURL, m_name);
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
}

void HTMLFrameElement::parseMappedAttribute(MappedAttribute *attr)
{
    if (attr->name() == srcAttr)
        setLocation(WebCore::parseURL(attr->value()));
    else if (attr->name() == idAttr) {
        // Important to call through to base for the id attribute so the hasID bit gets set.
        HTMLElement::parseMappedAttribute(attr);
        m_name = attr->value();
    } else if (attr->name() == nameAttr) {
        m_name = attr->value();
        // FIXME: If we are already attached, this doesn't actually change the frame's name.
        // FIXME: If we are already attached, this doesn't check for frame name
        // conflicts and generate a unique frame name.
    } else if (attr->name() == frameborderAttr) {
        m_frameBorder = attr->value().toInt();
        m_frameBorderSet = !attr->isNull();
        // FIXME: If we are already attached, this has no effect.
    } else if (attr->name() == marginwidthAttr) {
        m_marginWidth = attr->value().toInt();
        // FIXME: If we are already attached, this has no effect.
    } else if (attr->name() == marginheightAttr) {
        m_marginHeight = attr->value().toInt();
        // FIXME: If we are already attached, this has no effect.
    } else if (attr->name() == noresizeAttr) {
        m_noResize = true;
        // FIXME: If we are already attached, this has no effect.
    } else if (attr->name() == scrollingAttr) {
        // Auto and yes both simply mean "allow scrolling." No means "don't allow scrolling."
        if (equalIgnoringCase(attr->value(), "auto") || equalIgnoringCase(attr->value(), "yes"))
            m_scrolling = ScrollBarAuto;
        else if (equalIgnoringCase(attr->value(), "no"))
            m_scrolling = ScrollBarAlwaysOff;
        // FIXME: If we are already attached, this has no effect.
161
162
163
164
    } else if (attr->name() == viewsourceAttr) {
        m_viewSource = !attr->isNull();
        if (contentFrame())
            contentFrame()->setInViewSourceMode(viewSourceMode());
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
    } else if (attr->name() == onloadAttr) {
        setHTMLEventListener(loadEvent, attr);
    } else if (attr->name() == onbeforeunloadAttr) {
        // FIXME: should <frame> elements have beforeunload handlers?
        setHTMLEventListener(beforeunloadEvent, attr);
    } else if (attr->name() == onunloadAttr) {
        setHTMLEventListener(unloadEvent, attr);
    } else
        HTMLElement::parseMappedAttribute(attr);
}

bool HTMLFrameElement::rendererIsNeeded(RenderStyle *style)
{
    // Ignore display: none.
    return isURLAllowed(m_URL);
}

RenderObject *HTMLFrameElement::createRenderer(RenderArena *arena, RenderStyle *style)
{
    return new (arena) RenderFrame(this);
}

void HTMLFrameElement::attach()
{
    m_name = getAttribute(nameAttr);
    if (m_name.isNull())
        m_name = getAttribute(idAttr);

    // inherit default settings from parent frameset
    for (Node *node = parentNode(); node; node = node->parentNode())
        if (node->hasTagName(framesetTag)) {
            HTMLFrameSetElement* frameset = static_cast<HTMLFrameSetElement*>(node);
            if (!m_frameBorderSet)
                m_frameBorder = frameset->frameBorder();
            if (!m_noResize)
                m_noResize = frameset->noResize();
            break;
        }

    HTMLElement::attach();

    if (!renderer())
        return;

    Frame* frame = document()->frame();

    if (!frame)
        return;

    frame->page()->incrementFrameCount();
    
    AtomicString relativeURL = m_URL;
    if (relativeURL.isEmpty())
        relativeURL = "about:blank";

    m_name = frame->tree()->uniqueChildName(m_name);

    // load the frame contents
andersca's avatar
andersca committed
223
    frame->requestFrame(this, relativeURL, m_name);
224
225
226
    
    if (contentFrame())
        contentFrame()->setInViewSourceMode(viewSourceMode());
227
228
229
230
231
232
}

void HTMLFrameElement::close()
{
    Frame* frame = document()->frame();
    if (renderer() && frame) {
233
        frame->disconnectOwnerElement();
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
        frame->page()->decrementFrameCount();
        if (Frame* childFrame = frame->tree()->child(m_name))
            childFrame->frameDetached();
    }
}

void HTMLFrameElement::willRemove()
{
    // close the frame and dissociate the renderer, but leave the
    // node attached so that frame does not get re-attached before
    // actually leaving the document.  see <rdar://problem/4132581>
    close();
    if (renderer()) {
        renderer()->destroy();
        setRenderer(0);
    }
    
    HTMLElement::willRemove();
}

void HTMLFrameElement::detach()
{
    close();
    HTMLElement::detach();
}

void HTMLFrameElement::setLocation(const String& str)
{
    m_URL = AtomicString(str);

    if (!attached()) {
        return;
    }
    
    // Handle the common case where we decided not to make a frame the first time.
    // Detach and the let attach() decide again whether to make the frame for this URL.
andersca's avatar
andersca committed
270
    if (!renderer() && !hasTagName(iframeTag)) {
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
        detach();
        attach();
        return;
    }
    
    if (!isURLAllowed(m_URL))
        return;

    openURL();
}

bool HTMLFrameElement::isFocusable() const
{
    return renderer();
}

void HTMLFrameElement::setFocus(bool received)
{
    HTMLElement::setFocus(received);
    WebCore::RenderFrame *renderFrame = static_cast<WebCore::RenderFrame *>(renderer());
    if (!renderFrame || !renderFrame->widget())
        return;
    if (received)
        renderFrame->widget()->setFocus();
    else
        renderFrame->widget()->clearFocus();
}

Frame* HTMLFrameElement::contentFrame() const
{
    // Start with the part that contains this element, our ownerDocument.
    Frame* parentFrame = document()->frame();
    if (!parentFrame)
        return 0;

    // Find the part for the subframe that this element represents.
    return parentFrame->tree()->child(m_name);
}

Document* HTMLFrameElement::contentDocument() const
{
    Frame* frame = contentFrame();
    if (!frame)
        return 0;
    return frame->document();
}

bool HTMLFrameElement::isURLAttribute(Attribute *attr) const
{
    return attr->name() == srcAttr;
}

String HTMLFrameElement::frameBorder() const
{
    return getAttribute(frameborderAttr);
}

void HTMLFrameElement::setFrameBorder(const String &value)
{
    setAttribute(frameborderAttr, value);
}

String HTMLFrameElement::longDesc() const
{
    return getAttribute(longdescAttr);
}

void HTMLFrameElement::setLongDesc(const String &value)
{
    setAttribute(longdescAttr, value);
}

String HTMLFrameElement::marginHeight() const
{
    return getAttribute(marginheightAttr);
}

void HTMLFrameElement::setMarginHeight(const String &value)
{
    setAttribute(marginheightAttr, value);
}

String HTMLFrameElement::marginWidth() const
{
    return getAttribute(marginwidthAttr);
}

void HTMLFrameElement::setMarginWidth(const String &value)
{
    setAttribute(marginwidthAttr, value);
}

String HTMLFrameElement::name() const
{
    return getAttribute(nameAttr);
}

void HTMLFrameElement::setName(const String &value)
{
    setAttribute(nameAttr, value);
}

void HTMLFrameElement::setNoResize(bool noResize)
{
    setAttribute(noresizeAttr, noResize ? "" : 0);
}

String HTMLFrameElement::scrolling() const
{
    return getAttribute(scrollingAttr);
}

void HTMLFrameElement::setScrolling(const String &value)
{
    setAttribute(scrollingAttr, value);
}

String HTMLFrameElement::src() const
{
    return getAttribute(srcAttr);
}

void HTMLFrameElement::setSrc(const String &value)
{
    setAttribute(srcAttr, value);
}

int HTMLFrameElement::frameWidth() const
{
    if (!renderer())
        return 0;
    
    document()->updateLayoutIgnorePendingStylesheets();
    return renderer()->width();
}

int HTMLFrameElement::frameHeight() const
{
    if (!renderer())
        return 0;
    
    document()->updateLayoutIgnorePendingStylesheets();
    return renderer()->height();
}

}