2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * 3. Neither the name of Google Inc. nor the names of its contributors
15 * may be used to endorse or promote products derived from this
16 * software without specific prior written permission.
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.
32 #include "core/dom/custom/CustomElement.h"
34 #include "core/HTMLNames.h"
35 #include "core/MathMLNames.h"
36 #include "core/SVGNames.h"
37 #include "core/dom/Document.h"
38 #include "core/dom/Element.h"
39 #include "core/dom/custom/CustomElementMicrotaskRunQueue.h"
40 #include "core/dom/custom/CustomElementObserver.h"
41 #include "core/dom/custom/CustomElementScheduler.h"
45 CustomElementMicrotaskImportStep* CustomElement::didCreateImport(HTMLImportChild* import)
47 return CustomElementScheduler::scheduleImport(import);
50 void CustomElement::didFinishLoadingImport(Document& master)
52 master.customElementMicrotaskRunQueue()->requestDispatchIfNeeded();
55 Vector<AtomicString>& CustomElement::embedderCustomElementNames()
57 DEFINE_STATIC_LOCAL(Vector<AtomicString>, names, ());
61 void CustomElement::addEmbedderCustomElementName(const AtomicString& name)
63 AtomicString lower = name.lower();
64 if (isValidName(lower, EmbedderNames))
66 embedderCustomElementNames().append(lower);
69 static inline bool isValidNCName(const AtomicString& name)
71 if (kNotFound != name.find(':'))
74 if (!name.string().is8Bit()) {
75 const UChar32 c = name.characters16()[0];
76 // These characters comes under CombiningChar in NCName and according to
77 // NCName only BaseChar and Ideodgraphic can come as first chars.
78 // Also these characters come under Letter_Other in UnicodeData, thats
79 // why they pass as valid document name.
80 if (c == 0x0B83 || c == 0x0F88 || c == 0x0F89 || c == 0x0F8A || c == 0x0F8B)
84 return Document::isValidName(name.string());
87 bool CustomElement::isValidName(const AtomicString& name, NameSet validNames)
89 if ((validNames & EmbedderNames) && kNotFound != embedderCustomElementNames().find(name))
90 return Document::isValidName(name);
92 if ((validNames & StandardNames) && kNotFound != name.find('-')) {
93 DEFINE_STATIC_LOCAL(Vector<AtomicString>, reservedNames, ());
94 if (reservedNames.isEmpty()) {
95 // FIXME(crbug.com/426605): We should be able to remove this.
96 reservedNames.append(MathMLNames::annotation_xmlTag.localName());
99 if (kNotFound == reservedNames.find(name))
100 return isValidNCName(name);
106 void CustomElement::define(Element* element, PassRefPtr<CustomElementDefinition> passDefinition)
108 RefPtr<CustomElementDefinition> definition(passDefinition);
110 switch (element->customElementState()) {
111 case Element::NotCustomElement:
112 case Element::Upgraded:
113 ASSERT_NOT_REACHED();
116 case Element::WaitingForUpgrade:
117 element->setCustomElementDefinition(definition);
118 CustomElementScheduler::scheduleCallback(definition->callbacks(), element, CustomElementLifecycleCallbacks::CreatedCallback);
123 void CustomElement::attributeDidChange(Element* element, const AtomicString& name, const AtomicString& oldValue, const AtomicString& newValue)
125 ASSERT(element->customElementState() == Element::Upgraded);
126 CustomElementScheduler::scheduleAttributeChangedCallback(element->customElementDefinition()->callbacks(), element, name, oldValue, newValue);
129 void CustomElement::didAttach(Element* element, const Document& document)
131 ASSERT(element->customElementState() == Element::Upgraded);
132 if (!document.domWindow())
134 CustomElementScheduler::scheduleCallback(element->customElementDefinition()->callbacks(), element, CustomElementLifecycleCallbacks::AttachedCallback);
137 void CustomElement::didDetach(Element* element, const Document& document)
139 ASSERT(element->customElementState() == Element::Upgraded);
140 if (!document.domWindow())
142 CustomElementScheduler::scheduleCallback(element->customElementDefinition()->callbacks(), element, CustomElementLifecycleCallbacks::DetachedCallback);
145 void CustomElement::wasDestroyed(Element* element)
147 switch (element->customElementState()) {
148 case Element::NotCustomElement:
149 ASSERT_NOT_REACHED();
152 case Element::WaitingForUpgrade:
153 case Element::Upgraded:
154 CustomElementObserver::notifyElementWasDestroyed(element);