Skip to content
Permalink
Browse files
Move some of the work from Element::insertedIntoAncestor() / removedF…
…romAncestor() to subclasses

https://bugs.webkit.org/show_bug.cgi?id=240914

Reviewed by Ryosuke Niwa and Darin Adler.

Move some of the work from Element::insertedIntoAncestor() / removedFromAncestor() to subclasses.
These functions are hot and should be kept as efficient as possible. There is no reason for every
Element to pay run-time cost for checks that only apply to article or label elements.

* Source/WebCore/Headers.cmake:
* Source/WebCore/Sources.txt:
* Source/WebCore/WebCore.xcodeproj/project.pbxproj:
* Source/WebCore/dom/Element.cpp:
(WebCore::Element::insertedIntoAncestor):
(WebCore::Element::removedFromAncestor):
* Source/WebCore/dom/Element.h:
* Source/WebCore/html/HTMLArticleElement.cpp: Added.
(WebCore::HTMLArticleElement::create):
(WebCore::HTMLArticleElement::HTMLArticleElement):
(WebCore::HTMLArticleElement::insertedIntoAncestor):
(WebCore::HTMLArticleElement::removedFromAncestor):
* Source/WebCore/html/HTMLArticleElement.h: Added.
* Source/WebCore/html/HTMLLabelElement.cpp:
(WebCore::HTMLLabelElement::insertedIntoAncestor):
(WebCore::HTMLLabelElement::removedFromAncestor):
* Source/WebCore/html/HTMLLabelElement.h:
* Source/WebCore/html/HTMLTagNames.in:

