MCPcopy
hub / github.com/unjs/fontaine / parseFontFace

Function parseFontFace

packages/fontaine/src/css.ts:56–124  ·  view source on GitHub ↗
(css: string | CssNode)

Source from the content-addressed store, hash-verified

54 * @returns Array<{ family?: string, source?: string }> - Array of objects with font family and source information
55 */
56export function parseFontFace(css: string | CssNode): Array<{ index: number, family: string, source?: string, properties: FontProperties }> {
57 const families: Array<{ index: number, family: string, source?: string, properties: FontProperties }> = []
58 const ast = typeof css === 'string' ? parse(css, { positions: true }) : css
59
60 walk(ast, {
61 visit: 'Atrule',
62 enter(node) {
63 if (node.name !== 'font-face')
64 return
65
66 let family: string | undefined
67 const sources: string[] = []
68 const properties: FontProperties = {}
69
70 if (node.block) {
71 walk(node.block, {
72 visit: 'Declaration',
73 enter(declaration) {
74 if (declaration.property === 'font-family' && declaration.value.type === 'Value') {
75 for (const child of declaration.value.children) {
76 if (child.type === 'String') {
77 family = withoutQuotes(child.value)
78 break
79 }
80 if (child.type === 'Identifier' && !genericCSSFamilies.has(child.name)) {
81 family = child.name
82 break
83 }
84 }
85 }
86
87 if (fontProperties.has(declaration.property)) {
88 if (declaration.value.type === 'Value') {
89 for (const child of declaration.value.children) {
90 const hasValue = !!properties[declaration.property as keyof FontProperties]
91 properties[declaration.property as keyof FontProperties] ||= ''
92 properties[declaration.property as keyof FontProperties] += (hasValue ? ' ' : '') + generate(child)
93 }
94 }
95 }
96
97 if (declaration.property === 'src') {
98 walk(declaration.value, {
99 visit: 'Url',
100 enter(urlNode) {
101 const source = withoutQuotes(urlNode.value)
102 if (source) {
103 sources.push(source)
104 }
105 },
106 })
107 }
108 },
109 })
110 }
111
112 if (family) {
113 for (const source of sources) {

Callers 2

index.spec.tsFile · 0.90
handlerFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected