~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Source/WebCore/bindings/v8/custom/V8HTMLDocumentCustom.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2007, 2008, 2009 Google Inc. All rights reserved.
 
3
 *
 
4
 * Redistribution and use in source and binary forms, with or without
 
5
 * modification, are permitted provided that the following conditions are
 
6
 * met:
 
7
 *
 
8
 *     * Redistributions of source code must retain the above copyright
 
9
 * notice, this list of conditions and the following disclaimer.
 
10
 *     * Redistributions in binary form must reproduce the above
 
11
 * copyright notice, this list of conditions and the following disclaimer
 
12
 * in the documentation and/or other materials provided with the
 
13
 * distribution.
 
14
 *     * Neither the name of Google Inc. nor the names of its
 
15
 * contributors may be used to endorse or promote products derived from
 
16
 * this software without specific prior written permission.
 
17
 *
 
18
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
19
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
20
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 
21
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 
22
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
23
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
24
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
25
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
26
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
27
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
28
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
29
 */
 
30
 
 
31
#include "config.h"
 
32
#include "V8HTMLDocument.h"
 
33
 
 
34
#include "BindingState.h"
 
35
#include "Frame.h"
 
36
#include "HTMLAllCollection.h"
 
37
#include "HTMLDocument.h"
 
38
#include "HTMLCollection.h"
 
39
#include "HTMLIFrameElement.h"
 
40
#include "HTMLNames.h"
 
41
#include "V8Binding.h"
 
42
#include "V8DOMWindow.h"
 
43
#include "V8DOMWindowShell.h"
 
44
#include "V8HTMLAllCollection.h"
 
45
#include "V8HTMLCollection.h"
 
46
#include "V8Node.h"
 
47
#include "V8RecursionScope.h"
 
48
#include <wtf/text/StringBuilder.h>
 
49
#include <wtf/OwnArrayPtr.h>
 
50
#include <wtf/RefPtr.h>
 
51
#include <wtf/StdLibExtras.h>
 
