LibWeb/SVG: Prefer href to xlink:href attribute on images
Some checks are pending
CI / macOS, arm64, Sanitizer, Clang (push) Waiting to run
CI / Linux, x86_64, Fuzzers, Clang (push) Waiting to run
CI / Linux, x86_64, Sanitizer, GNU (push) Waiting to run
CI / Linux, x86_64, Sanitizer, Clang (push) Waiting to run
Package the js repl as a binary artifact / Linux, arm64 (push) Waiting to run
Package the js repl as a binary artifact / macOS, arm64 (push) Waiting to run
Package the js repl as a binary artifact / Linux, x86_64 (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Label PRs with merge conflicts / auto-labeler (push) Waiting to run
Push notes / build (push) Waiting to run

This commit is contained in:
Tim Ledbetter 2025-07-10 22:52:47 +01:00 committed by Jelle Raaijmakers
commit 4dd538e708
Notes: github-actions[bot] 2025-07-12 11:14:56 +00:00
5 changed files with 31 additions and 1 deletions

View file

@ -14,6 +14,7 @@
#include <LibWeb/HTML/PotentialCORSRequest.h> #include <LibWeb/HTML/PotentialCORSRequest.h>
#include <LibWeb/HTML/SharedResourceRequest.h> #include <LibWeb/HTML/SharedResourceRequest.h>
#include <LibWeb/Layout/SVGImageBox.h> #include <LibWeb/Layout/SVGImageBox.h>
#include <LibWeb/Namespace.h>
#include <LibWeb/Painting/Paintable.h> #include <LibWeb/Painting/Paintable.h>
#include <LibWeb/SVG/SVGDecodedImageData.h> #include <LibWeb/SVG/SVGDecodedImageData.h>
@ -60,7 +61,19 @@ void SVGImageElement::attribute_changed(FlyString const& name, Optional<String>
auto parsed_value = AttributeParser::parse_coordinate(value.value_or(String {})); auto parsed_value = AttributeParser::parse_coordinate(value.value_or(String {}));
MUST(height()->base_val()->set_value(parsed_value.value_or(0))); MUST(height()->base_val()->set_value(parsed_value.value_or(0)));
} else if (name == SVG::AttributeNames::href) { } else if (name == SVG::AttributeNames::href) {
process_the_url(value); // https://svgwg.org/svg2-draft/linking.html#XLinkRefAttrs
// For backwards compatibility, elements with an href attribute also recognize an href attribute in the
// XLink namespace. If the element is in the XLink namespace, it does not recognize an href attribute in the
// SVG namespace. When the href attribute is present in both the XLink namespace and without a namespace, the
// value of the attribute without a namespace shall be used. The attribute in the XLink namespace shall be ignored.
if (namespace_ == Namespace::XLink && has_attribute_ns({}, name))
return;
auto href = value;
if (!namespace_.has_value() && !href.has_value())
href = get_attribute_ns(SVG::AttributeNames::href, Namespace::XLink);
process_the_url(href);
} }
} }

View file

@ -0,0 +1,3 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 50 50">
<rect fill="red" width="50" height="50"/>
</svg>

After

Width:  |  Height:  |  Size: 114 B

View file

@ -0,0 +1,4 @@
<!DOCTYPE html>
<svg>
<rect width="100" height="100" fill="green"></rect>
</svg>

View file

@ -0,0 +1,5 @@
<!DOCTYPE html>
<link rel="match" href="../../expected/svg/image-xlink-href-ref.html" />
<svg>
<image id="test" href="../../data/50x50-green.svg" xlink:href="../../data/50x50-red.svg" width="100" height="100" />
</svg>

View file

@ -0,0 +1,5 @@
<!DOCTYPE html>
<link rel="match" href="../../expected/svg/image-xlink-href-ref.html" />
<svg>
<image id="test" xlink:href="../../data/50x50-green.svg" width="100" height="100" />
</svg>