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