fix: concatenate contiguous font-family tokens (#2219)

This commit is contained in:
James Addison 2020-08-09 05:41:53 +01:00 committed by GitHub
parent d7d17adf70
commit bacfadff96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 5 deletions

View 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'",
]));
});
});

View File

@ -1,6 +1,6 @@
import {IPropertyListDescriptor, PropertyDescriptorParsingType} from '../IPropertyDescriptor';
import {CSSValue} from '../syntax/parser';
import {StringValueToken, TokenType} from '../syntax/tokenizer';
import {TokenType} from '../syntax/tokenizer';
export type FONT_FAMILY = string;
@ -12,9 +12,26 @@ export const fontFamily: IPropertyListDescriptor<FontFamily> = {
prefix: false,
type: PropertyDescriptorParsingType.LIST,
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;