Skip to content

Handle an icon file that exists but isn't valid SVG - #19

Open
pcbeingused333 wants to merge 2 commits into
Rails-Designer:mainfrom
pcbeingused333:fix/sprite-malformed-svg
Open

pcbeingused333 wants to merge 2 commits into
Rails-Designer:mainfrom
pcbeingused333:fix/sprite-malformed-svg

Conversation

@pcbeingused333

@pcbeingused333 pcbeingused333 commented Sep 9, 2026

Copy link
Copy Markdown

Three code paths read an icon file and call at_css("svg") without checking the result. When the file exists but isn't valid SVG — empty, truncated, or an HTML error page from a bad sync — at_css("svg") returns nil and the caller crashes with NoMethodError.

Sprite#symbol_from

svg_element = Nokogiri::XML(File.read(reference.file_path)).at_css("svg")
view_box = svg_element["viewBox"] || "0 0 24 24"
# => NoMethodError: undefined method `[]' for nil:NilClass

A missing icon is already handled (rescue Icons::IconNotFound → warn → skip). An unparseable one now is too — warn and skip.

Icon#svg

Nokogiri::HTML::DocumentFragment.parse(File.read(file_path)).at_css("svg")
  .tap { |svg| attach_attributes(to: svg) }   # attach_attributes(to: nil) => NoMethodError

Now raises Icons::IconNotFound (with a message noting the file isn't valid SVG), next to the existing Errno::ENOENT rescue.

SpriteIcon#sprite_svg parses a fixed template, so it's fine.

Tests

  • sprite_test.rb — a temp library with one junk .svg and one real one: the sprite keeps the real symbol, omits the junk one, warns.
  • icon_test.rb — an empty .svg raises IconNotFound.

Both fail on main (NoMethodError). bundle exec rake is green (91 runs, 0 failures).

🤖 Generated with Claude Code

pcbeingused333 and others added 2 commits September 8, 2026 20:09
`Sprite#symbol_from` reads the icon file and calls `["viewBox"]` on
`Nokogiri::XML(...).at_css("svg")`. When the file exists but isn't valid
SVG (empty, truncated, or HTML), `at_css("svg")` returns `nil` and the
whole sprite build dies with `NoMethodError: undefined method '[]' for
nil`. A missing icon is already handled by warning and skipping; an
unparseable one now is too.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GtYgwYNfJ3wHrw9xrooVoB
`Icon#svg` has the same `at_css("svg")` -> `nil` path as the sprite
builder: a file that exists but isn't valid SVG passed `nil` into
`attach_attributes`, raising `NoMethodError`. It now raises
`Icons::IconNotFound` with a message noting the file is not valid SVG,
alongside the existing `Errno::ENOENT` rescue.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GtYgwYNfJ3wHrw9xrooVoB
@pcbeingused333 pcbeingused333 changed the title Skip an SVG sprite icon whose file isn't parseable instead of crashing Handle an icon file that exists but isn't valid SVG Sep 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant