Skip to content

Commit 69754ad

Browse files
Ahmad-S792Ahmad Saleem
authored andcommitted
Fix SVG length attributes to reset to defaults when removed
https://bugs.webkit.org/show_bug.cgi?id=307876 rdar://170360351 Reviewed by Brent Fulgham. When removeAttribute() is called on SVG width/height attributes, Safari was trying to parse an empty string instead of restoring the default values (300 for width, 150 for height), causing an "Invalid value" error. This occurred because SVGLengthValue::construct() only checked for null strings, not empty strings. When an attribute is removed, an empty string is passed, which should be treated the same as null, both indicate the attribute should use its default value. This patch fixes it by checking isEmpty() and using the fallback value without attempting to parse, matching Firefox and Chrome behavior. It is covered by existing tests, where earlier, we were throwing error message incorrectly. * Source/WebCore/svg/SVGLengthValue.cpp: (WebCore::SVGLengthValue::construct): > Rebaselines: * LayoutTests/svg/custom/invalid-length-units-expected.txt: * LayoutTests/svg/parser/whitespace-length-expected.txt: * LayoutTests/svg/parser/whitespace-length-invalid-1-expected.txt: * LayoutTests/svg/parser/whitespace-length-invalid-2-expected.txt: * LayoutTests/svg/parser/whitespace-length-invalid-3-expected.txt: * LayoutTests/svg/parser/whitespace-length-invalid-4-expected.txt: Canonical link: https://commits.webkit.org/307585@main
1 parent 16defb5 commit 69754ad

File tree

7 files changed

+10
-6145
lines changed

7 files changed

+10
-6145
lines changed

LayoutTests/svg/custom/invalid-length-units-expected.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
CONSOLE MESSAGE: Error: Invalid value for <rect> attribute x=""
21
CONSOLE MESSAGE: Error: Invalid value for <rect> attribute x=" "
32
CONSOLE MESSAGE: Error: Invalid value for <rect> attribute x="foo"
43
CONSOLE MESSAGE: Error: Invalid value for <rect> attribute x="10foo"

LayoutTests/svg/parser/whitespace-length-expected.txt

Lines changed: 0 additions & 2142 deletions
Large diffs are not rendered by default.

LayoutTests/svg/parser/whitespace-length-invalid-1-expected.txt

Lines changed: 0 additions & 1000 deletions
Large diffs are not rendered by default.

LayoutTests/svg/parser/whitespace-length-invalid-2-expected.txt

Lines changed: 0 additions & 1000 deletions
Large diffs are not rendered by default.

LayoutTests/svg/parser/whitespace-length-invalid-3-expected.txt

Lines changed: 0 additions & 1000 deletions
Large diffs are not rendered by default.

LayoutTests/svg/parser/whitespace-length-invalid-4-expected.txt

Lines changed: 0 additions & 1000 deletions
Large diffs are not rendered by default.

Source/WebCore/svg/SVGLengthValue.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,21 @@ SVGLengthValue SVGLengthValue::construct(SVGLengthMode lengthMode, StringView va
120120
SVGLengthValue length(lengthMode);
121121

122122
parseError = SVGParsingError::None;
123+
124+
// Empty string should use fallback.
125+
if (valueAsString.isEmpty()) {
126+
if (!fallbackValue.isNull())
127+
return SVGLengthValue(lengthMode, fallbackValue);
128+
return length;
129+
}
130+
123131
if (length.setValueAsString(valueAsString).hasException())
124132
parseError = SVGParsingError::ParsingFailed;
125133
else if (negativeValuesMode == SVGLengthNegativeValuesMode::Forbid && length.valueInSpecifiedUnits() < 0)
126134
parseError = SVGParsingError::ForbiddenNegativeValue;
127135

128-
// If parsing failed or value is null, and we have a fallback, use it
129-
if (!fallbackValue.isNull() && (parseError != SVGParsingError::None || valueAsString.isNull()))
136+
// If parsing failed and we have a fallback, use it.
137+
if (!fallbackValue.isNull() && parseError != SVGParsingError::None)
130138
return SVGLengthValue(lengthMode, fallbackValue);
131139

132140
return length;

0 commit comments

Comments
 (0)