1 /* 2 * Copyright (C) 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 "V8HTMLOptionElementConstructor.h" 33 34 #include "HTMLOptionElement.h" 35 #include "Document.h" 36 #include "Frame.h" 37 #include "HTMLNames.h" 38 #include "Text.h" 39 #include "V8Binding.h" 40 #include "V8HTMLOptionElement.h" 41 #include "V8Proxy.h" 42 43 #include <wtf/RefPtr.h> 44 45 namespace WebCore { 46 47 WrapperTypeInfo V8HTMLOptionElementConstructor::info = { V8HTMLOptionElementConstructor::GetTemplate, 0, 0, 0 }; 48 49 static v8::Handle<v8::Value> v8HTMLOptionElementConstructorCallback(const v8::Arguments& args) 50 { 51 INC_STATS("DOM.HTMLOptionElement.Contructor"); 52 53 if (!args.IsConstructCall()) 54 return throwError("DOM object constructor cannot be called as a function."); 55 56 Frame* frame = V8Proxy::retrieveFrameForCurrentContext(); 57 if (!frame) 58 return throwError("Option constructor associated frame is unavailable", V8Proxy::ReferenceError); 59 60 Document* document = frame->document(); 61 if (!document) 62 return throwError("Option constructor associated document is unavailable", V8Proxy::ReferenceError); 63 64 String data; 65 String value; 66 bool defaultSelected = false; 67 bool selected = false; 68 if (args.Length() > 0 && !args[0]->IsUndefined()) 69 data = toWebCoreString(args[0]); 70 if (args.Length() > 1 && !args[1]->IsUndefined()) 71 value = toWebCoreString(args[1]); 72 if (args.Length() > 2) 73 defaultSelected = args[2]->BooleanValue(); 74 if (args.Length() > 3) 75 selected = args[3]->BooleanValue(); 76 77 ExceptionCode ec = 0; 78 RefPtr<HTMLOptionElement> option = HTMLOptionElement::createForJSConstructor(document, data, value, defaultSelected, selected, ec); 79 80 if (ec) 81 throwError(ec); 82 83 V8DOMWrapper::setDOMWrapper(args.Holder(), &V8HTMLOptionElementConstructor::info, option.get()); 84 option->ref(); 85 V8DOMWrapper::setJSWrapperForDOMNode(option.get(), v8::Persistent<v8::Object>::New(args.Holder())); 86 return args.Holder(); 87 } 88 89 v8::Persistent<v8::FunctionTemplate> V8HTMLOptionElementConstructor::GetTemplate() 90 { 91 static v8::Persistent<v8::FunctionTemplate> cachedTemplate; 92 if (!cachedTemplate.IsEmpty()) 93 return cachedTemplate; 94 95 v8::HandleScope scope; 96 v8::Local<v8::FunctionTemplate> result = v8::FunctionTemplate::New(v8HTMLOptionElementConstructorCallback); 97 98 v8::Local<v8::ObjectTemplate> instance = result->InstanceTemplate(); 99 instance->SetInternalFieldCount(V8HTMLOptionElement::internalFieldCount); 100 result->SetClassName(v8::String::New("HTMLOptionElement")); 101 result->Inherit(V8HTMLOptionElement::GetTemplate()); 102 103 cachedTemplate = v8::Persistent<v8::FunctionTemplate>::New(result); 104 return cachedTemplate; 105 } 106 107 } // namespace WebCore 108