File: serialize.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 (67 lines) | stat: -rw-r--r-- 1,776 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
import logging

import cssutils

cssutils.log.setLevel(logging.FATAL)


css = r'''@import "example.css";
a {
    color: blue !important;
    c\olor: green !important;
    c\olor: pink;
    color: red;
    }'''

sheet = cssutils.parseString(css)
print("\nORIGINAL CSS:")
print(css)
print("------------")

print(repr(cssutils.ser.prefs))

print("\nCSS Serialized")
print(sheet.cssText)

print("\nCSS Serialized with ``keepAllProperties`` = False")
cssutils.ser.prefs.keepAllProperties = False
print(sheet.cssText)

print("\nCSS Serialized with ``defaultPropertyName`` = True")
cssutils.ser.prefs.defaultPropertyName = True
print(sheet.cssText)

print("\nCSS Serialized with ``defaultPropertyName`` = False")
cssutils.ser.prefs.defaultPropertyName = False
print(sheet.cssText)


print('\nBUT: Reading value programmatic uses normalized property')
print('     name and results in actual value only:')
print('\t.getPropertyValue("color") ==', end=' ')
print(sheet.cssRules[1].style.getPropertyValue('color'))
print('\t.getPropertyValue("c\\olor") ==', end=' ')
print(sheet.cssRules[1].style.getPropertyValue(r'c\olor'))
print('\t.getPropertyValue("c\\o\\l\\o\\r") ==', end=' ')
print(sheet.cssRules[1].style.getPropertyValue('c\\o\\l\\o\\r'))
print()

print(
    '\nCSS Serialized with indent = 2*" ", importHrefFormat="string", lineNumbers=True'
)
cssutils.ser.prefs.indent = 2 * ' '
# used to set indentation string, default is 4*' '
cssutils.ser.prefs.importHrefFormat = 'string'
# or 'uri', defaults to the format used in parsed stylesheet
cssutils.ser.prefs.lineNumbers = True
print(sheet.cssText)

print('\nCSS Serialized with useMinified()')
cssutils.ser.prefs.useMinified()
print(sheet.cssText)

# OUTPUTS
# 1: @import "example.css";
# 2: body {
# 3:   color: red
# 4:   }