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

https://github.com/aharish/totoro-gb-opensource-update2 · C++ · 150 lines · 88 code · 28 blank · 34 comment · 16 complexity · f49a4d9f201e9fae26ea69881951fe01 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 "V8HTMLCollection.h"
  32. #include "HTMLCollection.h"
  33. #include "V8Binding.h"
  34. #include "V8HTMLAllCollection.h"
  35. #include "V8NamedNodesCollection.h"
  36. #include "V8Node.h"
  37. #include "V8NodeList.h"
  38. #include "V8Proxy.h"
  39. namespace WebCore {
  40. static v8::Handle<v8::Value> getNamedItems(HTMLCollection* collection, AtomicString name)
  41. {
  42. Vector<RefPtr<Node> > namedItems;
  43. collection->namedItems(name, namedItems);
  44. if (!namedItems.size())
  45. return v8::Handle<v8::Value>();
  46. if (namedItems.size() == 1)
  47. return toV8(namedItems.at(0).release());
  48. NodeList* list = new V8NamedNodesCollection(namedItems);
  49. return toV8(list);
  50. }
  51. static v8::Handle<v8::Value> getItem(HTMLCollection* collection, v8::Handle<v8::Value> argument)
  52. {
  53. v8::Local<v8::Uint32> index = argument->ToArrayIndex();
  54. if (index.IsEmpty()) {
  55. v8::Handle<v8::Value> result = getNamedItems(collection, toWebCoreString(argument->ToString()));
  56. if (result.IsEmpty())
  57. return v8::Undefined();
  58. return result;
  59. }
  60. RefPtr<Node> result = collection->item(index->Uint32Value());
  61. return toV8(result.release());
  62. }
  63. v8::Handle<v8::Value> V8HTMLCollection::namedPropertyGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
  64. {
  65. INC_STATS("DOM.HTMLCollection.NamedPropertyGetter");
  66. // Search the prototype chain first.
  67. v8::Handle<v8::Value> value = info.Holder()->GetRealNamedPropertyInPrototypeChain(name);
  68. if (!value.IsEmpty())
  69. return value;
  70. // Search local callback properties next to find IDL defined
  71. // properties.
  72. if (info.Holder()->HasRealNamedCallbackProperty(name))
  73. return v8::Handle<v8::Value>();
  74. // Finally, search the DOM structure.
  75. HTMLCollection* imp = V8HTMLCollection::toNative(info.Holder());
  76. return getNamedItems(imp, v8StringToAtomicWebCoreString(name));
  77. }
  78. v8::Handle<v8::Value> V8HTMLCollection::itemCallback(const v8::Arguments& args)
  79. {
  80. INC_STATS("DOM.HTMLCollection.item()");
  81. HTMLCollection* imp = V8HTMLCollection::toNative(args.Holder());
  82. return getItem(imp, args[0]);
  83. }
  84. v8::Handle<v8::Value> V8HTMLCollection::namedItemCallback(const v8::Arguments& args)
  85. {
  86. INC_STATS("DOM.HTMLCollection.namedItem()");
  87. HTMLCollection* imp = V8HTMLCollection::toNative(args.Holder());
  88. v8::Handle<v8::Value> result = getNamedItems(imp, toWebCoreString(args[0]));
  89. if (result.IsEmpty())
  90. return v8::Undefined();
  91. return result;
  92. }
  93. v8::Handle<v8::Value> V8HTMLCollection::callAsFunctionCallback(const v8::Arguments& args)
  94. {
  95. INC_STATS("DOM.HTMLCollection.callAsFunction()");
  96. if (args.Length() < 1)
  97. return v8::Undefined();
  98. HTMLCollection* imp = V8HTMLCollection::toNative(args.Holder());
  99. if (args.Length() == 1)
  100. return getItem(imp, args[0]);
  101. // If there is a second argument it is the index of the item we want.
  102. String name = toWebCoreString(args[0]);
  103. v8::Local<v8::Uint32> index = args[1]->ToArrayIndex();
  104. if (index.IsEmpty())
  105. return v8::Undefined();
  106. unsigned current = index->Uint32Value();
  107. Node* node = imp->namedItem(name);
  108. while (node) {
  109. if (!current)
  110. return toV8(node);
  111. node = imp->nextNamedItem(name);
  112. current--;
  113. }
  114. return v8::Undefined();
  115. }
  116. v8::Handle<v8::Value> toV8(HTMLCollection* impl)
  117. {
  118. if (impl->type() == DocAll)
  119. return toV8(static_cast<HTMLAllCollection*>(impl));
  120. return V8HTMLCollection::wrap(impl);
  121. }
  122. } // namespace WebCore