html2canvas/src/css/types/image.ts

118 lines
3.6 KiB
TypeScript
Raw Normal View History

Typescript conversion (#1828) * initial typescript conversion * test: update overflow+transform ref test * fix: correctly render pseudo element content * fix: testrunner build * fix: karma test urls * test: update underline tests with <u> elements * test: update to es6-promise polyfill * test: remove watch from server * test: remove flow * format: update prettier for typescript * test: update eslint to use typescript parser * test: update linear gradient reftest * test: update test runner * test: update testrunner promise polyfill * fix: handle display: -webkit-flex correctly (fix #1817) * fix: correctly render gradients with clip & repeat (fix #1773) * fix: webkit-gradient function support * fix: implement radial gradients * fix: text-decoration rendering * fix: missing scroll positions for elements * ci: fix ios 11 tests * fix: ie logging * ci: improve device availability logging * fix: lint errors * ci: update to ios 12 * fix: check for console availability * ci: fix build dependency * test: update text reftests * fix: window reference for unit tests * feat: add hsl/hsla color support * fix: render options * fix: CSSKeyframesRule cssText Permission Denied on Internet Explorer 11 (#1830) * fix: option lint * fix: list type rendering * test: fix platform import * fix: ie css parsing for numbers * ci: add minified build * fix: form element rendering * fix: iframe rendering * fix: re-introduce experimental foreignobject renderer * fix: text-shadow rendering * feat: improve logging * fix: unit test logging * fix: cleanup resources * test: update overflow scrolling to work with ie * build: update build to include typings * fix: do not parse select element children * test: fix onclone test to work with older IEs * test: reduce reftest canvas sizes * test: remove dynamic setUp from list tests * test: update linear-gradient tests * build: remove old source files * build: update docs dependencies * build: fix typescript definition path * ci: include test.js on docs website
2019-05-26 01:54:41 +03:00
import {CSSValue} from '../syntax/parser';
import {TokenType} from '../syntax/tokenizer';
import {Color} from './color';
import {linearGradient} from './functions/linear-gradient';
import {prefixLinearGradient} from './functions/-prefix-linear-gradient';
import {ITypeDescriptor} from '../ITypeDescriptor';
import {CacheStorage} from '../../core/cache-storage';
import {LengthPercentage} from './length-percentage';
import {webkitGradient} from './functions/-webkit-gradient';
import {radialGradient} from './functions/radial-gradient';
import {prefixRadialGradient} from './functions/-prefix-radial-gradient';
export enum CSSImageType {
URL,
LINEAR_GRADIENT,
RADIAL_GRADIENT
}
export const isLinearGradient = (background: ICSSImage): background is CSSLinearGradientImage => {
return background.type === CSSImageType.LINEAR_GRADIENT;
};
export const isRadialGradient = (background: ICSSImage): background is CSSRadialGradientImage => {
return background.type === CSSImageType.RADIAL_GRADIENT;
};
export interface UnprocessedGradientColorStop {
color: Color;
stop: LengthPercentage | null;
}
export interface GradientColorStop {
color: Color;
stop: number;
}
export interface ICSSImage {
type: CSSImageType;
}
export interface CSSURLImage extends ICSSImage {
url: string;
type: CSSImageType.URL;
}
// interface ICSSGeneratedImage extends ICSSImage {}
export type GradientCorner = [LengthPercentage, LengthPercentage];
interface ICSSGradientImage extends ICSSImage {
stops: UnprocessedGradientColorStop[];
}
export interface CSSLinearGradientImage extends ICSSGradientImage {
angle: number | GradientCorner;
type: CSSImageType.LINEAR_GRADIENT;
}
export enum CSSRadialShape {
CIRCLE,
ELLIPSE
}
export enum CSSRadialExtent {
CLOSEST_SIDE,
FARTHEST_SIDE,
CLOSEST_CORNER,
FARTHEST_CORNER
}
export type CSSRadialSize = CSSRadialExtent | LengthPercentage[];
export interface CSSRadialGradientImage extends ICSSGradientImage {
type: CSSImageType.RADIAL_GRADIENT;
shape: CSSRadialShape;
size: CSSRadialSize;
position: LengthPercentage[];
}
export const image: ITypeDescriptor<ICSSImage> = {
name: 'image',
parse: (value: CSSValue): ICSSImage => {
if (value.type === TokenType.URL_TOKEN) {
const image: CSSURLImage = {url: value.value, type: CSSImageType.URL};
CacheStorage.getInstance().addImage(value.value);
return image;
}
if (value.type === TokenType.FUNCTION) {
const imageFunction = SUPPORTED_IMAGE_FUNCTIONS[value.name];
if (typeof imageFunction === 'undefined') {
throw new Error(`Attempting to parse an unsupported image function "${value.name}"`);
}
return imageFunction(value.values);
}
throw new Error(`Unsupported image type`);
}
};
export function isSupportedImage(value: CSSValue): boolean {
return value.type !== TokenType.FUNCTION || !!SUPPORTED_IMAGE_FUNCTIONS[value.name];
}
const SUPPORTED_IMAGE_FUNCTIONS: Record<string, (args: CSSValue[]) => ICSSImage> = {
Typescript conversion (#1828) * initial typescript conversion * test: update overflow+transform ref test * fix: correctly render pseudo element content * fix: testrunner build * fix: karma test urls * test: update underline tests with <u> elements * test: update to es6-promise polyfill * test: remove watch from server * test: remove flow * format: update prettier for typescript * test: update eslint to use typescript parser * test: update linear gradient reftest * test: update test runner * test: update testrunner promise polyfill * fix: handle display: -webkit-flex correctly (fix #1817) * fix: correctly render gradients with clip & repeat (fix #1773) * fix: webkit-gradient function support * fix: implement radial gradients * fix: text-decoration rendering * fix: missing scroll positions for elements * ci: fix ios 11 tests * fix: ie logging * ci: improve device availability logging * fix: lint errors * ci: update to ios 12 * fix: check for console availability * ci: fix build dependency * test: update text reftests * fix: window reference for unit tests * feat: add hsl/hsla color support * fix: render options * fix: CSSKeyframesRule cssText Permission Denied on Internet Explorer 11 (#1830) * fix: option lint * fix: list type rendering * test: fix platform import * fix: ie css parsing for numbers * ci: add minified build * fix: form element rendering * fix: iframe rendering * fix: re-introduce experimental foreignobject renderer * fix: text-shadow rendering * feat: improve logging * fix: unit test logging * fix: cleanup resources * test: update overflow scrolling to work with ie * build: update build to include typings * fix: do not parse select element children * test: fix onclone test to work with older IEs * test: reduce reftest canvas sizes * test: remove dynamic setUp from list tests * test: update linear-gradient tests * build: remove old source files * build: update docs dependencies * build: fix typescript definition path * ci: include test.js on docs website
2019-05-26 01:54:41 +03:00
'linear-gradient': linearGradient,
'-moz-linear-gradient': prefixLinearGradient,
'-ms-linear-gradient': prefixLinearGradient,
'-o-linear-gradient': prefixLinearGradient,
'-webkit-linear-gradient': prefixLinearGradient,
'radial-gradient': radialGradient,
'-moz-radial-gradient': prefixRadialGradient,
'-ms-radial-gradient': prefixRadialGradient,
'-o-radial-gradient': prefixRadialGradient,
'-webkit-radial-gradient': prefixRadialGradient,
'-webkit-gradient': webkitGradient
};