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 "Frame.h" 35 #include "HTMLAllCollection.h" 36 #include "HTMLDocument.h" 37 #include "HTMLCollection.h" 38 #include "HTMLIFrameElement.h" 39 #include "HTMLNames.h" 40 #include "V8Binding.h" 41 #include "V8DOMWindow.h" 42 #include "V8HTMLAllCollection.h" 43 #include "V8HTMLCollection.h" 44 #include "V8IsolatedContext.h" 45 #include "V8Node.h" 46 #include "V8Proxy.h" 47 #include <wtf/RefPtr.h> 48 #include <wtf/StdLibExtras.h> 49 50 namespace WebCore { 51 52 v8::Local<v8::Object> V8HTMLDocument::WrapInShadowObject(v8::Local<v8::Object> wrapper, Node* impl) 53 { 54 DEFINE_STATIC_LOCAL(v8::Persistent<v8::FunctionTemplate>, shadowTemplate, ()); 55 if (shadowTemplate.IsEmpty()) { 56 shadowTemplate = v8::Persistent<v8::FunctionTemplate>::New(v8::FunctionTemplate::New()); 57 if (shadowTemplate.IsEmpty()) 58 return v8::Local<v8::Object>(); 59 shadowTemplate->SetClassName(v8::String::New("HTMLDocument")); 60 shadowTemplate->Inherit(V8HTMLDocument::GetTemplate()); 61 shadowTemplate->InstanceTemplate()->SetInternalFieldCount(V8HTMLDocument::internalFieldCount); 62 } 63 64 v8::Local<v8::Function> shadowConstructor = shadowTemplate->GetFunction(); 65 if (shadowConstructor.IsEmpty()) 66 return v8::Local<v8::Object>(); 67 68 v8::Local<v8::Object> shadow = shadowConstructor->NewInstance(); 69 if (shadow.IsEmpty()) 70 return v8::Local<v8::Object>(); 71 V8DOMWrapper::setDOMWrapper(shadow, &V8HTMLDocument::info, impl); 72 shadow->SetPrototype(wrapper); 73 return shadow; 74 } 75 76 v8::Handle<v8::Value> V8HTMLDocument::GetNamedProperty(HTMLDocument* htmlDocument, const AtomicString& key) 77 { 78 if (!htmlDocument->hasNamedItem(key.impl()) && !htmlDocument->hasExtraNamedItem(key.impl())) 79 return v8::Handle<v8::Value>(); 80 81 RefPtr<HTMLCollection> items = htmlDocument->documentNamedItems(key); 82 if (!items->length()) 83 return v8::Handle<v8::Value>(); 84 85 if (items->length() == 1) { 86 Node* node = items->firstItem(); 87 Frame* frame = 0; 88 if (node->hasTagName(HTMLNames::iframeTag) && (frame = static_cast<HTMLIFrameElement*>(node)->contentFrame())) 89 return toV8(frame->domWindow()); 90 91 return toV8(node); 92 } 93 94 return toV8(items.release()); 95 } 96 97 // HTMLDocument ---------------------------------------------------------------- 98 99 // Concatenates "args" to a string. If args is empty, returns empty string. 100 // Firefox/Safari/IE support non-standard arguments to document.write, ex: 101 // document.write("a", "b", "c") --> document.write("abc") 102 // document.write() --> document.write("") 103 static String writeHelperGetString(const v8::Arguments& args) 104 { 105 String str = ""; 106 for (int i = 0; i < args.Length(); ++i) 107 str += toWebCoreString(args[i]); 108 return str; 109 } 110 111 v8::Handle<v8::Value> V8HTMLDocument::writeCallback(const v8::Arguments& args) 112 { 113 INC_STATS("DOM.HTMLDocument.write()"); 114 HTMLDocument* htmlDocument = V8HTMLDocument::toNative(args.Holder()); 115 Frame* frame = V8Proxy::retrieveFrameForCallingContext(); 116 htmlDocument->write(writeHelperGetString(args), frame ? frame->document() : NULL); 117 return v8::Undefined(); 118 } 119 120 v8::Handle<v8::Value> V8HTMLDocument::writelnCallback(const v8::Arguments& args) 121 { 122 INC_STATS("DOM.HTMLDocument.writeln()"); 123 HTMLDocument* htmlDocument = V8HTMLDocument::toNative(args.Holder()); 124 Frame* frame = V8Proxy::retrieveFrameForCallingContext(); 125 htmlDocument->writeln(writeHelperGetString(args), frame ? frame->document() : NULL); 126 return v8::Undefined(); 127 } 128 129 v8::Handle<v8::Value> V8HTMLDocument::openCallback(const v8::Arguments& args) 130 { 131 INC_STATS("DOM.HTMLDocument.open()"); 132 HTMLDocument* htmlDocument = V8HTMLDocument::toNative(args.Holder()); 133 134 if (args.Length() > 2) { 135 if (Frame* frame = htmlDocument->frame()) { 136 // Fetch the global object for the frame. 137 v8::Local<v8::Context> context = V8Proxy::context(frame); 138 // Bail out if we cannot get the context. 139 if (context.IsEmpty()) 140 return v8::Undefined(); 141 v8::Local<v8::Object> global = context->Global(); 142 // Get the open property of the global object. 143 v8::Local<v8::Value> function = global->Get(v8::String::New("open")); 144 // If the open property is not a function throw a type error. 145 if (!function->IsFunction()) { 146 throwError("open is not a function"); 147 return v8::Undefined(); 148 } 149 // Wrap up the arguments and call the function. 150 v8::Local<v8::Value>* params = new v8::Local<v8::Value>[args.Length()]; 151 for (int i = 0; i < args.Length(); i++) 152 params[i] = args[i]; 153 154 V8Proxy* proxy = V8Proxy::retrieve(frame); 155 ASSERT(proxy); 156 157 v8::Local<v8::Value> result = proxy->callFunction(v8::Local<v8::Function>::Cast(function), global, args.Length(), params); 158 delete[] params; 159 return result; 160 } 161 } 162 163 Frame* frame = V8Proxy::retrieveFrameForCallingContext(); 164 htmlDocument->open(frame ? frame->document() : NULL); 165 // Return the document. 166 return args.Holder(); 167 } 168 169 v8::Handle<v8::Value> V8HTMLDocument::allAccessorGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info) 170 { 171 INC_STATS("DOM.HTMLDocument.all._get"); 172 v8::Handle<v8::Object> holder = info.Holder(); 173 HTMLDocument* htmlDocument = V8HTMLDocument::toNative(holder); 174 return toV8(htmlDocument->all()); 175 } 176 177 void V8HTMLDocument::allAccessorSetter(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::AccessorInfo& info) 178 { 179 // Just emulate a normal JS behaviour---install a property on this. 180 info.This()->ForceSet(name, value); 181 } 182 183 v8::Handle<v8::Value> toV8(HTMLDocument* impl, bool forceNewObject) 184 { 185 if (!impl) 186 return v8::Null(); 187 v8::Handle<v8::Object> wrapper = V8HTMLDocument::wrap(impl, forceNewObject); 188 if (wrapper.IsEmpty()) 189 return wrapper; 190 if (!V8IsolatedContext::getEntered()) { 191 if (V8Proxy* proxy = V8Proxy::retrieve(impl->frame())) 192 proxy->windowShell()->updateDocumentWrapper(wrapper); 193 } 194 return wrapper; 195 } 196 197 } // namespace WebCore 198