/platform/external/webkit/WebCore/bindings/v8/custom/V8HTMLOptionElementConstructor.cpp

https://github.com/aharish/totoro-gb-opensource-update2 · C++ · 113 lines · 65 code · 19 blank · 29 comment · 12 complexity · e55180e7f67fa77630157eb6b3f20d71 MD5 · raw file

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