Skip to content

Commit d274842

Browse files
committed
Update shorthand parser and add tests
1 parent 3f32551 commit d274842

File tree

2 files changed

+97
-6
lines changed

2 files changed

+97
-6
lines changed

lib/parsers.js

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -469,15 +469,50 @@ exports.parseShorthand = function parseShorthand(val, shorthandFor, preserve = f
469469
}
470470
const parts = splitValue(val);
471471
const shorthandArr = [...shorthandFor];
472-
for (const part of parts) {
472+
for (let i = 0; i < parts.length; i++) {
473+
const part = parts[i];
473474
let partValid = false;
474-
for (let i = 0; i < shorthandArr.length; i++) {
475-
const [property, value] = shorthandArr[i];
476-
if (value.isValid(part)) {
475+
for (let j = 0; j < shorthandArr.length; j++) {
476+
const [property, value] = shorthandArr[j];
477+
if (property === "font-family") {
478+
// font-family must be the last part.
479+
if (i === parts.length - 1) {
480+
if (value.isValid(part)) {
481+
const prevPart = parts[i - 1];
482+
// Check if prevPart is valid font-size or line-height.
483+
if (
484+
exports.parseMeasurement(prevPart) ||
485+
exports.parseNumber(prevPart) ||
486+
exports.parseKeyword(prevPart, [
487+
"xx-small",
488+
"x-small",
489+
"small",
490+
"medium",
491+
"large",
492+
"x-large",
493+
"xx-large",
494+
"xxx-large",
495+
"smaller",
496+
"larger",
497+
"normal"
498+
])
499+
) {
500+
partValid = true;
501+
obj[property] = value.parse(part);
502+
if (!preserve) {
503+
shorthandArr.splice(j, 1);
504+
break;
505+
}
506+
}
507+
}
508+
} else {
509+
continue;
510+
}
511+
} else if (value.isValid(part)) {
477512
partValid = true;
478513
obj[property] = value.parse(part);
479514
if (!preserve) {
480-
shorthandArr.splice(i, 1);
515+
shorthandArr.splice(j, 1);
481516
break;
482517
}
483518
}

test/parsers.test.js

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,63 @@ describe("parseShorthand", () => {
983983
]);
984984

985985
it("should return undefined for invalid font-family", () => {
986-
const input = "medium Ahem!, sans-serif";
986+
const input = "normal medium Goudy Bookletter 1911, sans-serif";
987+
const output = parsers.parseShorthand(input, shorthandForFont, true);
988+
989+
assert.deepEqual(output, undefined);
990+
});
991+
992+
it("should return undefined for invalid font-family", () => {
993+
const input = "normal medium Red/Black, sans-serif";
994+
const output = parsers.parseShorthand(input, shorthandForFont, true);
995+
996+
assert.deepEqual(output, undefined);
997+
});
998+
999+
it("should return undefined for invalid font-family", () => {
1000+
const input = "normal medium Red/Black, sans-serif";
1001+
const output = parsers.parseShorthand(input, shorthandForFont, true);
1002+
1003+
assert.deepEqual(output, undefined);
1004+
});
1005+
1006+
it("should return undefined for invalid font-family", () => {
1007+
const input = 'normal medium "Lucida" Grande, sans-serif';
1008+
const output = parsers.parseShorthand(input, shorthandForFont, true);
1009+
1010+
assert.deepEqual(output, undefined);
1011+
});
1012+
1013+
it("should return undefined for invalid font-family", () => {
1014+
const input = 'normal medium Lucida "Grande", sans-serif';
1015+
const output = parsers.parseShorthand(input, shorthandForFont, true);
1016+
1017+
assert.deepEqual(output, undefined);
1018+
});
1019+
1020+
it("should return undefined for invalid font-family", () => {
1021+
const input = "normal medium Ahem!, sans-serif";
1022+
const output = parsers.parseShorthand(input, shorthandForFont, true);
1023+
1024+
assert.deepEqual(output, undefined);
1025+
});
1026+
1027+
it("should return undefined for invalid font-family", () => {
1028+
const input = "normal medium test@foo, sans-serif";
1029+
const output = parsers.parseShorthand(input, shorthandForFont, true);
1030+
1031+
assert.deepEqual(output, undefined);
1032+
});
1033+
1034+
it("should return undefined for invalid font-family", () => {
1035+
const input = "normal medium #POUND, sans-serif";
1036+
const output = parsers.parseShorthand(input, shorthandForFont, true);
1037+
1038+
assert.deepEqual(output, undefined);
1039+
});
1040+
1041+
it("should return undefined for invalid font-family", () => {
1042+
const input = "normal medium Hawaii 5-0, sans-serif";
9871043
const output = parsers.parseShorthand(input, shorthandForFont, true);
9881044

9891045
assert.deepEqual(output, undefined);

0 commit comments

Comments
 (0)