Skip to content

Commit

Permalink
Extract StyledElementInlineStylePropertyMap from StyledElement.cpp an…
Browse files Browse the repository at this point in the history
…d to its own file

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

Reviewed by Antoine Quint.

Extract StyledElementInlineStylePropertyMap from StyledElement.cpp and to its
own file. Also rename it to InlineStylePropertyMap for simplicity.

* Source/WebCore/Sources.txt:
* Source/WebCore/WebCore.xcodeproj/project.pbxproj:
* Source/WebCore/css/typedom/InlineStylePropertyMap.cpp: Added.
(WebCore::InlineStylePropertyMap::create):
(WebCore::InlineStylePropertyMap::InlineStylePropertyMap):
(WebCore::InlineStylePropertyMap::propertyValue const):
(WebCore::InlineStylePropertyMap::customPropertyValue const):
(WebCore::InlineStylePropertyMap::size const):
(WebCore::InlineStylePropertyMap::entries const):
(WebCore::InlineStylePropertyMap::removeProperty):
(WebCore::InlineStylePropertyMap::setShorthandProperty):
(WebCore::InlineStylePropertyMap::setProperty):
(WebCore::InlineStylePropertyMap::setCustomProperty):
(WebCore::InlineStylePropertyMap::removeCustomProperty):
(WebCore::InlineStylePropertyMap::clear):
(WebCore::InlineStylePropertyMap::clearElement):
* Source/WebCore/css/typedom/InlineStylePropertyMap.h: Added.
* Source/WebCore/dom/StyledElement.cpp:
(WebCore::StyledElement::ensureAttributeStyleMap):
(): Deleted.

Canonical link: https://commits.webkit.org/257292@main
  • Loading branch information
