mirror of
https://github.com/niklasvh/html2canvas.git
synced 2023-08-10 21:13:10 +03:00
feat: ignore unsupported image functions (#1873)
This commit is contained in:
parent
86a650bfd5
commit
61f4819e02
@ -1,7 +1,7 @@
|
|||||||
import {TokenType} from '../syntax/tokenizer';
|
import {TokenType} from '../syntax/tokenizer';
|
||||||
import {ICSSImage, image} from '../types/image';
|
import {ICSSImage, image, isSupportedImage} from '../types/image';
|
||||||
import {IPropertyListDescriptor, PropertyDescriptorParsingType} from '../IPropertyDescriptor';
|
import {IPropertyListDescriptor, PropertyDescriptorParsingType} from '../IPropertyDescriptor';
|
||||||
import {CSSValue, nonFunctionArgSeperator} from '../syntax/parser';
|
import {CSSValue, nonFunctionArgSeparator} from '../syntax/parser';
|
||||||
|
|
||||||
export const backgroundImage: IPropertyListDescriptor<ICSSImage[]> = {
|
export const backgroundImage: IPropertyListDescriptor<ICSSImage[]> = {
|
||||||
name: 'background-image',
|
name: 'background-image',
|
||||||
@ -19,6 +19,6 @@ export const backgroundImage: IPropertyListDescriptor<ICSSImage[]> = {
|
|||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
return tokens.filter(nonFunctionArgSeperator).map(image.parse);
|
return tokens.filter(value => nonFunctionArgSeparator(value) && isSupportedImage(value)).map(image.parse);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -149,7 +149,7 @@ export const isIdentWithValue = (token: CSSValue, value: string): boolean =>
|
|||||||
isIdentToken(token) && token.value === value;
|
isIdentToken(token) && token.value === value;
|
||||||
|
|
||||||
export const nonWhiteSpace = (token: CSSValue) => token.type !== TokenType.WHITESPACE_TOKEN;
|
export const nonWhiteSpace = (token: CSSValue) => token.type !== TokenType.WHITESPACE_TOKEN;
|
||||||
export const nonFunctionArgSeperator = (token: CSSValue) =>
|
export const nonFunctionArgSeparator = (token: CSSValue) =>
|
||||||
token.type !== TokenType.WHITESPACE_TOKEN && token.type !== TokenType.COMMA_TOKEN;
|
token.type !== TokenType.WHITESPACE_TOKEN && token.type !== TokenType.COMMA_TOKEN;
|
||||||
|
|
||||||
export const parseFunctionArgs = (tokens: CSSValue[]): CSSValue[][] => {
|
export const parseFunctionArgs = (tokens: CSSValue[]): CSSValue[][] => {
|
||||||
|
@ -696,7 +696,7 @@ export class Tokenizer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
i++;
|
i++;
|
||||||
} while (true);
|
} while (true);
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import {CSSValue, nonFunctionArgSeperator} from '../syntax/parser';
|
import {CSSValue, nonFunctionArgSeparator} from '../syntax/parser';
|
||||||
import {TokenType} from '../syntax/tokenizer';
|
import {TokenType} from '../syntax/tokenizer';
|
||||||
import {ITypeDescriptor} from '../ITypeDescriptor';
|
import {ITypeDescriptor} from '../ITypeDescriptor';
|
||||||
import {angle, deg} from './angle';
|
import {angle, deg} from './angle';
|
||||||
@ -86,7 +86,7 @@ const getTokenColorValue = (token: CSSValue, i: number): number => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const rgb = (args: CSSValue[]): number => {
|
const rgb = (args: CSSValue[]): number => {
|
||||||
const tokens = args.filter(nonFunctionArgSeperator);
|
const tokens = args.filter(nonFunctionArgSeparator);
|
||||||
|
|
||||||
if (tokens.length === 3) {
|
if (tokens.length === 3) {
|
||||||
const [r, g, b] = tokens.map(getTokenColorValue);
|
const [r, g, b] = tokens.map(getTokenColorValue);
|
||||||
@ -121,7 +121,7 @@ function hue2rgb(t1: number, t2: number, hue: number): number {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const hsl = (args: CSSValue[]): number => {
|
const hsl = (args: CSSValue[]): number => {
|
||||||
const tokens = args.filter(nonFunctionArgSeperator);
|
const tokens = args.filter(nonFunctionArgSeparator);
|
||||||
const [hue, saturation, lightness, alpha] = tokens;
|
const [hue, saturation, lightness, alpha] = tokens;
|
||||||
|
|
||||||
const h = (hue.type === TokenType.NUMBER_TOKEN ? deg(hue.number) : angle.parse(hue)) / (Math.PI * 2);
|
const h = (hue.type === TokenType.NUMBER_TOKEN ? deg(hue.number) : angle.parse(hue)) / (Math.PI * 2);
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import {CSSValue, isIdentToken, isNumberToken, nonFunctionArgSeperator, parseFunctionArgs} from '../../syntax/parser';
|
import {CSSValue, isIdentToken, isNumberToken, nonFunctionArgSeparator, parseFunctionArgs} from '../../syntax/parser';
|
||||||
import {
|
import {
|
||||||
CSSImageType,
|
CSSImageType,
|
||||||
CSSLinearGradientImage,
|
CSSLinearGradientImage,
|
||||||
@ -40,7 +40,7 @@ export const webkitGradient = (tokens: CSSValue[]): CSSLinearGradientImage | CSS
|
|||||||
const color = colorType.parse(firstToken.values[0]);
|
const color = colorType.parse(firstToken.values[0]);
|
||||||
stops.push({stop: HUNDRED_PERCENT, color});
|
stops.push({stop: HUNDRED_PERCENT, color});
|
||||||
} else if (firstToken.name === 'color-stop') {
|
} else if (firstToken.name === 'color-stop') {
|
||||||
const values = firstToken.values.filter(nonFunctionArgSeperator);
|
const values = firstToken.values.filter(nonFunctionArgSeparator);
|
||||||
if (values.length === 2) {
|
if (values.length === 2) {
|
||||||
const color = colorType.parse(values[1]);
|
const color = colorType.parse(values[1]);
|
||||||
const stop = values[0];
|
const stop = values[0];
|
||||||
|
@ -98,9 +98,11 @@ export const image: ITypeDescriptor<ICSSImage> = {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const SUPPORTED_IMAGE_FUNCTIONS: {
|
export function isSupportedImage(value: CSSValue) {
|
||||||
[key: string]: (args: CSSValue[]) => ICSSImage;
|
return value.type !== TokenType.FUNCTION || SUPPORTED_IMAGE_FUNCTIONS[value.name];
|
||||||
} = {
|
}
|
||||||
|
|
||||||
|
const SUPPORTED_IMAGE_FUNCTIONS: Record<string, (args: CSSValue[]) => ICSSImage> = {
|
||||||
'linear-gradient': linearGradient,
|
'linear-gradient': linearGradient,
|
||||||
'-moz-linear-gradient': prefixLinearGradient,
|
'-moz-linear-gradient': prefixLinearGradient,
|
||||||
'-ms-linear-gradient': prefixLinearGradient,
|
'-ms-linear-gradient': prefixLinearGradient,
|
||||||
|
@ -12,7 +12,7 @@ import {
|
|||||||
isTextNode
|
isTextNode
|
||||||
} from './node-parser';
|
} from './node-parser';
|
||||||
import {Logger} from '../core/logger';
|
import {Logger} from '../core/logger';
|
||||||
import {isIdentToken, nonFunctionArgSeperator} from '../css/syntax/parser';
|
import {isIdentToken, nonFunctionArgSeparator} from '../css/syntax/parser';
|
||||||
import {TokenType} from '../css/syntax/tokenizer';
|
import {TokenType} from '../css/syntax/tokenizer';
|
||||||
import {CounterState, createCounterText} from '../css/types/functions/counter';
|
import {CounterState, createCounterText} from '../css/types/functions/counter';
|
||||||
import {LIST_STYLE_TYPE, listStyleType} from '../css/property-descriptors/list-style-type';
|
import {LIST_STYLE_TYPE, listStyleType} from '../css/property-descriptors/list-style-type';
|
||||||
@ -355,7 +355,7 @@ export class DocumentCloner {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
} else if (token.name === 'counter') {
|
} else if (token.name === 'counter') {
|
||||||
const [counter, counterStyle] = token.values.filter(nonFunctionArgSeperator);
|
const [counter, counterStyle] = token.values.filter(nonFunctionArgSeparator);
|
||||||
if (counter && isIdentToken(counter)) {
|
if (counter && isIdentToken(counter)) {
|
||||||
const counterState = this.counters.getCounterValue(counter.value);
|
const counterState = this.counters.getCounterValue(counter.value);
|
||||||
const counterType =
|
const counterType =
|
||||||
@ -368,7 +368,7 @@ export class DocumentCloner {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
} else if (token.name === 'counters') {
|
} else if (token.name === 'counters') {
|
||||||
const [counter, delim, counterStyle] = token.values.filter(nonFunctionArgSeperator);
|
const [counter, delim, counterStyle] = token.values.filter(nonFunctionArgSeparator);
|
||||||
if (counter && isIdentToken(counter)) {
|
if (counter && isIdentToken(counter)) {
|
||||||
const counterStates = this.counters.getCounterValues(counter.value);
|
const counterStates = this.counters.getCounterValues(counter.value);
|
||||||
const counterType =
|
const counterType =
|
||||||
|
Loading…
Reference in New Issue
Block a user