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/CustomElementRegistrationContext.h" 33 34 #include "HTMLNames.h" 35 #include "SVGNames.h" 36 #include "bindings/v8/ExceptionState.h" 37 #include "core/dom/Element.h" 38 #include "core/dom/custom/CustomElement.h" 39 #include "core/dom/custom/CustomElementCallbackScheduler.h" 40 #include "core/dom/custom/CustomElementDefinition.h" 41 #include "core/html/HTMLElement.h" 42 #include "core/html/HTMLUnknownElement.h" 43 #include "core/svg/SVGUnknownElement.h" 44 #include "wtf/RefPtr.h" 45 46 namespace WebCore { 47 48 void CustomElementRegistrationContext::registerElement(Document* document, CustomElementConstructorBuilder* constructorBuilder, const AtomicString& type, CustomElement::NameSet validNames, ExceptionState& exceptionState) 49 { 50 CustomElementDefinition* definition = m_registry.registerElement(document, constructorBuilder, type, validNames, exceptionState); 51 52 if (!definition) 53 return; 54 55 // Upgrade elements that were waiting for this definition. 56 const CustomElementUpgradeCandidateMap::ElementSet& upgradeCandidates = m_candidates.takeUpgradeCandidatesFor(definition->descriptor()); 57 for (CustomElementUpgradeCandidateMap::ElementSet::const_iterator it = upgradeCandidates.begin(); it != upgradeCandidates.end(); ++it) 58 didResolveElement(definition, *it); 59 } 60 61 PassRefPtr<Element> CustomElementRegistrationContext::createCustomTagElement(Document& document, const QualifiedName& tagName) 62 { 63 ASSERT(CustomElement::isValidName(tagName.localName())); 64 65 RefPtr<Element> element; 66 67 if (HTMLNames::xhtmlNamespaceURI == tagName.namespaceURI()) { 68 element = HTMLElement::create(tagName, document); 69 } else if (SVGNames::svgNamespaceURI == tagName.namespaceURI()) { 70 element = SVGUnknownElement::create(tagName, document); 71 } else { 72 // XML elements are not custom elements, so return early. 73 return Element::create(tagName, &document); 74 } 75 76 element->setCustomElementState(Element::WaitingForUpgrade); 77 resolve(element.get(), nullAtom); 78 return element.release(); 79 } 80 81 void CustomElementRegistrationContext::didGiveTypeExtension(Element* element, const AtomicString& type) 82 { 83 resolve(element, type); 84 } 85 86 void CustomElementRegistrationContext::resolve(Element* element, const AtomicString& typeExtension) 87 { 88 // If an element has a custom tag name it takes precedence over 89 // the "is" attribute (if any). 90 const AtomicString& type = CustomElement::isValidName(element->localName()) 91 ? element->localName() 92 : typeExtension; 93 ASSERT(!type.isNull()); 94 95 CustomElementDescriptor descriptor(type, element->namespaceURI(), element->localName()); 96 CustomElementDefinition* definition = m_registry.find(descriptor); 97 if (definition) 98 didResolveElement(definition, element); 99 else 100 didCreateUnresolvedElement(descriptor, element); 101 } 102 103 void CustomElementRegistrationContext::didResolveElement(CustomElementDefinition* definition, Element* element) 104 { 105 CustomElement::define(element, definition); 106 } 107 108 void CustomElementRegistrationContext::didCreateUnresolvedElement(const CustomElementDescriptor& descriptor, Element* element) 109 { 110 ASSERT(element->customElementState() == Element::WaitingForUpgrade); 111 m_candidates.add(descriptor, element); 112 } 113 114 PassRefPtr<CustomElementRegistrationContext> CustomElementRegistrationContext::create() 115 { 116 return adoptRef(new CustomElementRegistrationContext()); 117 } 118 119 void CustomElementRegistrationContext::setIsAttributeAndTypeExtension(Element* element, const AtomicString& type) 120 { 121 ASSERT(element); 122 ASSERT(!type.isEmpty()); 123 element->setAttribute(HTMLNames::isAttr, type); 124 setTypeExtension(element, type); 125 } 126 127 void CustomElementRegistrationContext::setTypeExtension(Element* element, const AtomicString& type) 128 { 129 if (!element->isHTMLElement() && !element->isSVGElement()) 130 return; 131 132 if (element->isCustomElement()) { 133 // This can happen if: 134 // 1. The element has a custom tag, which takes precedence over 135 // type extensions. 136 // 2. Undoing a command (eg ReplaceNodeWithSpan) recycles an 137 // element but tries to overwrite its attribute list. 138 return; 139 } 140 141 // Custom tags take precedence over type extensions 142 ASSERT(!CustomElement::isValidName(element->localName())); 143 144 element->setCustomElementState(Element::WaitingForUpgrade); 145 146 if (CustomElementRegistrationContext* context = element->document().registrationContext()) 147 context->didGiveTypeExtension(element, type); 148 } 149 150 } // namespace WebCore 151