Skip to content

Commit 49d9291

Browse files
test(css): cover the family-longhand collector's declines
1 parent be32b09 commit 49d9291

2 files changed

Lines changed: 183 additions & 8 deletions

File tree

‎test/CssValueSyntax.unittest.js‎

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ const {
88
checkStatedClassSpellings,
99
collectAlphaValueProperties,
1010
collectData,
11+
collectFamilyLonghands,
1112
collectGradientFunctions,
1213
collectMergeableAtRules,
1314
collectRatioProperties,
1415
isSpelledSyntax,
16+
longhandType,
1517
parseValueSyntax,
1618
shorthandSlots,
1719
slotSpellings,
@@ -483,6 +485,168 @@ describe("CssValueSyntax", () => {
483485
});
484486
});
485487

488+
describe("longhandType", () => {
489+
it("reads the one type a longhand's whole value is", () => {
490+
expect(longhandType("border-top-color", 0)).toBe("color");
491+
});
492+
493+
it("follows the `<'other'>` a longhand is stated as", () => {
494+
expect(
495+
longhandType("a", 0, {
496+
a: { syntax: "<'b'>" },
497+
b: { syntax: "<color>" }
498+
})
499+
).toBe("color");
500+
});
501+
502+
it("reads none for a name the table does not hold", () => {
503+
expect(longhandType("gone", 0, {})).toBeNull();
504+
});
505+
506+
it("reads none where the entry states no syntax", () => {
507+
expect(longhandType("a", 0, { a: {} })).toBeNull();
508+
});
509+
510+
it("reads none where the chain of references has no end", () => {
511+
expect(longhandType("a", 0, { a: { syntax: "<'a'>" } })).toBeNull();
512+
});
513+
514+
it("reads none where the value is more than one type", () => {
515+
expect(
516+
longhandType("a", 0, { a: { syntax: "red | <color>" } })
517+
).toBeNull();
518+
});
519+
});
520+
521+
describe("collectFamilyLonghands", () => {
522+
it("names each shorthand's slots in grammar order", () => {
523+
expect(collectFamilyLonghands()).toEqual(
524+
expect.arrayContaining([
525+
[
526+
"border-top",
527+
["border-top-width", "border-top-style", "border-top-color"]
528+
],
529+
["outline", ["outline-width", "outline-style", "outline-color"]]
530+
])
531+
);
532+
});
533+
534+
it("names them in one order whatever order the table states them in", () => {
535+
expect(
536+
collectFamilyLonghands(
537+
{
538+
b: {
539+
syntax: "<'b-one'> || <'b-two'>",
540+
computed: ["b-one", "b-two"]
541+
},
542+
a: {
543+
syntax: "<'a-one'> || <'a-two'>",
544+
computed: ["a-one", "a-two"]
545+
}
546+
},
547+
["a", "b"]
548+
)
549+
).toEqual([
550+
["a", ["a-one", "a-two"]],
551+
["b", ["b-one", "b-two"]]
552+
]);
553+
});
554+
555+
it("names none where a slot is a keyword rather than a longhand", () => {
556+
expect(
557+
collectFamilyLonghands(
558+
{ a: { syntax: "none || <length>", computed: ["a-one", "a-two"] } },
559+
["a"]
560+
)
561+
).toEqual([]);
562+
});
563+
564+
it("names none where a type names more than one of the longhands", () => {
565+
expect(
566+
collectFamilyLonghands(
567+
{
568+
a: { syntax: "<color> || <length>", computed: ["a-one", "a-two"] },
569+
"a-one": { syntax: "<color>" },
570+
"a-two": { syntax: "<color>" }
571+
},
572+
["a"]
573+
)
574+
).toEqual([]);
575+
});
576+
577+
it("names none where the grammar reaches fewer slots than longhands", () => {
578+
expect(
579+
collectFamilyLonghands(
580+
{
581+
a: {
582+
syntax: "<'a-one'> || <'a-two'>",
583+
computed: ["a-one", "a-two", "a-three"]
584+
}
585+
},
586+
["a"]
587+
)
588+
).toEqual([]);
589+
});
590+
591+
it("names none where the grammar is not an order-free alternation", () => {
592+
expect(
593+
collectFamilyLonghands(
594+
{
595+
a: { syntax: "<'a-one'> <'a-two'>", computed: ["a-one", "a-two"] }
596+
},
597+
["a"]
598+
)
599+
).toEqual([]);
600+
});
601+
602+
it("names none where the grammar cannot be parsed", () => {
603+
expect(
604+
collectFamilyLonghands(
605+
{ a: { syntax: "<", computed: ["a-one", "a-two"] } },
606+
["a"]
607+
)
608+
).toEqual([]);
609+
});
610+
611+
it("names none where the entry states no syntax or no longhands", () => {
612+
expect(
613+
collectFamilyLonghands(
614+
{
615+
a: { computed: ["a-one", "a-two"] },
616+
b: { syntax: "<'b-one'> || <'b-two'>" },
617+
c: { syntax: "<'c-one'> || <'c-two'>", computed: ["c-one"] }
618+
},
619+
["a", "b", "c"]
620+
)
621+
).toEqual([]);
622+
});
623+
624+
it("names none for a shorthand no verified list holds", () => {
625+
expect(
626+
collectFamilyLonghands(
627+
{
628+
a: {
629+
syntax: "<'a-one'> || <'a-two'>",
630+
computed: ["a-one", "a-two"]
631+
}
632+
},
633+
[]
634+
)
635+
).toEqual([]);
636+
});
637+
638+
it("names none where a slot is not one of the longhands", () => {
639+
expect(
640+
collectFamilyLonghands(
641+
{
642+
a: { syntax: "<'x'> || <'a-two'>", computed: ["a-one", "a-two"] }
643+
},
644+
["a"]
645+
)
646+
).toEqual([]);
647+
});
648+
});
649+
486650
describe("the guards on what those datasets still publish", () => {
487651
it("accepts a `syntaxes.json` that states no class the SUPPLEMENT does", () => {
488652
expect(() => checkStatedClassSpellings({})).not.toThrow();

‎tooling/generate-css-data.js‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@ const colorName = require("color-name");
3030
/** @typedef {{ version: string }} PackageManifest */
3131
/** @typedef {{ [name: string]: { syntax: string } }} SyntaxTable */
3232
/** @typedef {{ [name: string]: { syntax?: string } }} PartialSyntaxTable */
33+
/** @typedef {{ [name: string]: { syntax?: string, status?: string, computed?: string | string[] } }} PartialPropertyTable */
3334
/** @type {PackageManifest} */
3435
const colorNamePackage = require("color-name/package.json");
3536
/** @type {PartialSyntaxTable} */
3637
const atRules = require("mdn-data/css/at-rules.json");
3738
/** @type {SyntaxTable} */
3839
const functions = require("mdn-data/css/functions.json");
39-
/** @type {{ [name: string]: { syntax?: string, status?: string, computed?: string | string[] } }} */
40+
/** @type {PartialPropertyTable} */
4041
const properties = require("mdn-data/css/properties.json");
4142
const selectors = require("mdn-data/css/selectors.json");
4243
/** @type {SyntaxTable} */
@@ -739,16 +740,19 @@ const lowerSorted = (keywords) =>
739740
* some longhands are stated as.
740741
* @param {string} name the property name
741742
* @param {number} depth the references followed so far
743+
* @param {PartialPropertyTable} propertyTable the `properties.json` to read
742744
* @returns {string | null} the type's name, or `null` when it is not one type
743745
*/
744-
const longhandType = (name, depth) => {
745-
const entry = properties[name];
746+
const longhandType = (name, depth, propertyTable = properties) => {
747+
const entry = propertyTable[name];
746748
if (entry === undefined || typeof entry.syntax !== "string" || depth > 4) {
747749
return null;
748750
}
749751
const syntax = entry.syntax.trim();
750752
const referenced = /^<'([a-z-]+)'>$/.exec(syntax);
751-
if (referenced !== null) return longhandType(referenced[1], depth + 1);
753+
if (referenced !== null) {
754+
return longhandType(referenced[1], depth + 1, propertyTable);
755+
}
752756
const type = /^<([a-z-]+)>$/.exec(syntax);
753757
return type === null ? null : type[1];
754758
};
@@ -759,13 +763,18 @@ const longhandType = (name, depth) => {
759763
* value in grammar order, which `||` accepts in any order, so the only question
760764
* is whether each value parses back into the longhand it was authored on — the
761765
* per-slot tables below are what answers it.
766+
* @param {PartialPropertyTable} propertyTable the `properties.json` to read
767+
* @param {string[]} verifiedShorthands the shorthands a merge may emit
762768
* @returns {[string, string[]][]} `[shorthand, longhands]` in grammar order
763769
*/
764-
const collectFamilyLonghands = () => {
765-
const verified = new Set(SUPPLEMENT.familyShorthands);
770+
const collectFamilyLonghands = (
771+
propertyTable = properties,
772+
verifiedShorthands = SUPPLEMENT.familyShorthands
773+
) => {
774+
const verified = new Set(verifiedShorthands);
766775
/** @type {[string, string[]][]} */
767776
const out = [];
768-
for (const [name, property] of Object.entries(properties)) {
777+
for (const [name, property] of Object.entries(propertyTable)) {
769778
if (!verified.has(name)) continue;
770779
if (typeof property.syntax !== "string") continue;
771780
const longhands = property.computed;
@@ -784,7 +793,7 @@ const collectFamilyLonghands = () => {
784793
// is the longhand whose own value is exactly that type, followed
785794
// through the `<'other'>` some of them are stated as.
786795
const named = longhands.filter(
787-
(one) => longhandType(one, 0) === item.name
796+
(one) => longhandType(one, 0, propertyTable) === item.name
788797
);
789798
return named.length === 1 ? named[0] : null;
790799
});
@@ -4001,10 +4010,12 @@ module.exports.assertClassesArePrintable = assertClassesArePrintable;
40014010
module.exports.checkStatedClassSpellings = checkStatedClassSpellings;
40024011
module.exports.collectAlphaValueProperties = collectAlphaValueProperties;
40034012
module.exports.collectData = collectData;
4013+
module.exports.collectFamilyLonghands = collectFamilyLonghands;
40044014
module.exports.collectGradientFunctions = collectGradientFunctions;
40054015
module.exports.collectMergeableAtRules = collectMergeableAtRules;
40064016
module.exports.collectRatioProperties = collectRatioProperties;
40074017
module.exports.isSpelledSyntax = isSpelledSyntax;
4018+
module.exports.longhandType = longhandType;
40084019
module.exports.parseValueSyntax = parseValueSyntax;
40094020
module.exports.shorthandSlots = shorthandSlots;
40104021
module.exports.slotSpellings = slotSpellings;

0 commit comments

Comments
 (0)