Skip to content

Commit

Permalink
style: bracketSpacing
Browse files Browse the repository at this point in the history
  • Loading branch information
yorickshan committed Jul 1, 2024
1 parent bf7f59e commit 01a3aae
Show file tree
Hide file tree
Showing 132 changed files with 729 additions and 709 deletions.
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"trailingComma": "none",
"tabWidth": 4,
"bracketSpacing": false,
"bracketSpacing": true,
"singleQuote": true,
"printWidth": 120
}
8 changes: 4 additions & 4 deletions scripts/create-reftest-list.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

import {readFileSync, writeFileSync} from 'fs';
import {resolve, relative} from 'path';
import {sync} from 'glob';
import { readFileSync, writeFileSync } from 'fs';
import { resolve, relative } from 'path';
import { sync } from 'glob';

const slash = require('slash');

Expand All @@ -22,7 +22,7 @@ const ignoredTests = readFileSync(path)
.toString()
.split(/\r\n|\r|\n/)
.filter((l) => l.length)
.reduce((acc: {[key: string]: string[]}, l) => {
.reduce((acc: { [key: string]: string[] }, l) => {
const m = l.match(/^(\[(.+)\])?(.+)$/i);
if (m) {
acc[m[3]] = m[2] ? m[2].split(',') : [];
Expand Down
4 changes: 2 additions & 2 deletions scripts/create-reftest-result-list.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {readdirSync, readFileSync, writeFileSync} from 'fs';
import {resolve} from 'path';
import { readdirSync, readFileSync, writeFileSync } from 'fs';
import { resolve } from 'path';

if (process.argv.length <= 2) {
console.log('No metadata path provided');
Expand Down
16 changes: 8 additions & 8 deletions src/__tests__/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import html2canvas from '../index';

import {CanvasRenderer} from '../render/canvas/canvas-renderer';
import {DocumentCloner} from '../dom/document-cloner';
import {COLORS} from '../css/types/color';
import { CanvasRenderer } from '../render/canvas/canvas-renderer';
import { DocumentCloner } from '../dom/document-cloner';
import { COLORS } from '../css/types/color';

jest.mock('../core/logger');
jest.mock('../css/layout/bounds');
Expand All @@ -12,7 +12,7 @@ jest.mock('../dom/node-parser', () => {
isBodyElement: () => false,
isHTMLElement: () => false,
parseTree: jest.fn().mockImplementation(() => {
return {styles: {}};
return { styles: {} };
})
};
});
Expand All @@ -37,7 +37,7 @@ describe('html2canvas', () => {
expect.objectContaining({
cache: expect.any(Object),
logger: expect.any(Object),
windowBounds: expect.objectContaining({left: 12, top: 34})
windowBounds: expect.objectContaining({ left: 12, top: 34 })
}),
expect.objectContaining({
backgroundColor: 0xffffffff,
Expand All @@ -53,7 +53,7 @@ describe('html2canvas', () => {
});

it('should have transparent background with backgroundColor: null', async () => {
await html2canvas(element, {backgroundColor: null});
await html2canvas(element, { backgroundColor: null });
expect(CanvasRenderer).toHaveBeenLastCalledWith(
expect.anything(),
expect.objectContaining({
Expand All @@ -64,7 +64,7 @@ describe('html2canvas', () => {

it('should use existing canvas when given as option', async () => {
const canvas = {} as HTMLCanvasElement;
await html2canvas(element, {canvas});
await html2canvas(element, { canvas });
expect(CanvasRenderer).toHaveBeenLastCalledWith(
expect.anything(),
expect.objectContaining({
Expand All @@ -75,7 +75,7 @@ describe('html2canvas', () => {

it('should not remove cloned window when removeContainer: false', async () => {
DocumentCloner.destroy = jest.fn();
await html2canvas(element, {removeContainer: false});
await html2canvas(element, { removeContainer: false });
expect(CanvasRenderer).toHaveBeenLastCalledWith(
expect.anything(),
expect.objectContaining({
Expand Down
4 changes: 2 additions & 2 deletions src/core/__mocks__/context.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {logger, Logger} from './logger';
import { logger, Logger } from './logger';

export class Context {
readonly logger: Logger = logger;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
readonly _cache: {[key: string]: Promise<any>} = {};
readonly _cache: { [key: string]: Promise<any> } = {};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
readonly cache: any;

Expand Down
46 changes: 23 additions & 23 deletions src/core/__tests__/cache-storage.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {deepStrictEqual, fail} from 'assert';
import {FEATURES} from '../features';
import {CacheStorage} from '../cache-storage';
import {Context} from '../context';
import {Bounds} from '../../css/layout/bounds';
import { deepStrictEqual, fail } from 'assert';
import { FEATURES } from '../features';
import { CacheStorage } from '../cache-storage';
import { Context } from '../context';
import { Bounds } from '../../css/layout/bounds';

const proxy = 'http://example.com/proxy';

Expand Down Expand Up @@ -97,14 +97,14 @@ class XMLHttpRequestMock {
}
}

Object.defineProperty(global, 'Image', {value: ImageMock, writable: true});
Object.defineProperty(global, 'Image', { value: ImageMock, writable: true });
Object.defineProperty(global, 'XMLHttpRequest', {
value: XMLHttpRequestMock,
writable: true
});

const setFeatures = (opts: {[key: string]: boolean} = {}) => {
const defaults: {[key: string]: boolean} = {
const setFeatures = (opts: { [key: string]: boolean } = {}) => {
const defaults: { [key: string]: boolean } = {
SUPPORT_SVG_DRAWING: true,
SUPPORT_CORS_IMAGES: true,
SUPPORT_CORS_XHR: true,
Expand All @@ -126,7 +126,7 @@ describe('cache-storage', () => {
images.splice(0, images.length);
});
it('addImage adds images to cache', async () => {
const {cache} = createMockContext('http://example.com', {proxy: null});
const { cache } = createMockContext('http://example.com', { proxy: null });
await cache.addImage('http://example.com/test.jpg');
await cache.addImage('http://example.com/test2.jpg');

Expand All @@ -136,7 +136,7 @@ describe('cache-storage', () => {
});

it('addImage should not add duplicate entries', async () => {
const {cache} = createMockContext('http://example.com');
const { cache } = createMockContext('http://example.com');
await cache.addImage('http://example.com/test.jpg');
await cache.addImage('http://example.com/test.jpg');

Expand All @@ -146,7 +146,7 @@ describe('cache-storage', () => {

describe('svg', () => {
it('should add svg images correctly', async () => {
const {cache} = createMockContext('http://example.com');
const { cache } = createMockContext('http://example.com');
await cache.addImage('http://example.com/test.svg');
await cache.addImage('http://example.com/test2.svg');

Expand All @@ -156,8 +156,8 @@ describe('cache-storage', () => {
});

it('should omit svg images if not supported', async () => {
setFeatures({SUPPORT_SVG_DRAWING: false});
const {cache} = createMockContext('http://example.com');
setFeatures({ SUPPORT_SVG_DRAWING: false });
const { cache } = createMockContext('http://example.com');
await cache.addImage('http://example.com/test.svg');
await cache.addImage('http://example.com/test2.svg');

Expand All @@ -167,15 +167,15 @@ describe('cache-storage', () => {

describe('cross-origin', () => {
it('addImage should not add images it cannot load/render', async () => {
const {cache} = createMockContext('http://example.com', {
const { cache } = createMockContext('http://example.com', {
proxy: undefined
});
await cache.addImage('http://html2canvas.hertzen.com/test.jpg');
deepStrictEqual(images.length, 0);
});

it('addImage should add images if tainting enabled', async () => {
const {cache} = createMockContext('http://example.com', {
const { cache } = createMockContext('http://example.com', {
allowTaint: true,
proxy: undefined
});
Expand All @@ -186,17 +186,17 @@ describe('cache-storage', () => {
});

it('addImage should add images if cors enabled', async () => {
const {cache} = createMockContext('http://example.com', {useCORS: true});
const { cache } = createMockContext('http://example.com', { useCORS: true });
await cache.addImage('http://html2canvas.hertzen.com/test.jpg');
deepStrictEqual(images.length, 1);
deepStrictEqual(images[0].src, 'http://html2canvas.hertzen.com/test.jpg');
deepStrictEqual(images[0].crossOrigin, 'anonymous');
});

it('addImage should not add images if cors enabled but not supported', async () => {
setFeatures({SUPPORT_CORS_IMAGES: false});
setFeatures({ SUPPORT_CORS_IMAGES: false });

const {cache} = createMockContext('http://example.com', {
const { cache } = createMockContext('http://example.com', {
useCORS: true,
proxy: undefined
});
Expand All @@ -205,15 +205,15 @@ describe('cache-storage', () => {
});

it('addImage should not add images to proxy if cors enabled', async () => {
const {cache} = createMockContext('http://example.com', {useCORS: true});
const { cache } = createMockContext('http://example.com', { useCORS: true });
await cache.addImage('http://html2canvas.hertzen.com/test.jpg');
deepStrictEqual(images.length, 1);
deepStrictEqual(images[0].src, 'http://html2canvas.hertzen.com/test.jpg');
deepStrictEqual(images[0].crossOrigin, 'anonymous');
});

it('addImage should use proxy ', async () => {
const {cache} = createMockContext('http://example.com');
const { cache } = createMockContext('http://example.com');
await cache.addImage('http://html2canvas.hertzen.com/test.jpg');
deepStrictEqual(xhr.length, 1);
deepStrictEqual(
Expand All @@ -227,7 +227,7 @@ describe('cache-storage', () => {
});

it('proxy should respect imageTimeout', async () => {
const {cache} = createMockContext('http://example.com', {
const { cache } = createMockContext('http://example.com', {
imageTimeout: 10
});
await cache.addImage('http://html2canvas.hertzen.com/test.jpg');
Expand All @@ -249,7 +249,7 @@ describe('cache-storage', () => {
});

it('match should return cache entry', async () => {
const {cache} = createMockContext('http://example.com');
const { cache } = createMockContext('http://example.com');
await cache.addImage('http://example.com/test.jpg');

if (images[0].onload) {
Expand All @@ -262,7 +262,7 @@ describe('cache-storage', () => {
});

it('image should respect imageTimeout', async () => {
const {cache} = createMockContext('http://example.com', {imageTimeout: 10});
const { cache } = createMockContext('http://example.com', { imageTimeout: 10 });
cache.addImage('http://example.com/test.jpg');

try {
Expand Down
6 changes: 3 additions & 3 deletions src/core/__tests__/logger.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Logger} from '../logger';
import { Logger } from '../logger';

describe('logger', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand All @@ -16,14 +16,14 @@ describe('logger', () => {

it('should call console.info when logger enabled', () => {
const id = Math.random().toString();
const logger = new Logger({id, enabled: true});
const logger = new Logger({ id, enabled: true });
logger.info('testing');
expect(infoSpy).toHaveBeenLastCalledWith(id, expect.stringMatching(/\d+ms/), 'testing');
});

it("shouldn't call console.info when logger disabled", () => {
const id = Math.random().toString();
const logger = new Logger({id, enabled: false});
const logger = new Logger({ id, enabled: false });
logger.info('testing');
expect(infoSpy).not.toHaveBeenCalled();
});
Expand Down
6 changes: 3 additions & 3 deletions src/core/cache-storage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {FEATURES} from './features';
import {Context} from './context';
import { FEATURES } from './features';
import { Context } from './context';

export class CacheStorage {
private static _link?: HTMLAnchorElement;
Expand Down Expand Up @@ -35,7 +35,7 @@ export interface ResourceOptions {

export class Cache {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private readonly _cache: {[key: string]: Promise<any>} = {};
private readonly _cache: { [key: string]: Promise<any> } = {};

constructor(private readonly context: Context, private readonly _options: ResourceOptions) {}

Expand Down
8 changes: 4 additions & 4 deletions src/core/context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Logger} from './logger';
import {Cache, ResourceOptions} from './cache-storage';
import {Bounds} from '../css/layout/bounds';
import { Logger } from './logger';
import { Cache, ResourceOptions } from './cache-storage';
import { Bounds } from '../css/layout/bounds';

export type ContextOptions = {
logging: boolean;
Expand All @@ -15,7 +15,7 @@ export class Context {
private static instanceCount = 1;

constructor(options: ContextOptions, public windowBounds: Bounds) {
this.logger = new Logger({id: this.instanceName, enabled: options.logging});
this.logger = new Logger({ id: this.instanceName, enabled: options.logging });
this.cache = options.cache ?? new Cache(this, options);
}
}
18 changes: 9 additions & 9 deletions src/core/features.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {fromCodePoint, toCodePoints} from 'css-line-break';
import { fromCodePoint, toCodePoints } from 'css-line-break';

const testRangeBounds = (document: Document) => {
const TEST_HEIGHT = 123;
Expand Down Expand Up @@ -170,19 +170,19 @@ export const FEATURES = {
get SUPPORT_RANGE_BOUNDS(): boolean {
'use strict';
const value = testRangeBounds(document);
Object.defineProperty(FEATURES, 'SUPPORT_RANGE_BOUNDS', {value});
Object.defineProperty(FEATURES, 'SUPPORT_RANGE_BOUNDS', { value });
return value;
},
get SUPPORT_WORD_BREAKING(): boolean {
'use strict';
const value = FEATURES.SUPPORT_RANGE_BOUNDS && testIOSLineBreak(document);
Object.defineProperty(FEATURES, 'SUPPORT_WORD_BREAKING', {value});
Object.defineProperty(FEATURES, 'SUPPORT_WORD_BREAKING', { value });
return value;
},
get SUPPORT_SVG_DRAWING(): boolean {
'use strict';
const value = testSVG(document);
Object.defineProperty(FEATURES, 'SUPPORT_SVG_DRAWING', {value});
Object.defineProperty(FEATURES, 'SUPPORT_SVG_DRAWING', { value });
return value;
},
get SUPPORT_FOREIGNOBJECT_DRAWING(): Promise<boolean> {
Expand All @@ -191,32 +191,32 @@ export const FEATURES = {
typeof Array.from === 'function' && typeof window.fetch === 'function'
? testForeignObject(document)
: Promise.resolve(false);
Object.defineProperty(FEATURES, 'SUPPORT_FOREIGNOBJECT_DRAWING', {value});
Object.defineProperty(FEATURES, 'SUPPORT_FOREIGNOBJECT_DRAWING', { value });
return value;
},
get SUPPORT_CORS_IMAGES(): boolean {
'use strict';
const value = testCORS();
Object.defineProperty(FEATURES, 'SUPPORT_CORS_IMAGES', {value});
Object.defineProperty(FEATURES, 'SUPPORT_CORS_IMAGES', { value });
return value;
},
get SUPPORT_RESPONSE_TYPE(): boolean {
'use strict';
const value = testResponseType();
Object.defineProperty(FEATURES, 'SUPPORT_RESPONSE_TYPE', {value});
Object.defineProperty(FEATURES, 'SUPPORT_RESPONSE_TYPE', { value });
return value;
},
get SUPPORT_CORS_XHR(): boolean {
'use strict';
const value = 'withCredentials' in new XMLHttpRequest();
Object.defineProperty(FEATURES, 'SUPPORT_CORS_XHR', {value});
Object.defineProperty(FEATURES, 'SUPPORT_CORS_XHR', { value });
return value;
},
get SUPPORT_NATIVE_TEXT_SEGMENTATION(): boolean {
'use strict';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const value = !!(typeof Intl !== 'undefined' && (Intl as any).Segmenter);
Object.defineProperty(FEATURES, 'SUPPORT_NATIVE_TEXT_SEGMENTATION', {value});
Object.defineProperty(FEATURES, 'SUPPORT_NATIVE_TEXT_SEGMENTATION', { value });
return value;
}
};
Loading

0 comments on commit 01a3aae

Please sign in to comment.