From a316e3fe3fb1491ddd61a07c047e8d8eaf65dc38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomas=20Nork=C5=ABnas?= Date: Tue, 24 Jan 2023 15:34:53 +0200 Subject: [PATCH] fix: Parse CSS with duplicate selectors (#201) --- .github/workflows/php.yml | 12 ++++++------ src/Utilities/SVGStyleParser.php | 6 ++++-- tests/Utilities/SVGStyleParserTest.php | 7 +++++++ 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 44848573..6fc683df 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -10,7 +10,7 @@ jobs: phpcs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Setup PHP and PHPCS uses: shivammathur/setup-php@v2 @@ -24,7 +24,7 @@ jobs: - name: Get composer cache directory id: composer-cache - run: echo "::set-output name=dir::$(composer config cache-files-dir)" + run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT - name: Cache Composer packages uses: actions/cache@v3 @@ -45,7 +45,7 @@ jobs: matrix: php-versions: ['7.3', '7.4', '8.0', '8.1', '8.2'] steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Setup PHP ${{ matrix.php-versions }} uses: shivammathur/setup-php@v2 @@ -56,7 +56,7 @@ jobs: - name: Get composer cache directory id: composer-cache - run: echo "::set-output name=dir::$(composer config cache-files-dir)" + run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT - name: Cache Composer packages uses: actions/cache@v3 @@ -74,7 +74,7 @@ jobs: coverage: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Setup PHP with tools uses: shivammathur/setup-php@v2 @@ -84,7 +84,7 @@ jobs: - name: Get composer cache directory id: composer-cache - run: echo "::set-output name=dir::$(composer config cache-files-dir)" + run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT - name: Cache Composer packages uses: actions/cache@v3 diff --git a/src/Utilities/SVGStyleParser.php b/src/Utilities/SVGStyleParser.php index ee82727d..d090f171 100644 --- a/src/Utilities/SVGStyleParser.php +++ b/src/Utilities/SVGStyleParser.php @@ -53,13 +53,15 @@ public static function parseCss(string $css): array preg_match_all('/(?ims)([a-z0-9\s\,\.\:#_\-@^*()\[\]\"\'=]+)\{([^\}]*)\}/', $css, $arr); foreach ($arr[0] as $i => $x) { - $selectors = explode(',', Str::trim($arr[1][$i])); + $selectors = array_map(function (string $selector) { + return Str::trim($selector); + }, explode(',', Str::trim($arr[1][$i]))); if (in_array($selectors[0], ['@font-face', '@keyframes', '@media'])) { continue; } $rules = self::parseStyles(Str::trim($arr[2][$i])); foreach ($selectors as $selector) { - $result[Str::trim($selector)] = $rules; + $result[$selector] = array_merge($result[$selector] ?? [], $rules); } } diff --git a/tests/Utilities/SVGStyleParserTest.php b/tests/Utilities/SVGStyleParserTest.php index a5da05d7..e396fd90 100644 --- a/tests/Utilities/SVGStyleParserTest.php +++ b/tests/Utilities/SVGStyleParserTest.php @@ -29,4 +29,11 @@ public function testParseCssWithSkippedElement() $this->assertCount(0, $result); } + + public function testParseDuplicateSelectors() + { + $result = SVGStyleParser::parseCss('svg {background-color: beige;}; svg {stroke: none;} svg { fill: blue }'); + + $this->assertSame(['svg' => ['background-color' => 'beige', 'stroke' => 'none', 'fill' => 'blue']], $result); + } }