| Index: third_party/WebKit/Source/core/dom/custom/CustomElementsRegistry.cpp
|
| diff --git a/third_party/WebKit/Source/core/dom/custom/CustomElementsRegistry.cpp b/third_party/WebKit/Source/core/dom/custom/CustomElementsRegistry.cpp
|
| index f22190cea9e0545388421215b5afaafbf2006b51..2597a176f89e3c81ad3f8190494e7f6049d66899 100644
|
| --- a/third_party/WebKit/Source/core/dom/custom/CustomElementsRegistry.cpp
|
| +++ b/third_party/WebKit/Source/core/dom/custom/CustomElementsRegistry.cpp
|
| @@ -4,26 +4,120 @@
|
|
|
| #include "core/dom/custom/CustomElementsRegistry.h"
|
|
|
| +#include "bindings/core/v8/DOMWrapperWorld.h"
|
| #include "bindings/core/v8/ExceptionState.h"
|
| #include "bindings/core/v8/ScriptState.h"
|
| +#include "bindings/core/v8/ScriptValue.h"
|
| +#include "bindings/core/v8/V8Binding.h"
|
| #include "core/dom/ElementRegistrationOptions.h"
|
| +#include "core/dom/ExceptionCode.h"
|
| +#include "core/dom/custom/CustomElement.h"
|
| +#include "core/dom/custom/CustomElementDefinition.h"
|
|
|
| namespace blink {
|
|
|
| -CustomElementsRegistry::CustomElementsRegistry()
|
| +CustomElementsRegistry::CustomElementsRegistry(v8::Isolate* isolate)
|
| + : m_constructorIdMap(isolate, v8::NativeWeakMap::New(isolate))
|
| {
|
| + ASSERT(DOMWrapperWorld::current(isolate).isMainWorld());
|
| }
|
|
|
| +// http://w3c.github.io/webcomponents/spec/custom/#dfn-element-definition
|
| void CustomElementsRegistry::define(ScriptState* scriptState,
|
| const AtomicString& name, const ScriptValue& constructor,
|
| const ElementRegistrationOptions& options, ExceptionState& exceptionState)
|
| {
|
| - // TODO(kojii): Element definition process not implemented yet.
|
| - // http://w3c.github.io/webcomponents/spec/custom/#dfn-element-definition
|
| + // TODO(dominicc): Check that all of the early returns are
|
| + // throwing an exception or terminating.
|
| +
|
| + v8::Isolate* isolate = scriptState->isolate();
|
| + v8::Local<v8::Context> context = scriptState->context();
|
| +
|
| + // TODO(dominicc): Make this check for constructors and not just
|
| + // functions when
|
| + // https://bugs.chromium.org/p/v8/issues/detail?id=4993 is fixed.
|
| + v8::Local<v8::Value> constructorValue = constructor.v8Value();
|
| + if (!constructorValue->IsFunction()) {
|
| + exceptionState.throwTypeError(
|
| + "constructor argument is not a constructor");
|
| + return;
|
| + }
|
| + v8::Local<v8::Object> constructorObject =
|
| + constructorValue.As<v8::Object>();
|
| +
|
| + // Raise an exception if the name is not valid.
|
| + if (!CustomElement::isValidName(name)) {
|
| + exceptionState.throwSyntaxError(
|
| + "\"" + name + "\" is not a valid custom element name");
|
| + return;
|
| + }
|
| +
|
| + // Raise an exception if the name is already in use.
|
| + if (m_names.contains(name)) {
|
| + exceptionState.throwDOMException(
|
| + NotSupportedError,
|
| + "this name has already been used with this registry");
|
| + return;
|
| + }
|
| +
|
| + // Raise an exception if the constructor is already registered.
|
| + if (definitionForConstructor(context, constructorObject)) {
|
| + exceptionState.throwDOMException(
|
| + NotSupportedError,
|
| + "this constructor has already been used with this registry");
|
| + return;
|
| + }
|
| +
|
| + // TODO(dominicc): Implement steps:
|
| + // 5: localName
|
| + // 6-7: extends processing
|
| + // 8-9: observed attributes caching
|
| +
|
| + v8::MaybeLocal<v8::Value> maybePrototype = constructorObject->Get(
|
| + context,
|
| + v8AtomicString(isolate, "prototype"));
|
| + v8::Local<v8::Value> prototypeValue;
|
| + // TODO(dominicc): Check whether returning undefined results in an
|
| + // empty handle; we must be throwing an exception on exit.
|
| + if (!maybePrototype.ToLocal(&prototypeValue))
|
| + return;
|
| + if (!prototypeValue->IsObject()) {
|
| + exceptionState.throwTypeError("constructor prototype is not an object");
|
| + return;
|
| + }
|
| + // TODO(dominicc): Consider making this a v8::Isolate+v8::Object
|
| + ScriptValue prototype(scriptState, prototypeValue);
|
| +
|
| + // TODO(dominicc): Implement steps:
|
| + // 12-13: connected callback
|
| + // 14-15: disconnected callback
|
| + // 16-17: attribute changed callback
|
| +
|
| + size_t id = m_definitions.size();
|
| + m_definitions.append(new CustomElementDefinition(name, prototype));
|
| + m_constructorIdMap.Get(isolate)->Set(
|
| + constructorObject,
|
| + v8::Integer::NewFromUnsigned(isolate, id));
|
| + m_names.add(name);
|
| +
|
| + // TODO(dominicc): Implement steps:
|
| + // 20: when-defined promise processing
|
| +}
|
| +
|
| +CustomElementDefinition* CustomElementsRegistry::definitionForConstructor(
|
| + v8::Handle<v8::Context> context,
|
| + v8::Handle<v8::Value> constructor)
|
| +{
|
| + v8::Local<v8::Value> entry =
|
| + m_constructorIdMap.Get(context->GetIsolate())->Get(constructor);
|
| + if (!entry->IsUint32())
|
| + return nullptr;
|
| + return m_definitions[entry->Uint32Value(context).FromJust()];
|
| }
|
|
|
| DEFINE_TRACE(CustomElementsRegistry)
|
| {
|
| + visitor->trace(m_definitions);
|
| }
|
|
|
| } // namespace blink
|
|
|