Implement StyleRule copying
authorantti@apple.com <antti@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 24 Apr 2012 21:56:48 +0000 (21:56 +0000)
committerantti@apple.com <antti@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 24 Apr 2012 21:56:48 +0000 (21:56 +0000)
https://bugs.webkit.org/show_bug.cgi?id=84752

Reviewed by Andreas Kling.

We'll need this for copy-on-write when style sheet data is shared between documents.

Implement copy() and copy constructor for style rules their children as needed.

The code is not yet used.

* css/CSSSelectorList.cpp:
(WebCore::CSSSelectorList::CSSSelectorList):
(WebCore):
* css/CSSSelectorList.h:
(CSSSelectorList):
* css/MediaList.h:
(WebCore::MediaQuerySet::copy):
(MediaQuerySet):
* css/MediaQuery.cpp:
(WebCore::MediaQuery::MediaQuery):
(WebCore):
* css/MediaQuery.h:
(MediaQuery):
(WebCore::MediaQuery::copy):
* css/MediaQueryExp.h:
(WebCore::MediaQueryExp::copy):
(MediaQueryExp):
* css/StylePropertySet.cpp:
(WebCore::StylePropertySet::StylePropertySet):
(WebCore):
(WebCore::StylePropertySet::copy):
* css/StylePropertySet.h:

    Make this copy parse mode too. As a result ElementAttributeData no longer needs to set it explicitly after copying.

(StylePropertySet):
* css/StyleRule.cpp:
(WebCore::StyleRuleBase::copy):
(WebCore):
(WebCore::StyleRule::StyleRule):
(WebCore::StyleRulePage::StyleRulePage):
(WebCore::StyleRuleFontFace::StyleRuleFontFace):
(WebCore::StyleRuleBlock::StyleRuleBlock):
(WebCore::StyleRuleMedia::StyleRuleMedia):
(WebCore::StyleRuleRegion::StyleRuleRegion):
* css/StyleRule.h:
(StyleRuleBase):
(WebCore::StyleRuleBase::StyleRuleBase):
(WebCore::StyleRule::copy):
(StyleRule):
(WebCore::StyleRuleFontFace::copy):
(StyleRuleFontFace):
(WebCore::StyleRulePage::copy):
(StyleRulePage):
(StyleRuleBlock):
(WebCore::StyleRuleMedia::copy):
(StyleRuleMedia):
(WebCore::StyleRuleRegion::copy):
(StyleRuleRegion):
* css/WebKitCSSKeyframesRule.cpp:
(WebCore):
(WebCore::StyleRuleKeyframes::StyleRuleKeyframes):
* css/WebKitCSSKeyframesRule.h:
(WebCore):
(WebCore::StyleRuleKeyframes::copy):
(StyleRuleKeyframes):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@115115 268f45cc-cd09-0410-ab3c-d52691b4dbfc

15 files changed:
Source/WebCore/ChangeLog
Source/WebCore/css/CSSSelectorList.cpp
Source/WebCore/css/CSSSelectorList.h
Source/WebCore/css/MediaList.cpp
Source/WebCore/css/MediaList.h
Source/WebCore/css/MediaQuery.cpp
Source/WebCore/css/MediaQuery.h
Source/WebCore/css/MediaQueryExp.h
Source/WebCore/css/StylePropertySet.cpp
Source/WebCore/css/StylePropertySet.h
Source/WebCore/css/StyleRule.cpp
Source/WebCore/css/StyleRule.h
Source/WebCore/css/WebKitCSSKeyframesRule.cpp
Source/WebCore/css/WebKitCSSKeyframesRule.h
Source/WebCore/dom/ElementAttributeData.cpp

