-
-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BUGFIX: Render table dropdowns with correct icons
The icons were broken and the fallback title was not aligned pretty well. So the SVG markup string will be transformed now to an data-uri and this can be used as image sorce.
- Loading branch information
1 parent
da8c62c
commit b8ecc24
Showing
3 changed files
with
53 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
const REGEX = { | ||
whitespace: /\s+/g, | ||
urlHexPairs: /%[\dA-F]{2}/g, | ||
quotes: /"/g | ||
}; | ||
|
||
// Function to collapse whitespace in a string | ||
const collapseWhitespace = (str: string): string => | ||
str.trim().replace(REGEX.whitespace, ' '); | ||
|
||
// Function to encode data for a URI payload | ||
const dataURIPayload = (string: string): string => | ||
encodeURIComponent(string).replace(REGEX.urlHexPairs, specialHexEncode); | ||
|
||
// Function to handle special hex encoding | ||
const specialHexEncode = (match: string): string => { | ||
switch (match) { | ||
case '%20': | ||
return ' '; | ||
case '%3D': | ||
return '='; | ||
case '%3A': | ||
return ':'; | ||
case '%2F': | ||
return '/'; | ||
default: | ||
return match.toLowerCase(); // Compresses better | ||
} | ||
}; | ||
|
||
// Function to convert an SVG string to a tiny data URI | ||
const svgToDataUri = (svgString: string): string => { | ||
// Strip the Byte-Order Mark if the SVG has one | ||
if (svgString.charCodeAt(0) === 0xfeff) { | ||
svgString = svgString.slice(1); | ||
} | ||
|
||
const body = collapseWhitespace(svgString); | ||
return `data:image/svg+xml,${dataURIPayload(body)}`; | ||
}; | ||
|
||
// Add a static method to handle srcset conversions | ||
svgToDataUri.toSrcset = (svgString: string): string => | ||
svgToDataUri(svgString).replace(/ /g, '%20'); | ||
|
||
// Export the function as the default export | ||
export default svgToDataUri; |