Index: Source/core/dom/CustomElementRegistrationContextualizer.cpp |
diff --git a/Source/core/dom/CustomElementRegistrationContextualizer.cpp b/Source/core/dom/CustomElementRegistrationContextualizer.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f013d52697fd27b9dac8f8dbeea1397996725430 |
--- /dev/null |
+++ b/Source/core/dom/CustomElementRegistrationContextualizer.cpp |
@@ -0,0 +1,135 @@ |
+/* |
+ * 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 "config.h" |
+#include "CustomElementRegistrationContextualizer.h" |
+ |
+#include "RuntimeEnabledFeatures.h" |
+#include "core/dom/CustomElementRegistrationContext.h" |
+#include "core/dom/Document.h" |
+ |
+namespace WebCore { |
+ |
+// These three methods--documentCanProcessCustomElements and |
+// documentShouldProcessCustomElements (overloaded)--implement the |
+// policy of whether a document should process Custom Elements at all. |
+ |
+static bool documentCanProcessCustomElements(Document* document) |
+{ |
+ return (document->isHTMLDocument() || document->isSVGDocument()) |
+ && !document->isPluginDocument() |
+ && RuntimeEnabledFeatures::customDOMElementsEnabled(); |
+} |
+ |
+bool CustomElementRegistrationContextualizer::documentShouldProcessCustomElements(DocumentCreationReason reason, Document* document) |
+{ |
+ if (!documentCanProcessCustomElements(document)) |
+ return false; |
+ |
+ switch (reason) { |
+ case Loader: |
+ return true; |
+ |
+ case DOMParser_parseFromString: |
+ case DocumentWithXSLTPI: |
+ case XHR_responseXML: |
+ case XSLTProcessor_transformToDocument: |
+ return false; |
+ |
+ default: |
+ ASSERT_NOT_REACHED(); |
+ return false; |
+ } |
+} |
+ |
+bool CustomElementRegistrationContextualizer::documentShouldProcessCustomElements(ContextualizedDocumentCreationReason reason, Document* document) |
+{ |
+ if (!documentCanProcessCustomElements(document)) |
+ return false; |
+ |
+ switch (reason) { |
+ case DOMImplementation_createDocument: |
+ case HTMLImport: |
+ case Template: |
+ return true; |
+ |
+ case InspectorDOMPatch: |
+ case PasteboardFragment: |
+ return false; |
+ |
+ default: |
+ ASSERT_NOT_REACHED(); |
+ return false; |
+ } |
+} |
+ |
+// documentShouldShareRegistrationContext implements the policy of |
+// whether a document should share the registration context it was |
+// created in, or get a new context. |
+bool CustomElementRegistrationContextualizer::documentShouldShareRegistrationContext(ContextualizedDocumentCreationReason reason, Document* document) |
+{ |
+ switch (reason) { |
+ case DOMImplementation_createDocument: |
+ case HTMLImport: |
+ case Template: |
+ return true; |
+ |
+ case InspectorDOMPatch: |
+ case PasteboardFragment: |
+ default: |
+ ASSERT_NOT_REACHED(); |
+ return false; |
+ } |
+} |
+ |
+void CustomElementRegistrationContextualizer::didCreateDocument(DocumentCreationReason reason, Document* document) |
+{ |
+ document->setRegistrationContext( |
+ documentShouldProcessCustomElements(reason, document) |
+ ? CustomElementRegistrationContext::create() |
+ : CustomElementRegistrationContext::nullRegistrationContext()); |
+} |
+ |
+void CustomElementRegistrationContextualizer::didCreateRelatedDocument(PassRefPtr<CustomElementRegistrationContext> existingContext, ContextualizedDocumentCreationReason reason, Document* document) |
+{ |
+ if (!documentShouldProcessCustomElements(reason, document)) { |
+ document->setRegistrationContext(CustomElementRegistrationContext::nullRegistrationContext()); |
+ return; |
+ } |
+ |
+ if (documentShouldShareRegistrationContext(reason, document)) { |
+ document->setRegistrationContext(existingContext); |
+ } else { |
+ ASSERT_NOT_REACHED(); // Note: There are none of these right now. |
+ document->setRegistrationContext(CustomElementRegistrationContext::create()); |
+ } |
+} |
+ |
+} // namespace WebCore |