Canonical link: https://commits.webkit.org/251041@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@294930 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
cdumez committed May 27, 2022
1 parent 5713e41 commit 8a2121d8654e746aa02fb32eff46cad9dbc9df12
Showing 10 changed files with 153 additions and 20 deletions.
@@ -838,6 +838,7 @@ set(WebCore_PRIVATE_FRAMEWORK_HEADERS
html/HTMLAnchorElement.h
html/HTMLAnchorElementInlines.h
html/HTMLAreaElement.h
html/HTMLArticleElement.h
html/HTMLAttachmentElement.h
html/HTMLAudioElement.h
html/HTMLBRElement.h
@@ -1211,6 +1211,7 @@ html/GenericCachedHTMLCollection.cpp
html/HTMLAllCollection.cpp
html/HTMLAnchorElement.cpp
html/HTMLAreaElement.cpp
html/HTMLArticleElement.cpp
html/HTMLAttachmentElement.cpp
html/HTMLAudioElement.cpp
html/HTMLBDIElement.cpp
@@ -769,8 +769,8 @@
2AEF6FDB26E7ECC700326D02 /* CSSNumericArray.h in Headers */ = {isa = PBXBuildFile; fileRef = 2AEF6FBF26E716EE00326D02 /* CSSNumericArray.h */; };
2AEF6FDC26E7ECCC00326D02 /* CSSNumericBaseType.h in Headers */ = {isa = PBXBuildFile; fileRef = 2AEF6FCF26E71F2E00326D02 /* CSSNumericBaseType.h */; };
2AEF6FDD26E7ECCF00326D02 /* CSSNumericType.h in Headers */ = {isa = PBXBuildFile; fileRef = 2AEF6FCE26E71F2D00326D02 /* CSSNumericType.h */; };
2B365C841525119E0091D27B /* LegacyRenderSVGEllipse.h in Headers */ = {isa = PBXBuildFile; fileRef = 2B4235A015250F6000DBBCD8 /* LegacyRenderSVGEllipse.h */; };
2B365C743425119AAB91D27B /* RenderSVGEllipse.h in Headers */ = {isa = PBXBuildFile; fileRef = 2B4235A015250EF555DB5CD8 /* RenderSVGEllipse.h */; };
2B365C841525119E0091D27B /* LegacyRenderSVGEllipse.h in Headers */ = {isa = PBXBuildFile; fileRef = 2B4235A015250F6000DBBCD8 /* LegacyRenderSVGEllipse.h */; };
2BE8E2C712A589EC00FAD550 /* HTMLMetaCharsetParser.h in Headers */ = {isa = PBXBuildFile; fileRef = 2BE8E2C612A589EC00FAD550 /* HTMLMetaCharsetParser.h */; };
2D0621441DA639B600A7FB26 /* WebKitMediaKeyMessageEvent.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2D0621421DA6398800A7FB26 /* WebKitMediaKeyMessageEvent.cpp */; };
2D0621451DA639BA00A7FB26 /* WebKitMediaKeyMessageEvent.h in Headers */ = {isa = PBXBuildFile; fileRef = 2D0621431DA6398800A7FB26 /* WebKitMediaKeyMessageEvent.h */; };
@@ -1384,6 +1384,7 @@
46BCBBC22085008F00710638 /* JSRemoteDOMWindowBase.h in Headers */ = {isa = PBXBuildFile; fileRef = 46BCBBC02085007F00710638 /* JSRemoteDOMWindowBase.h */; settings = {ATTRIBUTES = (Private, ); }; };
46BD05C525BB6E5900225F30 /* AddEventListenerOptions.h in Headers */ = {isa = PBXBuildFile; fileRef = 46BD05C225BB6E4E00225F30 /* AddEventListenerOptions.h */; settings = {ATTRIBUTES = (Private, ); }; };
46BD05C625BB6E6C00225F30 /* EventListenerOptions.h in Headers */ = {isa = PBXBuildFile; fileRef = 46BD05C325BB6E4E00225F30 /* EventListenerOptions.h */; settings = {ATTRIBUTES = (Private, ); }; };
46BD15F8283E8C69000989F3 /* HTMLArticleElement.h in Headers */ = {isa = PBXBuildFile; fileRef = 46BD15F7283E8C4E000989F3 /* HTMLArticleElement.h */; };
46C0962123D78DD600657C09 /* HTTPCookieAcceptPolicy.h in Headers */ = {isa = PBXBuildFile; fileRef = 46C0962023D78DD600657C09 /* HTTPCookieAcceptPolicy.h */; settings = {ATTRIBUTES = (Private, ); }; };
46C376622085177D00C73829 /* JSRemoteDOMWindow.h in Headers */ = {isa = PBXBuildFile; fileRef = 46C376612085176D00C73829 /* JSRemoteDOMWindow.h */; };
46C696CB1E7205F700597937 /* CPUMonitor.h in Headers */ = {isa = PBXBuildFile; fileRef = 46C696C91E7205E400597937 /* CPUMonitor.h */; settings = {ATTRIBUTES = (Private, ); }; };
@@ -8050,10 +8051,10 @@
2AEF6FE326E802F000326D02 /* CSSMathNegate.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = CSSMathNegate.cpp; sourceTree = "<group>"; };
2AEF6FE426E802F000326D02 /* CSSMathProduct.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = CSSMathProduct.cpp; sourceTree = "<group>"; };
2AEF6FE526E802F100326D02 /* CSSMathSum.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = CSSMathSum.cpp; sourceTree = "<group>"; };
2B42359F15250F6000DBBCD8 /* LegacyRenderSVGEllipse.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = LegacyRenderSVGEllipse.cpp; sourceTree = "<group>"; };
2B4235A015250F6000DBBCD8 /* LegacyRenderSVGEllipse.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LegacyRenderSVGEllipse.h; sourceTree = "<group>"; };
2B42359F15250EF555DB5CD8 /* RenderSVGEllipse.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RenderSVGEllipse.cpp; sourceTree = "<group>"; };
2B42359F15250F6000DBBCD8 /* LegacyRenderSVGEllipse.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = LegacyRenderSVGEllipse.cpp; sourceTree = "<group>"; };
2B4235A015250EF555DB5CD8 /* RenderSVGEllipse.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RenderSVGEllipse.h; sourceTree = "<group>"; };
2B4235A015250F6000DBBCD8 /* LegacyRenderSVGEllipse.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LegacyRenderSVGEllipse.h; sourceTree = "<group>"; };
2BE8E2C612A589EC00FAD550 /* HTMLMetaCharsetParser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HTMLMetaCharsetParser.h; sourceTree = "<group>"; };
2BE8E2C812A58A0100FAD550 /* HTMLMetaCharsetParser.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = HTMLMetaCharsetParser.cpp; sourceTree = "<group>"; };
2D0621421DA6398800A7FB26 /* WebKitMediaKeyMessageEvent.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WebKitMediaKeyMessageEvent.cpp; sourceTree = "<group>"; };
@@ -9349,6 +9350,8 @@
46BD05C225BB6E4E00225F30 /* AddEventListenerOptions.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AddEventListenerOptions.h; sourceTree = "<group>"; };
46BD05C325BB6E4E00225F30 /* EventListenerOptions.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = EventListenerOptions.h; sourceTree = "<group>"; };
46BD05C425BB6E4F00225F30 /* AddEventListenerOptions.idl */ = {isa = PBXFileReference; lastKnownFileType = text; path = AddEventListenerOptions.idl; sourceTree = "<group>"; };
46BD15F6283E8C4D000989F3 /* HTMLArticleElement.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = HTMLArticleElement.cpp; sourceTree = "<group>"; };
46BD15F7283E8C4E000989F3 /* HTMLArticleElement.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HTMLArticleElement.h; sourceTree = "<group>"; };
46C0962023D78DD600657C09 /* HTTPCookieAcceptPolicy.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = HTTPCookieAcceptPolicy.h; sourceTree = "<group>"; };
46C3765F2085176C00C73829 /* JSRemoteDOMWindow.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSRemoteDOMWindow.cpp; sourceTree = "<group>"; };
46C376612085176D00C73829 /* JSRemoteDOMWindow.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSRemoteDOMWindow.h; sourceTree = "<group>"; };
@@ -25832,6 +25835,8 @@
A8EA7D270A19385500A8EF5F /* HTMLAreaElement.cpp */,
A8EA7D260A19385500A8EF5F /* HTMLAreaElement.h */,
1AE2A9F00A1CDA5700B42B25 /* HTMLAreaElement.idl */,
46BD15F6283E8C4D000989F3 /* HTMLArticleElement.cpp */,
46BD15F7283E8C4E000989F3 /* HTMLArticleElement.h */,
7C5F28F91A827D8400C0F31F /* HTMLAttachmentElement.cpp */,
7C5F28FA1A827D8400C0F31F /* HTMLAttachmentElement.h */,
7C9DBFEA1A9C489F000D6B25 /* HTMLAttachmentElement.idl */,
@@ -35027,6 +35032,7 @@
A8CFF7AB0A156978000A4234 /* HTMLAnchorElement.h in Headers */,
CD1F9B7D270E667800617EB6 /* HTMLAnchorElementInlines.h in Headers */,
A8EA7D2E0A19385500A8EF5F /* HTMLAreaElement.h in Headers */,
46BD15F8283E8C69000989F3 /* HTMLArticleElement.h in Headers */,
7C5F28FC1A827D8400C0F31F /* HTMLAttachmentElement.h in Headers */,
E44613A20CD6331000FADA75 /* HTMLAudioElement.h in Headers */,
A871DC1F0A15205700B12A68 /* HTMLBaseElement.h in Headers */,
@@ -2461,11 +2461,6 @@ Node::InsertedIntoAncestorResult Element::insertedIntoAncestor(InsertionType ins
updateNameForDocument(*newDocument, nullAtom(), nameValue);
}

