mrkeyoor.com_
Sat 08 Aug 22:48 UTC
npmWeb Frontendupdated 08 Aug 2026

cssfontparser

cssfontparser parses a CSS font shorthand string into a plain object with style, variant, weight, numeric pixel size, numeric pixel line height, and an array of font families. It can resolve em and percentage sizes against a parent font string, convert physical units using a supplied DPI, handle inherit, and serialize its result through toString(). The implementation is a dependency-free CommonJS module built around one regular expression and a module-level cache, with a grammar much narrower than a browser's current CSS parser.

Verdict

cssfontparser is compact compatibility code, not a current CSS parser. Keep it for controlled legacy inputs, but choose a maintained CSS grammar tool when correctness, isolation of results, modern syntax, or typed modules matter.

API stability3/5The positional parse(string, parent, dpi) API and result fields have remained unchanged since 2015, and the module has no dependency tree that can alter behavior underneath it. Existing controlled calls are therefore predictable. The score is limited by less obvious contracts: repeated calls return a cached shared object, invalid input alternates between null and undefined paths, relative sizes can yield objects without size, and toString captures parsing-time state rather than acting as a general live serializer.
Docs2/5The README includes installation, one comprehensive example, the three-argument signature, default DPI, and the requirement for a parent when resolving em or percentage sizes. The small test file provides additional evidence for physical-unit conversions, inherit, family splitting, generics, and serialization. Missing are the accepted grammar, return behavior on invalid input, unsupported units and functions, shared cache, mutation risk, toString normalization, CommonJS-only packaging, and limitations compared with browser parsing.
Maintenance1/5The latest npm release, 1.2.1, was published in May 2015. Two commits in December 2022 added the license to the repository and README, but the parser implementation and tests have not changed since the release. The repository is not archived and shows no open issues or pull requests, yet there is also no evidence of syntax updates as CSS font grammar, module packaging, and JavaScript tooling evolved. Quiet issue counts do not substitute for maintained compatibility.
Ecosystem2/5The package recorded 3,268,511 downloads in the measured week and its dependency-free 2.1 KB uncompressed bundle remains attractive to transitive consumers. Direct ecosystem evidence is thin: the GitHub repository has 9 stars and one fork, the README lists no integrations or extensions, and the package has no TypeScript declarations. It interoperates only through a plain object result, with no plugin surface or shared AST used by modern PostCSS and CSS tooling.

Use it if

  • You maintain code already built around cssfontparser's exact object shape and pixel conversion rules
  • Your input is a controlled, old-style font shorthand containing simple ASCII family names
  • You need em or percentage size resolution against one explicitly supplied parent shorthand
  • A dependency-free CommonJS parser around 1.1 KB gzipped is more important than full CSS grammar coverage
Skip it if

Setup reality

Install with npm install cssfontparser and call the CommonJS default function. There are no dependencies, native builds, peer requirements, credentials, or config files. The parser accepts up to three positional arguments: the shorthand, an optional parent shorthand, and DPI, which defaults to 96. The parent is not a computed-style object; it must itself be a string this parser can understand. Without a parent, em and percentage sizes remain undefined even though the function can still return an object with a family. Physical units are converted to pixels with the DPI value, while unitless line-height is multiplied by the parsed font size. The narrow regular expression is the main surprise. Family text is limited largely to ASCII letters, spaces, hyphens, commas, and quotes; CSS functions, variables, escaped identifiers, digits in family names, and much modern syntax can fail. Invalid input usually returns undefined or null, so check the result before reading it. More importantly, results are cached and reused by reference. Treat them as immutable or clone the enumerable fields and family array before passing them to code that mutates data. The non-enumerable toString method closes over values assembled during parsing; changing fields after parsing does not reliably update serialized output. Serialization also normalizes generic family names, quotes names containing spaces, converts sizes to px, and omits normal or numeric 400 weight, so it is not a source-preserving formatter.

Patterns

Parse a basic font shorthandparse-font-shorthand

