annotate doc/filters.txt @ 532:f9ad40cae2f7 stable-0.4.x 0.4.2

Ported [634:637] to 0.4.x branch.
author cmlenz
date Wed, 20 Jun 2007 10:50:18 +0000
parents 1a29617a5d87
children
rev   line source
438
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
1 .. -*- mode: rst; encoding: utf-8 -*-
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
2
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
3 ==============
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
4 Stream Filters
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
5 ==============
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
6
6fd7e4dc0318 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
6fd7e4dc0318 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
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
9 come with Genshi itself.
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
10
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
11 .. _`Markup Streams`: streams.html
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
12
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
13 .. contents:: Contents
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
14 :depth: 1
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
15 .. sectnum::
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
16
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
17
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
18 HTML Form Filler
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
19 ================
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
20
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
21 The filter ``genshi.filters.HTMLFormFiller`` can automatically populate an HTML
6fd7e4dc0318 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
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
23 basically omit any ``value``, ``selected``, or ``checked`` attributes from form
6fd7e4dc0318 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.
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
25
6fd7e4dc0318 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
6fd7e4dc0318 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
1a29617a5d87 Ported [611:614] to 0.4.x branch.
cmlenz
parents: 438
diff changeset
28 values of those controls. For example:
1a29617a5d87 Ported [611:614] to 0.4.x branch.
cmlenz
parents: 438
diff changeset
29
1a29617a5d87 Ported [611:614] to 0.4.x branch.
cmlenz
parents: 438
diff changeset
30 .. code-block:: pycon
438
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
31
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
32 >>> from genshi.filters import HTMLFormFiller
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
33 >>> from genshi.template import MarkupTemplate
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
34 >>> template = MarkupTemplate("""<form>
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
35 ... <p>
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
36 ... <label>User name:
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
37 ... <input type="text" name="username" />
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
38 ... </label><br />
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
39 ... <label>Password:
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
40 ... <input type="password" name="password" />
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
41 ... </label><br />
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
42 ... <label>
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
43 ... <input type="checkbox" name="remember" /> Remember me
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
44 ... </label>
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
45 ... </p>
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
46 ... </form>""")
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
47 >>> filler = HTMLFormFiller(data=dict(username='john', remember=True))
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
48 >>> print template.generate() | filler
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
49 <form>
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
50 <p>
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
51 <label>User name:
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
52 <input type="text" name="username" value="john"/>
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
53 </label><br/>
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
54 <label>Password:
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
55 <input type="password" name="password"/>
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
56 </label><br/>
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
57 <label>
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
58 <input type="checkbox" name="remember" checked="checked"/> Remember me
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
59 </label>
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
60 </p>
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
61 </form>
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
62
6fd7e4dc0318 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
6fd7e4dc0318 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
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
65 generated but *before* that output is actually serialized.
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
66
6fd7e4dc0318 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
6fd7e4dc0318 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
6fd7e4dc0318 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>``
6fd7e4dc0318 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
6fd7e4dc0318 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
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
72 dictionary needs to match the ``value`` attribute of the ``<option>`` element,
6fd7e4dc0318 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
6fd7e4dc0318 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
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
75 for security reasons.
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
76
6fd7e4dc0318 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
6fd7e4dc0318 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
6fd7e4dc0318 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
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
80 attempt any conversion or not produce the desired results.
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
81
6fd7e4dc0318 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
6fd7e4dc0318 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.
6fd7e4dc0318 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
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
85 an attribute matching the specified value.
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
86
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
87
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
88 HTML Sanitizer
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
89 ==============
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
90
6fd7e4dc0318 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
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
92 user-submitted HTML markup, removing potentially dangerous constructs that could
511
1a29617a5d87 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:
1a29617a5d87 Ported [611:614] to 0.4.x branch.
cmlenz
parents: 438
diff changeset
94
1a29617a5d87 Ported [611:614] to 0.4.x branch.
cmlenz
parents: 438
diff changeset
95 .. code-block:: pycon
438
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
96
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
97 >>> from genshi.filters import HTMLSanitizer
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
98 >>> from genshi.input import HTML
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
99 >>> html = HTML("""<div>
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
100 ... <p>Innocent looking text.</p>
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
101 ... <script>alert("Danger: " + document.cookie)</script>
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
102 ... </div>""")
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
103 >>> sanitize = HTMLSanitizer()
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
104 >>> print html | sanitize
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
105 <div>
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
106 <p>Innocent looking text.</p>
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
107 </div>
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
108
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
109 In this example, the ``<script>`` tag was removed from the output.
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
110
6fd7e4dc0318 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
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
112 the filter with corresponding sets. See the API documentation for more
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
113 information.
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
114
6fd7e4dc0318 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
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
116 filter will still perform sanitization on the contents any encountered inline
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
117 styles: the proprietary ``expression()`` function (supported only by Internet
6fd7e4dc0318 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
1a29617a5d87 Ported [611:614] to 0.4.x branch.
cmlenz
parents: 438
diff changeset
119 dangerous URL scheme (such as ``javascript:``) are also stripped out:
1a29617a5d87 Ported [611:614] to 0.4.x branch.
cmlenz
parents: 438
diff changeset
120
1a29617a5d87 Ported [611:614] to 0.4.x branch.
cmlenz
parents: 438
diff changeset
121 .. code-block:: pycon
438
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
122
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
123 >>> from genshi.filters import HTMLSanitizer
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
124 >>> from genshi.input import HTML
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
125 >>> html = HTML("""<div>
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
126 ... <br style="background: url(javascript:alert(document.cookie); color: #000" />
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
127 ... </div>""")
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
128 >>> sanitize = HTMLSanitizer(safe_attrs=HTMLSanitizer.SAFE_ATTRS | set(['style']))
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
129 >>> print html | sanitize
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
130 <div>
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
131 <br style="color: #000"/>
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
132 </div>
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
133
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
134 .. warning:: You should probably not rely on the ``style`` filtering, as
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
135 sanitizing mixed HTML, CSS, and Javascript is very complicated and
6fd7e4dc0318 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
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
137 not allowing inline styles in user-submitted content, that would
6fd7e4dc0318 Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
138 definitely be the safer route to follow.
511
1a29617a5d87 Ported [611:614] to 0.4.x branch.
cmlenz
parents: 438
diff changeset
139
1a29617a5d87 Ported [611:614] to 0.4.x branch.
cmlenz
parents: 438
diff changeset
140
1a29617a5d87 Ported [611:614] to 0.4.x branch.
cmlenz
parents: 438
diff changeset
141 Translator
1a29617a5d87 Ported [611:614] to 0.4.x branch.
cmlenz
parents: 438
diff changeset
142 ==========
1a29617a5d87 Ported [611:614] to 0.4.x branch.
cmlenz
parents: 438
diff changeset
143
1a29617a5d87 Ported [611:614] to 0.4.x branch.
cmlenz
parents: 438
diff changeset
144 The ``genshi.filters.i18n.Translator`` filter implements basic support for
1a29617a5d87 Ported [611:614] to 0.4.x branch.
cmlenz
parents: 438
diff changeset
145 internationalizing and localizing templates. When used as a filter, it
1a29617a5d87 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
1a29617a5d87 Ported [611:614] to 0.4.x branch.
cmlenz
parents: 438
diff changeset
147 ``gettext``-style translation function.
1a29617a5d87 Ported [611:614] to 0.4.x branch.
cmlenz
parents: 438
diff changeset
148
1a29617a5d87 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
1a29617a5d87 Ported [611:614] to 0.4.x branch.
cmlenz
parents: 438
diff changeset
150 be used to extract localizable messages from a template.
1a29617a5d87 Ported [611:614] to 0.4.x branch.
cmlenz
parents: 438
diff changeset
151
1a29617a5d87 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.
1a29617a5d87 Ported [611:614] to 0.4.x branch.
cmlenz
parents: 438
diff changeset
153
1a29617a5d87 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