tdf#156038, tdf#78232: support css child combinator

Change-Id: I874c368f66db97017357030867f1255551996228
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/153626
Tested-by: Jenkins
Reviewed-by: Xisco Fauli <xiscofauli@libreoffice.org>
(cherry picked from commit 930eb99a712a3ad9b76e9edb68bbcea68af36656)
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/153604
diff --git a/svgio/inc/svgnode.hxx b/svgio/inc/svgnode.hxx
index a859589..0d8008d 100644
--- a/svgio/inc/svgnode.hxx
+++ b/svgio/inc/svgnode.hxx
@@ -126,6 +126,8 @@ namespace svgio::svgreader
            void fillCssStyleVectorUsingHierarchyAndSelectors(
                const SvgNode& rCurrent,
                const OUString& aConcatenated);
            void fillCssStyleVectorUsingParent(
                const SvgNode& rCurrent);

        public:
            SvgNode(
diff --git a/svgio/qa/cppunit/SvgImportTest.cxx b/svgio/qa/cppunit/SvgImportTest.cxx
index 651e134..a433fe8 100644
--- a/svgio/qa/cppunit/SvgImportTest.cxx
+++ b/svgio/qa/cppunit/SvgImportTest.cxx
@@ -340,6 +340,30 @@ CPPUNIT_TEST_FIXTURE(Test, testTdf156034)
    assertXPath(pDocument, "/primitive2D/transform/polypolygoncolor[4]", "color", "#008000");
}

CPPUNIT_TEST_FIXTURE(Test, testTdf156038)
{
    Primitive2DSequence aSequence = parseSvg(u"/svgio/qa/cppunit/data/tdf156038.svg");
    CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(aSequence.getLength()));

    drawinglayer::Primitive2dXmlDump dumper;
    xmlDocUniquePtr pDocument = dumper.dumpAndParse(aSequence);

    CPPUNIT_ASSERT (pDocument);

    assertXPath(pDocument, "/primitive2D/transform/polypolygoncolor[1]", "color", "#0000ff");

    // Without the fix in place, this test would have failed with
    // - Expected: #008000
    // - Actual  : #0000ff
    assertXPath(pDocument, "/primitive2D/transform/polypolygoncolor[2]", "color", "#008000");
    assertXPath(pDocument, "/primitive2D/transform/polypolygoncolor[3]", "color", "#0000ff");
    assertXPath(pDocument, "/primitive2D/transform/polypolygoncolor[4]", "color", "#008000");
    assertXPath(pDocument, "/primitive2D/transform/polypolygoncolor[5]", "color", "#0000ff");
    assertXPath(pDocument, "/primitive2D/transform/polypolygoncolor[6]", "color", "#008000");
    assertXPath(pDocument, "/primitive2D/transform/polypolygoncolor[7]", "color", "#0000ff");
    assertXPath(pDocument, "/primitive2D/transform/polypolygoncolor[8]", "color", "#008000");
}

