OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CustomElementTestHelpers_h |
| 6 #define CustomElementTestHelpers_h |
| 7 |
| 8 #include "bindings/core/v8/ExceptionState.h" |
| 9 #include "core/dom/Document.h" |
| 10 #include "core/dom/Element.h" |
| 11 #include "core/dom/QualifiedName.h" |
| 12 #include "core/html/HTMLDocument.h" |
| 13 #include "platform/heap/Handle.h" |
| 14 #include "wtf/text/AtomicString.h" |
| 15 |
| 16 #include <utility> |
| 17 #include <vector> |
| 18 |
| 19 namespace blink { |
| 20 |
| 21 class CreateElement { |
| 22 STACK_ALLOCATED() |
| 23 public: |
| 24 CreateElement(const AtomicString& localName) |
| 25 : m_namespaceURI(HTMLNames::xhtmlNamespaceURI) |
| 26 , m_localName(localName) |
| 27 { |
| 28 } |
| 29 |
| 30 CreateElement& inDocument(Document* document) |
| 31 { |
| 32 m_document = document; |
| 33 return *this; |
| 34 } |
| 35 |
| 36 CreateElement& inNamespace(const AtomicString& uri) |
| 37 { |
| 38 m_namespaceURI = uri; |
| 39 return *this; |
| 40 } |
| 41 |
| 42 CreateElement& withId(const AtomicString& id) |
| 43 { |
| 44 m_attributes.push_back(std::make_pair(HTMLNames::idAttr, id)); |
| 45 return *this; |
| 46 } |
| 47 |
| 48 CreateElement& withIsAttribute(const AtomicString& value) |
| 49 { |
| 50 m_attributes.push_back(std::make_pair(HTMLNames::isAttr, value)); |
| 51 return *this; |
| 52 } |
| 53 |
| 54 operator Element*() const |
| 55 { |
| 56 Document* document = m_document.get(); |
| 57 if (!document) |
| 58 document = HTMLDocument::create(); |
| 59 NonThrowableExceptionState noExceptions; |
| 60 Element* element = document->createElementNS( |
| 61 m_namespaceURI, |
| 62 m_localName, |
| 63 noExceptions); |
| 64 for (const auto& attribute : m_attributes) |
| 65 element->setAttribute(attribute.first, attribute.second); |
| 66 return element; |
| 67 } |
| 68 |
| 69 private: |
| 70 Member<Document> m_document; |
| 71 AtomicString m_namespaceURI; |
| 72 AtomicString m_localName; |
| 73 std::vector<std::pair<QualifiedName, AtomicString>> m_attributes; |
| 74 }; |
| 75 |
| 76 } // namespace blink |
| 77 |
| 78 #endif // CustomElementTestHelpers_h |
OLD | NEW |