Skip to content

Commit

Permalink
fix: split font-family for Firefox (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
qq15725 committed Jun 3, 2023
1 parent 1528381 commit a1d1bbc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
8 changes: 3 additions & 5 deletions src/clone-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
isStyleElement,
isTextNode,
isVideoElement,
splitFontFamily,
} from './utils'
import { cloneElement } from './clone-element'
import type { Context } from './context'
Expand Down Expand Up @@ -112,11 +113,8 @@ export async function cloneNode<T extends Node>(

copyInputValue(node, cloned)

diffStyle.get('font-family')?.[0]
.split(',')
.filter(Boolean)
.map(val => val.toLowerCase())
.forEach(val => fontFamilies.add(val))
splitFontFamily(diffStyle.get('font-family')?.[0])
?.forEach(val => fontFamilies.add(val))

if (!isVideoElement(node)) {
await cloneChildNodes(node, cloned, context)
Expand Down
9 changes: 3 additions & 6 deletions src/embed-web-font.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { URL_RE, hasCssUrl, replaceCssUrlToDataUrl } from './css-url'
import { contextFetch } from './fetch'
import { consoleWarn, isCSSImportRule, isCssFontFaceRule, resolveUrl } from './utils'
import { consoleWarn, isCSSImportRule, isCssFontFaceRule, resolveUrl, splitFontFamily } from './utils'
import type { Context } from './context'

export async function embedWebFont<T extends Element>(
Expand Down Expand Up @@ -78,11 +78,8 @@ export async function embedWebFont<T extends Element>(
.filter(cssRule => (
isCssFontFaceRule(cssRule)
&& hasCssUrl(cssRule.style.getPropertyValue('src'))
&& cssRule.style.getPropertyValue('font-family')
.split(',')
.filter(Boolean)
.map(val => val.toLowerCase())
.some(val => fontFamilies.has(val))
&& splitFontFamily(cssRule.style.getPropertyValue('font-family'))
?.some(val => fontFamilies.has(val))
))
.forEach((value) => {
const rule = value as CSSFontFaceRule
Expand Down
9 changes: 9 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,12 @@ export const uuid = (function uuid() {
return `u${ random() }${ counter }`
}
})()

export function splitFontFamily(fontFamily?: string): undefined | string[] {
return fontFamily
?.split(',')
// Chrome '__Niconne_7b96fe, __Niconne_Fallback_7b96fe'
// Firefox '"__Niconne_7b96fe", "__Niconne_Fallback_7b96fe"'
.map(val => val.trim().replace(/"|'/g, '').toLowerCase())
.filter(Boolean)
}

1 comment on commit a1d1bbc

@vercel
Copy link

@vercel vercel bot commented on a1d1bbc Jun 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

modern-screenshot – ./

modern-screenshot-qq15725.vercel.app
modern-screenshot-git-main-qq15725.vercel.app
modern-screenshot.vercel.app

Please sign in to comment.