diff --git a/jsvg/src/main/java/com/github/weisj/jsvg/parser/impl/ParserUtil.java b/jsvg/src/main/java/com/github/weisj/jsvg/parser/impl/ParserUtil.java index e08e7179..6e9223cb 100644 --- a/jsvg/src/main/java/com/github/weisj/jsvg/parser/impl/ParserUtil.java +++ b/jsvg/src/main/java/com/github/weisj/jsvg/parser/impl/ParserUtil.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2025 Jannis Weis + * Copyright (c) 2025-2026 Jannis Weis * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and * associated documentation files (the "Software"), to deal in the Software without restriction, @@ -101,15 +101,18 @@ public static float parseFloat(@Nullable String value, float fallback) { if (value == null || value.isEmpty()) return fallback; List list = new ArrayList<>(); int max = value.length(); + boolean splitOnWhitespace = splitter.splitOnWhitespace(); int start = 0; int i = 0; boolean inWhiteSpace = false; + boolean lastSplitWasWhiteSpace = false; for (; i < max; i++) { char c = value.charAt(i); if (Character.isWhitespace(c)) { - if (!inWhiteSpace && splitter.splitOnWhitespace() && i - start > 0) { + if (!inWhiteSpace && splitOnWhitespace && i - start > 0) { list.add(value.substring(start, i)); start = i + 1; + lastSplitWasWhiteSpace = true; } inWhiteSpace = true; continue; @@ -117,9 +120,12 @@ public static float parseFloat(@Nullable String value, float fallback) { inWhiteSpace = false; ListSplitter.SplitResult result = splitter.testChar(c, i - start); if (result.shouldSplit()) { - list.add(value.substring(start, i)); + if (!(lastSplitWasWhiteSpace && i == start)) list.add(value.substring(start, i)); start = result.shouldIncludeChar() ? i : i + 1; + lastSplitWasWhiteSpace = false; + continue; } + lastSplitWasWhiteSpace = false; } if (i - start > 0) list.add(value.substring(start, i)); return list.toArray(new String[0]); diff --git a/jsvg/src/test/java/com/github/weisj/jsvg/attribute/AttributeParserTest.java b/jsvg/src/test/java/com/github/weisj/jsvg/attribute/AttributeParserTest.java index d208b6f2..3800add2 100644 --- a/jsvg/src/test/java/com/github/weisj/jsvg/attribute/AttributeParserTest.java +++ b/jsvg/src/test/java/com/github/weisj/jsvg/attribute/AttributeParserTest.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2021-2025 Jannis Weis + * Copyright (c) 2021-2026 Jannis Weis * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and * associated documentation files (the "Software"), to deal in the Software without restriction, @@ -22,11 +22,15 @@ package com.github.weisj.jsvg.attribute; import java.util.Random; +import java.util.function.BiConsumer; +import java.util.function.BiFunction; +import com.github.weisj.jsvg.parser.NumberListSplitter; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import com.github.weisj.jsvg.attributes.transform.TransformPart; import com.github.weisj.jsvg.paint.impl.DefaultPaintParser; import com.github.weisj.jsvg.parser.impl.AttributeParser; import com.github.weisj.jsvg.parser.impl.SeparatorMode; @@ -51,6 +55,21 @@ void testStringListRequiredComma() { testStringList(true); } + @Test + void testNumberSplitter() { + BiConsumer test = (str, expected) -> { + String[] result = parser.parseStringList(str, NumberListSplitter.INSTANCE); + Assertions.assertArrayEquals(expected, result); + }; + test.accept("1,1", new String[] {"1", "1"}); + test.accept("1 1", new String[] {"1", "1"}); + test.accept("1-1", new String[] {"1", "-1"}); + test.accept("1, 1", new String[] {"1", "1"}); + test.accept("1, 1", new String[] {"1", "1"}); + test.accept("1 , 1", new String[] {"1", "1"}); + test.accept("1, ,1", new String[] {"1", "", "1"}); + } + @Test void testFloatList() { Random r = new Random(); @@ -91,4 +110,22 @@ private String appendToList(T[] arr, Random r, boolean requireComma) { } return builder.toString(); } + + @Test + void testTransformScaleAllowsCommaAndWhitespace() { + TransformPart part = parser.parseTransformPart(TransformPart.TransformType.SCALE, "-1, 1"); + Assertions.assertNotNull(part); + } + + @Test + void testTransformScaleAllowsMixedCommaAndWhitespace() { + TransformPart part = parser.parseTransformPart(TransformPart.TransformType.SCALE, "-1 , 1"); + Assertions.assertNotNull(part); + } + + @Test + void testTransformCommaOnlyKeepsEmptyEntryBetweenCommas() { + String[] values = parser.parseStringList("1,,1", SeparatorMode.COMMA_ONLY); + Assertions.assertArrayEquals(new String[] {"1", "", "1"}, values); + } }