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

BUGFIX: Render table dropdowns with correct icons #3898

Open
wants to merge 3 commits into
base: 8.3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import PropTypes from 'prop-types';
import {$get} from 'plow-js';

import {neos} from '@neos-project/neos-ui-decorators';
import {svgToDataUri} from '@neos-project/utils-helpers';
import ckeIcons from './icons';

import style from './TableDropDown.module.css';
Expand Down Expand Up @@ -40,12 +41,13 @@ export default class TableDropDownButton extends PureComponent {
}

render() {
const iconDataUri = svgToDataUri(ckeIcons[this.props.icon]);
return (
<DropDown
padded={false}
>
<DropDown.Header title={this.props.i18nRegistry.translate(this.props.tooltip)}>
<img style={{verticalAlign: 'text-top'}} src={ckeIcons[this.props.icon]} alt={this.props.i18nRegistry.translate(this.props.tooltip)} />
<img style={{verticalAlign: 'text-top'}} src={iconDataUri} alt={this.props.i18nRegistry.translate(this.props.tooltip)} />
Copy link
Member

Choose a reason for hiding this comment

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

öhm do i get that right that we want to have the svgs as data-url and not as raw content string?
That can be done via the build setup. For the neos ui i introduced the convention that files ending with .dataurl.svg will get that treatment via the build setup already introduced via:

#3836

Copy link
Member Author

Choose a reason for hiding this comment

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

The issue here was that the content of this.props.icon is just the SVG markup.
And that in a src tag is not working, and I bet that the build pipeline also doesn't handle it.

</DropDown.Header>
<DropDown.Contents className={style.contents} scrollable={false}>
{this.props.options.map(item => item.type === 'checkBox' ? (
Expand Down
2 changes: 2 additions & 0 deletions packages/utils-helpers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import isEmail from './isEmail';
import {isUri} from './isUri';
import isEqualSet from './isEqualSet';
import isNil from './isNil';
import svgToDataUri from './svgToDataUri';

export {
delay,
Expand All @@ -23,6 +24,7 @@ export {
isEqualSet,
stripTags,
stripTagsEncoded,
svgToDataUri,
cancelIdleCallback,
requestIdleCallback
};
21 changes: 21 additions & 0 deletions packages/utils-helpers/src/svgToDataUri.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import svgToDataUri from './svgToDataUri';

describe('svgToDataUri', () => {
it('should convert an SVG string to a valid data URI', () => {
const svgContent = `<svg width="1" height="1" xmlns="http://www.w3.org/2000/svg"><rect width="100%" height="100%" fill="blue"/></svg>`;
const base64Content = 'PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjxyZWN0IHdpZHRoPSIxMDAlIiBoZWlnaHQ9IjEwMCUiIGZpbGw9ImJsdWUiLz48L3N2Zz4=';
const dataUri = svgToDataUri(svgContent);
expect(dataUri).toBe(`data:image/svg+xml;base64,${base64Content}`);
});

it('should handle special characters correctly', () => {
const svgContent = `<svg xmlns="http://www.w3.org/2000/svg"><text x="10" y="20">Héllo, Wörld!</text></svg>`;
const base64Content = 'PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjx0ZXh0IHg9IjEwIiB5PSIyMCI+SMOpbGxvLCBXw7ZybGQhPC90ZXh0Pjwvc3ZnPg==';
const dataUri = svgToDataUri(svgContent);
expect(dataUri).toBe(`data:image/svg+xml;base64,${base64Content}`);
});

it('should throw an error for invalid SVG input', () => {
expect(() => svgToDataUri('<div>Not an SVG</div>')).not.toThrow();
});
});
10 changes: 10 additions & 0 deletions packages/utils-helpers/src/svgToDataUri.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Function to convert an SVG content string to a tiny data URI using base64 encoding.
* @param svgContent
*/
const svgToDataUri = (svgContent: string): string => {
const base64EncodedSVG = btoa(unescape(encodeURIComponent(svgContent)));
return `data:image/svg+xml;base64,${base64EncodedSVG}`;
};

export default svgToDataUri;
Loading