Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 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" |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame] | 36 | #include "bindings/v8/ExceptionState.h" |
Ben Murdoch | 8375017 | 2013-07-24 10:36:59 +0100 | [diff] [blame] | 37 | #include "core/dom/CustomElement.h" |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 38 | #include "core/dom/CustomElementDefinition.h" |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 39 | #include "core/dom/Element.h" |
| 40 | #include "core/html/HTMLElement.h" |
| 41 | #include "core/html/HTMLUnknownElement.h" |
Torne (Richard Coles) | f5e4ad5 | 2013-08-05 13:57:57 +0100 | [diff] [blame] | 42 | #include "core/svg/SVGUnknownElement.h" |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 43 | #include "wtf/RefPtr.h" |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 44 | |
| 45 | namespace WebCore { |
| 46 | |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame] | 47 | void CustomElementRegistrationContext::registerElement(Document* document, CustomElementConstructorBuilder* constructorBuilder, const AtomicString& type, ExceptionState& es) |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 48 | { |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame] | 49 | CustomElementDefinition* definition = m_registry.registerElement(document, constructorBuilder, type, es); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 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 | |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 60 | PassRefPtr<Element> CustomElementRegistrationContext::createCustomTagElement(Document* document, const QualifiedName& tagName) |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 61 | { |
Ben Murdoch | 1fad5ca | 2013-08-07 11:05:11 +0100 | [diff] [blame] | 62 | ASSERT(CustomElement::isCustomTagName(tagName.localName())); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 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()) { |
Torne (Richard Coles) | f5e4ad5 | 2013-08-05 13:57:57 +0100 | [diff] [blame] | 72 | element = SVGUnknownElement::create(tagName, document); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 73 | } else { |
| 74 | // XML elements are not custom elements, so return early. |
| 75 | return Element::create(tagName, document); |
| 76 | } |
| 77 | |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 78 | resolve(element.get(), nullAtom); |
Ben Murdoch | 0019e4e | 2013-07-18 11:57:54 +0100 | [diff] [blame] | 79 | return element.release(); |
| 80 | } |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 81 | |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 82 | void CustomElementRegistrationContext::didGiveTypeExtension(Element* element, const AtomicString& type) |
Ben Murdoch | 0019e4e | 2013-07-18 11:57:54 +0100 | [diff] [blame] | 83 | { |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 84 | resolve(element, type); |
Ben Murdoch | 0019e4e | 2013-07-18 11:57:54 +0100 | [diff] [blame] | 85 | } |
| 86 | |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 87 | void CustomElementRegistrationContext::resolve(Element* element, const AtomicString& typeExtension) |
Ben Murdoch | 0019e4e | 2013-07-18 11:57:54 +0100 | [diff] [blame] | 88 | { |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 89 | // If an element has a custom tag name it takes precedence over |
| 90 | // the "is" attribute (if any). |
Ben Murdoch | 1fad5ca | 2013-08-07 11:05:11 +0100 | [diff] [blame] | 91 | const AtomicString& type = CustomElement::isCustomTagName(element->localName()) |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 92 | ? element->localName() |
| 93 | : typeExtension; |
| 94 | ASSERT(!type.isNull()); |
| 95 | |
| 96 | CustomElementDescriptor descriptor(type, element->namespaceURI(), element->localName()); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 97 | CustomElementDefinition* definition = m_registry.find(descriptor); |
| 98 | if (definition) |
Ben Murdoch | 0019e4e | 2013-07-18 11:57:54 +0100 | [diff] [blame] | 99 | didResolveElement(definition, element); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 100 | else |
Ben Murdoch | 0019e4e | 2013-07-18 11:57:54 +0100 | [diff] [blame] | 101 | didCreateUnresolvedElement(descriptor, element); |
| 102 | } |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 103 | |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 104 | void CustomElementRegistrationContext::didResolveElement(CustomElementDefinition* definition, Element* element) |
Ben Murdoch | 0019e4e | 2013-07-18 11:57:54 +0100 | [diff] [blame] | 105 | { |
Ben Murdoch | 8375017 | 2013-07-24 10:36:59 +0100 | [diff] [blame] | 106 | CustomElement::define(element, definition); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 107 | } |
| 108 | |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 109 | void CustomElementRegistrationContext::didCreateUnresolvedElement(const CustomElementDescriptor& descriptor, Element* element) |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 110 | { |
| 111 | m_candidates.add(descriptor, element); |
| 112 | } |
| 113 | |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 114 | void CustomElementRegistrationContext::customElementWasDestroyed(Element* element) |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 115 | { |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 116 | m_candidates.remove(element); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | PassRefPtr<CustomElementRegistrationContext> CustomElementRegistrationContext::create() |
| 120 | { |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 121 | return adoptRef(new CustomElementRegistrationContext()); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 122 | } |
| 123 | |
Ben Murdoch | 0019e4e | 2013-07-18 11:57:54 +0100 | [diff] [blame] | 124 | void CustomElementRegistrationContext::setIsAttributeAndTypeExtension(Element* element, const AtomicString& type) |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 125 | { |
| 126 | ASSERT(element); |
Ben Murdoch | 0019e4e | 2013-07-18 11:57:54 +0100 | [diff] [blame] | 127 | ASSERT(!type.isEmpty()); |
| 128 | element->setAttribute(HTMLNames::isAttr, type); |
| 129 | setTypeExtension(element, type); |
| 130 | } |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 131 | |
Ben Murdoch | 0019e4e | 2013-07-18 11:57:54 +0100 | [diff] [blame] | 132 | void CustomElementRegistrationContext::setTypeExtension(Element* element, const AtomicString& type) |
| 133 | { |
| 134 | if (!element->isHTMLElement() && !element->isSVGElement()) |
| 135 | return; |
| 136 | |
Ben Murdoch | 1fad5ca | 2013-08-07 11:05:11 +0100 | [diff] [blame] | 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::isCustomTagName(element->localName())); |
Ben Murdoch | 0019e4e | 2013-07-18 11:57:54 +0100 | [diff] [blame] | 148 | |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 149 | if (CustomElementRegistrationContext* context = element->document()->registrationContext()) |
| 150 | context->didGiveTypeExtension(element, type); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | } // namespace WebCore |