2
* Copyright (C) 2007, 2008, 2009 Google Inc. All rights reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions are
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
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.
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.
32
#include "V8HTMLDocument.h"
34
#include "BindingState.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"
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>
55
v8::Local<v8::Object> V8HTMLDocument::wrapInShadowObject(v8::Local<v8::Object> wrapper, Node* impl)
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);
67
v8::Local<v8::Function> shadowConstructor = shadowTemplate->GetFunction();
68
if (shadowConstructor.IsEmpty())
69
return v8::Local<v8::Object>();
71
v8::Local<v8::Object> shadow;
73
V8RecursionScope::MicrotaskSuppression scope;
74
shadow = shadowConstructor->NewInstance();
77
return v8::Local<v8::Object>();
78
shadow->SetPrototype(wrapper);
79
V8DOMWrapper::setDOMWrapper(wrapper, &V8HTMLDocument::info, impl);
83
v8::Handle<v8::Value> V8HTMLDocument::getNamedProperty(HTMLDocument* htmlDocument, const AtomicString& key, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
85
if (!htmlDocument->hasNamedItem(key.impl()) && !htmlDocument->hasExtraNamedItem(key.impl()))
88
RefPtr<HTMLCollection> items = htmlDocument->documentNamedItems(key);
92
if (items->hasExactlyOneItem()) {
93
Node* node = items->item(0);
95
if (node->hasTagName(HTMLNames::iframeTag) && (frame = static_cast<HTMLIFrameElement*>(node)->contentFrame()))
96
return toV8(frame->document()->domWindow(), creationContext, isolate);
98
return toV8(node, creationContext, isolate);
101
return toV8(items.release(), creationContext, isolate);
104
// HTMLDocument ----------------------------------------------------------------
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)
112
StringBuilder builder;
113
for (int i = 0; i < args.Length(); ++i)
114
builder.append(toWebCoreString(args[i]));
115
return builder.toString();
118
v8::Handle<v8::Value> V8HTMLDocument::writeCallback(const v8::Arguments& args)
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();
126
v8::Handle<v8::Value> V8HTMLDocument::writelnCallback(const v8::Arguments& args)
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();
134
v8::Handle<v8::Value> V8HTMLDocument::openCallback(const v8::Arguments& args)
136
INC_STATS("DOM.HTMLDocument.open()");
137
HTMLDocument* htmlDocument = V8HTMLDocument::toNative(args.Holder());
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++)
157
return frame->script()->callFunction(v8::Local<v8::Function>::Cast(function), global, args.Length(), params.get());
161
htmlDocument->open(activeDOMWindow(BindingState::instance())->document());
162
return args.Holder();
165
void V8HTMLDocument::allAccessorSetter(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::AccessorInfo& info)
167
// Just emulate a normal JS behaviour---install a property on this.
168
info.This()->ForceSet(name, value);
171
v8::Handle<v8::Object> wrap(HTMLDocument* impl, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
174
v8::Handle<v8::Object> wrapper = V8HTMLDocument::createWrapper(impl, creationContext, isolate);
175
if (wrapper.IsEmpty())
177
if (!worldForEnteredContextIfIsolated()) {
178
if (Frame* frame = impl->frame())
179
frame->script()->windowShell(mainThreadNormalWorld())->updateDocumentWrapper(wrapper);
184
} // namespace WebCore