Skip to content

Cleaner output is nested incorrectly if safelist and input document have differing cases #2049

@Balf

Description

@Balf

@jhy We're running into an issue which might be related to #2040. In version 1.16.1 we used to code snippet below to clean SVG's and it worked fine. In 1.16.2 the same code breaks, because the cleaned SVG is invalid. Self closing elements are not parsed properly. How should we do this in 1.16.2?

String svg = "...see below for the source svg";
Safelist safeList = Safelist.none();

String[] lowercaseElementsArray = Arrays.stream(DEFAULT_SVG_ELEMENTS)
            .map(String::toLowerCase)
            .toList()
            .toArray(new String[0]);

String[] lowercaseAttributesArray = Arrays.stream(DEFAULT_SVG_ATTRIBUTES)
            .map(String::toLowerCase)
            .toList()
            .toArray(new String[0]);

safelist.addTags(lowercaseElementsArray);
safelist.addAttributes(":all", lowercaseAttributesArray);

String cleanedSvg = Jsoup.clean(svg, safeList)

SVG Source

<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
    <filter id="feOffset" x="-40" y="-20" width="100" height="200">
        <feOffset in="SourceGraphic" dx="60" dy="60" />
        <feGaussianBlur in="SourceGraphic" stdDeviation="5" result="blur2" />
        <feMerge>
            <feMergeNode
                    in="blur2"></feMergeNode>
            <feMergeNode></feMergeNode>
            <feMergeNode in="SourceGraphic" />
        </feMerge>
    </filter>
    <filter id="noise2" x="0" y="0" width="100%" height="100%">
        <feTurbulence baseFrequency="0.05" />
    </filter>
    <clipPath id="myClip2"          clipPathUnits="objectBoundingBox">
        <circle cx=".5" cy=".5" r=".35" />
    </clipPath>
    <filter id="convolveMatrix2" x="0" y="0" width="100%" height="100%">
        <feConvolveMatrix
                kernelMatrix="-1 0 0 0 0 0 0 0 1" />
    </filter>
    <rect
            x="40"
            y="40"
            width="100"
            height="100"
            style="stroke: #000000; fill: green; filter: url("#feOffset");" />
    <rect
            x="40"
            y="40"
            width="100"
            height="100"
            style="stroke: #000000; fill: green;" />
</svg> ```

Expected result

<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
 <filter id="feOffset" x="-40" y="-20" width="100" height="200">
  <feOffset in="SourceGraphic" dx="60" dy="60"></feOffset>
  <feGaussianBlur in="SourceGraphic" stdDeviation="5" result="blur2"></feGaussianBlur>
  <feMerge>
   <feMergeNode in="blur2"></feMergeNode>
   <feMergeNode></feMergeNode>
   <feMergeNode in="SourceGraphic"></feMergeNode>
  </feMerge>
 </filter> <filter id="noise2" x="0" y="0" width="100%" height="100%">
  <feTurbulence baseFrequency="0.05"></feTurbulence>
 </filter> <clipPath id="myClip2" clipPathUnits="objectBoundingBox">
  <circle cx=".5" cy=".5" r=".35"></circle>
 </clipPath> <filter id="convolveMatrix2" x="0" y="0" width="100%" height="100%">
  <feConvolveMatrix kernelMatrix="-1 0 0 0 0 0 0 0 1"></feConvolveMatrix>
 </filter> <rect x="40" y="40" width="100" height="100" style="stroke: #000000; fill: green; filter: url("#feOffset");"></rect> <rect x="40" y="40" width="100" height="100" style="stroke: #000000; fill: green;"></rect>
</svg>

Actual result

<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
 <filter id="feOffset" x="-40" y="-20" width="100" height="200">
  <feOffset in="SourceGraphic" dx="60" dy="60">
   <feGaussianBlur in="SourceGraphic" result="blur2">
    <feMerge>
     <feMergeNode in="blur2">
      <feMergeNode>
       <feMergeNode in="SourceGraphic">
       </feMergeNode>
       <filter id="noise2" x="0" y="0" width="100%" height="100%">
        <feTurbulence>
        </feTurbulence>
        <clipPath id="myClip2">
         <circle cx=".5" cy=".5" r=".35"></circle>
         <filter id="convolveMatrix2" x="0" y="0" width="100%" height="100%">
          <feConvolveMatrix>
          </feConvolveMatrix>
          <rect x="40" y="40" width="100" height="100" style="stroke: #000000; fill: green; filter: url("#feOffset");"></rect>
          <rect x="40" y="40" width="100" height="100" style="stroke: #000000; fill: green;"></rect>
         </filter>
        </clipPath>
       </filter>
      </feMergeNode>
     </feMergeNode>
    </feMerge>
   </feGaussianBlur>
  </feOffset>
 </filter>
</svg>

Metadata

Metadata

Assignees

Labels

bugA confirmed bug, that we should fixfixedAn {bug|improvement} that has been {fixed|implemented}

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions