Skip to content

Handle relative image hrefs without base URI#498

Merged
wieslawsoltes merged 2 commits into
masterfrom
fix/issue-472-image-base-uri
May 9, 2026
Merged

Handle relative image hrefs without base URI#498
wieslawsoltes merged 2 commits into
masterfrom
fix/issue-472-image-base-uri

Conversation

@wieslawsoltes
Copy link
Copy Markdown
Owner

PR Summary: Handle Relative Image Hrefs Without a Base URI

Overview

Fixes SvgService.GetImageUri / image loading behavior for SVG documents that contain a relative image reference, such as xlink:href="6F03BD87.png", when the owning document has no BaseUri.

Previously, unresolved relative image URIs could flow into the external image loading path. In issue #472, this showed up as a null-base URI failure while trying to resolve a relative image reference. The intended behavior for this case is to gracefully skip the missing/unresolvable image and continue rendering the rest of the SVG.

Changes

  • Updated SvgService.GetImage so unresolved relative image URIs return null directly.
  • Kept data URI handling unchanged.
  • Kept absolute URI image loading unchanged.
  • Added model-level regression coverage for:
    • Relative image hrefs without a document base URI.
    • GetImage returning null for unresolved relative image hrefs.
    • The document-owner overload preserving relative URI behavior when BaseUri is missing.
    • The document-owner overload resolving relative hrefs when BaseUri exists.
  • Added SKSvg rendering regression coverage for an issue-shaped SVG that includes a missing relative xlink:href.
  • Strengthened the rendering regression by checking output dimensions and representative rendered pixels, proving normal SVG content still renders after the missing image is skipped.

Rationale

An SVG loaded from a stream or inline string may not have a usable BaseUri. In that case a relative external image reference cannot be resolved safely. Returning null from image loading matches the existing renderer behavior for unavailable images: the image node becomes non-renderable while the rest of the document continues through the render pipeline.

This avoids depending on exception handling from WebRequest.Create or URI construction for normal missing-resource control flow.

Verification

Commands run during development:

git submodule update --init --recursive
dotnet restore Svg.Skia.slnx
dotnet format Svg.Skia.slnx --no-restore --include src/Svg.Model/Services/SvgService.cs tests/Svg.Model.UnitTests/SvgServiceImageUriTests.cs tests/Svg.Skia.UnitTests/SKSvgTests.cs
git diff --check
dotnet test tests/Svg.Model.UnitTests/Svg.Model.UnitTests.csproj -f net10.0 -c Release --no-restore /p:IsTestProject=true --filter "FullyQualifiedName~SvgServiceImageUriTests"
dotnet test tests/Svg.Skia.UnitTests/Svg.Skia.UnitTests.csproj -f net10.0 -c Release --no-restore /p:IsTestProject=true --filter "FullyQualifiedName~SKSvgTests.Load_RelativeImageHrefWithoutBaseUri_SkipsMissingImage"
dotnet build Svg.Skia.slnx -c Release --no-restore

Results:

  • Model regression tests passed: 4 passed, 0 failed.
  • SKSvg rendering regression passed: 1 passed, 0 failed.
  • Release build passed with existing package advisory warnings.

Notes

  • The PR summary file itself is intentionally not committed.
  • The branch contains two commits:
    • Skip unresolved relative image loads
    • Cover missing relative image base URI

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant