File: test_cssfontfacerule.py

package info (click to toggle)
python-css-parser 1.0.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,248 kB
  • sloc: python: 20,584; sh: 11; makefile: 7
file content (244 lines) | stat: -rw-r--r-- 8,689 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
"""Testcases for css_parser.css.CSSFontFaceRule"""

from __future__ import absolute_import
from __future__ import unicode_literals
import xml.dom
from . import test_cssrule
import css_parser


class CSSFontFaceRuleTestCase(test_cssrule.CSSRuleTestCase):

    def setUp(self):
        super(CSSFontFaceRuleTestCase, self).setUp()
        self.r = css_parser.css.CSSFontFaceRule()
        self.rRO = css_parser.css.CSSFontFaceRule(readonly=True)
        self.r_type = css_parser.css.CSSFontFaceRule.FONT_FACE_RULE
        self.r_typeString = 'FONT_FACE_RULE'

    def test_init(self):
        "CSSFontFaceRule.__init__()"
        super(CSSFontFaceRuleTestCase, self).test_init()

        r = css_parser.css.CSSFontFaceRule()
        self.assertIsInstance(r.style, css_parser.css.CSSStyleDeclaration)
        self.assertEqual(r, r.style.parentRule)

        # until any properties
        self.assertEqual('', r.cssText)

        # only possible to set @... similar name
        self.assertRaises(xml.dom.InvalidModificationErr, self.r._setAtkeyword, 'x')

        def checkrefs(ff):
            self.assertEqual(ff, ff.style.parentRule)
            for p in ff.style:
                self.assertEqual(ff.style, p.parent)

        checkrefs(css_parser.css.CSSFontFaceRule(
            style=css_parser.css.CSSStyleDeclaration('font-family: x')))

        r = css_parser.css.CSSFontFaceRule()
        r.cssText = '@font-face { font-family: x }'
        checkrefs(r)

        r = css_parser.css.CSSFontFaceRule()
        r.style.setProperty('font-family', 'y')
        checkrefs(r)

        r = css_parser.css.CSSFontFaceRule()
        r.style['font-family'] = 'z'
        checkrefs(r)

        r = css_parser.css.CSSFontFaceRule()
        r.style.fontFamily = 'a'
        checkrefs(r)

    def test_cssText(self):
        "CSSFontFaceRule.cssText"
        tests = {
            '''@font-face {
    font-family: x;
    src: url(../fonts/LateefRegAAT.ttf) format("truetype-aat"), url(../fonts/LateefRegOT.ttf) format("opentype");
    font-style: italic;
    font-weight: 500;
    font-stretch: condensed;
    unicode-range: u+1-ff, u+111
    }''': None,
            '@font-face{font-family: x;}': '@font-face {\n    font-family: x\n    }',
            '@font-face  {  font-family: x;  }': '@font-face {\n    font-family: x\n    }',
            '@f\\ont\\-face{font-family : x;}': '@font-face {\n    font-family: x\n    }',
            # comments
            '@font-face/*1*//*2*/{font-family: x;}':
                '@font-face /*1*/ /*2*/ {\n    font-family: x\n    }',
            # WS
            '@font-face\n\t\f {\n\t\f font-family:x;\n\t\f }':
                '@font-face {\n    font-family: x\n    }',
        }
        self.do_equal_r(tests)
        self.do_equal_p(tests)

        tests = {
            '@font-face;': xml.dom.SyntaxErr,
            '@font-face }': xml.dom.SyntaxErr,
        }
        self.do_raise_p(tests)  # parse
        tests.update({
            '@font-face {': xml.dom.SyntaxErr,  # no }
            # trailing
            '@font-face {}1': xml.dom.SyntaxErr,
            '@font-face {}/**/': xml.dom.SyntaxErr,
            '@font-face {} ': xml.dom.SyntaxErr,
        })
        self.do_raise_r(tests)  # set cssText

    def test_style(self):
        "CSSFontFaceRule.style (and references)"
        r = css_parser.css.CSSFontFaceRule()
        s1 = r.style
        self.assertEqual(r, s1.parentRule)
        self.assertEqual('', s1.cssText)

        # set rule.cssText
        r.cssText = '@font-face { font-family: x1 }'
        self.assertNotEqual(r.style, s1)
        self.assertEqual(r, r.style.parentRule)
        self.assertEqual(r.cssText, '@font-face {\n    font-family: x1\n    }')
        self.assertEqual(r.style.cssText, 'font-family: x1')
        self.assertEqual(s1.cssText, '')
        s2 = r.style

        # set invalid rule.cssText
        try:
            r.cssText = '@font-face { $ }'
        except xml.dom.SyntaxErr as e:
            pass
        self.assertEqual(r.style, s2)
        self.assertEqual(r, s2.parentRule)
        self.assertEqual(r.cssText, '@font-face {\n    font-family: x1\n    }')
        self.assertEqual(s2.cssText, 'font-family: x1')
        self.assertEqual(r.style.cssText, 'font-family: x1')

        # set rule.style.cssText
        r.style.cssText = 'font-family: x2'
        self.assertEqual(r.style, s2)
        self.assertEqual(r, s2.parentRule)
        self.assertEqual(r.cssText, '@font-face {\n    font-family: x2\n    }')
        self.assertEqual(s2.cssText, 'font-family: x2')
        self.assertEqual(r.style.cssText, 'font-family: x2')

        # set new style object s2
        sn = css_parser.css.CSSStyleDeclaration('font-family: y1')
        r.style = sn
        self.assertEqual(r.style, sn)
        self.assertEqual(r, sn.parentRule)
        self.assertEqual(r.cssText, '@font-face {\n    font-family: y1\n    }')
        self.assertEqual(sn.cssText, 'font-family: y1')
        self.assertEqual(r.style.cssText, 'font-family: y1')
        self.assertEqual(s2.cssText, 'font-family: x2')  # old

        # set s2.cssText
        sn.cssText = 'font-family: y2'
        self.assertEqual(r.style, sn)
        self.assertEqual(r.cssText, '@font-face {\n    font-family: y2\n    }')
        self.assertEqual(r.style.cssText, 'font-family: y2')
        self.assertEqual(s2.cssText, 'font-family: x2')  # old

        # set invalid s2.cssText
        try:
            sn.cssText = '$'
        except xml.dom.SyntaxErr as e:
            pass
        self.assertEqual(r.style, sn)
        self.assertEqual(r.style.cssText, 'font-family: y2')
        self.assertEqual(r.cssText, '@font-face {\n    font-family: y2\n    }')

        # set r.style with text
        r.style = 'font-family: z'
        self.assertNotEqual(r.style, sn)
        self.assertEqual(r.cssText, '@font-face {\n    font-family: z\n    }')
        self.assertEqual(r.style.cssText, 'font-family: z')
        self.assertEqual(sn.cssText, 'font-family: y2')

    def test_properties(self):
        "CSSFontFaceRule.style properties"
        r = css_parser.css.CSSFontFaceRule()
        r.style.cssText = '''
        src: url(x)
        '''
        exp = '''@font-face {
    src: url(x)
    }'''
        self.assertEqual(exp, r.cssText)

        tests = {
            'font-family': [  # ('serif', True),
                #                            ('x', True),
                #                            ('"x"', True),
                ('x, y', False),
                ('"x", y', False),
                ('x, "y"', False),
                #                            ('"x", "y"', False)
            ]
        }
        for n, t in tests.items():
            for (v, valid) in t:
                r = css_parser.css.CSSFontFaceRule()
                r.style[n] = v
                self.assertEqual(r.style.getProperty(n).parent, r.style)
                self.assertEqual(r.style.getProperty(n).valid, valid)

    def test_incomplete(self):
        "CSSFontFaceRule (incomplete)"
        tests = {
            '@font-face{':
                '',  # no } and no content
            '@font-face { ':
                '',  # no } and no content
            '@font-face { font-family: x':
                '@font-face {\n    font-family: x\n    }',  # no }
        }
        self.do_equal_p(tests)  # parse

    def test_InvalidModificationErr(self):
        "CSSFontFaceRule.cssText InvalidModificationErr"
        self._test_InvalidModificationErr('@font-face')
        tests = {
            '@font-fac {}': xml.dom.InvalidModificationErr,
        }
        self.do_raise_r(tests)

    def test_valid(self):
        "CSSFontFaceRule.valid"
        r = css_parser.css.CSSFontFaceRule()
        self.assertEqual(False, r.valid)
        N = 'font-family: x; src: local(x);'
        tests = {
            True: (N,
                   N + 'font-style: italic; font-weight: bold',
                   ),
            False: ('',
                    'font-family: x, y; src: local(x);',
                    N + 'font-style: inherit',
                    N + 'invalid: 1')
        }
        for valid, testlist in tests.items():
            for test in testlist:
                r.style.cssText = test
                self.assertEqual(valid, r.valid)

    def test_reprANDstr(self):
        "CSSFontFaceRule.__repr__(), .__str__()"
        style = 'src: url(x)'
        s = css_parser.css.CSSFontFaceRule(style=style)

        self.assertIn(style, str(s))

        s2 = eval(repr(s))
        self.assertIsInstance(s2, s.__class__)
        self.assertEqual(style, s2.style.cssText)


if __name__ == '__main__':
    import unittest
    unittest.main()