Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(43)

Side by Side Diff: third_party/WebKit/Source/core/dom/custom/CustomElement.cpp

Issue 2054433002: Implement "create an element" when sync for Custom Element V1 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@async-ce
Patch Set: dominicc review Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "core/dom/custom/CustomElement.h" 5 #include "core/dom/custom/CustomElement.h"
6 6
7 #include "core/dom/Document.h" 7 #include "core/dom/Document.h"
8 #include "core/dom/QualifiedName.h" 8 #include "core/dom/QualifiedName.h"
9 #include "core/dom/custom/CEReactionsScope.h" 9 #include "core/dom/custom/CEReactionsScope.h"
10 #include "core/dom/custom/CustomElementDefinition.h" 10 #include "core/dom/custom/CustomElementDefinition.h"
(...skipping 13 matching lines...) Expand all
24 return registry(element.document()); 24 return registry(element.document());
25 } 25 }
26 26
27 CustomElementsRegistry* CustomElement::registry(const Document& document) 27 CustomElementsRegistry* CustomElement::registry(const Document& document)
28 { 28 {
29 if (LocalDOMWindow* window = document.domWindow()) 29 if (LocalDOMWindow* window = document.domWindow())
30 return window->customElements(); 30 return window->customElements();
31 return nullptr; 31 return nullptr;
32 } 32 }
33 33
34 CustomElementDefinition* CustomElement::definitionForName(const Document& docume nt, const AtomicString& name)
dominicc (has gone to gerrit) 2016/06/13 07:37:24 This is not the right signature for when we implem
kojii 2016/06/13 13:44:31 Made this static to this file for now to avoid exp
35 {
36 if (CustomElementsRegistry* registry = CustomElement::registry(document))
37 return registry->definitionForName(name);
38 return nullptr;
39 }
40
34 bool CustomElement::isValidName(const AtomicString& name) 41 bool CustomElement::isValidName(const AtomicString& name)
35 { 42 {
36 if (!name.length() || name[0] < 'a' || name[0] > 'z') 43 if (!name.length() || name[0] < 'a' || name[0] > 'z')
37 return false; 44 return false;
38 45
39 bool hasHyphens = false; 46 bool hasHyphens = false;
40 for (size_t i = 1; i < name.length(); ) { 47 for (size_t i = 1; i < name.length(); ) {
41 UChar32 ch; 48 UChar32 ch;
42 if (name.is8Bit()) 49 if (name.is8Bit())
43 ch = name[i++]; 50 ch = name[i++];
(...skipping 28 matching lines...) Expand all
72 return RuntimeEnabledFeatures::customElementsV1Enabled() 79 return RuntimeEnabledFeatures::customElementsV1Enabled()
73 && document.frame() && isValidName(localName); 80 && document.frame() && isValidName(localName);
74 } 81 }
75 82
76 bool CustomElement::shouldCreateCustomElement(Document& document, const Qualifie dName& tagName) 83 bool CustomElement::shouldCreateCustomElement(Document& document, const Qualifie dName& tagName)
77 { 84 {
78 return shouldCreateCustomElement(document, tagName.localName()) 85 return shouldCreateCustomElement(document, tagName.localName())
79 && tagName.namespaceURI() == HTMLNames::xhtmlNamespaceURI; 86 && tagName.namespaceURI() == HTMLNames::xhtmlNamespaceURI;
80 } 87 }
81 88
82 HTMLElement* CustomElement::createCustomElement(Document& document, const Atomic String& localName, CreateElementFlags flags) 89 HTMLElement* CustomElement::createCustomElementSync(Document& document, const At omicString& localName, ExceptionState& exceptionState)
83 { 90 {
84 return createCustomElement(document, 91 return createCustomElementSync(document,
85 QualifiedName(nullAtom, localName, HTMLNames::xhtmlNamespaceURI), 92 QualifiedName(nullAtom, localName, HTMLNames::xhtmlNamespaceURI),
86 flags); 93 exceptionState);
87 } 94 }
88 95
89 HTMLElement* CustomElement::createCustomElement(Document& document, const Qualif iedName& tagName, CreateElementFlags flags) 96 HTMLElement* CustomElement::createCustomElementSync(Document& document, const Qu alifiedName& tagName, ExceptionState& exceptionState)
90 { 97 {
91 DCHECK(shouldCreateCustomElement(document, tagName)); 98 DCHECK(shouldCreateCustomElement(document, tagName));
92 99
93 // To create an element: 100 // To create an element:
94 // https://dom.spec.whatwg.org/#concept-create-element 101 // https://dom.spec.whatwg.org/#concept-create-element
95 // 6. If definition is non-null, then: 102 // 6. If definition is non-null, then:
103 // 6.1. If the synchronous custom elements flag is set:
104 if (CustomElementDefinition* definition = definitionForName(document, tagNam e.localName()))
dominicc (has gone to gerrit) 2016/06/13 07:37:24 This is error-prone because it throws away the nam
kojii 2016/06/13 13:44:31 I agree with you in general direction we should mo
105 return definition->createElementSync(document, tagName, exceptionState);
106
107 return createUndefinedElement(document, tagName);
108 }
109
110 HTMLElement* CustomElement::createCustomElementSync(Document& document, const Qu alifiedName& tagName)
111 {
112 DCHECK(shouldCreateCustomElement(document, tagName));
113
114 // When invoked from "create an element for a token":
115 // https://html.spec.whatwg.org/multipage/syntax.html#create-an-element-for- the-token
116 // 7. If this step throws an exception, then report the exception,
117 // and let element be instead a new element that implements
118 // HTMLUnknownElement.
119 if (CustomElementDefinition* definition = definitionForName(document, tagNam e.localName()))
120 return definition->createElementSync(document, tagName);
121
122 return createUndefinedElement(document, tagName);
123 }
124
125 HTMLElement* CustomElement::createCustomElementAsync(Document& document, const Q ualifiedName& tagName)
126 {
127 DCHECK(shouldCreateCustomElement(document, tagName));
128
129 // To create an element:
130 // https://dom.spec.whatwg.org/#concept-create-element
131 // 6. If definition is non-null, then:
132 // 6.2. If the synchronous custom elements flag is not set:
133 if (CustomElementDefinition* definition = definitionForName(document, tagNam e.localName()))
134 return definition->createElementAsync(document, tagName);
135
136 return createUndefinedElement(document, tagName);
137 }
138
139 HTMLElement* CustomElement::createUndefinedElement(Document& document, const Qua lifiedName& tagName)
140 {
141 DCHECK(shouldCreateCustomElement(document, tagName));
142
96 HTMLElement* element; 143 HTMLElement* element;
97 if (CustomElementsRegistry* registry = CustomElement::registry(document)) {
98 if (CustomElementDefinition* definition = registry->definitionForName(ta gName.localName())) {
99 // 6.2. If the synchronous custom elements flag is not set:
100 if (flags & AsynchronousCustomElements)
101 return createCustomElementAsync(document, *definition, tagName);
102
103 // TODO(kojii): Synchronous mode implementation coming after async.
104 }
105 }
106
107 if (V0CustomElement::isValidName(tagName.localName()) && document.registrati onContext()) { 144 if (V0CustomElement::isValidName(tagName.localName()) && document.registrati onContext()) {
108 Element* v0element = document.registrationContext()->createCustomTagElem ent(document, tagName); 145 Element* v0element = document.registrationContext()->createCustomTagElem ent(document, tagName);
109 SECURITY_DCHECK(v0element->isHTMLElement()); 146 SECURITY_DCHECK(v0element->isHTMLElement());
110 element = toHTMLElement(v0element); 147 element = toHTMLElement(v0element);
111 } else { 148 } else {
112 element = HTMLElement::create(tagName, document); 149 element = HTMLElement::create(tagName, document);
113 } 150 }
114 151
115 element->setCustomElementState(CustomElementState::Undefined); 152 element->setCustomElementState(CustomElementState::Undefined);
116 153
117 return element; 154 return element;
118 } 155 }
119 156
120 HTMLElement* CustomElement::createCustomElementAsync(Document& document,
121 CustomElementDefinition& definition, const QualifiedName& tagName)
122 {
123 // https://dom.spec.whatwg.org/#concept-create-element
124 // 6. If definition is non-null, then:
125 // 6.2. If the synchronous custom elements flag is not set:
126 // 6.2.1. Set result to a new element that implements the HTMLElement
127 // interface, with no attributes, namespace set to the HTML namespace,
128 // namespace prefix set to prefix, local name set to localName, custom
129 // element state set to "undefined", and node document set to document.
130 HTMLElement* element = HTMLElement::create(tagName, document);
131 element->setCustomElementState(CustomElementState::Undefined);
132 // 6.2.2. Enqueue a custom element upgrade reaction given result and
133 // definition.
134 enqueueUpgradeReaction(element, &definition);
135 return element;
136 }
137
138 void CustomElement::enqueueUpgradeReaction(Element* element, CustomElementDefini tion* definition) 157 void CustomElement::enqueueUpgradeReaction(Element* element, CustomElementDefini tion* definition)
139 { 158 {
140 // CEReactionsScope must be created by [CEReactions] in IDL, 159 // CEReactionsScope must be created by [CEReactions] in IDL,
141 // or callers must setup explicitly if it does not go through bindings. 160 // or callers must setup explicitly if it does not go through bindings.
142 DCHECK(CEReactionsScope::current()); 161 DCHECK(CEReactionsScope::current());
143 CEReactionsScope::current()->enqueue(element, 162 CEReactionsScope::current()->enqueue(element,
144 new CustomElementUpgradeReaction(definition)); 163 new CustomElementUpgradeReaction(definition));
145 } 164 }
146 165
147 } // namespace blink 166 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698