File: cssencodings.py

package info (click to toggle)
cssutils 2.11.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,312 kB
  • sloc: python: 23,625; javascript: 803; sh: 62; makefile: 8
file content (82 lines) | stat: -rw-r--r-- 1,669 bytes parent folder | download
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
"""
example how to use encodings

example css is in default UTF-8 encoding
"""

from cssutils import CSSParser

EXPOUT = '''cssText in different encodings, depending on the console some
     chars may look broken but are actually not

@charset "ascii";
/* some umlauts \\E4 \\F6 \\FC  and EURO sign \\20AC  */
a:before {
    content: "\\E4 "
    }

@charset "iso-8859-1";
/* some umlauts \xe4\xf6\xfc and EURO sign \\20AC  */
a:before {
    content: "\xe4"
    }

@charset "iso-8859-15";
/* some umlauts \xe4\xf6\xfc and EURO sign \xa4 */
a:before {
    content: "\xe4"
    }

@charset "utf-8";
/* some umlauts \xc3\xa4\xc3\xb6\xc3\xbc and EURO sign \xe2\x82\xac */
a:before {
    content: "\xc3\xa4"
    }

/* some umlauts \xc3\xa4\xc3\xb6\xc3\xbc and EURO sign \xe2\x82\xac */
a:before {
    content: "\xc3\xa4"
    }
'''
EXPERR = 'Property: Found valid "CSS Level 2.1" value: "\xe4" [4:8: content]\n'


def main():
    css = '''
    /* some umlauts äöü and EURO sign € */
    a:before {
       content: "ä";
        }'''

    p = CSSParser()
    sheet = p.parseString(css)

    print(
        """cssText in different encodings, depending on the console some
     chars may look broken but are actually not"""
    )
    print()

    sheet.encoding = 'ascii'
    print(sheet.cssText)
    print()

    sheet.encoding = 'iso-8859-1'
    print(sheet.cssText)
    print()

    sheet.encoding = 'iso-8859-15'
    print(sheet.cssText)
    print()

    sheet.encoding = 'utf-8'
    print(sheet.cssText)
    print()

    # results in default UTF-8 encoding without @charset rule
    sheet.encoding = None
    print(sheet.cssText)


if __name__ == '__main__':
    main()