if (newScope && hasTagName(labelTag)) {
if (newScope->shouldCacheLabelsByForAttribute())
updateLabel(*newScope, nullAtom(), attributeWithoutSynchronization(forAttr));
}

if (becomeConnected) {
if (UNLIKELY(isCustomElementUpgradeCandidate())) {
ASSERT(isConnected());
@@ -2475,9 +2470,6 @@ Node::InsertedIntoAncestorResult Element::insertedIntoAncestor(InsertionType ins
CustomElementReactionQueue::enqueueConnectedCallbackIfNeeded(*this);
}

if (UNLIKELY(hasTagName(articleTag) && newDocument))
newDocument->registerArticleElement(*this);

if (shouldAutofocus(*this))
document().topDocument().appendAutofocusCandidate(*this);

@@ -2523,16 +2515,9 @@ void Element::removedFromAncestor(RemovalType removalType, ContainerNode& oldPar
updateNameForDocument(*oldHTMLDocument, nameValue, nullAtom());
}

if (oldScope && hasTagName(labelTag)) {
if (oldScope->shouldCacheLabelsByForAttribute())
updateLabel(*oldScope, attributeWithoutSynchronization(forAttr), nullAtom());
}

if (oldDocument) {
if (oldDocument->cssTarget() == this)
oldDocument->setCSSTarget(nullptr);
if (UNLIKELY(hasTagName(articleTag)))
oldDocument->unregisterArticleElement(*this);
}

if (removalType.disconnectedFromDocument && UNLIKELY(isDefinedCustomElement()))
@@ -700,6 +700,8 @@ class Element : public ContainerNode {
void setAttributeStyleMap(Ref<StylePropertyMap>&&);
#endif

void updateLabel(TreeScope&, const AtomString& oldForAttributeValue, const AtomString& newForAttributeValue);

private:
Frame* documentFrameWithNonNullView() const;

@@ -735,7 +737,6 @@ class Element : public ContainerNode {

enum HTMLDocumentNamedItemMapsUpdatingCondition { AlwaysUpdateHTMLDocumentNamedItemMaps, UpdateHTMLDocumentNamedItemMapsOnlyIfDiffersFromNameAttribute };
void updateIdForDocument(HTMLDocument&, const AtomString& oldId, const AtomString& newId, HTMLDocumentNamedItemMapsUpdatingCondition);
void updateLabel(TreeScope&, const AtomString& oldForAttributeValue, const AtomString& newForAttributeValue);

ExceptionOr<Node*> insertAdjacent(const String& where, Ref<Node>&& newChild);

@@ -0,0 +1,68 @@
/*
* Copyright (C) 2022 Apple 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.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS 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 APPLE INC. OR ITS 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 "HTMLArticleElement.h"

#include "HTMLDocument.h"
#include "HTMLNames.h"
#include <wtf/IsoMallocInlines.h>

namespace WebCore {

WTF_MAKE_ISO_ALLOCATED_IMPL(HTMLArticleElement);

Ref<HTMLArticleElement> HTMLArticleElement::create(const QualifiedName& tagName, Document& document)
{
return adoptRef(*new HTMLArticleElement(tagName, document));
}

HTMLArticleElement::HTMLArticleElement(const QualifiedName& tagName, Document& document)
: HTMLElement(tagName, document)
{
ASSERT(tagName == HTMLNames::articleTag);
}

auto HTMLArticleElement::insertedIntoAncestor(InsertionType insertionType, ContainerNode& parentOfInsertedTree) -> InsertedIntoAncestorResult
{
auto result = HTMLElement::insertedIntoAncestor(insertionType, parentOfInsertedTree);

if (insertionType.connectedToDocument) {
if (auto* newDocument = dynamicDowncast<HTMLDocument>(parentOfInsertedTree.document()))
newDocument->registerArticleElement(*this);
}

return result;
}

void HTMLArticleElement::removedFromAncestor(RemovalType removalType, ContainerNode& oldParentOfRemovedTree)
{
if (removalType.disconnectedFromDocument)
oldParentOfRemovedTree.document().unregisterArticleElement(*this);

HTMLElement::removedFromAncestor(removalType, oldParentOfRemovedTree);
}

} // namespace WebCore
@@ -0,0 +1,44 @@
/*
* Copyright (C) 2022 Apple 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.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS 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 APPLE INC. OR ITS 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.
*/

#pragma once

#include "HTMLElement.h"

namespace WebCore {

class HTMLArticleElement final : public HTMLElement {
WTF_MAKE_ISO_ALLOCATED(HTMLArticleElement);
public:
static Ref<HTMLArticleElement> create(const QualifiedName&, Document&);

private:
InsertedIntoAncestorResult insertedIntoAncestor(InsertionType, ContainerNode&) final;
void removedFromAncestor(RemovalType, ContainerNode& oldParentOfRemovedTree) final;

HTMLArticleElement(const QualifiedName&, Document&);
};

} // namespace WebCore
@@ -198,4 +198,28 @@ bool HTMLLabelElement::accessKeyAction(bool sendMouseEvents)
return HTMLElement::accessKeyAction(sendMouseEvents);
}

auto HTMLLabelElement::insertedIntoAncestor(InsertionType insertionType, ContainerNode& parentOfInsertedTree) -> InsertedIntoAncestorResult
{
auto result = HTMLElement::insertedIntoAncestor(insertionType, parentOfInsertedTree);

if (parentOfInsertedTree.isInTreeScope() && insertionType.treeScopeChanged) {
auto& newScope = parentOfInsertedTree.treeScope();
if (newScope.shouldCacheLabelsByForAttribute())
updateLabel(newScope, nullAtom(), attributeWithoutSynchronization(forAttr));
}

return result;
}

void HTMLLabelElement::removedFromAncestor(RemovalType removalType, ContainerNode& oldParentOfRemovedTree)
{
if (oldParentOfRemovedTree.isInTreeScope() && removalType.treeScopeChanged) {
auto& oldScope = oldParentOfRemovedTree.treeScope();
if (oldScope.shouldCacheLabelsByForAttribute())
updateLabel(oldScope, attributeWithoutSynchronization(forAttr), nullAtom());
}

HTMLElement::removedFromAncestor(removalType, oldParentOfRemovedTree);
}

} // namespace
@@ -40,6 +40,9 @@ class HTMLLabelElement final : public HTMLElement {
private:
HTMLLabelElement(const QualifiedName&, Document&);

InsertedIntoAncestorResult insertedIntoAncestor(InsertionType, ContainerNode& parentOfInsertedTree) final;
void removedFromAncestor(RemovalType, ContainerNode& oldParentOfRemovedTree) final;

bool isEventTargetedAtInteractiveDescendants(Event&) const;

bool accessKeyAction(bool sendMouseEvents) final;
@@ -10,7 +10,7 @@ acronym interfaceName=HTMLElement
address interfaceName=HTMLElement
applet interfaceName=HTMLUnknownElement
area
article interfaceName=HTMLElement
article interfaceName=HTMLArticleElement, JSInterfaceName=HTMLElement
aside interfaceName=HTMLElement
attachment conditional=ATTACHMENT_ELEMENT, runtimeEnabled=attachmentElement
audio wrapperOnlyIfMediaIsAvailable, conditional=VIDEO, constructorNeedsCreatedByParser, customTypeHelper

0 comments on commit 8a2121d

Please sign in to comment.