svgio/inc/svgstylenode.hxx              |    4 ++--
 svgio/source/svgreader/svgstylenode.cxx |   31 ++++++++++++++-----------------
 2 files changed, 16 insertions(+), 19 deletions(-)

New commits:
commit c9f71c923f82cf430c3b67ee7af8ff9c681e7c0b
Author:     offtkp <parisop...@gmail.com>
AuthorDate: Sun Jun 19 23:52:58 2022 +0300
Commit:     Xisco Fauli <xiscofa...@libreoffice.org>
CommitDate: Tue Jul 12 13:51:11 2022 +0200

    tdf#149449 Don't ignore CSS class redefinition inside SVGs
    
    Previously if a css class was redefined like so:
    
    .cls2,.cls3{fill:#fff;}.cls-2{opacity:0.1;}
    
    the second definition of .cls-2 would get ignored and opacity would
    remain 1.
    
    This patch keeps track of the names of each previously defined class and
    makes sure to append the future redefinition instead of ignoring it.
    
    Change-Id: I20b55aea247d11774cd743505a90f1466f622b1e
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/136109
    Tested-by: Jenkins
    Reviewed-by: Tomaž Vajngerl <qui...@gmail.com>
    (cherry picked from commit 99f8e8aa0ccb741c2b5ede6cab75798c1793d899)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/136244
    Reviewed-by: Xisco Fauli <xiscofa...@libreoffice.org>
    (cherry picked from commit 34e08a82a317e06d2526f5a16a522323c3902bd7)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/136981

diff --git a/svgio/inc/svgstylenode.hxx b/svgio/inc/svgstylenode.hxx
index 320b4fa17dcc..1a5a43ca858c 100644
--- a/svgio/inc/svgstylenode.hxx
+++ b/svgio/inc/svgstylenode.hxx
@@ -19,6 +19,7 @@
 
 #pragma once
 
+#include <unordered_map>
 #include "svgnode.hxx"
 #include "svgstyleattributes.hxx"
 
@@ -28,7 +29,7 @@ namespace svgio::svgreader
         {
         private:
             /// use styles
-            std::vector< SvgStyleAttributes* >      maSvgStyleAttributes;
+            std::unordered_map< OUString, std::unique_ptr<SvgStyleAttributes> 
> maSvgStyleAttributes;
 
             bool                                    mbTextCss : 1; // true == 
type is 'text/css'
 
@@ -36,7 +37,6 @@ namespace svgio::svgreader
             SvgStyleNode(
                 SvgDocument& rDocument,
                 SvgNode* pParent);
-            virtual ~SvgStyleNode() override;
 
             /// #i125258# tell if this node is allowed to have a parent style 
(e.g. defs do not)
             virtual bool supportsParentStyle() const override;
diff --git a/svgio/source/svgreader/svgstylenode.cxx 
b/svgio/source/svgreader/svgstylenode.cxx
index 89f6200138bd..f9d33d61ced3 100644
--- a/svgio/source/svgreader/svgstylenode.cxx
+++ b/svgio/source/svgreader/svgstylenode.cxx
@@ -31,15 +31,6 @@ namespace svgio::svgreader
         {
         }
 
-        SvgStyleNode::~SvgStyleNode()
-        {
-            while(!maSvgStyleAttributes.empty())
-            {
-                delete *(maSvgStyleAttributes.end() - 1);
-                maSvgStyleAttributes.pop_back();
-            }
-        }
-
         // #i125258# no parent when we are a CssStyle holder to break 
potential loops because
         // when using CssStyles we jump uncontrolled inside the node tree 
hierarchy
         bool SvgStyleNode::supportsParentStyle() const
@@ -148,13 +139,6 @@ namespace svgio::svgreader
             if(aSelectors.isEmpty() || aContent.isEmpty())
                 return;
 
-            // create new style and add to local list (for ownership control)
-            SvgStyleAttributes* pNewStyle = new SvgStyleAttributes(*this);
-            maSvgStyleAttributes.push_back(pNewStyle);
-
-            // fill with content
-            pNewStyle->readCssStyle(aContent);
-
             // comma-separated split (Css abbreviation for same style for 
multiple selectors)
             const sal_Int32 nLen(aSelectors.getLength());
             sal_Int32 nPos(0);
@@ -168,9 +152,22 @@ namespace svgio::svgreader
 
                 const OUString aSingleName(aToken.makeStringAndClear().trim());
 
+                // add the current css class only if wasn't previously added
+                auto [aIterator, bIsNew] = 
maSvgStyleAttributes.try_emplace(aSingleName);
+                if (bIsNew)
+                {
+                    // create new style and add to local list (for ownership 
control) and
+                    // in case it's written to again in future classes to 
prevent overwrites
+                    aIterator->second = 
std::make_unique<SvgStyleAttributes>(*this);
+                }
+                const std::unique_ptr<SvgStyleAttributes>& pCurrentStyle = 
aIterator->second;
+
+                // fill with content
+                pCurrentStyle->readCssStyle(aContent);
+
                 if(aSingleName.getLength())
                 {
-                    addCssStyleSheet(aSingleName, *pNewStyle);
+                    addCssStyleSheet(aSingleName, *pCurrentStyle);
                 }
 
                 if(nInitPos == nPos)

Reply via email to