52
 
 
53
namespace WebCore {
 
54
 
 
55
v8::Local<v8::Object> V8HTMLDocument::wrapInShadowObject(v8::Local<v8::Object> wrapper, Node* impl)
 
56
{
 
57
    DEFINE_STATIC_LOCAL(v8::Persistent<v8::FunctionTemplate>, shadowTemplate, ());
 
58
    if (shadowTemplate.IsEmpty()) {
 
59
        shadowTemplate = v8::Persistent<v8::FunctionTemplate>::New(v8::FunctionTemplate::New());
 
60
        if (shadowTemplate.IsEmpty())
 
61
            return v8::Local<v8::Object>();
 
62
        shadowTemplate->SetClassName(v8::String::New("HTMLDocument"));
 
63
        shadowTemplate->Inherit(V8HTMLDocument::GetTemplate());
 
64
        shadowTemplate->InstanceTemplate()->SetInternalFieldCount(V8HTMLDocument::internalFieldCount);
 
65
    }
 
66
 
 
67
    v8::Local<v8::Function> shadowConstructor = shadowTemplate->GetFunction();
 
68
    if (shadowConstructor.IsEmpty())
 
69
        return v8::Local<v8::Object>();
 
70
 
 
71
    v8::Local<v8::Object> shadow;
 
72
    {
 
73
        V8RecursionScope::MicrotaskSuppression scope;
 
74
        shadow = shadowConstructor->NewInstance();
 
75
    }
 
76
    if (shadow.IsEmpty())
 
77
        return v8::Local<v8::Object>();
 
78
    shadow->SetPrototype(wrapper);
 
79
    V8DOMWrapper::setDOMWrapper(wrapper, &V8HTMLDocument::info, impl);
 
80
    return shadow;
 
81
}
 
82
 
 
83
v8::Handle<v8::Value> V8HTMLDocument::getNamedProperty(HTMLDocument* htmlDocument, const AtomicString& key, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
 
84
{
 
85
    if (!htmlDocument->hasNamedItem(key.impl()) && !htmlDocument->hasExtraNamedItem(key.impl()))
 
86
        return v8Undefined();
 
87
 
 
88
    RefPtr<HTMLCollection> items = htmlDocument->documentNamedItems(key);
 
89
    if (items->isEmpty())
 
90
        return v8Undefined();
 
91
 
 
92
    if (items->hasExactlyOneItem()) {
 
93
        Node* node = items->item(0);
 
94
        Frame* frame = 0;
 
95
        if (node->hasTagName(HTMLNames::iframeTag) && (frame = static_cast<HTMLIFrameElement*>(node)->contentFrame()))
 
96
            return toV8(frame->document()->domWindow(), creationContext, isolate);
 
97
 
 
98
        return toV8(node, creationContext, isolate);
 
99
    }
 
100
 
 
101
    return toV8(items.release(), creationContext, isolate);
 
102
}
 
103
 
 
104
// HTMLDocument ----------------------------------------------------------------
 
105
 
 
106
// Concatenates "args" to a string. If args is empty, returns empty string.
 
107
// Firefox/Safari/IE support non-standard arguments to document.write, ex:
 
108
//   document.write("a", "b", "c") --> document.write("abc")
 
109
//   document.write() --> document.write("")
 
110
static String writeHelperGetString(const v8::Arguments& args)
 
111
{
 
112
    StringBuilder builder;
 
113
    for (int i = 0; i < args.Length(); ++i)
 
114
        builder.append(toWebCoreString(args[i]));
 
115
    return builder.toString();
 
116
}
 
117
 
 
118
v8::Handle<v8::Value> V8HTMLDocument::writeCallback(const v8::Arguments& args)
 
119
{
 
120
    INC_STATS("DOM.HTMLDocument.write()");
 
121
    HTMLDocument* htmlDocument = V8HTMLDocument::toNative(args.Holder());
 
122
    htmlDocument->write(writeHelperGetString(args), activeDOMWindow(BindingState::instance())->document());
 
123
    return v8::Undefined();
 
124
}
 
125
 
 
126
v8::Handle<v8::Value> V8HTMLDocument::writelnCallback(const v8::Arguments& args)
 
127
{
 
128
    INC_STATS("DOM.HTMLDocument.writeln()");
 
129
    HTMLDocument* htmlDocument = V8HTMLDocument::toNative(args.Holder());
 
130
    htmlDocument->writeln(writeHelperGetString(args), activeDOMWindow(BindingState::instance())->document());
 
131
    return v8::Undefined();
 
132
}
 
133
 
 
134
v8::Handle<v8::Value> V8HTMLDocument::openCallback(const v8::Arguments& args)
 
135
{
 
136
    INC_STATS("DOM.HTMLDocument.open()");
 
137
    HTMLDocument* htmlDocument = V8HTMLDocument::toNative(args.Holder());
 
138
 
 
139
    if (args.Length() > 2) {
 
140
        if (RefPtr<Frame> frame = htmlDocument->frame()) {
 
141
            // Fetch the global object for the frame.
 
142
            v8::Local<v8::Context> context = frame->script()->currentWorldContext();
 
143
            // Bail out if we cannot get the context.
 
144
            if (context.IsEmpty())
 
145
                return v8::Undefined();
 
146
            v8::Local<v8::Object> global = context->Global();
 
147
            // Get the open property of the global object.
 
148
            v8::Local<v8::Value> function = global->Get(v8::String::New("open"));
 
149
            // If the open property is not a function throw a type error.
 
150
            if (!function->IsFunction())
 
151
                return throwTypeError("open is not a function", args.GetIsolate());
 
152
            // Wrap up the arguments and call the function.
 
153
            OwnArrayPtr<v8::Local<v8::Value> > params = adoptArrayPtr(new v8::Local<v8::Value>[args.Length()]);
 
154
            for (int i = 0; i < args.Length(); i++)
 
155
                params[i] = args[i];
 
156
 
 
157
            return frame->script()->callFunction(v8::Local<v8::Function>::Cast(function), global, args.Length(), params.get());
 
158
        }
 
159
    }
 
160
 
 
161
    htmlDocument->open(activeDOMWindow(BindingState::instance())->document());
 
162
    return args.Holder();
 
163
}
 
164
 
 
165
void V8HTMLDocument::allAccessorSetter(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::AccessorInfo& info)
 
166
{
 
167
    // Just emulate a normal JS behaviour---install a property on this.
 
168
    info.This()->ForceSet(name, value);
 
169
}
 
170
 
 
171
v8::Handle<v8::Object> wrap(HTMLDocument* impl, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
 
172
{
 
173
    ASSERT(impl);
 
174
    v8::Handle<v8::Object> wrapper = V8HTMLDocument::createWrapper(impl, creationContext, isolate);
 
175
    if (wrapper.IsEmpty())
 
176
        return wrapper;
 
177
    if (!worldForEnteredContextIfIsolated()) {
 
178
        if (Frame* frame = impl->frame())
 
179
            frame->script()->windowShell(mainThreadNormalWorld())->updateDocumentWrapper(wrapper);
 
180
    }
 
181
    return wrapper;
 
182
}
 
183
 
 
184
} // namespace WebCore