AngusJohnson / Image32

An extensive 2D graphics library written in Delphi Pascal
Boost Software License 1.0
137 stars 31 forks source link

CSS parser error in SVG files with multiple CSS blocks per class #60

Closed ahausladen closed 4 months ago

ahausladen commented 4 months ago

If the SVG style-tag contains multiple CSS declarations for a class and there are white-spaces in the CSS block, the TClassStylesList.AddAppendStyle() method inserts an extra ";". This causes TXmlEl.ParseStyleAttribute() to ignore all style properties after the double ';' for that class as it expects a property name but gets the second ";".

This commit fixes this by trimming the style declaration's contents before calling AddAppendStyle(). It introduces a new ToTrimmedUTF8String() function and removes the now unused AllTrim() function.

Example SVG:

<?xml version="1.0" encoding="UTF-8"?>
<svg version="1.1" xmlns="http://www.w3.org/2000/svg"
     xmlns:xlink="http://www.w3.org/1999/xlink"
     viewBox="0 0 800 600">
  <defs>
    <style>
      .cls-1, .cls-2 { stroke-width: 2px; }
      .cls-1 { fill: #8080ff; }
      .cls-2 { fill: #501088; }
    </style>
  </defs>
  <rect class="cls-1" width="800" height="600"/>
  <rect class="cls-2" width="200" height="200"/>
</svg>

Without the patch: You see one black rectangle. classStyles.GetStyle() = ' stroke-width: 0px; ; fill: #8080ff; '

With the patch: You see two colored rectangles. classStyles.GetStyle() = 'stroke-width: 0px;fill: #8080ff;'