diff --git a/.husky/prepare-commit-msg b/.husky/prepare-commit-msg deleted file mode 100755 index dab0ce62..00000000 --- a/.husky/prepare-commit-msg +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/sh -. "$(dirname "$0")/_/husky.sh" -. "$(dirname "$0")/common.sh" - -npx devmoji -e --lint diff --git a/package.json b/package.json index 326e7f71..b1d6cd18 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "scripts": { "lint": "eslint 'src/**/*.{js,ts}?(x)' --fix", "clean": "rimraf dist es lib", - "build:esm": "tsc --module esnext --target es2015 --outDir ./es", + "build:esm": "tsc --module esnext --target es2017 --outDir ./es", "build:cjs": "tsc --module commonjs --target es5 --outDir ./lib", "build:umd": "rollup -c", "build": "run-s build:esm build:cjs build:umd", @@ -68,44 +68,36 @@ } ], "devDependencies": { - "@commitlint/cli": "^13.1.0", - "@commitlint/config-conventional": "^13.1.0", - "@fortawesome/fontawesome-free": "^5.15.4", - "@rollup/plugin-commonjs": "^20.0.0", - "@rollup/plugin-node-resolve": "^13.0.4", - "@rollup/plugin-typescript": "^8.2.5", - "@types/jasmine": "^3.9.1", - "@types/jquery": "^3.5.6", - "@typescript-eslint/eslint-plugin": "^4.18.0", - "@typescript-eslint/parser": "^4.18.0", + "@bubkoo/eslint-config": "^1.1.0", + "@bubkoo/rollup-config": "^1.0.3", + "@bubkoo/semantic-release-config": "^1.3.0", + "@bubkoo/tsconfig": "^1.0.0", + "@commitlint/cli": "^17.0.3", + "@commitlint/config-conventional": "^17.0.3", + "@fortawesome/fontawesome-free": "^6.1.2", + "@types/jasmine": "^4.0.3", + "@types/jquery": "^3.5.14", "coveralls": "^3.1.1", - "husky": "^7.0.2", + "eslint": "^8.21.0", + "husky": "^8.0.1", "imagediff": "^1.0.8", - "is-ci": "^3.0.0", - "jasmine-core": "^3.9.0", - "karma": "^6.3.4", - "karma-chrome-launcher": "^3.1.0", - "karma-coverage": "^2.0.3", - "karma-jasmine": "^4.0.1", - "karma-spec-reporter": "^0.0.32", - "karma-typescript": "^5.3.0", - "lint-staged": "^11.1.2", + "is-ci": "^3.0.1", + "jasmine-core": "^4.3.0", + "karma": "^6.4.0", + "karma-chrome-launcher": "^3.1.1", + "karma-coverage": "^2.2.0", + "karma-jasmine": "^5.1.0", + "karma-spec-reporter": "^0.0.34", + "karma-typescript": "^5.5.3", + "lint-staged": "^13.0.3", "npm-run-all": "^4.1.5", - "prettier": "^2.4.0", - "pretty-quick": "^3.1.1", + "prettier": "^2.7.1", + "pretty-quick": "^3.1.3", "rimraf": "^3.0.2", - "rollup": "^2.56.3", - "rollup-plugin-filesize": "^9.1.1", - "rollup-plugin-terser": "^7.0.2", - "tslib": "^2.3.1", - "eslint": "^7.22.0", - "eslint-config-airbnb-base": "^14.2.1", - "eslint-config-prettier": "^8.1.0", - "eslint-plugin-eslint-comments": "^3.2.0", - "eslint-plugin-import": "^2.22.1", - "eslint-plugin-prettier": "^4.0.0", - "eslint-plugin-promise": "^5.1.0", - "typescript": "^4.4.3" + "rollup": "^2.77.2", + "semantic-release": "^19.0.3", + "tslib": "^2.4.0", + "typescript": "^4.7.4" }, "repository": { "type": "git", diff --git a/src/embedWebFonts.ts b/src/embedWebFonts.ts index b96af12a..71b29374 100644 --- a/src/embedWebFonts.ts +++ b/src/embedWebFonts.ts @@ -74,10 +74,12 @@ function parseCSS(source: string) { // strip out comments let cssText = source.replace(commentsRegex, '') + // eslint-disable-next-line prefer-regex-literals const keyframesRegex = new RegExp( '((@.*?keyframes [\\s\\S]*?){([\\s\\S]*?}\\s*?)})', 'gi', ) + // eslint-disable-next-line no-constant-condition while (true) { const matches = keyframesRegex.exec(cssText) @@ -125,42 +127,38 @@ async function getCSSRules( styleSheets.forEach((sheet) => { if ('cssRules' in sheet) { try { - toArray(sheet.hasOwnProperty('cssRules')).forEach( - (item: CSSRule, index: number) => { - if (item.type === CSSRule.IMPORT_RULE) { - let importIndex = index + 1 - const url = (item as CSSImportRule).href - const deferred = fetchCSS(url) - .then((metadata) => - metadata ? embedFonts(metadata, options) : '', - ) - .then((cssText) => - parseCSS(cssText).forEach((rule) => { - try { - sheet.insertRule( - rule, - rule.startsWith('@import') - ? (importIndex += 1) - : // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - sheet.hasOwnProperty('cssRules').length, - ) - } catch (error) { - console.error('Error inserting rule from remote css', { - rule, - error, - }) - } - }), - ) - .catch((e) => { - console.error('Error loading remote css', e.toString()) - }) + toArray(sheet.cssRules || []).forEach((item, index) => { + if (item.type === CSSRule.IMPORT_RULE) { + let importIndex = index + 1 + const url = (item as CSSImportRule).href + const deferred = fetchCSS(url) + .then((metadata) => + metadata ? embedFonts(metadata, options) : '', + ) + .then((cssText) => + parseCSS(cssText).forEach((rule) => { + try { + sheet.insertRule( + rule, + rule.startsWith('@import') + ? (importIndex += 1) + : sheet.cssRules.length, + ) + } catch (error) { + console.error('Error inserting rule from remote css', { + rule, + error, + }) + } + }), + ) + .catch((e) => { + console.error('Error loading remote css', e.toString()) + }) - deferreds.push(deferred) - } - }, - ) + deferreds.push(deferred) + } + }) } catch (e) { const inline = styleSheets.find((a) => a.href == null) || document.styleSheets[0] @@ -172,12 +170,7 @@ async function getCSSRules( ) .then((cssText) => parseCSS(cssText).forEach((rule) => { - inline.insertRule( - rule, - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - sheet.hasOwnProperty('cssRules').length, - ) + inline.insertRule(rule, sheet.cssRules.length) }), ) .catch((err) => { @@ -195,11 +188,9 @@ async function getCSSRules( styleSheets.forEach((sheet) => { if ('cssRules' in sheet) { try { - toArray(sheet.hasOwnProperty('cssRules')).forEach( - (item: CSSStyleRule) => { - ret.push(item) - }, - ) + toArray(sheet.cssRules || []).forEach((item) => { + ret.push(item) + }) } catch (e) { console.error( `Error while reading CSS rules from ${sheet.href}`, diff --git a/src/util.ts b/src/util.ts index b6154c10..c1c074ad 100644 --- a/src/util.ts +++ b/src/util.ts @@ -69,7 +69,7 @@ export function parseDataUrlContent(dataURL: string) { return dataURL.split(/,/)[1] } -export const uuid = (function uuid() { +export const uuid = (() => { // generate uuid for className of pseudo elements. // We should not use GUIDs, otherwise pseudo elements sometimes cannot be captured. let counter = 0 @@ -85,10 +85,12 @@ export const uuid = (function uuid() { } })() -export const delay = - (ms: number) => - (args: T) => - new Promise((resolve) => setTimeout(() => resolve(args), ms)) +export function delay(ms: number) { + return (args: T) => + new Promise((resolve) => { + setTimeout(() => resolve(args), ms) + }) +} export function toArray(arrayLike: any): T[] { const arr: T[] = [] @@ -145,13 +147,13 @@ export function canvasToBlob( options: Options = {}, ): Promise { if (canvas.toBlob) { - return new Promise((resolve) => + return new Promise((resolve) => { canvas.toBlob( resolve, options.type ? options.type : 'image/png', options.quality ? options.quality : 1, - ), - ) + ) + }) } return new Promise((resolve) => { diff --git a/test/spec/helper.ts b/test/spec/helper.ts index bf96aa97..f065396e 100644 --- a/test/spec/helper.ts +++ b/test/spec/helper.ts @@ -187,7 +187,11 @@ export namespace Helper { async function recognize(dataUrl: string) { const data = new FormData() data.append('base64Image', dataUrl) - data.append('apikey', 'aa8c3d7de088957') + + // You may only perform this action upto maximum 180 number of times within + // 3600 seconds. + // data.append('apikey', 'aa8c3d7de088957') + data.append('apikey', 'K89675126388957') return fetch('https://api.ocr.space/parse/image', { method: 'post', @@ -204,10 +208,12 @@ export namespace Helper { } }) } - return result.join('\n') + const text = result.join('\n').trim().replace('\r\n', '\n') + // console.log(`recognized text: ${text}`) + return text }) .catch((err) => { - console.log(err) + // console.log(`text recognize error: ${err}`) return '' }) } diff --git a/test/spec/index.spec.ts b/test/spec/index.spec.ts index 1e1f7ce7..f2af38e0 100644 --- a/test/spec/index.spec.ts +++ b/test/spec/index.spec.ts @@ -35,8 +35,8 @@ describe('html to image', () => { it('should render to blob', (done) => { Helper.bootstrap('small/node.html', 'small/style.css', 'small/image') .then(htmlToImage.toBlob) - .then(global.URL.createObjectURL) - .then(Helper.check) + .then(global.URL.createObjectURL as any) + .then(Helper.check as any) .then(done) .catch(done) }) @@ -197,25 +197,26 @@ describe('html to image', () => { const input = document.getElementById('input') as HTMLInputElement input.value = text }) - .then(() => Helper.assertTextRendered([text])) + .then(Helper.assertTextRendered([text]) as any) .then(done) .catch(done) }) it('should render user input from