File: serialize.rst

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 (66 lines) | stat: -rw-r--r-- 1,785 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
.. module:: cssutils.serialize

.. index::
    single: ser, cssutils.ser
    pair: object; cssutils.ser

===============
serializing CSS
===============
To serialize any stylesheet use::

    print sheet.cssText

Also most other objects have a similar property which contains the *text* content of each object. Some use a slightly different name (e.g. ``selectorText``) but all use the global serializer::

    >>> sheet = cssutils.parseString('a, b { color: green }')
    >>> print sheet.cssRules[0].cssText
    a, b {
        color: green
        }
    >>> print sheet.cssRules[0].selectorText
    a, b
    >>> print sheet.cssRules[0].selectorList[1].selectorText
    b


.. _Preferences:

.. index::
    single: cssutils.ser.prefs
    pair: object; cssutils.ser.prefs

``Preferences``
===============
Quite a few preferences of the cssutils serializer may be tweaked.

To set a preference use::

    cssutils.ser.prefs.PREFNAME = NEWVALUE

Preferences are always used *globally*, so for all stylesheets until preferences are set again.


.. autoclass:: cssutils.serialize.Preferences
   :members:
   :inherited-members:



``CSSSerializer``
=================
There is a single global serializer used throughout the library. You may configure it by setting specific Preferences_ or completely replace it with your own.

A custom serializer must implement all methods the default one provides. Easiest would be to subclass :class:`cssutils.serialize.CSSSerializer`.

To set a new serializer, use::

    cssutils.setSerializer(serializer)

You may also set ``cssutils.ser`` directly but the above method is the preferred one.

For most cases adjusting the ``cssutils.ser.prefs`` of the default serializer should be sufficient though.

.. autoclass:: cssutils.serialize.CSSSerializer