forked from jaraco/cssutils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.html
More file actions
455 lines (431 loc) · 27.5 KB
/
parse.html
File metadata and controls
455 lines (431 loc) · 27.5 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>parsing CSS — cssutils 0.9.8 documentation</title>
<link rel="stylesheet" href="../_static/default.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../',
VERSION: '0.9.8',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<link rel="top" title="cssutils 0.9.8 documentation" href="../index.html" />
<link rel="next" title="utilities" href="utilities.html" />
<link rel="prev" title="version migration" href="migrate.html" />
</head>
<body>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="utilities.html" title="utilities"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="migrate.html" title="version migration"
accesskey="P">previous</a> |</li>
<li><a href="../index.html">cssutils 0.9.8 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<span class="target" id="module-cssutils.parse"></span><div class="section" id="parsing-css">
<h1>parsing CSS<a class="headerlink" href="#parsing-css" title="Permalink to this headline">¶</a></h1>
<p>Options to parse a given stylesheet: Get an instance of <a class="reference internal" href="#cssutils.CSSParser" title="cssutils.CSSParser"><tt class="xref py py-class docutils literal"><span class="pre">cssutils.CSSParser</span></tt></a> and use the provided <tt class="docutils literal"><span class="pre">parse*</span></tt> methods or for simpler parsing use the <tt class="docutils literal"><span class="pre">parse*</span></tt> <a class="reference internal" href="#convienience-functions">convienience functions</a>.</p>
<div class="section" id="convienience-functions">
<h2>Convienience Functions<a class="headerlink" href="#convienience-functions" title="Permalink to this headline">¶</a></h2>
<p>Shortcuts for initializing a new <a class="reference internal" href="#cssutils.CSSParser" title="cssutils.CSSParser"><tt class="xref py py-class docutils literal"><span class="pre">cssutils.CSSParser</span></tt></a> and use its <tt class="docutils literal"><span class="pre">parse*</span></tt> methods. Parsing a stylesheet this way does not raise any exceptions if an error occurs but parses CSS as defined in the specifications. If you need advanced parser handline use <a class="reference internal" href="#cssutils.CSSParser" title="cssutils.CSSParser"><tt class="xref py py-class docutils literal"><span class="pre">cssutils.CSSParser</span></tt></a> directly.</p>
<div class="section" id="parsestring">
<h3><tt class="docutils literal"><span class="pre">parseString</span></tt><a class="headerlink" href="#parsestring" title="Permalink to this headline">¶</a></h3>
<dl class="function">
<dt id="cssutils.parseString">
<tt class="descclassname">cssutils.</tt><tt class="descname">parseString</tt><big>(</big><em>cssText</em>, <em>encoding=None</em>, <em>href=None</em>, <em>media=None</em>, <em>title=None</em>, <em>validate=None</em><big>)</big><a class="headerlink" href="#cssutils.parseString" title="Permalink to this definition">¶</a></dt>
<dd><p>Parse <cite>cssText</cite> as <a class="reference internal" href="css.html#cssutils.css.CSSStyleSheet" title="cssutils.css.CSSStyleSheet"><tt class="xref py py-class docutils literal"><span class="pre">CSSStyleSheet</span></tt></a>.
Errors may be raised (e.g. UnicodeDecodeError).</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>cssText</strong> – CSS string to parse</li>
<li><strong>encoding</strong> – If <tt class="docutils literal"><span class="pre">None</span></tt> the encoding will be read from BOM or an @charset
rule or defaults to UTF-8.
If given overrides any found encoding including the ones for
imported sheets.
It also will be used to decode <cite>cssText</cite> if given as a (byte)
string.</li>
<li><strong>href</strong> – The <tt class="docutils literal"><span class="pre">href</span></tt> attribute to assign to the parsed style sheet.
Used to resolve other urls in the parsed sheet like @import hrefs.</li>
<li><strong>media</strong> – The <tt class="docutils literal"><span class="pre">media</span></tt> attribute to assign to the parsed style sheet
(may be a MediaList, list or a string).</li>
<li><strong>title</strong> – The <tt class="docutils literal"><span class="pre">title</span></tt> attribute to assign to the parsed style sheet.</li>
<li><strong>validate</strong> – If given defines if validation is used. Uses CSSParser settings as
fallback</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last"><a class="reference internal" href="css.html#cssutils.css.CSSStyleSheet" title="cssutils.css.CSSStyleSheet"><tt class="xref py py-class docutils literal"><span class="pre">CSSStyleSheet</span></tt></a>.</p>
</td>
</tr>
</tbody>
</table>
</dd></dl>
</div>
<div class="section" id="parsefile">
<h3><tt class="docutils literal"><span class="pre">parseFile</span></tt><a class="headerlink" href="#parsefile" title="Permalink to this headline">¶</a></h3>
<dl class="function">
<dt id="cssutils.parseFile">
<tt class="descclassname">cssutils.</tt><tt class="descname">parseFile</tt><big>(</big><em>filename</em>, <em>encoding=None</em>, <em>href=None</em>, <em>media=None</em>, <em>title=None</em>, <em>validate=None</em><big>)</big><a class="headerlink" href="#cssutils.parseFile" title="Permalink to this definition">¶</a></dt>
<dd><p>Retrieve content from <cite>filename</cite> and parse it. Errors may be raised
(e.g. IOError).</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>filename</strong> – of the CSS file to parse, if no <cite>href</cite> is given filename is
converted to a (file:) URL and set as <tt class="docutils literal"><span class="pre">href</span></tt> of resulting
stylesheet.
If <cite>href</cite> is given it is set as <tt class="docutils literal"><span class="pre">sheet.href</span></tt>. Either way
<tt class="docutils literal"><span class="pre">sheet.href</span></tt> is used to resolve e.g. stylesheet imports via
@import rules.</li>
<li><strong>encoding</strong> – Value <tt class="docutils literal"><span class="pre">None</span></tt> defaults to encoding detection via BOM or an
@charset rule.
Other values override detected encoding for the sheet at
<cite>filename</cite> including any imported sheets.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last"><a class="reference internal" href="css.html#cssutils.css.CSSStyleSheet" title="cssutils.css.CSSStyleSheet"><tt class="xref py py-class docutils literal"><span class="pre">CSSStyleSheet</span></tt></a>.</p>
</td>
</tr>
</tbody>
</table>
</dd></dl>
</div>
<div class="section" id="parseurl">
<h3><tt class="docutils literal"><span class="pre">parseUrl</span></tt><a class="headerlink" href="#parseurl" title="Permalink to this headline">¶</a></h3>
<dl class="function">
<dt id="cssutils.parseUrl">
<tt class="descclassname">cssutils.</tt><tt class="descname">parseUrl</tt><big>(</big><em>href</em>, <em>encoding=None</em>, <em>media=None</em>, <em>title=None</em>, <em>validate=None</em><big>)</big><a class="headerlink" href="#cssutils.parseUrl" title="Permalink to this definition">¶</a></dt>
<dd><p>Retrieve content from URL <cite>href</cite> and parse it. Errors may be raised
(e.g. URLError).</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>href</strong> – URL of the CSS file to parse, will also be set as <tt class="docutils literal"><span class="pre">href</span></tt> of
resulting stylesheet</li>
<li><strong>encoding</strong> – Value <tt class="docutils literal"><span class="pre">None</span></tt> defaults to encoding detection via HTTP, BOM or an
@charset rule.
A value overrides detected encoding for the sheet at <tt class="docutils literal"><span class="pre">href</span></tt>
including any imported sheets.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last"><a class="reference internal" href="css.html#cssutils.css.CSSStyleSheet" title="cssutils.css.CSSStyleSheet"><tt class="xref py py-class docutils literal"><span class="pre">CSSStyleSheet</span></tt></a>.</p>
</td>
</tr>
</tbody>
</table>
</dd></dl>
</div>
</div>
<div class="section" id="working-with-inline-styles">
<h2>Working with inline styles<a class="headerlink" href="#working-with-inline-styles" title="Permalink to this headline">¶</a></h2>
<div class="section" id="parsestyle">
<h3><tt class="docutils literal"><span class="pre">parseStyle</span></tt><a class="headerlink" href="#parsestyle" title="Permalink to this headline">¶</a></h3>
<dl class="function">
<dt id="cssutils.parseStyle">
<tt class="descclassname">cssutils.</tt><tt class="descname">parseStyle</tt><big>(</big><em>cssText</em>, <em>encoding='utf-8'</em><big>)</big><a class="headerlink" href="#cssutils.parseStyle" title="Permalink to this definition">¶</a></dt>
<dd><p>Parse given <cite>cssText</cite> which is assumed to be the content of
a HTML style attribute.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>cssText</strong> – CSS string to parse</li>
<li><strong>encoding</strong> – It will be used to decode <cite>cssText</cite> if given as a (byte)
string.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last"><a class="reference internal" href="css.html#cssutils.css.CSSStyleDeclaration" title="cssutils.css.CSSStyleDeclaration"><tt class="xref py py-class docutils literal"><span class="pre">CSSStyleDeclaration</span></tt></a></p>
</td>
</tr>
</tbody>
</table>
</dd></dl>
</div>
</div>
<div class="section" id="cssparser">
<h2><tt class="docutils literal"><span class="pre">CSSParser</span></tt><a class="headerlink" href="#cssparser" title="Permalink to this headline">¶</a></h2>
<p>The parser is reusable.</p>
<dl class="class">
<dt id="cssutils.CSSParser">
<em class="property">class </em><tt class="descclassname">cssutils.</tt><tt class="descname">CSSParser</tt><big>(</big><em>log=None</em>, <em>loglevel=None</em>, <em>raiseExceptions=None</em>, <em>fetcher=None</em>, <em>parseComments=True</em>, <em>validate=True</em><big>)</big><a class="headerlink" href="#cssutils.CSSParser" title="Permalink to this definition">¶</a></dt>
<dd><p>Parse a CSS StyleSheet from URL, string or file and return a DOM Level 2
CSS StyleSheet object.</p>
<p>Usage:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span> <span class="o">=</span> <span class="n">CSSParser</span><span class="p">()</span>
<span class="c"># optionally</span>
<span class="n">parser</span><span class="o">.</span><span class="n">setFetcher</span><span class="p">(</span><span class="n">fetcher</span><span class="p">)</span>
<span class="n">sheet</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parseFile</span><span class="p">(</span><span class="s">'test1.css'</span><span class="p">,</span> <span class="s">'ascii'</span><span class="p">)</span>
<span class="k">print</span> <span class="n">sheet</span><span class="o">.</span><span class="n">cssText</span>
</pre></div>
</div>
<dl class="method">
<dt id="cssutils.CSSParser.parseFile">
<tt class="descname">parseFile</tt><big>(</big><em>filename</em>, <em>encoding=None</em>, <em>href=None</em>, <em>media=None</em>, <em>title=None</em>, <em>validate=None</em><big>)</big><a class="headerlink" href="#cssutils.CSSParser.parseFile" title="Permalink to this definition">¶</a></dt>
<dd><p>Retrieve content from <cite>filename</cite> and parse it. Errors may be raised
(e.g. IOError).</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>filename</strong> – of the CSS file to parse, if no <cite>href</cite> is given filename is
converted to a (file:) URL and set as <tt class="docutils literal"><span class="pre">href</span></tt> of resulting
stylesheet.
If <cite>href</cite> is given it is set as <tt class="docutils literal"><span class="pre">sheet.href</span></tt>. Either way
<tt class="docutils literal"><span class="pre">sheet.href</span></tt> is used to resolve e.g. stylesheet imports via
@import rules.</li>
<li><strong>encoding</strong> – Value <tt class="docutils literal"><span class="pre">None</span></tt> defaults to encoding detection via BOM or an
@charset rule.
Other values override detected encoding for the sheet at
<cite>filename</cite> including any imported sheets.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last"><a class="reference internal" href="css.html#cssutils.css.CSSStyleSheet" title="cssutils.css.CSSStyleSheet"><tt class="xref py py-class docutils literal"><span class="pre">CSSStyleSheet</span></tt></a>.</p>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="method">
<dt id="cssutils.CSSParser.parseString">
<tt class="descname">parseString</tt><big>(</big><em>cssText</em>, <em>encoding=None</em>, <em>href=None</em>, <em>media=None</em>, <em>title=None</em>, <em>validate=None</em><big>)</big><a class="headerlink" href="#cssutils.CSSParser.parseString" title="Permalink to this definition">¶</a></dt>
<dd><p>Parse <cite>cssText</cite> as <a class="reference internal" href="css.html#cssutils.css.CSSStyleSheet" title="cssutils.css.CSSStyleSheet"><tt class="xref py py-class docutils literal"><span class="pre">CSSStyleSheet</span></tt></a>.
Errors may be raised (e.g. UnicodeDecodeError).</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>cssText</strong> – CSS string to parse</li>
<li><strong>encoding</strong> – If <tt class="docutils literal"><span class="pre">None</span></tt> the encoding will be read from BOM or an @charset
rule or defaults to UTF-8.
If given overrides any found encoding including the ones for
imported sheets.
It also will be used to decode <cite>cssText</cite> if given as a (byte)
string.</li>
<li><strong>href</strong> – The <tt class="docutils literal"><span class="pre">href</span></tt> attribute to assign to the parsed style sheet.
Used to resolve other urls in the parsed sheet like @import hrefs.</li>
<li><strong>media</strong> – The <tt class="docutils literal"><span class="pre">media</span></tt> attribute to assign to the parsed style sheet
(may be a MediaList, list or a string).</li>
<li><strong>title</strong> – The <tt class="docutils literal"><span class="pre">title</span></tt> attribute to assign to the parsed style sheet.</li>
<li><strong>validate</strong> – If given defines if validation is used. Uses CSSParser settings as
fallback</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last"><a class="reference internal" href="css.html#cssutils.css.CSSStyleSheet" title="cssutils.css.CSSStyleSheet"><tt class="xref py py-class docutils literal"><span class="pre">CSSStyleSheet</span></tt></a>.</p>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="method">
<dt id="cssutils.CSSParser.parseStyle">
<tt class="descname">parseStyle</tt><big>(</big><em>cssText</em>, <em>encoding='utf-8'</em><big>)</big><a class="headerlink" href="#cssutils.CSSParser.parseStyle" title="Permalink to this definition">¶</a></dt>
<dd><p>Parse given <cite>cssText</cite> which is assumed to be the content of
a HTML style attribute.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>cssText</strong> – CSS string to parse</li>
<li><strong>encoding</strong> – It will be used to decode <cite>cssText</cite> if given as a (byte)
string.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last"><a class="reference internal" href="css.html#cssutils.css.CSSStyleDeclaration" title="cssutils.css.CSSStyleDeclaration"><tt class="xref py py-class docutils literal"><span class="pre">CSSStyleDeclaration</span></tt></a></p>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="method">
<dt id="cssutils.CSSParser.parseUrl">
<tt class="descname">parseUrl</tt><big>(</big><em>href</em>, <em>encoding=None</em>, <em>media=None</em>, <em>title=None</em>, <em>validate=None</em><big>)</big><a class="headerlink" href="#cssutils.CSSParser.parseUrl" title="Permalink to this definition">¶</a></dt>
<dd><p>Retrieve content from URL <cite>href</cite> and parse it. Errors may be raised
(e.g. URLError).</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>href</strong> – URL of the CSS file to parse, will also be set as <tt class="docutils literal"><span class="pre">href</span></tt> of
resulting stylesheet</li>
<li><strong>encoding</strong> – Value <tt class="docutils literal"><span class="pre">None</span></tt> defaults to encoding detection via HTTP, BOM or an
@charset rule.
A value overrides detected encoding for the sheet at <tt class="docutils literal"><span class="pre">href</span></tt>
including any imported sheets.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last"><a class="reference internal" href="css.html#cssutils.css.CSSStyleSheet" title="cssutils.css.CSSStyleSheet"><tt class="xref py py-class docutils literal"><span class="pre">CSSStyleSheet</span></tt></a>.</p>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="method">
<dt id="cssutils.CSSParser.setFetcher">
<tt class="descname">setFetcher</tt><big>(</big><em>fetcher=None</em><big>)</big><a class="headerlink" href="#cssutils.CSSParser.setFetcher" title="Permalink to this definition">¶</a></dt>
<dd><p>Replace the default URL fetch function with a custom one.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>fetcher</strong> – <p>A function which gets a single parameter</p>
<dl class="docutils">
<dt><tt class="docutils literal"><span class="pre">url</span></tt></dt>
<dd>the URL to read</dd>
</dl>
<p>and must return <tt class="docutils literal"><span class="pre">(encoding,</span> <span class="pre">content)</span></tt> where <tt class="docutils literal"><span class="pre">encoding</span></tt> is the
HTTP charset normally given via the Content-Type header (which may
simply omit the charset in which case <tt class="docutils literal"><span class="pre">encoding</span></tt> would be
<tt class="docutils literal"><span class="pre">None</span></tt>) and <tt class="docutils literal"><span class="pre">content</span></tt> being the string (or unicode) content.</p>
<p>The Mimetype should be ‘text/css’ but this has to be checked by the
fetcher itself (the default fetcher emits a warning if encountering
a different mimetype).</p>
<p>Calling <tt class="docutils literal"><span class="pre">setFetcher</span></tt> with <tt class="docutils literal"><span class="pre">fetcher=None</span></tt> resets cssutils
to use its default function.</p>
</td>
</tr>
</tbody>
</table>
</dd></dl>
</dd></dl>
<div class="section" id="the-url-fetcher">
<h3>The URL Fetcher<a class="headerlink" href="#the-url-fetcher" title="Permalink to this headline">¶</a></h3>
<p>If you want to control how imported stylesheets are read you may define a custom URL fetcher (e.g. would be needed on Google AppEngine as urllib2, which is normally used, is not available. A GAE specific fetcher is included in cssutils from 0.9.5a1 though.)</p>
<p>A custom URL fetcher may be used during parsing via <tt class="docutils literal"><span class="pre">CSSParser.setFetcher(fetcher)</span></tt> (or as an init parameter). The so customized parser is reusable. The fetcher is called when an <tt class="docutils literal"><span class="pre">@import</span></tt> rule is found and the referenced stylesheet is about to be retrieved.</p>
<p>Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">fetcher</span><span class="p">(</span><span class="n">url</span><span class="p">):</span>
<span class="k">return</span> <span class="s">'ascii'</span><span class="p">,</span> <span class="s">'/*test*/'</span>
<span class="n">parser</span> <span class="o">=</span> <span class="n">cssutils</span><span class="o">.</span><span class="n">CSSParser</span><span class="p">(</span><span class="n">fetcher</span><span class="o">=</span><span class="n">fetcher</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">parse</span><span class="o">...</span>
</pre></div>
</div>
<p>Example 2 with a fetcher returning a unicode string:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">fetcher</span><span class="p">(</span><span class="n">url</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">None</span><span class="p">,</span> <span class="s">u'/*test*/'</span>
<span class="n">parser</span> <span class="o">=</span> <span class="n">cssutils</span><span class="o">.</span><span class="n">CSSParser</span><span class="p">(</span><span class="n">fetcher</span><span class="o">=</span><span class="n">fetcher</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">parse</span><span class="o">...</span>
</pre></div>
</div>
<p>To omit parsing of imported sheets just define a fetcher like <tt class="docutils literal"><span class="pre">lambda</span> <span class="pre">url:</span> <span class="pre">None</span></tt> (A single <tt class="docutils literal"><span class="pre">None</span></tt> is sufficient but returning <tt class="docutils literal"><span class="pre">None,</span> <span class="pre">None</span></tt> would be clearer).</p>
<p>You may also define a fetcher which overrides the internal encoding for imported sheets with a fetcher that returns a (normally HTTP) encoding depending e.g on the URL.</p>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<h3><a href="../index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">parsing CSS</a><ul>
<li><a class="reference internal" href="#convienience-functions">Convienience Functions</a><ul>
<li><a class="reference internal" href="#parsestring"><tt class="docutils literal"><span class="pre">parseString</span></tt></a></li>
<li><a class="reference internal" href="#parsefile"><tt class="docutils literal"><span class="pre">parseFile</span></tt></a></li>
<li><a class="reference internal" href="#parseurl"><tt class="docutils literal"><span class="pre">parseUrl</span></tt></a></li>
</ul>
</li>
<li><a class="reference internal" href="#working-with-inline-styles">Working with inline styles</a><ul>
<li><a class="reference internal" href="#parsestyle"><tt class="docutils literal"><span class="pre">parseStyle</span></tt></a></li>
</ul>
</li>
<li><a class="reference internal" href="#cssparser"><tt class="docutils literal"><span class="pre">CSSParser</span></tt></a><ul>
<li><a class="reference internal" href="#the-url-fetcher">The URL Fetcher</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="migrate.html"
title="previous chapter">version migration</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="utilities.html"
title="next chapter">utilities</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../_sources/docs/parse.txt"
rel="nofollow">Show Source</a></li>
</ul>
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="../search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="utilities.html" title="utilities"
>next</a> |</li>
<li class="right" >
<a href="migrate.html" title="version migration"
>previous</a> |</li>
<li><a href="../index.html">cssutils 0.9.8 documentation</a> »</li>
</ul>
</div>
<div class="footer">
© Copyright 2004-2011, Christof Höke.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.2.
</div>
</body>
</html>