annotate doc/filters.txt @ 614:6d584267d410 stable-0.4.x

Ported [728] to 0.4.x branch.
author cmlenz
date Wed, 29 Aug 2007 19:36:01 +0000
parents 26cf27d4f2b3
children
rev   line source
438
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
1 .. -*- mode: rst; encoding: utf-8 -*-
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
2
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
3 ==============
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
4 Stream Filters
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
5 ==============
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
6
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
7 `Markup Streams`_ showed how to write filters and how they are applied to
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
8 markup streams. This page describes the features of the various filters that
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
9 come with Genshi itself.
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
10
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
11 .. _`Markup Streams`: streams.html
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
12
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
13 .. contents:: Contents
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
14 :depth: 1
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
15 .. sectnum::
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
16
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
17
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
18 HTML Form Filler
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
19 ================
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
20
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
21 The filter ``genshi.filters.HTMLFormFiller`` can automatically populate an HTML
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
22 form from values provided as a simple dictionary. When using thi filter, you can
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
23 basically omit any ``value``, ``selected``, or ``checked`` attributes from form
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
24 controls in your templates, and let the filter do all that work for you.
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
25
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
26 ``HTMLFormFiller`` takes a dictionary of data to populate the form with, where
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
27 the keys should match the names of form elements, and the values determine the
511
26cf27d4f2b3 Ported [611:614] to 0.4.x branch.
cmlenz
parents: 438
diff changeset
28 values of those controls. For example:
26cf27d4f2b3 Ported [611:614] to 0.4.x branch.
cmlenz
parents: 438
diff changeset
29
26cf27d4f2b3 Ported [611:614] to 0.4.x branch.
cmlenz
parents: 438
diff changeset
30 .. code-block:: pycon
438
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
31
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
32 >>> from genshi.filters import HTMLFormFiller
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
33 >>> from genshi.template import MarkupTemplate
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
34 >>> template = MarkupTemplate("""<form>
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
35 ... <p>
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
36 ... <label>User name:
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
37 ... <input type="text" name="username" />
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
38 ... </label><br />
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
39 ... <label>Password:
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
40 ... <input type="password" name="password" />
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
41 ... </label><br />
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
42 ... <label>
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
43 ... <input type="checkbox" name="remember" /> Remember me
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
44 ... </label>
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
45 ... </p>
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
46 ... </form>""")
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
47 >>> filler = HTMLFormFiller(data=dict(username='john', remember=True))
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
48 >>> print template.generate() | filler
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
49 <form>
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
50 <p>
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
51 <label>User name:
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
52 <input type="text" name="username" value="john"/>
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
53 </label><br/>
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
54 <label>Password:
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
55 <input type="password" name="password"/>
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
56 </label><br/>
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
57 <label>
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
58 <input type="checkbox" name="remember" checked="checked"/> Remember me
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
59 </label>
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
60 </p>
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
61 </form>
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
62
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
63 .. note:: This processing is done without in any way reparsing the template
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
64 output. As any stream filter it operates after the template output is
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
65 generated but *before* that output is actually serialized.
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
66
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
67 The filter will of course also handle radio buttons as well as ``<select>`` and
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
68 ``<textarea>`` elements. For radio buttons to be marked as checked, the value in
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
69 the data dictionary needs to match the ``value`` attribute of the ``<input>``
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
70 element, or evaluate to a truth value if the element has no such attribute. For
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
71 options in a ``<select>`` box to be marked as selected, the value in the data
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
72 dictionary needs to match the ``value`` attribute of the ``<option>`` element,
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
73 or the text content of the option if it has no ``value`` attribute. Password and
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
74 file input fields are not populated, as most browsers would ignore that anyway
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
75 for security reasons.
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
76
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
77 You'll want to make sure that the values in the data dictionary have already
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
78 been converted to strings. While the filter may be able to deal with non-string
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
79 data in some cases (such as check boxes), in most cases it will either not
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
80 attempt any conversion or not produce the desired results.
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
81
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
82 You can restrict the form filler to operate only on a specific ``<form>`` by
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
83 passing either the ``id`` or the ``name`` keyword argument to the initializer.
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
84 If either of those is specified, the filter will only apply to form tags with
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
85 an attribute matching the specified value.
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
86
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
87
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
88 HTML Sanitizer
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
89 ==============
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
90
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
91 The filter ``genshi.filters.HTMLSanitizer`` filter can be used to clean up
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
92 user-submitted HTML markup, removing potentially dangerous constructs that could
511
26cf27d4f2b3 Ported [611:614] to 0.4.x branch.
cmlenz
parents: 438
diff changeset
93 be used for various kinds of abuse, such as cross-site scripting (XSS) attacks:
26cf27d4f2b3 Ported [611:614] to 0.4.x branch.
cmlenz
parents: 438
diff changeset
94
26cf27d4f2b3 Ported [611:614] to 0.4.x branch.
cmlenz
parents: 438
diff changeset
95 .. code-block:: pycon
438
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
96
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
97 >>> from genshi.filters import HTMLSanitizer
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
98 >>> from genshi.input import HTML
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
99 >>> html = HTML("""<div>
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
100 ... <p>Innocent looking text.</p>
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
101 ... <script>alert("Danger: " + document.cookie)</script>
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
102 ... </div>""")
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
103 >>> sanitize = HTMLSanitizer()
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
104 >>> print html | sanitize
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
105 <div>
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
106 <p>Innocent looking text.</p>
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
107 </div>
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
108
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
109 In this example, the ``<script>`` tag was removed from the output.
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
110
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
111 You can determine which tags and attributes should be allowed by initializing
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
112 the filter with corresponding sets. See the API documentation for more
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
113 information.
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
114
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
115 Inline ``style`` attributes are forbidden by default. If you allow them, the
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
116 filter will still perform sanitization on the contents any encountered inline
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
117 styles: the proprietary ``expression()`` function (supported only by Internet
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
118 Explorer) is removed, and any property using an ``url()`` which a potentially
511
26cf27d4f2b3 Ported [611:614] to 0.4.x branch.
cmlenz
parents: 438
diff changeset
119 dangerous URL scheme (such as ``javascript:``) are also stripped out:
26cf27d4f2b3 Ported [611:614] to 0.4.x branch.
cmlenz
parents: 438
diff changeset
120
26cf27d4f2b3 Ported [611:614] to 0.4.x branch.
cmlenz
parents: 438
diff changeset
121 .. code-block:: pycon
438
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
122
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
123 >>> from genshi.filters import HTMLSanitizer
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
124 >>> from genshi.input import HTML
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
125 >>> html = HTML("""<div>
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
126 ... <br style="background: url(javascript:alert(document.cookie); color: #000" />
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
127 ... </div>""")
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
128 >>> sanitize = HTMLSanitizer(safe_attrs=HTMLSanitizer.SAFE_ATTRS | set(['style']))
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
129 >>> print html | sanitize
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
130 <div>
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
131 <br style="color: #000"/>
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
132 </div>
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
133
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
134 .. warning:: You should probably not rely on the ``style`` filtering, as
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
135 sanitizing mixed HTML, CSS, and Javascript is very complicated and
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
136 suspect to various browser bugs. If you can somehow get away with
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
137 not allowing inline styles in user-submitted content, that would
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
138 definitely be the safer route to follow.
511
26cf27d4f2b3 Ported [611:614] to 0.4.x branch.
cmlenz
parents: 438
diff changeset
139
26cf27d4f2b3 Ported [611:614] to 0.4.x branch.
cmlenz
parents: 438
diff changeset
140
26cf27d4f2b3 Ported [611:614] to 0.4.x branch.
cmlenz
parents: 438
diff changeset
141 Translator
26cf27d4f2b3 Ported [611:614] to 0.4.x branch.
cmlenz
parents: 438
diff changeset
142 ==========
26cf27d4f2b3 Ported [611:614] to 0.4.x branch.
cmlenz
parents: 438
diff changeset
143
26cf27d4f2b3 Ported [611:614] to 0.4.x branch.
cmlenz
parents: 438
diff changeset
144 The ``genshi.filters.i18n.Translator`` filter implements basic support for
26cf27d4f2b3 Ported [611:614] to 0.4.x branch.
cmlenz
parents: 438
diff changeset
145 internationalizing and localizing templates. When used as a filter, it
26cf27d4f2b3 Ported [611:614] to 0.4.x branch.
cmlenz
parents: 438
diff changeset
146 translates a configurable set of text nodes and attribute values using a
26cf27d4f2b3 Ported [611:614] to 0.4.x branch.
cmlenz
parents: 438
diff changeset
147 ``gettext``-style translation function.
26cf27d4f2b3 Ported [611:614] to 0.4.x branch.
cmlenz
parents: 438
diff changeset
148
26cf27d4f2b3 Ported [611:614] to 0.4.x branch.
cmlenz
parents: 438
diff changeset
149 The ``Translator`` class also defines the ``extract`` class method, which can
26cf27d4f2b3 Ported [611:614] to 0.4.x branch.
cmlenz
parents: 438
diff changeset
150 be used to extract localizable messages from a template.
26cf27d4f2b3 Ported [611:614] to 0.4.x branch.
cmlenz
parents: 438
diff changeset
151
26cf27d4f2b3 Ported [611:614] to 0.4.x branch.
cmlenz
parents: 438
diff changeset
152 Please refer to the API documentation for more information on this filter.
26cf27d4f2b3 Ported [611:614] to 0.4.x branch.
cmlenz
parents: 438
diff changeset
153
26cf27d4f2b3 Ported [611:614] to 0.4.x branch.
cmlenz
parents: 438
diff changeset
154 .. note:: The translation filter was added in Genshi 0.4.
Copyright (C) 2012-2017 Edgewall Software