index 7ae2956..74d112b 100644 (file)
@@ -1,3 +1,73 @@
+2012-04-24  Antti Koivisto  <antti@apple.com>
+
+        Implement StyleRule copying
+        https://bugs.webkit.org/show_bug.cgi?id=84752
+
+        Reviewed by Andreas Kling.
+        
+        We'll need this for copy-on-write when style sheet data is shared between documents.
+
+        Implement copy() and copy constructor for style rules their children as needed.
+        
+        The code is not yet used.
+
+        * css/CSSSelectorList.cpp:
+        (WebCore::CSSSelectorList::CSSSelectorList):
+        (WebCore):
+        * css/CSSSelectorList.h:
+        (CSSSelectorList):
+        * css/MediaList.h:
+        (WebCore::MediaQuerySet::copy):
+        (MediaQuerySet):
+        * css/MediaQuery.cpp:
+        (WebCore::MediaQuery::MediaQuery):
+        (WebCore):
+        * css/MediaQuery.h:
+        (MediaQuery):
+        (WebCore::MediaQuery::copy):
+        * css/MediaQueryExp.h:
+        (WebCore::MediaQueryExp::copy):
+        (MediaQueryExp):
+        * css/StylePropertySet.cpp:
+        (WebCore::StylePropertySet::StylePropertySet):
+        (WebCore):
+        (WebCore::StylePropertySet::copy):
+        * css/StylePropertySet.h:
+        
+            Make this copy parse mode too. As a result ElementAttributeData no longer needs to set it explicitly after copying.
+
+        (StylePropertySet):
+        * css/StyleRule.cpp:
+        (WebCore::StyleRuleBase::copy):
+        (WebCore):
+        (WebCore::StyleRule::StyleRule):
+        (WebCore::StyleRulePage::StyleRulePage):
+        (WebCore::StyleRuleFontFace::StyleRuleFontFace):
+        (WebCore::StyleRuleBlock::StyleRuleBlock):
+        (WebCore::StyleRuleMedia::StyleRuleMedia):
+        (WebCore::StyleRuleRegion::StyleRuleRegion):
+        * css/StyleRule.h:
+        (StyleRuleBase):
+        (WebCore::StyleRuleBase::StyleRuleBase):
+        (WebCore::StyleRule::copy):
+        (StyleRule):
+        (WebCore::StyleRuleFontFace::copy):
+        (StyleRuleFontFace):
+        (WebCore::StyleRulePage::copy):
+        (StyleRulePage):
+        (StyleRuleBlock):
+        (WebCore::StyleRuleMedia::copy):
+        (StyleRuleMedia):
+        (WebCore::StyleRuleRegion::copy):
+        (StyleRuleRegion):
+        * css/WebKitCSSKeyframesRule.cpp:
+        (WebCore):
+        (WebCore::StyleRuleKeyframes::StyleRuleKeyframes):
+        * css/WebKitCSSKeyframesRule.h:
+        (WebCore):
+        (WebCore::StyleRuleKeyframes::copy):
+        (StyleRuleKeyframes):
+
 2012-04-24  Ryosuke Niwa  <rniwa@webkit.org>
 
         Revert r115009; It doesn't make necessary changes to NodeRareData and Node.
index 8043053..f814bc7 100644 (file)
@@ -37,6 +37,16 @@ CSSSelectorList::~CSSSelectorList()
     deleteSelectors();
 }
 
