Pewarta Proxy

📋 Server Request & Proxy Logs

Loading active proxy settings...
Developed by Khairul Adha (adhexa.id)
Mengambil log server...

⚙️ Pengaturan Proxy Target

Skip to content

Fix viewBox SVG size when root tag is not at start - #16149

Open
jawshooah wants to merge 1 commit into
darkreader:mainfrom
jawshooah:fix-svg-viewbox-root-tag
Open

jawshooah wants to merge 1 commit into
darkreader:mainfrom
jawshooah:fix-svg-viewbox-root-tag

Conversation

@jawshooah

Copy link
Copy Markdown

Fixes #16128.

Problem

getImageDetails() sets the size of a viewBox-only SVG from its viewBox. This code had three problems:

  • It ran only if the SVG text started with <svg . An SVG with an XML declaration, a comment, or leading whitespace skipped it. Chrome then loaded the SVG at its default size (150 × 150), so the filtered copy showed cropped or empty in a small element.
  • includes('width=') also matched stroke-width= on the root element, so such an SVG also skipped the code.
  • The rebuilt SVG used svgText.slice(5), which assumes that the root tag is at index 0.

Changes

  • Find the first <svg followed by whitespace or >, and read the opening tag from that position.
  • Require whitespace before the width, height, and viewbox attribute names.
  • Insert the width and height attributes after <svg at the found position, and keep the text before it.

A comment before the root element that contains <svg text would match first. This is rare, and a full prolog parser needs much more code.

Tests

Six new tests in tests/inject/dynamic/image-analysis.tests.ts:

  • XML declaration, leading comment, leading whitespace, and stroke-width on the root element. These four fail without the change (useViewBox is false, and the size is 150 × 150).
  • A plain viewBox-only SVG and an SVG with a real size. These two pass with and without the change.

npm run test:inject passed in headless Chrome 156 (146 of 146), and npm run lint and npm test passed.

getImageDetails() set the size of a viewBox-only SVG only if the text
started with "<svg ". An SVG with an XML declaration, a comment, or
leading whitespace kept the browser default size, so the filtered copy
showed cropped or empty.

Also require whitespace before the width, height, and viewbox attribute
names, so that "stroke-width=" does not count as "width=".

Fixes darkreader#16128
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.

[Bug] viewBox-only SVG background images show cropped when the SVG text does not start with "<svg "

1 participant