diff --git a/esbuild.config.mjs b/esbuild.config.mjs index c058c22..90cfa63 100644 --- a/esbuild.config.mjs +++ b/esbuild.config.mjs @@ -18,6 +18,9 @@ esbuild banner: { js: banner, }, + loader: { + ".svg": "text", + }, entryPoints: ["./src/main.ts"], bundle: true, external: [ diff --git a/package.json b/package.json index 7e3cf86..37c2e52 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "devDependencies": { "@types/chai": "^4.2.22", "@types/chai-as-promised": "^7.1.4", + "@types/jsdom": "^16.2.13", "@types/mime-types": "^2.1.1", "@types/mocha": "^9.0.0", "@types/node": "^14.14.37", @@ -32,6 +33,7 @@ "cross-env": "^7.0.3", "dotenv": "^10.0.0", "esbuild": "^0.14.0", + "jsdom": "^19.0.0", "mocha": "^9.1.3", "prettier": "^2.4.1", "ts-loader": "^9.2.6", diff --git a/src/misc.ts b/src/misc.ts index e66cae1..a6b0e75 100644 --- a/src/misc.ts +++ b/src/misc.ts @@ -155,3 +155,11 @@ export const getPathFolder = (a: string) => { export const setToString = (a: Set, delimiter: string = ",") => { return [...a].join(delimiter); }; + +export const extractSvgSub = (x: string, subEl: string = "rect") => { + const parser = new window.DOMParser(); + const dom = parser.parseFromString(x, "image/svg+xml"); + const svg = dom.querySelector("svg"); + svg.setAttribute("viewbox", "0 0 10 10"); + return svg.innerHTML; +}; diff --git a/tests/misc.test.ts b/tests/misc.test.ts index 4022a0a..c3cb791 100644 --- a/tests/misc.test.ts +++ b/tests/misc.test.ts @@ -1,6 +1,7 @@ import * as fs from "fs"; import * as path from "path"; import { expect } from "chai"; +import { JSDOM } from "jsdom"; import * as misc from "../src/misc"; @@ -133,3 +134,17 @@ describe("Misc: get dirname", () => { expect(y).to.equal("/"); }); }); + +describe("Misc: extract svg", () => { + beforeEach(function () { + const fakeBrowser = new JSDOM(""); + global.window = fakeBrowser.window as any; + }); + + it("should extract rect from svg correctly", () => { + const x = ""; + const y = misc.extractSvgSub(x); + // console.log(x) + expect(y).to.equal(""); + }); +}); diff --git a/webpack.config.js b/webpack.config.js index ff4875d..af4cf51 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -33,6 +33,10 @@ module.exports = { use: "ts-loader", exclude: /node_modules/, }, + { + test: /\.svg?$/, + type: "asset/source", + }, ], }, resolve: {