Skip to content

SVG improvements - #2926

Closed
TimothyGu wants to merge 8 commits into
jsdom:mainfrom
TimothyGu:svg
Closed

TimothyGu wants to merge 8 commits into
jsdom:mainfrom
TimothyGu:svg

Conversation

@TimothyGu

@TimothyGu TimothyGu commented Mar 28, 2020 •

Copy link
Copy Markdown
Member

New elements: <defs>, <desc>, <g>, <metadata>, <switch>, <symbol>, <use>.

New method: SVGSVGElement.createSVGRect

Auxiliary classes:

  • DOMRect (new)
  • DOMRectReadOnly (new)
  • SVGAnimatedPreserveAspectRatio (new, reflection support)
  • SVGAnimatedRect (new, reflection support)
  • SVGAnimatedString (reflection support)
  • SVGPreserveAspectRatio (new)
  • SVGRect (new)

Tests are being written:

  • DOMRect, DOMRectReadOnly
  • SVGAnimatedPreserveAspectRatio, SVGPreserveAspectRatio
  • SVGAnimatedRect, SVGRect
  • SVGAnimatedString
  • <defs>
  • <desc>
  • <g>
  • <metadata>
  • <switch>
  • <symbol>
  • <use>

Fixes #2734.


Some roadmap for future work:

  • Implement SVGAnimatedLength + reflection (complicated, as it requires us to implement a CSS dimension parser and unit converter – with support for relative units like em, ideally)
  • Grind out more elements
  • Scripting and styling and other fanciness

@domenic domenic left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, again, great work tackling a user pain point! It's awesome to see the reflection support is flexible enough to handle this sort of thing.

It looks like my biggest concern is basically a spec issue, about DOMRect/DOMRectReadonly not being specced in a way that actually makes sense in an SVG context. The rest are mostly nits.

And, I guess, tests will be annoying :(.

Comment thread lib/jsdom/living/geometry/DOMRect-impl.js
Comment thread lib/jsdom/living/geometry/DOMRectReadOnly-impl.js Outdated
Comment thread lib/jsdom/living/geometry/DOMRectReadOnly-impl.js
Comment thread lib/jsdom/living/nodes/SVGUseElement.webidl
Comment thread lib/jsdom/living/svg/SVGAnimatedString-impl.js Outdated
Comment thread lib/jsdom/living/svg/SVGPreserveAspectRatio-impl.js Outdated
Comment thread lib/jsdom/living/svg/SVGPreserveAspectRatio.webidl
Comment thread scripts/webidl/convert.js Outdated
@julienw

julienw commented Nov 5, 2020

Copy link
Copy Markdown

I was looking for DOMRect support in jsdom and found this PR. Maybe it's ready for another review ?

@ExE-Boss ExE-Boss left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@julienw
That requires webidl2js v16.2.0, but JSDOM first has to be fixed to work with webidl2js v16.1.0, which is being done in #2956.

optional unrestricted double width = 0, optional unrestricted double height = 0);

// https://github.com/jsdom/webidl2js/issues/186
// [NewObject] static DOMRect fromRect(optional DOMRectInit other = {});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// [NewObject] static DOMRect fromRect(optional DOMRectInit other = {});
[NewObject, WebIDL2JSCallWithGlobal] static DOMRect fromRect(optional DOMRectInit other = {});

@Kingwl

Kingwl commented Jan 14, 2021

Copy link
Copy Markdown

Hey folks. Any progress?

We have a huge svg based app and cannot work fine. I think this PR may resolve the problem.

@Zearin

Zearin commented Apr 25, 2021

Copy link
Copy Markdown

Same as @julienw for me; was looking for DOMRect[ReadOnly] support in jsdom and found this.

I second @Kingwl’s curiosity, too.

(Great work on JSDom in general, and @TimothyGu for this awesome work!)

@julienw

julienw commented Jul 7, 2021

Copy link
Copy Markdown

#2956 seems to be fixed now :-)

Namely, <defs>, <desc>, <g>, <metadata>, and <switch>.

