| 1 |
#!/usr/bin/env python |
| 2 |
# vim:fileencoding=utf-8 |
| 3 |
|
| 4 |
|
| 5 |
__license__ = 'GPL v3' |
| 6 |
__copyright__ = '2014, Kovid Goyal <kovid at kovidgoyal.net>' |
| 7 |
|
| 8 |
from polyglot.builtins import iteritems |
| 9 |
from tinycss.fonts3 import CSSFonts3Parser, parse_font, parse_font_family, serialize_font |
| 10 |
from tinycss.tests import BaseTest |
| 11 |
|
| 12 |
|
| 13 |
class TestFonts3(BaseTest): |
| 14 |
|
| 15 |
def test_font_face(self): |
| 16 |
'Test parsing of font face rules' |
| 17 |
for css, expected_declarations, expected_errors in [ |
| 18 |
('@font-face {}', [], []), |
| 19 |
|
| 20 |
('@font-face { font-family: Moose; src: url(font1.ttf) }', |
| 21 |
[('font-family', [('IDENT', 'Moose')]), ('src', [('URI', 'font1.ttf')])], []), |
| 22 |
]: |
| 23 |
stylesheet = CSSFonts3Parser().parse_stylesheet(css) |
| 24 |
self.assert_errors(stylesheet.errors, expected_errors) |
| 25 |
self.ae(len(stylesheet.rules), 1) |
| 26 |
rule = stylesheet.rules[0] |
| 27 |
self.ae(self.jsonify_declarations(rule), expected_declarations) |
| 28 |
|
| 29 |
stylesheet = CSSFonts3Parser().parse_stylesheet('@font-face;') |
| 30 |
self.assert_errors(stylesheet.errors, ['missing block']) |
| 31 |
|
| 32 |
def test_parse_font_family(self): |
| 33 |
' Test parsing of font-family values ' |
| 34 |
for raw, q in iteritems({ |
| 35 |
'"1as"': ['1as'], |
| 36 |
'A B C, serif': ['A B C', 'serif'], |
| 37 |
r'Red\/Black': ['Red/Black'], |
| 38 |
'A B': ['A B'], |
| 39 |
r'Ahem\!': ['Ahem!'], |
| 40 |
r'"Ahem!"': ['Ahem!'], |
| 41 |
'€42': ['€42'], |
| 42 |
r'Hawaii\ 5-0': ['Hawaii 5-0'], |
| 43 |
r'"X \"Y"': ['X "Y'], |
| 44 |
'A B, C D, "E", serif': ['A B', 'C D', 'E', 'serif'], |
| 45 |
'': [], |
| 46 |
'"", a': ['a'], |
| 47 |
}): |
| 48 |
self.ae(q, parse_font_family(raw)) |
| 49 |
for single in ('serif', 'sans-serif', 'A B C'): |
| 50 |
self.ae([single], parse_font_family(single)) |
| 51 |
|
| 52 |
def test_parse_font(self): |
| 53 |
def t(raw, **kw): |
| 54 |
q = {('line' if k == 'height' else 'font') + '-' + k:v for k, v in iteritems(kw)} |
| 55 |
self.ae(q, parse_font(raw)) |
| 56 |
self.ae(q, parse_font(serialize_font(q))) |
| 57 |
t('caption', family=['sans-serif']) |
| 58 |
t('serif', family=['serif']) |
| 59 |
t('12pt/14pt sans-serif', size='12pt', height='14pt', family=['sans-serif']) |
| 60 |
t('80% sans-serif', size='80%', family=['sans-serif']) |
| 61 |
t('x-large/110% "new century schoolbook", serif', size='x-large', height='110%', family=['new century schoolbook', 'serif']) |
| 62 |
t('bold italic large Palatino, serif', weight='bold', style='italic', size='large', family=['Palatino', 'serif']) |
| 63 |
t('normal small-caps 120%/120% fantasy', style='normal', variant='small-caps', size='120%', height='120%', family=['fantasy']) |
| 64 |
t('condensed oblique 12pt Helvetica Neue, serif', stretch='condensed', style='oblique', size='12pt', family=['Helvetica Neue', 'serif']) |
| 65 |
t('300 italic 1.3em/1.7em FB Armada, sans-serif', weight='300', style='italic', size='1.3em', height='1.7em', family=['FB Armada', 'sans-serif']) |