Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New approach to svg-image #1983

Merged
merged 10 commits into from
Dec 12, 2024
Merged

New approach to svg-image #1983

merged 10 commits into from
Dec 12, 2024

Conversation

SonicScrewdriver
Copy link
Contributor

@SonicScrewdriver SonicScrewdriver commented Dec 11, 2024

Summary:

This PR updates our approach to loading graphie images for svg-image.

An error will be thrown if the labels are not found, but should not impede users. Many tests were added using the failed cases provided on the ticket, in order to ensure that this new approach works for nearly all use cases.

Issue: LEMS-2524

Test plan:

  • New svg-image tests
  • New graphie-utils tests that include (most) of the test cases provided.

@SonicScrewdriver SonicScrewdriver self-assigned this Dec 11, 2024
@SonicScrewdriver SonicScrewdriver changed the title New parseDataFromJSONP to protect against previous JSONP implementation New approach to svg-image Dec 11, 2024
Copy link
Contributor

github-actions bot commented Dec 11, 2024

npm Snapshot: Published

Good news!! We've packaged up the latest commit from this PR (2152613) and published it to npm. You
can install it using the tag PR1983.

Example:

yarn add @khanacademy/perseus@PR1983

If you are working in Khan Academy's webapp, you can run:

./dev/tools/bump_perseus_version.sh -t PR1983

Copy link
Contributor

github-actions bot commented Dec 11, 2024

Size Change: -246 B (-0.02%)

Total Size: 1.27 MB

Filename Size Change
packages/perseus/dist/es/index.js 415 kB -246 B (-0.06%)
ℹ️ View Unchanged
Filename Size
packages/kas/dist/es/index.js 39 kB
packages/keypad-context/dist/es/index.js 760 B
packages/kmath/dist/es/index.js 4.27 kB
packages/math-input/dist/es/index.js 77.9 kB
packages/math-input/dist/es/strings.js 1.79 kB
packages/perseus-core/dist/es/index.js 1.48 kB
packages/perseus-editor/dist/es/index.js 688 kB
packages/perseus-linter/dist/es/index.js 22.2 kB
packages/perseus/dist/es/strings.js 3.7 kB
packages/pure-markdown/dist/es/index.js 3.66 kB
packages/simple-markdown/dist/es/index.js 12.5 kB

compressed-size-action

const svgLocalLabelsRegex = /^file\+graphie:/;
const hashRegex = /\/([^/]+)$/;

/**
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These types needed to come along to support the parsing. I'm exporting them for Graphie2000, but I do wonder which side should be in charge of these.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it makes sense for Perseus to own them. They are a part of how SvgImage works and consumes graphie images.


// also test the array of edge cases from the testdata file
edgeCases.forEach((edgeCase, i) => {
it(`should parse the edge case ${i}`, () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There's a lot of edge cases, and some of them are quite similar. I could reduce them down, potentially, but I did like that we could cover 99% of the cases outlined in the ticket as failing to parse. Happy to take feedback on this one!

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think the collection of edge cases you have is probably good. The ones that will fail (like that one with a u'' prefix on all strings, is likely broken in prod too so I'd guess it would need to be re-written to work anyways.

},
});
});
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I understand that this is simply copy-pasted from the svg-image.tsx file, but I think a bit of simplification might be helpful here. Perhaps the shouldUseLocalizedData() conditional block could be written as follows:

const dataErrorHandler = (x, status, error) => {
    Log.error("Data load failed for svg-image", Errors.Service, {
        cause: error,
        loggedMetadata: {
            dataUrl: Util.getDataUrl(url),
            status,
        },
});

if (shouldUseLocalizedData()) {
    retrieveData(getLocalizedDataUrl(url), (x, status, error) => {
        cacheData.localized = false;

        // If there is isn't any localized data, fall back to
        // the original, unlocalized data
        retrieveData(Util.getDataUrl(url), dataErrorHandler)
} else {
    retrieveData(Util.getDataUrl(url), dataErrorHandler)
}

Copy link
Collaborator

@jeremywiebe jeremywiebe left a comment

Choose a reason for hiding this comment

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

This is great. I had one idea for a couple of more tests to cover localized graphies, but otherwise this is looking great! Thank-you!

const svgLocalLabelsRegex = /^file\+graphie:/;
const hashRegex = /\/([^/]+)$/;

/**
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it makes sense for Perseus to own them. They are a part of how SvgImage works and consumes graphie images.


// also test the array of edge cases from the testdata file
edgeCases.forEach((edgeCase, i) => {
it(`should parse the edge case ${i}`, () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think the collection of edge cases you have is probably good. The ones that will fail (like that one with a u'' prefix on all strings, is likely broken in prod too so I'd guess it would need to be re-written to work anyways.

// Assert
expect(container).toMatchSnapshot();
});
});
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a great set. I do think it'd be good to add one or two tests to cover the path of loading a localized Graphie. There's branching in there where it tries to load the localized version and falls back to en if it fails. I think if we add a test for localized loading and the fallback, that'd be awesome.

Copy link
Contributor

@mark-fitzgerald mark-fitzgerald left a comment

Choose a reason for hiding this comment

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

Thank you for tightening all this up!

@SonicScrewdriver SonicScrewdriver merged commit 2748a1f into main Dec 12, 2024
9 checks passed
@SonicScrewdriver SonicScrewdriver deleted the jer/lems-2524-svg-image branch December 12, 2024 18:17
SonicScrewdriver added a commit that referenced this pull request Dec 12, 2024
This PR was opened by the [Changesets
release](https://github.com/changesets/action) GitHub action. When
you're ready to do a release, you can merge this and the packages will
be published to npm automatically. If you're not ready to do a release
yet, that's fine, whenever you add more changesets to main, this PR will
be updated.


# Releases
## @khanacademy/[email protected]

### Minor Changes

- [#1988](#1988)
[`cc9d3a4bc`](cc9d3a4)
Thanks [@SonicScrewdriver](https://github.com/SonicScrewdriver)! -
Hiding graphie labels from screen readers.


- [#1983](#1983)
[`2748a1ff8`](2748a1f)
Thanks [@SonicScrewdriver](https://github.com/SonicScrewdriver)! -
Updating how svg-image loads data

## @khanacademy/[email protected]

### Patch Changes

- Updated dependencies
\[[`cc9d3a4bc`](cc9d3a4),
[`2748a1ff8`](2748a1f)]:
    -   @khanacademy/[email protected]
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.

3 participants