Fixes jsdom#2734.
New SVG elements:

- SVGSymbolElement
- SVGUseElement

New SVG interface mixins:

- SVGFitToViewBox
- SVGURIReference

New auxiliary classes:

- DOMRect
- DOMRectReadOnly
- SVGAnimatedPreserveAspectRatio
- SVGAnimatedRect
- SVGPreserveAspectRatio

Implemented reflection for:

- SVGAnimatedPreserveAspectRatio
- SVGAnimatedRect
- SVGAnimatedString
@okry

okry commented Feb 25, 2022

Copy link
Copy Markdown

Any updates on this? Looks like this PR has been sitting around for a while and my team could really use the DOMRect support with our application that uses radix.

Comment on lines +13 to +15
const reflectDeprecatedAttr = idl.extAttrs.find(extAttr => extAttr.name === extAttrName);
if (reflectDeprecatedAttr) {
return JSON.parse(reflectDeprecatedAttr.rhs.value);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function supports any extended attribute, so the variable name should probably be more generic:

Suggested change
const reflectDeprecatedAttr = idl.extAttrs.find(extAttr => extAttr.name === extAttrName);
if (reflectDeprecatedAttr) {
return JSON.parse(reflectDeprecatedAttr.rhs.value);
const foundAttr = idl.extAttrs.find(extAttr => extAttr.name === extAttrName);
if (foundAttr) {
return JSON.parse(foundAttr.rhs.value);

Comment on lines +10 to +12
// Use a do-while "loop" as JavaScript `goto end`.
// eslint-disable-next-line no-unreachable-loop
do {

@ExE-Boss ExE-Boss Feb 26, 2022 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use a labelled block to achieve this:

Suggested change
// Use a do-while "loop" as JavaScript `goto end`.
// eslint-disable-next-line no-unreachable-loop
do {
// Use a labelled block as JavaScript `goto end`.
block: {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Obscure language features FTW!

do {
// When the attribute does not exist.
if (typeof str !== "string") {
break;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
break;
break block;

skipSpace();
const xStr = matchNumber();
if (!xStr) {
break;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
break;
break block;

break;
}
if (!skipDelimiter()) {
break;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
break;
break block;

}
const heightStr = matchNumber();
if (!heightStr) {
break;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
break;
break block;

// Test for trailing junk.
skipSpace();
if (i < str.length) {
break;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
break;
break block;

// A negative value for <width> or <height> is an error and invalidates the 'viewBox' attribute.
const width = Number(widthStr);
if (width < 0) {
break;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
break;
break block;

}
const height = Number(heightStr);
if (height < 0) {
break;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
break;
break block;

skipSpace();
return i > start;
}
} while (false);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} while (false);
}

@Zearin

Zearin commented Feb 25, 2023 •

Copy link
Copy Markdown

Almost a year two years† after my last visit to this issue. It looks like a lot of work has been going on in this PR.

Can I request a re-review?

Would love to use DOMRect and friends in my JSDOM tests…


† EDIT: (Still waiting for my coffee to reach my brain.) It’s almost two years since my last visit to this issue.

domenic pushed a commit that referenced this pull request May 27, 2023
domenic pushed a commit that referenced this pull request May 27, 2023
@Zearin

Zearin commented Dec 23, 2023

Copy link
Copy Markdown

@domenic Thank you for #3555! 🙏

@TimothyGu Are you still working on this?

If not, can someone mark this “HELP WANTED” (or similar), and find a steward to take over to completion?

@charlieforward9

Copy link
Copy Markdown

Found this from here. Would be awesome to see this merged! What is the status here. I see its been a few months.

@domenic domenic added the metr uplift A candidate for the METR Uplift experiment label Mar 13, 2025
@domenic

domenic commented Mar 23, 2025

Copy link
Copy Markdown
Member

Rebased and merged in #3843.

@domenic domenic closed this Mar 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

metr uplift A candidate for the METR Uplift experiment needs tests svg

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement SVGGElement

8 participants