Skip to content

Commit 29aa0ae

Browse files
authored
Merge pull request #1036 from tommedema/dev-fonts
Allow multiple at-rules, fixes #1032
2 parents 43d5e07 + e728eb8 commit 29aa0ae

File tree

4 files changed

+105
-2
lines changed

4 files changed

+105
-2
lines changed

src/css_composer/index.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,13 @@ module.exports = () => {
182182
var w = width || '';
183183
var opt = { ...opts };
184184
var rule = this.get(selectors, s, w, opt);
185-
if (rule) return rule;
186-
else {
185+
186+
// do not create rules that were found before
187+
// unless this is an at-rule, for which multiple declarations
188+
// make sense (e.g. multiple `@font-type`s)
189+
if (rule && rule.config && !rule.config.atRuleType) {
190+
return rule;
191+
} else {
187192
opt.state = s;
188193
opt.mediaText = w;
189194
opt.selectors = '';

test/specs/grapesjs/index.js

+21
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,27 @@ describe('GrapesJS', () => {
157157
expect(editor.getStyle().length).toEqual(2);
158158
});
159159

160+
it('Init editor from element with multiple font-face at-rules', () => {
161+
config.fromElement = 1;
162+
config.storageManager = { type: 0 };
163+
fixture.innerHTML =
164+
`
165+
<style>
166+
@font-face {
167+
font-family: 'Glyphicons Halflings';
168+
src: url(https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/fonts/glyphicons-halflings-regular.woff2) format('woff2');
169+
}
170+
@font-face {
171+
font-family: 'Droid Sans';
172+
src: url(https://fonts.gstatic.com/s/droidsans/v8/SlGVmQWMvZQIdix7AFxXkHNSbRYXags.woff2) format('woff2');
173+
}
174+
</style>` + htmlString;
175+
const editor = obj.init(config);
176+
const css = editor.getCss();
177+
const styles = editor.getStyle();
178+
expect(styles.length).toEqual(2);
179+
});
180+
160181
it('Set components as HTML', () => {
161182
var editor = obj.init(config);
162183
editor.setComponents(htmlString);

test/specs/parser/model/ParserCss.js

+33
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,39 @@ module.exports = {
277277
expect(obj.parse(str)).toEqual(result);
278278
});
279279

280+
it('Parses multiple font-face at-rules', () => {
281+
const str = `
282+
@font-face {
283+
font-family: "Open Sans";
284+
}
285+
@font-face {
286+
font-family: 'Glyphicons Halflings';
287+
src:url(https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/fonts/glyphicons-halflings-regular.eot)
288+
}`;
289+
const result = [
290+
{
291+
selectors: [],
292+
selectorsAdd: '',
293+
style: { 'font-family': '"Open Sans"' },
294+
singleAtRule: 1,
295+
atRuleType: 'font-face'
296+
},
297+
{
298+
selectors: [],
299+
selectorsAdd: '',
300+
style: {
301+
'font-family': "'Glyphicons Halflings'",
302+
src:
303+
'url(https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/fonts/glyphicons-halflings-regular.eot)'
304+
},
305+
singleAtRule: 1,
306+
atRuleType: 'font-face'
307+
}
308+
];
309+
const parsed = obj.parse(str);
310+
expect(parsed).toEqual(result);
311+
});
312+
280313
it('Parse ID rule', () => {
281314
var str = `#test { color: red }`;
282315
var result = {

test/specs/parser/model/ParserHtml.js

+44
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,50 @@ module.exports = {
372372
expect(res.css).toEqual(resCss);
373373
});
374374

375+
it('Respect multiple font-faces contained in styles in html', () => {
376+
const str = `
377+
<style>
378+
@font-face {
379+
font-family: "Open Sans";
380+
src:url(https://fonts.gstatic.com/s/droidsans/v8/SlGVmQWMvZQIdix7AFxXkHNSbRYXags.woff2)
381+
}
382+
@font-face {
383+
font-family: 'Glyphicons Halflings';
384+
src:url(https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/fonts/glyphicons-halflings-regular.eot)
385+
}
386+
</style>
387+
<div>a div</div>
388+
`;
389+
390+
const expected = [
391+
{
392+
selectors: [],
393+
selectorsAdd: '',
394+
style: {
395+
'font-family': '"Open Sans"',
396+
src:
397+
'url(https://fonts.gstatic.com/s/droidsans/v8/SlGVmQWMvZQIdix7AFxXkHNSbRYXags.woff2)'
398+
},
399+
singleAtRule: 1,
400+
atRuleType: 'font-face'
401+
},
402+
{
403+
selectors: [],
404+
selectorsAdd: '',
405+
style: {
406+
'font-family': "'Glyphicons Halflings'",
407+
src:
408+
'url(https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/fonts/glyphicons-halflings-regular.eot)'
409+
},
410+
singleAtRule: 1,
411+
atRuleType: 'font-face'
412+
}
413+
];
414+
415+
const res = obj.parse(str, new ParserCss());
416+
expect(res.css).toEqual(expected);
417+
});
418+
375419
it('Parse nested div with text and spaces', () => {
376420
var str = '<div> <p>TestText</p> </div>';
377421
var result = {

0 commit comments

Comments
 (0)