CPPUNIT_TEST_FIXTURE(Test, testTdf156018)
{
    Primitive2DSequence aSequence = parseSvg(u"/svgio/qa/cppunit/data/tdf156018.svg");
diff --git a/svgio/qa/cppunit/data/tdf156038.svg b/svgio/qa/cppunit/data/tdf156038.svg
new file mode 100644
index 0000000..9835453
--- /dev/null
+++ b/svgio/qa/cppunit/data/tdf156038.svg
@@ -0,0 +1,34 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="-0 0 800 800">

    <style>
        .g1 > rect {fill:green;}
        #g3 > rect {fill:green;}
        .g4 > #r1 {fill:green;}
        #g3 > .r5 {fill:green;}
    </style>

    <g class="g4 g1">
        <g class="g2">
            <rect x="0" y="0" height="50" width="50" fill="blue"></rect>
        </g>
        <rect x="60" y="0" height="50" width="50" fill="blue"></rect>
    </g>
    <g id="g3">
        <g id="g4">
            <rect x="120" y="0" height="50" width="50" fill="blue"></rect>
        </g>
        <rect x="180" y="0" height="50" width="50" fill="blue"></rect>
    </g>
    <g class="g4 g1">
        <g>
            <rect id="r1" x="240" y="0" height="50" width="50" fill="blue"></rect>
        </g>
        <rect id="r1" x="300" y="0" height="50" width="50" fill="blue"></rect>
    </g>
    <g id="g3">
        <g id="g4">
            <rect class="r5" x="360" y="0" height="50" width="50" fill="blue"></rect>
        </g>
        <rect class="r5" x="420" y="0" height="50" width="50" fill="blue"></rect>
    </g>
</svg>
diff --git a/svgio/source/svgreader/svgnode.cxx b/svgio/source/svgreader/svgnode.cxx
index 399b922..37b34de 100644
--- a/svgio/source/svgreader/svgnode.cxx
+++ b/svgio/source/svgreader/svgnode.cxx
@@ -184,6 +184,100 @@ namespace {
            }
        }

        void SvgNode::fillCssStyleVectorUsingParent(const SvgNode& rCurrent)
        {
            const SvgDocument& rDocument = getDocument();

            if(!rDocument.hasGlobalCssStyleAttributes())
                return;

            const SvgNode* pParent = rCurrent.getParent();

            if (!pParent)
                return;

            OUString sParentId;
            if (pParent->getId().has_value())
            {
                sParentId = pParent->getId().value();
            }
            std::vector <OUString> aParentClasses = parseClass(*pParent);
            OUString sParentType(SVGTokenToStr(pParent->getType()));

            if(rCurrent.getId())
            {
                const OUString& rId = *rCurrent.getId();

                if(!rId.isEmpty())
                {
                    if (!sParentId.isEmpty())
                    {
                        const OUString aConcatenated("#" + sParentId + ">#" + rId);
                        addCssStyle(rDocument, aConcatenated);
                    }

                    for(const auto &aParentClass : aParentClasses)
                    {
                        const OUString aConcatenated("." + aParentClass + ">#" + rId);
                        addCssStyle(rDocument, aConcatenated);
                    }

                    if (!sParentType.isEmpty())
                    {
                        const OUString aConcatenated(sParentType + ">#" + rId);
                        addCssStyle(rDocument, aConcatenated);
                    }
                }

            }

            std::vector <OUString> aClasses = parseClass(rCurrent);
            for(const auto &aClass : aClasses)
            {

                if (!sParentId.isEmpty())
                {
                    const OUString aConcatenated("#" + sParentId + ">." + aClass);
                    addCssStyle(rDocument, aConcatenated);
                }

                for(const auto &aParentClass : aParentClasses)
                {
                    const OUString aConcatenated("." + aParentClass + ">." + aClass);
                    addCssStyle(rDocument, aConcatenated);
                }

                if (!sParentType.isEmpty())
                {
                    const OUString aConcatenated(sParentType + ">." + aClass);
                    addCssStyle(rDocument, aConcatenated);
                }
            }

            OUString sCurrentType(SVGTokenToStr(getType()));

            if(!sCurrentType.isEmpty())
            {
                if (!sParentId.isEmpty())
                {
                    const OUString aConcatenated("#" + sParentId + ">" + sCurrentType);
                    addCssStyle(rDocument, aConcatenated);
                }

                for(const auto &aParentClass : aParentClasses)
                {
                    const OUString aConcatenated("." + aParentClass + ">" + sCurrentType);
                    addCssStyle(rDocument, aConcatenated);
                }

                if (!sParentType.isEmpty())
                {
                    const OUString aConcatenated(sParentType + ">" + sCurrentType);
                    addCssStyle(rDocument, aConcatenated);
                }
            }
        }

        void SvgNode::fillCssStyleVector(const SvgStyleAttributes& rOriginal)
        {
            OSL_ENSURE(!mbCssStyleVectorBuilt, "OOps, fillCssStyleVector called double ?!?");
@@ -211,9 +305,13 @@ namespace {
                maCssStyleVector.push_back(mpLocalCssStyle.get());
            }

            // tdf#156038 check for child combinator
            fillCssStyleVectorUsingParent(*this);

            // check the hierarchy for concatenated patterns of Selectors
            fillCssStyleVectorUsingHierarchyAndSelectors(*this, OUString());


            // tdf#99115, Add css selector '*' style only if the element is on top of the hierarchy
            // meaning its parent is <svg>
            const SvgNode* pParent = this->getParent();