Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / dom / custom / CustomElement.cpp
1 /*
2  * Copyright (C) 2013 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
6  * are met:
7  *
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
13  *    distribution.
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.
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
31 #include "config.h"
32 #include "core/dom/custom/CustomElement.h"
33
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"
42
43 namespace blink {
44
45 CustomElementMicrotaskImportStep* CustomElement::didCreateImport(HTMLImportChild* import)
46 {
47     return CustomElementScheduler::scheduleImport(import);
48 }
49
50 void CustomElement::didFinishLoadingImport(Document& master)
51 {
52     master.customElementMicrotaskRunQueue()->requestDispatchIfNeeded();
53 }
54
55 Vector<AtomicString>& CustomElement::embedderCustomElementNames()
56 {
57     DEFINE_STATIC_LOCAL(Vector<AtomicString>, names, ());
58     return names;
59 }
60
61 void CustomElement::addEmbedderCustomElementName(const AtomicString& name)
62 {
63     AtomicString lower = name.lower();
64     if (isValidName(lower, EmbedderNames))
65         return;
66     embedderCustomElementNames().append(lower);
67 }
68
69 static inline bool isValidNCName(const AtomicString& name)
70 {
71     if (kNotFound != name.find(':'))
72         return false;
73
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)
81             return false;
82     }
83
84     return Document::isValidName(name.string());
85 }
86
87 bool CustomElement::isValidName(const AtomicString& name, NameSet validNames)
88 {
89     if ((validNames & EmbedderNames) && kNotFound != embedderCustomElementNames().find(name))
90         return Document::isValidName(name);
91
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());
97         }
98
99         if (kNotFound == reservedNames.find(name))
100             return isValidNCName(name);
101     }
102
103     return false;
104 }
105
106 void CustomElement::define(Element* element, PassRefPtr<CustomElementDefinition> passDefinition)
107 {
108     RefPtr<CustomElementDefinition> definition(passDefinition);
109
110     switch (element->customElementState()) {
111     case Element::NotCustomElement:
112     case Element::Upgraded:
113         ASSERT_NOT_REACHED();
114         break;
115
116     case Element::WaitingForUpgrade:
117         element->setCustomElementDefinition(definition);
118         CustomElementScheduler::scheduleCallback(definition->callbacks(), element, CustomElementLifecycleCallbacks::CreatedCallback);
119         break;
120     }
121 }
122
123 void CustomElement::attributeDidChange(Element* element, const AtomicString& name, const AtomicString& oldValue, const AtomicString& newValue)
124 {
125     ASSERT(element->customElementState() == Element::Upgraded);
126     CustomElementScheduler::scheduleAttributeChangedCallback(element->customElementDefinition()->callbacks(), element, name, oldValue, newValue);
127 }
128
129 void CustomElement::didAttach(Element* element, const Document& document)
130 {
131     ASSERT(element->customElementState() == Element::Upgraded);
132     if (!document.domWindow())
133         return;
134     CustomElementScheduler::scheduleCallback(element->customElementDefinition()->callbacks(), element, CustomElementLifecycleCallbacks::AttachedCallback);
135 }
136
137 void CustomElement::didDetach(Element* element, const Document& document)
138 {
139     ASSERT(element->customElementState() == Element::Upgraded);
140     if (!document.domWindow())
141         return;
142     CustomElementScheduler::scheduleCallback(element->customElementDefinition()->callbacks(), element, CustomElementLifecycleCallbacks::DetachedCallback);
143 }
144
145 void CustomElement::wasDestroyed(Element* element)
146 {
147     switch (element->customElementState()) {
148     case Element::NotCustomElement:
149         ASSERT_NOT_REACHED();
150         break;
151
152     case Element::WaitingForUpgrade:
153     case Element::Upgraded:
154         CustomElementObserver::notifyElementWasDestroyed(element);
155         break;
156     }
157 }
158
159 } // namespace blink