cdumez committed Dec 2, 2022
1 parent f53b8be commit 3fa92bc
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 113 deletions.
1 change: 1 addition & 0 deletions Source/WebCore/Sources.txt
Expand Up @@ -879,6 +879,7 @@ css/query/MediaQueryEvaluator.cpp
css/query/MediaQueryFeatures.cpp
css/query/MediaQueryParser.cpp
css/typedom/ComputedStylePropertyMapReadOnly.cpp
css/typedom/InlineStylePropertyMap.cpp
css/typedom/StylePropertyMapReadOnly.cpp
css/typedom/CSSKeywordValue.cpp
css/typedom/CSSStyleImageValue.cpp
Expand Down
6 changes: 6 additions & 0 deletions Source/WebCore/WebCore.xcodeproj/project.pbxproj
Expand Up @@ -1444,6 +1444,7 @@
46AAAA3D25D3632000BAF42F /* AudioFileReaderCocoa.h in Headers */ = {isa = PBXBuildFile; fileRef = 46AAAA3A25D3631400BAF42F /* AudioFileReaderCocoa.h */; };
46AD2CD028E4FD5100040F10 /* ScreenOrientation.h in Headers */ = {isa = PBXBuildFile; fileRef = 46AD2CCF28E4FD3B00040F10 /* ScreenOrientation.h */; };
46B63F6C1C6E8D19002E914B /* JSEventTargetCustom.h in Headers */ = {isa = PBXBuildFile; fileRef = 46B63F6B1C6E8CDF002E914B /* JSEventTargetCustom.h */; settings = {ATTRIBUTES = (Private, ); }; };
46B6437E29393C1A00E6CE68 /* InlineStylePropertyMap.h in Headers */ = {isa = PBXBuildFile; fileRef = 46B6437D29393C1000E6CE68 /* InlineStylePropertyMap.h */; };
46B8593C261672270019EDC6 /* ScriptBufferSourceProvider.h in Headers */ = {isa = PBXBuildFile; fileRef = 46B8593A2616720C0019EDC6 /* ScriptBufferSourceProvider.h */; };
46B95195207D633400A7D2DD /* AbstractDOMWindow.h in Headers */ = {isa = PBXBuildFile; fileRef = 46B9518A207D632800A7D2DD /* AbstractDOMWindow.h */; settings = {ATTRIBUTES = (Private, ); }; };
46B95196207D633A00A7D2DD /* AbstractFrame.h in Headers */ = {isa = PBXBuildFile; fileRef = 46B9518F207D632B00A7D2DD /* AbstractFrame.h */; settings = {ATTRIBUTES = (Private, ); }; };
Expand Down Expand Up @@ -9675,6 +9676,8 @@
46AD2CCE28E4FD3B00040F10 /* ScreenOrientation.idl */ = {isa = PBXFileReference; lastKnownFileType = text; path = ScreenOrientation.idl; sourceTree = "<group>"; };
46AD2CCF28E4FD3B00040F10 /* ScreenOrientation.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ScreenOrientation.h; sourceTree = "<group>"; };
46B63F6B1C6E8CDF002E914B /* JSEventTargetCustom.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSEventTargetCustom.h; sourceTree = "<group>"; };
46B6437C29393C1000E6CE68 /* InlineStylePropertyMap.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = InlineStylePropertyMap.cpp; sourceTree = "<group>"; };
46B6437D29393C1000E6CE68 /* InlineStylePropertyMap.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = InlineStylePropertyMap.h; sourceTree = "<group>"; };
46B8593A2616720C0019EDC6 /* ScriptBufferSourceProvider.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ScriptBufferSourceProvider.h; sourceTree = "<group>"; };
46B9518A207D632800A7D2DD /* AbstractDOMWindow.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AbstractDOMWindow.h; sourceTree = "<group>"; };
46B9518C207D632900A7D2DD /* RemoteFrame.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RemoteFrame.cpp; sourceTree = "<group>"; };
Expand Down Expand Up @@ -22830,6 +22833,8 @@
4BAFD0D721921E7900C0AB64 /* CSSUnparsedValue.idl */,
464E5D282901A5340051ACDA /* DeclaredStylePropertyMap.cpp */,
464E5D272901A5330051ACDA /* DeclaredStylePropertyMap.h */,
46B6437C29393C1000E6CE68 /* InlineStylePropertyMap.cpp */,
46B6437D29393C1000E6CE68 /* InlineStylePropertyMap.h */,
46FC422829084F4400773DD5 /* MainThreadStylePropertyMapReadOnly.cpp */,
46FC422929084F4400773DD5 /* MainThreadStylePropertyMapReadOnly.h */,
464E5D262901A5330051ACDA /* StylePropertyMap.cpp */,
Expand Down Expand Up @@ -36350,6 +36355,7 @@
6F40DA822513033A00EC04B7 /* InlineRect.h in Headers */,
E4D33F3B252AEECD00837D05 /* InlineRunAndOffset.h in Headers */,
6F360E5023999421001512A7 /* InlineSoftLineBreakItem.h in Headers */,
46B6437E29393C1A00E6CE68 /* InlineStylePropertyMap.h in Headers */,
AA4C3A770B2B1679002334A2 /* InlineStyleSheetOwner.h in Headers */,
1C010701192594DF008A4201 /* InlineTextBoxStyle.h in Headers */,
6F1CC1DE225F8B4900720AD2 /* InlineTextItem.h in Headers */,
Expand Down
142 changes: 142 additions & 0 deletions Source/WebCore/css/typedom/InlineStylePropertyMap.cpp
@@ -0,0 +1,142 @@
/*
* Copyright (C) 2021-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. ``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
* 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 "InlineStylePropertyMap.h"

#include "StyledElement.h"

namespace WebCore {

Ref<InlineStylePropertyMap> InlineStylePropertyMap::create(StyledElement& element)
{
return adoptRef(*new InlineStylePropertyMap(element));
}

InlineStylePropertyMap::InlineStylePropertyMap(StyledElement& element)
: m_element(&element)
{
}

RefPtr<CSSValue> InlineStylePropertyMap::propertyValue(CSSPropertyID propertyID) const
{
if (auto* inlineStyle = m_element ? m_element->inlineStyle() : nullptr)
return inlineStyle->getPropertyCSSValue(propertyID);
return nullptr;
}

String InlineStylePropertyMap::shorthandPropertySerialization(CSSPropertyID propertyID) const
{
if (auto* inlineStyle = m_element ? m_element->inlineStyle() : nullptr)
return inlineStyle->getPropertyValue(propertyID);
return String();
}

RefPtr<CSSValue> InlineStylePropertyMap::customPropertyValue(const AtomString& property) const
{
if (auto* inlineStyle = m_element ? m_element->inlineStyle() : nullptr)
return inlineStyle->getCustomPropertyCSSValue(property.string());
return nullptr;
}

unsigned InlineStylePropertyMap::size() const
{
auto* inlineStyle = m_element ? m_element->inlineStyle() : nullptr;
return inlineStyle ? inlineStyle->propertyCount() : 0;
}

auto InlineStylePropertyMap::entries(ScriptExecutionContext* context) const -> Vector<StylePropertyMapEntry>
{
if (!m_element || !context)
return { };

auto& document = downcast<Document>(*context);
Vector<StylePropertyMapEntry> result;
auto* inlineStyle = m_element->inlineStyle();
if (!inlineStyle)
return { };

result.reserveInitialCapacity(inlineStyle->propertyCount());
for (unsigned i = 0; i < inlineStyle->propertyCount(); ++i) {
auto propertyReference = inlineStyle->propertyAt(i);
result.uncheckedAppend(makeKeyValuePair(propertyReference.cssName(), reifyValueToVector(RefPtr<CSSValue> { propertyReference.value() }, document)));
}
return result;
}

void InlineStylePropertyMap::removeProperty(CSSPropertyID propertyID)
{
if (m_element)
m_element->removeInlineStyleProperty(propertyID);
}

bool InlineStylePropertyMap::setShorthandProperty(CSSPropertyID propertyID, const String& value)
{
if (!m_element)
return false;
bool didFailParsing = false;
bool important = false;
m_element->setInlineStyleProperty(propertyID, value, important, &didFailParsing);
return !didFailParsing;
}

bool InlineStylePropertyMap::setProperty(CSSPropertyID propertyID, Ref<CSSValue>&& value)
{
if (!m_element)
return false;
bool didFailParsing = false;
bool important = false;
m_element->setInlineStyleProperty(propertyID, value->cssText(), important, &didFailParsing);
return !didFailParsing;
}

bool InlineStylePropertyMap::setCustomProperty(Document&, const AtomString& property, Ref<CSSVariableReferenceValue>&& value)
{
if (!m_element)
return false;

auto customPropertyValue = CSSCustomPropertyValue::createUnresolved(property, WTFMove(value));
m_element->setInlineStyleCustomProperty(WTFMove(customPropertyValue));
return true;
}

void InlineStylePropertyMap::removeCustomProperty(const AtomString& property)
{
if (m_element)
m_element->removeInlineStyleCustomProperty(property);
}

void InlineStylePropertyMap::clear()
{
if (m_element)
m_element->removeAllInlineStyleProperties();
}

void InlineStylePropertyMap::clearElement()
{
m_element = nullptr;
}

} // namespace WebCore
55 changes: 55 additions & 0 deletions Source/WebCore/css/typedom/InlineStylePropertyMap.h
@@ -0,0 +1,55 @@
/*
* Copyright (C) 2021-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. ``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
* 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 "StylePropertyMap.h"

namespace WebCore {

class InlineStylePropertyMap final : public StylePropertyMap {
public:
static Ref<InlineStylePropertyMap> create(StyledElement&);

RefPtr<CSSValue> propertyValue(CSSPropertyID) const final;
String shorthandPropertySerialization(CSSPropertyID) const final;
RefPtr<CSSValue> customPropertyValue(const AtomString& property) const final;
unsigned size() const final;
Vector<StylePropertyMapEntry> entries(ScriptExecutionContext*) const final;
void removeProperty(CSSPropertyID) final;
bool setShorthandProperty(CSSPropertyID, const String& value) final;
bool setProperty(CSSPropertyID, Ref<CSSValue>&&) final;
bool setCustomProperty(Document&, const AtomString& property, Ref<CSSVariableReferenceValue>&&) final;
void removeCustomProperty(const AtomString& property) final;
void clear() final;
void clearElement() final;

private:
explicit InlineStylePropertyMap(StyledElement&);

StyledElement* m_element;
};

} // namespace WebCore
115 changes: 2 additions & 113 deletions Source/WebCore/dom/StyledElement.cpp
Expand Up @@ -41,6 +41,7 @@
#include "HTMLElement.h"
#include "HTMLImageElement.h"
#include "HTMLParserIdioms.h"
#include "InlineStylePropertyMap.h"
#include "InspectorInstrumentation.h"
#include "PropertySetCSSStyleDeclaration.h"
#include "ScriptableDocumentParser.h"
Expand Down Expand Up @@ -79,122 +80,10 @@ CSSStyleDeclaration& StyledElement::cssomStyle()
return ensureMutableInlineStyle().ensureInlineCSSStyleDeclaration(*this);
}

class StyledElementInlineStylePropertyMap final : public StylePropertyMap {
public:
static Ref<StylePropertyMap> create(StyledElement& element)
{
return adoptRef(*new StyledElementInlineStylePropertyMap(element));
}

private:
RefPtr<CSSValue> propertyValue(CSSPropertyID propertyID) const final
{
if (auto* inlineStyle = m_element ? m_element->inlineStyle() : nullptr)
return inlineStyle->getPropertyCSSValue(propertyID);
return nullptr;
}

String shorthandPropertySerialization(CSSPropertyID propertyID) const final
{
if (auto* inlineStyle = m_element ? m_element->inlineStyle() : nullptr)
return inlineStyle->getPropertyValue(propertyID);
return String();
}

RefPtr<CSSValue> customPropertyValue(const AtomString& property) const final
{
if (auto* inlineStyle = m_element ? m_element->inlineStyle() : nullptr)
return inlineStyle->getCustomPropertyCSSValue(property.string());
return nullptr;
}

unsigned size() const final
{
auto* inlineStyle = m_element ? m_element->inlineStyle() : nullptr;
return inlineStyle ? inlineStyle->propertyCount() : 0;
}

Vector<StylePropertyMapEntry> entries(ScriptExecutionContext* context) const final
{
if (!m_element || !context)
return { };

auto& document = downcast<Document>(*context);
Vector<StylePropertyMapEntry> result;
auto* inlineStyle = m_element->inlineStyle();
if (!inlineStyle)
return { };

result.reserveInitialCapacity(inlineStyle->propertyCount());
for (unsigned i = 0; i < inlineStyle->propertyCount(); ++i) {
auto propertyReference = inlineStyle->propertyAt(i);
result.uncheckedAppend(makeKeyValuePair(propertyReference.cssName(), reifyValueToVector(RefPtr<CSSValue> { propertyReference.value() }, document)));
}
return result;
}

void removeProperty(CSSPropertyID propertyID) final
{
if (m_element)
m_element->removeInlineStyleProperty(propertyID);
}

bool setShorthandProperty(CSSPropertyID propertyID, const String& value) final
{
if (!m_element)
return false;
bool didFailParsing = false;
bool important = false;
m_element->setInlineStyleProperty(propertyID, value, important, &didFailParsing);
return !didFailParsing;
}

bool setProperty(CSSPropertyID propertyID, Ref<CSSValue>&& value) final
{
if (!m_element)
return false;
bool didFailParsing = false;
bool important = false;
m_element->setInlineStyleProperty(propertyID, value->cssText(), important, &didFailParsing);
return !didFailParsing;
}

bool setCustomProperty(Document&, const AtomString& property, Ref<CSSVariableReferenceValue>&& value) final
{
if (!m_element)
return false;

auto customPropertyValue = CSSCustomPropertyValue::createUnresolved(property, WTFMove(value));
m_element->setInlineStyleCustomProperty(WTFMove(customPropertyValue));
return true;
}

void removeCustomProperty(const AtomString& property) final
{
if (m_element)
m_element->removeInlineStyleCustomProperty(property);
}

void clear() final
{
if (m_element)
m_element->removeAllInlineStyleProperties();
}

explicit StyledElementInlineStylePropertyMap(StyledElement& element)
: m_element(&element)
{
}

void clearElement() override { m_element = nullptr; }

StyledElement* m_element { nullptr };
};

StylePropertyMap& StyledElement::ensureAttributeStyleMap()
{
if (!attributeStyleMap())
setAttributeStyleMap(StyledElementInlineStylePropertyMap::create(*this));
setAttributeStyleMap(InlineStylePropertyMap::create(*this));
return *attributeStyleMap();
}

Expand Down

0 comments on commit 3fa92bc

Please sign in to comment.