Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multiple @font-face of the same name #327

Merged
merged 2 commits into from
Jun 8, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import asap from 'asap';

import OrderedElements from './ordered-elements';
import {generateCSS} from './generate';
import {hashObject, hashString} from './util';
import { generateCSS } from './generate';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

My editor auto formats this. I can change back if need be.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I prefer it the way you have formatted it here, but Khan's style guide is the way it was. I don't think it matters too much, but these stylistic changes should probably be avoided to reduce diff churn. If we want to change the code style of this project, that should all be done at one time. Can you change this (and any other changes like this e.g. line 51) back?

import { hashObject, hashString } from './util';

/* ::
import type { SheetDefinition, SheetDefinitions } from './index.js';
Expand Down Expand Up @@ -48,7 +48,7 @@ const injectStyleTag = (cssRules /* : string[] */) => {
try {
sheet.insertRule(rule, numRules);
numRules += 1;
} catch(e) {
} catch (e) {
// The selector for this rule wasn't compatible with the browser
}
});
Expand All @@ -66,7 +66,13 @@ const stringHandlers = {
// an array of objects and strings.
fontFamily: function fontFamily(val) {
if (Array.isArray(val)) {
return val.map(fontFamily).join(",");
const nameMap = {};
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Was gonna use Set, but I didn't see that or Map used anywhere else. Playing it safe.

Copy link
Collaborator

Choose a reason for hiding this comment

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

That's right, we've avoided these things so it would be nice to keep it that way unless there is a compelling reason not to.

I think the order of the keys in the object are not guaranteed on older versions of Node, but that might not matter anymore so this should be okay.


val.forEach(v => {
nameMap[fontFamily(v)] = true;
});

return Object.keys(nameMap).join(",");
} else if (typeof val === "object") {
injectStyleOnce(val.src, "@font-face", [val], false);
return `"${val.fontFamily}"`;
Expand Down
72 changes: 70 additions & 2 deletions tests/inject_test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import asap from 'asap';
import {assert} from 'chai';
import {JSDOM} from 'jsdom';
import { assert } from 'chai';
import { JSDOM } from 'jsdom';

import { StyleSheet, css, minify } from '../src/index';
import {
Expand Down Expand Up @@ -404,6 +404,74 @@ describe('String handlers', () => {
assertStylesInclude('font-family: CoolFont;');
assertStylesInclude("src: url('coolfont.ttf');");
});

it('supports multiple @font-face with the same family name', () => {
const sheet = StyleSheet.create({
base: {
fontFamily: [
{
fontFamily: "CoolFont",
src: "url('coolfont.ttf')",
},
{
fontFamily: "CoolFont",
fontStyle: "italic",
src: "url('coolfont-italic.ttf')",
},
{
fontFamily: "CoolFont",
fontWeight: 300,
src: "url('coolfont-bold.ttf')",
},
"sans-serif",
],
},
});

startBuffering();
css(sheet.base);
flushToStyleTag();

assertStylesInclude('font-family: "CoolFont",sans-serif !important');
assertStylesInclude('font-family: CoolFont;');
assertStylesInclude("src: url('coolfont.ttf');");
assertStylesInclude("font-style: italic; src: url('coolfont-italic.ttf');");
assertStylesInclude("font-weight: 300; src: url('coolfont-bold.ttf');");
});

it('supports multiple @font-face with different family names', () => {
const sheet = StyleSheet.create({
base: {
fontFamily: [
{
fontFamily: "CoolFont",
src: "url('coolfont.ttf')",
},
{
fontFamily: "AwesomeFont",
src: "url('awesomefont.ttf')",
},
{
fontFamily: "SuperFont",
src: "url('superfont.ttf')",
},
"sans-serif",
],
},
});

startBuffering();
css(sheet.base);
flushToStyleTag();

assertStylesInclude('font-family: "CoolFont","AwesomeFont","SuperFont",sans-serif !important');
assertStylesInclude('font-family: CoolFont;');
assertStylesInclude("src: url('coolfont.ttf');");
assertStylesInclude('font-family: AwesomeFont;');
assertStylesInclude("src: url('awesomefont.ttf');");
assertStylesInclude('font-family: SuperFont;');
assertStylesInclude("src: url('superfont.ttf');");
});
});

describe('animationName', () => {
Expand Down