annotate doc/filters.txt @ 438:2c38ec4e2dff trunk

Added documentation page on the builtin stream filters.
author cmlenz
date Mon, 02 Apr 2007 18:21:03 +0000
parents
children 1cc1c38c6d6d 26cf27d4f2b3 9755836bb396
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
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
28 values of those controls. For example::
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
29
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
30 >>> from genshi.filters import HTMLFormFiller
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
31 >>> from genshi.template import MarkupTemplate
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
32 >>> template = MarkupTemplate("""<form>
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
33 ... <p>
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
34 ... <label>User name:
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
35 ... <input type="text" name="username" />
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
36 ... </label><br />
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
37 ... <label>Password:
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
38 ... <input type="password" name="password" />
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
39 ... </label><br />
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
40 ... <label>
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
41 ... <input type="checkbox" name="remember" /> Remember me
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 ... </p>
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
44 ... </form>""")
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
45 >>> filler = HTMLFormFiller(data=dict(username='john', remember=True))
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
46 >>> print template.generate() | filler
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
47 <form>
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
48 <p>
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
49 <label>User name:
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
50 <input type="text" name="username" value="john"/>
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
51 </label><br/>
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
52 <label>Password:
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
53 <input type="password" name="password"/>
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
54 </label><br/>
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
55 <label>
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
56 <input type="checkbox" name="remember" checked="checked"/> Remember me
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 </p>
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
59 </form>
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
60
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
61 .. 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
62 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
63 generated but *before* that output is actually serialized.
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
64
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
65 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
66 ``<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
67 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
68 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
69 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
70 dictionary needs to match the ``value`` attribute of the ``<option>`` element,
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
71 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
72 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
73 for security reasons.
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
74
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
75 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
76 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
77 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
78 attempt any conversion or not produce the desired results.
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
79
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
80 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
81 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
82 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
83 an attribute matching the specified value.
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
84
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
85
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
86 HTML Sanitizer
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
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
89 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
90 user-submitted HTML markup, removing potentially dangerous constructs that could
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
91 be used for various kinds of abuse, such as cross-site scripting (XSS) attacks::
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
92
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
93 >>> from genshi.filters import HTMLSanitizer
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
94 >>> from genshi.input import HTML
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
95 >>> html = HTML("""<div>
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
96 ... <p>Innocent looking text.</p>
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
97 ... <script>alert("Danger: " + document.cookie)</script>
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
98 ... </div>""")
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
99 >>> sanitize = HTMLSanitizer()
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
100 >>> print html | sanitize
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
101 <div>
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
102 <p>Innocent looking text.</p>
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
103 </div>
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
104
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
105 In this example, the ``<script>`` tag was removed from the output.
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
106
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
107 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
108 the filter with corresponding sets. See the API documentation for more
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
109 information.
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 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
112 filter will still perform sanitization on the contents any encountered inline
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
113 styles: the proprietary ``expression()`` function (supported only by Internet
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
114 Explorer) is removed, and any property using an ``url()`` which a potentially
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
115 dangerous URL scheme (such as ``javascript:``) are also stripped out::
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
116
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
117 >>> from genshi.filters import HTMLSanitizer
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
118 >>> from genshi.input import HTML
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
119 >>> html = HTML("""<div>
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
120 ... <br style="background: url(javascript:alert(document.cookie); color: #000" />
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
121 ... </div>""")
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
122 >>> sanitize = HTMLSanitizer(safe_attrs=HTMLSanitizer.SAFE_ATTRS | set(['style']))
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
123 >>> print html | sanitize
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
124 <div>
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
125 <br style="color: #000"/>
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
126 </div>
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
127
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
128 .. warning:: You should probably not rely on the ``style`` filtering, as
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
129 sanitizing mixed HTML, CSS, and Javascript is very complicated and
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
130 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
131 not allowing inline styles in user-submitted content, that would
2c38ec4e2dff Added documentation page on the builtin stream filters.
cmlenz
parents:
diff changeset
132 definitely be the safer route to follow.
Copyright (C) 2012-2017 Edgewall Software