+CSSSelectorList::CSSSelectorList(const CSSSelectorList& o)
+{
+    CSSSelector* current = o.m_selectorArray;
+    while (!current->isLastInSelectorList())
+        ++current;
+    unsigned length = (current - o.m_selectorArray) + 1;
+    m_selectorArray = reinterpret_cast<CSSSelector*>(fastMalloc(sizeof(CSSSelector) * length));
+    memcpy(m_selectorArray, o.m_selectorArray, sizeof(CSSSelector) * length);
+}
+
 void CSSSelectorList::adopt(CSSSelectorList& list)
 {
     deleteSelectors();
index 7d45a6a..e0aa65c 100644 (file)
@@ -33,9 +33,11 @@ namespace WebCore {
 class CSSParserSelector;
 
 class CSSSelectorList {
-    WTF_MAKE_NONCOPYABLE(CSSSelectorList); WTF_MAKE_FAST_ALLOCATED;
+    WTF_MAKE_FAST_ALLOCATED;
 public:
     CSSSelectorList() : m_selectorArray(0) { }
+    CSSSelectorList(const CSSSelectorList&);
+
     ~CSSSelectorList();
 
     void adopt(CSSSelectorList& list);
index b47183c..fe9cf7d 100644 (file)
@@ -82,6 +82,15 @@ MediaQuerySet::MediaQuerySet(const String& mediaString, bool fallbackToDescripto
         parse("invalid");
 }
 
+MediaQuerySet::MediaQuerySet(const MediaQuerySet& o)
+    : m_fallbackToDescriptor(o.m_fallbackToDescriptor)
+    , m_lastLine(o.m_lastLine)
+    , m_queries(o.m_queries.size())
+{
+    for (unsigned i = 0; i < m_queries.size(); ++i)
+        m_queries[i] = o.m_queries[i]->copy();
+}
+
 MediaQuerySet::~MediaQuerySet()
 {
 }
index 3cc22fc..88bdca2 100644 (file)
@@ -64,9 +64,12 @@ public:
     
     String mediaText() const;
 
+    PassRefPtr<MediaQuerySet> copy() const { return adoptRef(new MediaQuerySet(*this)); }
+
 private:
     MediaQuerySet();
     MediaQuerySet(const String& mediaQuery, bool fallbackToDescription);
+    MediaQuerySet(const MediaQuerySet&);
     
     unsigned m_fallbackToDescriptor : 1; // true if failed media query parsing should fallback to media description parsing.
     signed m_lastLine : 31;
index 8fa5820..704c00a 100644 (file)
@@ -103,6 +103,17 @@ MediaQuery::MediaQuery(Restrictor r, const String& mediaType, PassOwnPtr<Vector<
     }
 }
 
+MediaQuery::MediaQuery(const MediaQuery& o)
+    : m_restrictor(o.m_restrictor)
+    , m_mediaType(o.m_mediaType)
+    , m_expressions(adoptPtr(new Vector<OwnPtr<MediaQueryExp> >(o.m_expressions->size())))
+    , m_ignored(o.m_ignored)
+    , m_serializationCache(o.m_serializationCache)
+{
+    for (unsigned i = 0; i < m_expressions->size(); ++i)
+        (*m_expressions)[i] = o.m_expressions->at(i)->copy();
+}
+
 MediaQuery::~MediaQuery()
 {
 }
index cb14154..398e845 100644 (file)
@@ -38,7 +38,7 @@ namespace WebCore {
 class MediaQueryExp;
 
 class MediaQuery {
-    WTF_MAKE_NONCOPYABLE(MediaQuery); WTF_MAKE_FAST_ALLOCATED;
+    WTF_MAKE_FAST_ALLOCATED;
 public:
     enum Restrictor {
         Only, Not, None
@@ -56,7 +56,11 @@ public:
     String cssText() const;
     bool ignored() const { return m_ignored; }
 
+    PassOwnPtr<MediaQuery> copy() const { return adoptPtr(new MediaQuery(*this)); }
+
  private:
+    MediaQuery(const MediaQuery&);
+
     Restrictor m_restrictor;
     String m_mediaType;
     OwnPtr<ExpressionVector> m_expressions;
index feb61c0..003de02 100644 (file)
@@ -70,6 +70,8 @@ public:
 
     String serialize() const;
 
+    PassOwnPtr<MediaQueryExp> copy() const { return adoptPtr(new MediaQueryExp(*this)); }
+
 private:
     MediaQueryExp(const AtomicString& mediaFeature, CSSParserValueList* values);
 
index 4282ce4..6baa445 100644 (file)
@@ -81,6 +81,13 @@ StylePropertySet::StylePropertySet(const CSSProperty* properties, int numPropert
     }
 }
 
+StylePropertySet::StylePropertySet(const StylePropertySet& o)
+    : m_properties(o.m_properties)
+    , m_cssParserMode(o.m_cssParserMode)
+    , m_ownsCSSOMWrapper(false)
+{
+}
+
 StylePropertySet::~StylePropertySet()
 {
     ASSERT(!m_ownsCSSOMWrapper || propertySetCSSOMWrapperMap().contains(this));
@@ -931,7 +938,7 @@ void StylePropertySet::removeEquivalentProperties(const CSSStyleDeclaration* sty
 
 PassRefPtr<StylePropertySet> StylePropertySet::copy() const
 {
-    return adoptRef(new StylePropertySet(m_properties));
+    return adoptRef(new StylePropertySet(*this));
 }
 
 PassRefPtr<StylePropertySet> StylePropertySet::copyPropertiesInSet(const CSSPropertyID* set, unsigned length) const
index d47ac52..ae999ae 100644 (file)
@@ -113,10 +113,10 @@ public:
     // FIXME: Expand the concept of mutable/immutable StylePropertySet.
     bool isMutable() const { return m_ownsCSSOMWrapper; }
 
-    
 private:
     StylePropertySet(CSSParserMode);
     StylePropertySet(const Vector<CSSProperty>&);
+    StylePropertySet(const StylePropertySet&);
     StylePropertySet(const CSSProperty*, int numProperties, CSSParserMode);
 
     void setNeedsStyleRecalc();
index 0766120..fc52955 100644 (file)
@@ -77,6 +77,35 @@ void StyleRuleBase::destroy()
     ASSERT_NOT_REACHED();
 }
 
+PassRefPtr<StyleRuleBase> StyleRuleBase::copy() const
+{
+    switch (type()) {
+    case Style:
+        return static_cast<const StyleRule*>(this)->copy();
+    case Page:
+        return static_cast<const StyleRulePage*>(this)->copy();
+    case FontFace:
+        return static_cast<const StyleRuleFontFace*>(this)->copy();
+    case Media:
+        return static_cast<const StyleRuleMedia*>(this)->copy();
+    case Region:
+        return static_cast<const StyleRuleRegion*>(this)->copy();
+    case Import:
+        // FIXME: Copy import rules.
+        ASSERT_NOT_REACHED();
+        return 0;
+    case Keyframes:
+        return static_cast<const StyleRuleKeyframes*>(this)->copy();
+    case Unknown:
+    case Charset:
+    case Keyframe:
+        ASSERT_NOT_REACHED();
+        return 0;
+    }
+    ASSERT_NOT_REACHED();
+    return 0;
+}
+
 PassRefPtr<CSSRule> StyleRuleBase::createCSSOMWrapper(CSSStyleSheet* parentSheet, CSSRule* parentRule) const
 {
     RefPtr<CSSRule> rule;
@@ -119,6 +148,13 @@ StyleRule::StyleRule(int sourceLine)
 {
 }
 
+StyleRule::StyleRule(const StyleRule& o)
+    : StyleRuleBase(o)
+    , m_properties(o.m_properties->copy())
+    , m_selectorList(o.m_selectorList)
+{
+}
+
 StyleRule::~StyleRule()
 {
 }
@@ -133,6 +169,13 @@ StyleRulePage::StyleRulePage()
 {
 }
 
+StyleRulePage::StyleRulePage(const StyleRulePage& o)
+    : StyleRuleBase(o)
+    , m_properties(o.m_properties->copy())
+    , m_selectorList(o.m_selectorList)
+{
+}
+
 StyleRulePage::~StyleRulePage()
 {
 }
@@ -147,6 +190,12 @@ StyleRuleFontFace::StyleRuleFontFace()
 {
 }
 
+StyleRuleFontFace::StyleRuleFontFace(const StyleRuleFontFace& o)
+    : StyleRuleBase(o)
+    , m_properties(o.m_properties->copy())
+{
+}
+
 StyleRuleFontFace::~StyleRuleFontFace()
 {
 }
@@ -162,6 +211,14 @@ StyleRuleBlock::StyleRuleBlock(Type type, Vector<RefPtr<StyleRuleBase> >& adoptR
     m_childRules.swap(adoptRule);
 }
 
+StyleRuleBlock::StyleRuleBlock(const StyleRuleBlock& o)
+    : StyleRuleBase(o)
+    , m_childRules(o.m_childRules.size())
+{
+    for (unsigned i = 0; i < m_childRules.size(); ++i)
+        m_childRules[i] = o.m_childRules[i]->copy();
+}
+
 void StyleRuleBlock::wrapperInsertRule(unsigned index, PassRefPtr<StyleRuleBase> rule)
 {
     m_childRules.insert(index, rule);
@@ -178,10 +235,22 @@ StyleRuleMedia::StyleRuleMedia(PassRefPtr<MediaQuerySet> media, Vector<RefPtr<St
 {
 }
 
+StyleRuleMedia::StyleRuleMedia(const StyleRuleMedia& o)
+    : StyleRuleBlock(o)
+    , m_mediaQueries(o.m_mediaQueries->copy())
+{
+}
+
 StyleRuleRegion::StyleRuleRegion(Vector<OwnPtr<CSSParserSelector> >* selectors, Vector<RefPtr<StyleRuleBase> >& adoptRules)
     : StyleRuleBlock(Region, adoptRules)
 {
     m_selectorList.adoptSelectorVector(*selectors);
 }
 
+StyleRuleRegion::StyleRuleRegion(const StyleRuleRegion& o)
+    : StyleRuleBlock(o)
+    , m_selectorList(o.m_selectorList)
+{
+}
+
 } // namespace WebCore
index 230bd37..6a36325 100644 (file)
@@ -57,7 +57,9 @@ public:
     bool isStyleRule() const { return type() == Style; }
     bool isRegionRule() const { return type() == Region; }
     bool isImportRule() const { return type() == Import; }
-    
+
+    PassRefPtr<StyleRuleBase> copy() const;
+
     int sourceLine() const { return m_sourceLine; }
 
     void deref()
@@ -72,6 +74,8 @@ public:
 
 protected:
     StyleRuleBase(Type type, signed sourceLine = 0) : m_type(type), m_sourceLine(sourceLine) { }
+    StyleRuleBase(const StyleRuleBase& o) : m_type(o.m_type), m_sourceLine(o.m_sourceLine) { }
+
     ~StyleRuleBase() { }
 
 private:
@@ -96,8 +100,11 @@ public:
     void wrapperAdoptSelectorList(CSSSelectorList& selectors) { m_selectorList.adopt(selectors); }
     void setProperties(PassRefPtr<StylePropertySet>);
 
+    PassRefPtr<StyleRule> copy() const { return adoptRef(new StyleRule(*this)); }
+
 private:
     StyleRule(int sourceLine);
+    StyleRule(const StyleRule&);
 
     RefPtr<StylePropertySet> m_properties;
     CSSSelectorList m_selectorList;
@@ -113,8 +120,11 @@ public:
 
     void setProperties(PassRefPtr<StylePropertySet>);
 
+    PassRefPtr<StyleRuleFontFace> copy() const { return adoptRef(new StyleRuleFontFace(*this)); }
+
 private:
     StyleRuleFontFace();
+    StyleRuleFontFace(const StyleRuleFontFace&);
 
     RefPtr<StylePropertySet> m_properties;
 };
@@ -132,8 +142,11 @@ public:
     void wrapperAdoptSelectorList(CSSSelectorList& selectors) { m_selectorList.adopt(selectors); }
     void setProperties(PassRefPtr<StylePropertySet>);
 
+    PassRefPtr<StyleRulePage> copy() const { return adoptRef(new StyleRulePage(*this)); }
+
 private:
     StyleRulePage();
+    StyleRulePage(const StyleRulePage&);
     
     RefPtr<StylePropertySet> m_properties;
     CSSSelectorList m_selectorList;
@@ -148,6 +161,7 @@ public:
     
 protected:
     StyleRuleBlock(Type, Vector<RefPtr<StyleRuleBase> >& adoptRule);
+    StyleRuleBlock(const StyleRuleBlock&);
     
 private:
     Vector<RefPtr<StyleRuleBase> > m_childRules;
@@ -162,8 +176,11 @@ public:
 
     MediaQuerySet* mediaQueries() const { return m_mediaQueries.get(); }
 
+    PassRefPtr<StyleRuleMedia> copy() const { return adoptRef(new StyleRuleMedia(*this)); }
+
 private:
     StyleRuleMedia(PassRefPtr<MediaQuerySet>, Vector<RefPtr<StyleRuleBase> >& adoptRules);
+    StyleRuleMedia(const StyleRuleMedia&);
 
     RefPtr<MediaQuerySet> m_mediaQueries;
 };
@@ -177,8 +194,11 @@ public:
 
     const CSSSelectorList& selectorList() const { return m_selectorList; }
 
+    PassRefPtr<StyleRuleRegion> copy() const { return adoptRef(new StyleRuleRegion(*this)); }
+
 private:
     StyleRuleRegion(Vector<OwnPtr<CSSParserSelector> >*, Vector<RefPtr<StyleRuleBase> >& adoptRules);
+    StyleRuleRegion(const StyleRuleRegion&);
     
     CSSSelectorList m_selectorList;
 };
index 81b0475..cbf97d5 100644 (file)
@@ -39,7 +39,14 @@ StyleRuleKeyframes::StyleRuleKeyframes()
     : StyleRuleBase(Keyframes, 0)
 {
 }
-    
+
+StyleRuleKeyframes::StyleRuleKeyframes(const StyleRuleKeyframes& o)
+    : StyleRuleBase(o)
+    , m_keyframes(o.m_keyframes)
+    , m_name(o.m_name)
+{
+}
+
 StyleRuleKeyframes::~StyleRuleKeyframes()
 {
 }
index 98ff148..81abf87 100644 (file)
@@ -27,6 +27,7 @@
 #define WebKitCSSKeyframesRule_h
 
 #include "CSSRule.h"
+#include "ExceptionCode.h"
 #include "StyleRule.h"
 #include <wtf/Forward.h>
 #include <wtf/RefPtr.h>
@@ -38,8 +39,6 @@ class CSSRuleList;
 class StyleKeyframe;
 class WebKitCSSKeyframeRule;
 
-typedef int ExceptionCode;
-    
 class StyleRuleKeyframes : public StyleRuleBase {
 public:
     static PassRefPtr<StyleRuleKeyframes> create() { return adoptRef(new StyleRuleKeyframes()); }
@@ -57,9 +56,12 @@ public:
     
     int findKeyframeIndex(const String& key) const;
 
+    PassRefPtr<StyleRuleKeyframes> copy() const { return adoptRef(new StyleRuleKeyframes(*this)); }
+
 private:
     StyleRuleKeyframes();
-    
+    StyleRuleKeyframes(const StyleRuleKeyframes&);
+
     Vector<RefPtr<StyleKeyframe> > m_keyframes;
     AtomicString m_name;
 };
index 58a3fcc..cb8917a 100644 (file)
@@ -94,7 +94,6 @@ StylePropertySet* ElementAttributeData::ensureMutableInlineStyle(StyledElement*
 {
     if (m_inlineStyleDecl && !m_inlineStyleDecl->isMutable()) {
         m_inlineStyleDecl = m_inlineStyleDecl->copy();
-        m_inlineStyleDecl->setCSSParserMode(strictToCSSParserMode(element->isHTMLElement() && !element->document()->inQuirksMode()));
         return m_inlineStyleDecl.get();
     }
     return ensureInlineStyle(element);