const parseFont = require('cssfontparser');

const font = parseFont('italic small-caps 700 16px/1.5 Georgia, serif');
// { style: 'italic', variant: 'small-caps', weight: '700',
//   size: 16, lineHeight: 24, family: ['Georgia', 'serif'] }

The result's toString method is non-enumerable, so deep equality and JSON output show only the data fields.

Check parsing before reading fieldsguard-invalid-input

const parsed = parseFont(input);
if (!parsed || typeof parsed.size !== 'number' || !parsed.family) {
  throw new TypeError(`Unsupported font shorthand: ${input}`);
}

Invalid syntax can return null or undefined, while a relative size without a parent can produce an object whose size is undefined.

Resolve em against a parent fontresolve-em-size

const child = parseFont('1.5em sans-serif', '16px serif');
// child.size === 24

The second argument must be another shorthand string. Passing a numeric parent size or computed-style object is unsupported.

Resolve a percentage font sizeresolve-percent-size

const child = parseFont('75% Arial, sans-serif', '20px serif');
// child.size === 15

Without the parent shorthand, the percentage cannot resolve and size is left undefined rather than causing the entire parse to fail.

Convert points using an explicit DPIconvert-physical-units

const printFont = parseFont('12pt serif', null, 192);
// printFont.size === 32

DPI defaults to 96 when omitted. px ignores DPI, while pt, pc, mm, cm, and in use it for conversion.

Resolve unitless line-heightcompute-line-height

const font = parseFont('16px/1.25 Arial, sans-serif');
// font.lineHeight === 20

A unitless line-height is multiplied by the parsed size. Unit-bearing line-height is converted through the same numeric helper as size.

Resolve the inherit shorthandinherit-parent-font

const inherited = parseFont('inherit', 'italic 14px Georgia, serif');
// { style: 'italic', size: 14, family: ['Georgia', 'serif'] }

parseFont('inherit') without a parent returns undefined. The returned object is the cached parent parse result, not a clone.

Serialize the normalized resultserialize-normalized-font

const font = parseFont('italic 400 12px/2 Unknown Font, SANS-SERIF');
font.toString();
// italic 12px/24px "Unknown Font", sans-serif

Serialization normalizes and converts values; it does not reproduce the source. Weight 400 is omitted and multiword families are quoted.

Clone a result before changing itavoid-cache-mutation

const parsed = parseFont('16px Arial, sans-serif');
const editable = {
  ...parsed,
  family: parsed.family.slice(),
};
editable.size = 18;

Repeated parses with the same input, parent, and DPI return the same cached object. Mutating parsed directly can affect later callers.

Recognize the built-in generic family setinspect-generic-families

const { generics } = require('cssfontparser');

if (generics[family.toLowerCase()]) {
  console.log('classic generic family');
}

The exported table contains only serif, sans-serif, cursive, fantasy, and monospace. Newer CSS generic families are absent.

Build output after editing parsed fieldsserialize-edited-copy

const parsed = parseFont('16px Arial, sans-serif');
const size = 18;
const family = parsed.family.map((name) =>
  name.includes(' ') ? `"${name}"` : name
).join(', ');
const css = `${size}px ${family}`;

Do not rely on parsed.toString() after changing parsed.size or other scalar fields because its output was assembled during the original parse.

Read the parsed family fallback orderparse-family-list

const { family } = parseFont('14px "Open Sans", Arial, sans-serif');
for (const candidate of family) {
  console.log(candidate);
}

Quotes are stripped later during serialization, but the parser's family array behavior is regex-driven and not a complete CSS string-token parser. Test unusual family names before relying on it.

Alternatives

PackageRegistryPick it when
css-font-parsernpmYou specifically need a newer package dedicated to parsing the CSS font value
postcss-value-parsernpmYou need a maintained low-level value AST and will interpret font shorthand grammar yourself
css-treenpmYou need spec-aware CSS parsing, validation, walking, and generation beyond one shorthand