summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/html/custom/v0_custom_element_registration_context.cc
blob: fd728fecde313838370e42fdecf9ace83755ed46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * Copyright (C) 2013 Google Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer
 *    in the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name of Google Inc. nor the names of its contributors
 *    may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "third_party/blink/renderer/core/html/custom/v0_custom_element_registration_context.h"

#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/dom/element.h"
#include "third_party/blink/renderer/core/html/custom/v0_custom_element.h"
#include "third_party/blink/renderer/core/html/custom/v0_custom_element_definition.h"
#include "third_party/blink/renderer/core/html/custom/v0_custom_element_scheduler.h"
#include "third_party/blink/renderer/core/html/html_element.h"
#include "third_party/blink/renderer/core/html/html_unknown_element.h"
#include "third_party/blink/renderer/core/html_names.h"
#include "third_party/blink/renderer/core/svg/svg_unknown_element.h"
#include "third_party/blink/renderer/core/svg_names.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"

namespace blink {

V0CustomElementRegistrationContext::V0CustomElementRegistrationContext()
    : candidates_(V0CustomElementUpgradeCandidateMap::Create()) {}

void V0CustomElementRegistrationContext::RegisterElement(
    Document* document,
    V0CustomElementConstructorBuilder* constructor_builder,
    const AtomicString& type,
    V0CustomElement::NameSet valid_names,
    ExceptionState& exception_state) {
  V0CustomElementDefinition* definition = registry_.RegisterElement(
      document, constructor_builder, type, valid_names, exception_state);

  if (!definition)
    return;

  // Upgrade elements that were waiting for this definition.
  V0CustomElementUpgradeCandidateMap::ElementSet* upgrade_candidates =
      candidates_->TakeUpgradeCandidatesFor(definition->Descriptor());

  if (!upgrade_candidates)
    return;

  for (const auto& candidate : *upgrade_candidates)
    V0CustomElement::Define(candidate, definition);
}

Element* V0CustomElementRegistrationContext::CreateCustomTagElement(
    Document& document,
    const QualifiedName& tag_name) {
  DCHECK(V0CustomElement::IsValidName(tag_name.LocalName()));

  Element* element;

  if (HTMLNames::xhtmlNamespaceURI == tag_name.NamespaceURI()) {
    element = HTMLElement::Create(tag_name, document);
  } else if (SVGNames::svgNamespaceURI == tag_name.NamespaceURI()) {
    element = SVGUnknownElement::Create(tag_name, document);
  } else {
    // XML elements are not custom elements, so return early.
    return Element::Create(tag_name, &document);
  }

  element->SetV0CustomElementState(Element::kV0WaitingForUpgrade);
  ResolveOrScheduleResolution(element, g_null_atom);
  return element;
}

void V0CustomElementRegistrationContext::DidGiveTypeExtension(
    Element* element,
    const AtomicString& type) {
  ResolveOrScheduleResolution(element, type);
}

void V0CustomElementRegistrationContext::ResolveOrScheduleResolution(
    Element* element,
    const AtomicString& type_extension) {
  // If an element has a custom tag name it takes precedence over
  // the "is" attribute (if any).
  const AtomicString& type = V0CustomElement::IsValidName(element->localName())
                                 ? element->localName()
                                 : type_extension;
  DCHECK(!type.IsNull());

  V0CustomElementDescriptor descriptor(type, element->namespaceURI(),
                                       element->localName());
  DCHECK_EQ(element->GetV0CustomElementState(), Element::kV0WaitingForUpgrade);

  V0CustomElementScheduler::ResolveOrScheduleResolution(this, element,
                                                        descriptor);
}

void V0CustomElementRegistrationContext::Resolve(
    Element* element,
    const V0CustomElementDescriptor& descriptor) {
  V0CustomElementDefinition* definition = registry_.Find(descriptor);
  if (definition) {
    V0CustomElement::Define(element, definition);
  } else {
    DCHECK_EQ(element->GetV0CustomElementState(),
              Element::kV0WaitingForUpgrade);
    candidates_->Add(descriptor, element);
  }
}

void V0CustomElementRegistrationContext::SetIsAttributeAndTypeExtension(
    Element* element,
    const AtomicString& type) {
  DCHECK(element);
  DCHECK(!type.IsEmpty());
  element->setAttribute(HTMLNames::isAttr, type);
  SetTypeExtension(element, type);
}

void V0CustomElementRegistrationContext::SetTypeExtension(
    Element* element,
    const AtomicString& type) {
  if (!element->IsHTMLElement() && !element->IsSVGElement())
    return;

  V0CustomElementRegistrationContext* context =
      element->GetDocument().RegistrationContext();
  if (!context)
    return;

  if (element->IsV0CustomElement()) {
    // This can happen if:
    // 1. The element has a custom tag, which takes precedence over
    //    type extensions.
    // 2. Undoing a command (eg ReplaceNodeWithSpan) recycles an
    //    element but tries to overwrite its attribute list.
    return;
  }

  // Custom tags take precedence over type extensions
  DCHECK(!V0CustomElement::IsValidName(element->localName()));

  if (!V0CustomElement::IsValidName(type))
    return;

  element->SetV0CustomElementState(Element::kV0WaitingForUpgrade);
  context->DidGiveTypeExtension(element,
                                element->GetDocument().ConvertLocalName(type));
}

bool V0CustomElementRegistrationContext::NameIsDefined(
    const AtomicString& name) const {
  return registry_.NameIsDefined(name);
}

void V0CustomElementRegistrationContext::SetV1(
    const CustomElementRegistry* v1) {
  registry_.SetV1(v1);
}

void V0CustomElementRegistrationContext::Trace(blink::Visitor* visitor) {
  visitor->Trace(candidates_);
  visitor->Trace(registry_);
}

}  // namespace blink