mirror of
https://github.com/niklasvh/html2canvas.git
synced 2023-08-10 21:13:10 +03:00
fix: concatenate contiguous font-family tokens (#2219)
This commit is contained in:
parent
d7d17adf70
commit
bacfadff96
31
src/css/property-descriptors/__tests__/font-family.ts
Normal file
31
src/css/property-descriptors/__tests__/font-family.ts
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
import {deepEqual} from 'assert';
|
||||||
|
import {Parser} from '../../syntax/parser';
|
||||||
|
import {fontFamily} from '../font-family';
|
||||||
|
|
||||||
|
const fontFamilyParse = (value: string) => fontFamily.parse(Parser.parseValues(value));
|
||||||
|
|
||||||
|
describe('property-descriptors', () => {
|
||||||
|
describe('font-family', () => {
|
||||||
|
it('sans-serif', () =>
|
||||||
|
deepEqual(fontFamilyParse('sans-serif'), [
|
||||||
|
"sans-serif",
|
||||||
|
]));
|
||||||
|
|
||||||
|
it('great fonts 40 library', () =>
|
||||||
|
deepEqual(fontFamilyParse('great fonts 40 library'), [
|
||||||
|
"'great fonts 40 library'",
|
||||||
|
]));
|
||||||
|
|
||||||
|
it('preferred font, "quoted fallback font", font', () =>
|
||||||
|
deepEqual(fontFamilyParse('preferred font, "quoted fallback font", font'), [
|
||||||
|
"'preferred font'",
|
||||||
|
"'quoted fallback font'",
|
||||||
|
"font"
|
||||||
|
]));
|
||||||
|
|
||||||
|
it("'escaping test\\'s font'", () =>
|
||||||
|
deepEqual(fontFamilyParse("'escaping test\\'s font'"), [
|
||||||
|
"'escaping test\'s font'",
|
||||||
|
]));
|
||||||
|
});
|
||||||
|
});
|
@ -1,6 +1,6 @@
|
|||||||
import {IPropertyListDescriptor, PropertyDescriptorParsingType} from '../IPropertyDescriptor';
|
import {IPropertyListDescriptor, PropertyDescriptorParsingType} from '../IPropertyDescriptor';
|
||||||
import {CSSValue} from '../syntax/parser';
|
import {CSSValue} from '../syntax/parser';
|
||||||
import {StringValueToken, TokenType} from '../syntax/tokenizer';
|
import {TokenType} from '../syntax/tokenizer';
|
||||||
|
|
||||||
export type FONT_FAMILY = string;
|
export type FONT_FAMILY = string;
|
||||||
|
|
||||||
@ -12,9 +12,26 @@ export const fontFamily: IPropertyListDescriptor<FontFamily> = {
|
|||||||
prefix: false,
|
prefix: false,
|
||||||
type: PropertyDescriptorParsingType.LIST,
|
type: PropertyDescriptorParsingType.LIST,
|
||||||
parse: (tokens: CSSValue[]) => {
|
parse: (tokens: CSSValue[]) => {
|
||||||
return tokens.filter(isStringToken).map(token => token.value);
|
const accumulator: string[] = [];
|
||||||
|
const results: string[] = [];
|
||||||
|
tokens.forEach(token => {
|
||||||
|
switch (token.type) {
|
||||||
|
case TokenType.IDENT_TOKEN:
|
||||||
|
case TokenType.STRING_TOKEN:
|
||||||
|
accumulator.push(token.value);
|
||||||
|
break;
|
||||||
|
case TokenType.NUMBER_TOKEN:
|
||||||
|
accumulator.push(token.number.toString());
|
||||||
|
break;
|
||||||
|
case TokenType.COMMA_TOKEN:
|
||||||
|
results.push(accumulator.join(' '));
|
||||||
|
accumulator.length = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (accumulator.length) {
|
||||||
|
results.push(accumulator.join(' '));
|
||||||
|
}
|
||||||
|
return results.map(result => result.indexOf(' ') === -1 ? result : `'${result}'`);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const isStringToken = (token: CSSValue): token is StringValueToken =>
|
|
||||||
token.type === TokenType.STRING_TOKEN || token.type === TokenType.IDENT_TOKEN;
|
|
||||||
|
Loading…
Reference in New Issue
Block a user