|
Last change
on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago |
|
Fix frontend appearance
|
-
Property mode
set to
100644
|
|
File size:
1.0 KB
|
| Line | |
|---|
| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Natsu @xiaoxiaojx
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | // data URL scheme: "data:text/javascript;charset=utf-8;base64,some-string"
|
|---|
| 9 | // http://www.ietf.org/rfc/rfc2397.txt
|
|---|
| 10 | const URIRegEx = /^data:([^;,]+)?((?:;[^;,]+)*?)(?:;(base64)?)?,(.*)$/i;
|
|---|
| 11 |
|
|---|
| 12 | /**
|
|---|
| 13 | * Decodes the provided uri.
|
|---|
| 14 | * @param {string} uri data URI
|
|---|
| 15 | * @returns {Buffer | null} decoded data
|
|---|
| 16 | */
|
|---|
| 17 | const decodeDataURI = (uri) => {
|
|---|
| 18 | const match = URIRegEx.exec(uri);
|
|---|
| 19 | if (!match) return null;
|
|---|
| 20 |
|
|---|
| 21 | const isBase64 = match[3];
|
|---|
| 22 | const body = match[4];
|
|---|
| 23 |
|
|---|
| 24 | if (isBase64) {
|
|---|
| 25 | return Buffer.from(body, "base64");
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | // CSS allows to use `data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg"><rect width="100%" height="100%" style="stroke: rgb(223,224,225); stroke-width: 2px; fill: none; stroke-dasharray: 6px 3px" /></svg>`
|
|---|
| 29 | // so we return original body if we can't `decodeURIComponent`
|
|---|
| 30 | try {
|
|---|
| 31 | return Buffer.from(decodeURIComponent(body), "ascii");
|
|---|
| 32 | } catch (_) {
|
|---|
| 33 | return Buffer.from(body, "ascii");
|
|---|
| 34 | }
|
|---|
| 35 | };
|
|---|
| 36 |
|
|---|
| 37 | module.exports = {
|
|---|
| 38 | URIRegEx,
|
|---|
| 39 | decodeDataURI
|
|---|
| 40 | };
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.