From 178be1d89dbb322cd5c08668f2027388bb374f4a Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Fri, 20 Feb 2015 12:06:15 -0800 Subject: [PATCH] Remove almost all clients of isHTMLElement This CL is progress towards deleting the concept of an HTMLElement entirely. We won't actually get all the way there in this CL series, but we're getting closer. This CL also will let us make custom elements just be Elements instead of HTMLElements. R=eseidel@chromium.org Review URL: https://codereview.chromium.org/942933003 --- .../scripts/templates/ElementFactory.cpp.tmpl | 9 +++------ .../scripts/templates/ElementFactory.h.tmpl | 4 ++-- engine/core/dom/Document.cpp | 4 ++-- engine/core/dom/Element.cpp | 4 ---- engine/core/dom/Element.h | 6 ------ engine/core/dom/Node.cpp | 4 ++-- engine/core/dom/Node.h | 4 +--- engine/core/editing/ApplyBlockElementCommand.cpp | 8 -------- engine/core/editing/ApplyBlockElementCommand.h | 1 - engine/core/editing/EditingStyle.cpp | 2 +- engine/core/editing/EditingStyle.h | 2 +- engine/core/editing/ReplaceSelectionCommand.cpp | 4 ++-- engine/core/editing/htmlediting.cpp | 14 ++------------ engine/core/editing/htmlediting.h | 2 -- engine/core/html/HTMLElement.h | 3 +-- engine/core/html/parser/HTMLConstructionSite.cpp | 16 ++++------------ engine/core/html/parser/HTMLConstructionSite.h | 3 +-- 17 files changed, 22 insertions(+), 68 deletions(-) diff --git a/engine/build/scripts/templates/ElementFactory.cpp.tmpl b/engine/build/scripts/templates/ElementFactory.cpp.tmpl index f8e6ca6fb95..77b13be2e43 100644 --- a/engine/build/scripts/templates/ElementFactory.cpp.tmpl +++ b/engine/build/scripts/templates/ElementFactory.cpp.tmpl @@ -71,7 +71,7 @@ static void create{{namespace}}FunctionMap() g_constructors->set(data[i].tag.localName(), data[i].func); } -PassRefPtr<{{namespace}}Element> {{namespace}}ElementFactory::create{{namespace}}Element( +PassRefPtr {{namespace}}ElementFactory::createElement( const AtomicString& localName, Document& document, bool createdByParser) @@ -81,11 +81,8 @@ PassRefPtr<{{namespace}}Element> {{namespace}}ElementFactory::create{{namespace} if (ConstructorFunction function = g_constructors->get(localName)) return function(document, createdByParser); - if (CustomElement::isValidName(localName)) { - RefPtr element = document.registrationContext().createCustomTagElement(document, QualifiedName(localName)); - ASSERT_WITH_SECURITY_IMPLICATION(element->is{{namespace}}Element()); - return static_pointer_cast<{{namespace}}Element>(element.release()); - } + if (CustomElement::isValidName(localName)) + return document.registrationContext().createCustomTagElement(document, QualifiedName(localName)); return {{fallback_interface}}::create(QualifiedName(localName), document); } diff --git a/engine/build/scripts/templates/ElementFactory.h.tmpl b/engine/build/scripts/templates/ElementFactory.h.tmpl index a46d6983a6b..296d71aa55e 100644 --- a/engine/build/scripts/templates/ElementFactory.h.tmpl +++ b/engine/build/scripts/templates/ElementFactory.h.tmpl @@ -10,11 +10,11 @@ namespace blink { class Document; -class {{namespace}}Element; +class Element; class {{namespace}}ElementFactory { public: - static PassRefPtr<{{namespace}}Element> create{{namespace}}Element( + static PassRefPtr createElement( const AtomicString& localName, Document&, bool createdByParser = true); diff --git a/engine/core/dom/Document.cpp b/engine/core/dom/Document.cpp index d742219eb11..6c3752e9c22 100644 --- a/engine/core/dom/Document.cpp +++ b/engine/core/dom/Document.cpp @@ -415,7 +415,7 @@ PassRefPtr Document::createElement(const AtomicString& name, ExceptionS return nullptr; } - return HTMLElementFactory::createHTMLElement(name, *this, false); + return HTMLElementFactory::createElement(name, *this, false); } PassRefPtr Document::registerElement(DartState*, const AtomicString& name, ExceptionState& exceptionState) @@ -585,7 +585,7 @@ PassRefPtr Document::adoptNode(PassRefPtr source, ExceptionState& ex PassRefPtr Document::createElement(const QualifiedName& qName, bool createdByParser) { // FIXME(sky): This should only take a local name. - return HTMLElementFactory::createHTMLElement(qName.localName(), *this, createdByParser); + return HTMLElementFactory::createElement(qName.localName(), *this, createdByParser); } String Document::readyState() const diff --git a/engine/core/dom/Element.cpp b/engine/core/dom/Element.cpp index 276310d3893..be09a955fea 100644 --- a/engine/core/dom/Element.cpp +++ b/engine/core/dom/Element.cpp @@ -173,10 +173,6 @@ PassRefPtr Element::cloneElementWithChildren() PassRefPtr Element::cloneElementWithoutChildren() { RefPtr clone = cloneElementWithoutAttributesAndChildren(); - // This will catch HTML elements in the wrong namespace that are not correctly copied. - // This is a sanity check as HTML overloads some of the DOM methods. - ASSERT(isHTMLElement() == clone->isHTMLElement()); - clone->cloneDataFromElement(*this); return clone.release(); } diff --git a/engine/core/dom/Element.h b/engine/core/dom/Element.h index fbfe3298813..63671715ab6 100644 --- a/engine/core/dom/Element.h +++ b/engine/core/dom/Element.h @@ -135,7 +135,6 @@ public: String tagName() const { return nodeName(); } bool hasTagName(const QualifiedName& tagName) const { return m_tagName == tagName; } - bool hasTagName(const HTMLQualifiedName& name) const { return hasLocalName(name.localName()); } // A fast function for checking the local name against another atomic string. bool hasLocalName(const AtomicString& other) const { return m_tagName.localName() == other; } @@ -521,11 +520,6 @@ inline ShadowRoot* Node::shadowRoot() const return toElement(this)->shadowRoot(); } -inline bool Node::hasTagName(const HTMLQualifiedName& name) const -{ - return isHTMLElement() && toElement(*this).hasTagName(name); -} - inline void Element::invalidateStyleAttribute() { ASSERT(elementData()); diff --git a/engine/core/dom/Node.cpp b/engine/core/dom/Node.cpp index 73529a49a91..2837a490dbf 100644 --- a/engine/core/dom/Node.cpp +++ b/engine/core/dom/Node.cpp @@ -461,7 +461,7 @@ bool Node::hasEditableStyle(EditableLevel editableLevel, UserSelectAllTreatment // would fire in the middle of Document::setFocusedNode(). for (const Node* node = this; node; node = node->parentNode()) { - if (node->isHTMLElement() && node->renderer()) { + if (node->isElementNode() && node->renderer()) { // Elements with user-select: all style are considered atomic // therefore non editable. if (Position::nodeIsUserSelectAll(node) && treatment == UserSelectAllIsAlwaysNonEditable) @@ -1626,7 +1626,7 @@ void Node::setCustomElementState(CustomElementState newState) break; } - ASSERT(isHTMLElement()); + ASSERT(isElementNode()); setFlag(CustomElementFlag); setFlag(newState == Upgraded, CustomElementUpgradedFlag); } diff --git a/engine/core/dom/Node.h b/engine/core/dom/Node.h index 1b9166e115e..ac5dfdf75ef 100644 --- a/engine/core/dom/Node.h +++ b/engine/core/dom/Node.h @@ -53,7 +53,6 @@ class EventListener; class ExceptionState; class FloatPoint; class LocalFrame; -class HTMLQualifiedName; class IntRect; class KeyboardEvent; class NSResolver; @@ -134,7 +133,6 @@ public: // DOM methods & attributes for Node - bool hasTagName(const HTMLQualifiedName&) const; virtual String nodeName() const = 0; virtual NodeType nodeType() const = 0; ContainerNode* parentNode() const; @@ -197,7 +195,7 @@ public: // PseudoElements and VTTElements. It's possible we can just eliminate all the checks // since those elements will never have class names, inline style, or other things that // this apparently guards against. - bool isStyledElement() const { return isHTMLElement(); } + bool isStyledElement() const { return isElementNode(); } bool isDocumentNode() const; bool isTreeScope() const; diff --git a/engine/core/editing/ApplyBlockElementCommand.cpp b/engine/core/editing/ApplyBlockElementCommand.cpp index 121ffc2f587..eeecf7a6ede 100644 --- a/engine/core/editing/ApplyBlockElementCommand.cpp +++ b/engine/core/editing/ApplyBlockElementCommand.cpp @@ -230,12 +230,4 @@ VisiblePosition ApplyBlockElementCommand::endOfNextParagrahSplittingTextNodesIfN return VisiblePosition(Position(text.get(), position.offsetInContainerNode() - 1)); } -PassRefPtr ApplyBlockElementCommand::createBlockElement() const -{ - RefPtr element = createHTMLElement(document(), m_tagName); - if (m_inlineStyle.length()) - element->setAttribute(HTMLNames::styleAttr, m_inlineStyle); - return element.release(); -} - } diff --git a/engine/core/editing/ApplyBlockElementCommand.h b/engine/core/editing/ApplyBlockElementCommand.h index 5a55166c98e..69f04b021e6 100644 --- a/engine/core/editing/ApplyBlockElementCommand.h +++ b/engine/core/editing/ApplyBlockElementCommand.h @@ -42,7 +42,6 @@ protected: ApplyBlockElementCommand(Document&, const QualifiedName& tagName); virtual void formatSelection(const VisiblePosition& startOfSelection, const VisiblePosition& endOfSelection); - PassRefPtr createBlockElement() const; const QualifiedName tagName() const { return m_tagName; } private: diff --git a/engine/core/editing/EditingStyle.cpp b/engine/core/editing/EditingStyle.cpp index b5ba16e6497..e094d785756 100644 --- a/engine/core/editing/EditingStyle.cpp +++ b/engine/core/editing/EditingStyle.cpp @@ -459,7 +459,7 @@ static const Vector >& htmlAttributeEquivalents( return HTMLAttributeEquivalents; } -bool EditingStyle::elementIsStyledSpanOrHTMLEquivalent(const HTMLElement* element) +bool EditingStyle::elementIsStyledSpanOrHTMLEquivalent(const Element* element) { ASSERT(element); bool elementIsSpanOrElementEquivalent = false; diff --git a/engine/core/editing/EditingStyle.h b/engine/core/editing/EditingStyle.h index 6a4b2ab270b..7400724639d 100644 --- a/engine/core/editing/EditingStyle.h +++ b/engine/core/editing/EditingStyle.h @@ -104,7 +104,7 @@ public: void removeBlockProperties(); - static bool elementIsStyledSpanOrHTMLEquivalent(const HTMLElement*); + static bool elementIsStyledSpanOrHTMLEquivalent(const Element*); void mergeTypingStyle(Document*); diff --git a/engine/core/editing/ReplaceSelectionCommand.cpp b/engine/core/editing/ReplaceSelectionCommand.cpp index 3357f532dba..9382905b052 100644 --- a/engine/core/editing/ReplaceSelectionCommand.cpp +++ b/engine/core/editing/ReplaceSelectionCommand.cpp @@ -489,12 +489,12 @@ static bool isInlineHTMLElementWithStyle(const Node* node) if (isBlock(node)) return false; - if (!node->isHTMLElement()) + if (!node->isElementNode()) return false; // We can skip over elements whose class attribute is // one of our internal classes. - const HTMLElement* element = toHTMLElement(node); + const Element* element = toElement(node); const AtomicString& classAttributeValue = element->getAttribute(HTMLNames::classAttr); if (classAttributeValue == AppleTabSpanClass) return true; diff --git a/engine/core/editing/htmlediting.cpp b/engine/core/editing/htmlediting.cpp index 369121b493b..9ab7639ddea 100644 --- a/engine/core/editing/htmlediting.cpp +++ b/engine/core/editing/htmlediting.cpp @@ -390,7 +390,7 @@ bool isSpecialHTMLElement(const Node* n) if (!n) return false; - if (!n->isHTMLElement()) + if (!n->isElementNode()) return false; if (n->isLink()) @@ -608,7 +608,7 @@ Element* enclosingAnchorElement(const Position& p) bool canMergeLists(Element* firstList, Element* secondList) { - if (!firstList || !secondList || !firstList->isHTMLElement() || !secondList->isHTMLElement()) + if (!firstList || !secondList) return false; return firstList->hasTagName(secondList->tagQName()) // make sure the list types match (ol vs. ul) @@ -633,16 +633,6 @@ PassRefPtr createDefaultParagraphElement(Document& document) return nullptr; } -PassRefPtr createHTMLElement(Document& document, const QualifiedName& name) -{ - return createHTMLElement(document, name.localName()); -} - -PassRefPtr createHTMLElement(Document& document, const AtomicString& tagName) -{ - return HTMLElementFactory::createHTMLElement(tagName, document, false); -} - bool isNodeRendered(const Node *node) { return node && node->renderer(); diff --git a/engine/core/editing/htmlediting.h b/engine/core/editing/htmlediting.h index 059f00e1595..391f03b19f2 100644 --- a/engine/core/editing/htmlediting.h +++ b/engine/core/editing/htmlediting.h @@ -198,8 +198,6 @@ PassRefPtr createRange(Document&, const VisiblePosition& start, const Vis // Functions returning HTMLElement PassRefPtr createDefaultParagraphElement(Document&); -PassRefPtr createHTMLElement(Document&, const QualifiedName&); -PassRefPtr createHTMLElement(Document&, const AtomicString&); // ------------------------------------------------------------------------- // Element diff --git a/engine/core/html/HTMLElement.h b/engine/core/html/HTMLElement.h index f8d0db81726..3b07bc6f23f 100644 --- a/engine/core/html/HTMLElement.h +++ b/engine/core/html/HTMLElement.h @@ -56,8 +56,7 @@ inline HTMLElement::HTMLElement(const QualifiedName& tagName, Document& document #define DEFINE_HTMLELEMENT_TYPE_CASTS_WITH_FUNCTION(thisType) \ inline bool is##thisType(const thisType* element); \ inline bool is##thisType(const thisType& element); \ - inline bool is##thisType(const HTMLElement* element) { return element && is##thisType(*element); } \ - inline bool is##thisType(const Node& node) { return node.isHTMLElement() ? is##thisType(toHTMLElement(node)) : false; } \ + inline bool is##thisType(const Node& node) { return node.isElementNode() ? is##thisType(toElement(node)) : false; } \ inline bool is##thisType(const Node* node) { return node && is##thisType(*node); } \ inline bool is##thisType(const Element* element) { return element && is##thisType(*element); } \ template inline bool is##thisType(const PassRefPtr& node) { return is##thisType(node.get()); } \ diff --git a/engine/core/html/parser/HTMLConstructionSite.cpp b/engine/core/html/parser/HTMLConstructionSite.cpp index 1d6c8b1f909..b9aaa5691f8 100644 --- a/engine/core/html/parser/HTMLConstructionSite.cpp +++ b/engine/core/html/parser/HTMLConstructionSite.cpp @@ -275,7 +275,7 @@ void HTMLConstructionSite::finishedParsing() void HTMLConstructionSite::insertHTMLElement(AtomicHTMLToken* token) { - RefPtr element = createHTMLElement(token); + RefPtr element = createElement(token); attachLater(currentNode(), element); m_openElements.push(element.release()); } @@ -286,7 +286,7 @@ void HTMLConstructionSite::insertSelfClosingHTMLElement(AtomicHTMLToken* token) // Normally HTMLElementStack is responsible for calling finishParsingChildren, // but self-closing elements are never in the element stack so the stack // doesn't get a chance to tell them that we're done parsing their children. - attachLater(currentNode(), createHTMLElement(token), true); + attachLater(currentNode(), createElement(token), true); // FIXME: Do we want to acknowledge the token's self-closing flag? // http://www.whatwg.org/specs/web-apps/current-work/multipage/tokenization.html#acknowledge-self-closing-flag } @@ -316,14 +316,6 @@ void HTMLConstructionSite::insertTextNode(const String& string) m_pendingText.append(dummyTask.parent, string); } -PassRefPtr HTMLConstructionSite::createElement(AtomicHTMLToken* token, const AtomicString& namespaceURI) -{ - QualifiedName tagName(token->name()); - RefPtr element = ownerDocumentForCurrentNode().createElement(tagName, true); - setAttributes(element.get(), token); - return element.release(); -} - inline Document& HTMLConstructionSite::ownerDocumentForCurrentNode() { if (isHTMLTemplateElement(*currentNode())) @@ -331,10 +323,10 @@ inline Document& HTMLConstructionSite::ownerDocumentForCurrentNode() return currentNode()->document(); } -PassRefPtr HTMLConstructionSite::createHTMLElement(AtomicHTMLToken* token) +PassRefPtr HTMLConstructionSite::createElement(AtomicHTMLToken* token) { Document& document = ownerDocumentForCurrentNode(); - RefPtr element = HTMLElementFactory::createHTMLElement(token->name(), document, true); + RefPtr element = HTMLElementFactory::createElement(token->name(), document, true); setAttributes(element.get(), token); return element.release(); } diff --git a/engine/core/html/parser/HTMLConstructionSite.h b/engine/core/html/parser/HTMLConstructionSite.h index 2e12c0f003c..4a91d01d86f 100644 --- a/engine/core/html/parser/HTMLConstructionSite.h +++ b/engine/core/html/parser/HTMLConstructionSite.h @@ -120,8 +120,7 @@ private: void attachLater(ContainerNode* parent, PassRefPtr child, bool selfClosing = false); - PassRefPtr createHTMLElement(AtomicHTMLToken*); - PassRefPtr createElement(AtomicHTMLToken*, const AtomicString& namespaceURI); + PassRefPtr createElement(AtomicHTMLToken*); void queueTask(const HTMLConstructionSiteTask&);