|
1 |
| -import urllib |
| 1 | +"""Helpers for inlining extracts of documents in tests.""" |
2 | 2 |
|
| 3 | +import urllib |
3 | 4 |
|
4 |
| -def inline(doc, |
5 |
| - doctype="html", |
6 |
| - mime="text/html;charset=utf-8", |
7 |
| - **kwargs): |
8 |
| - from .fixtures import server_config, url |
9 |
| - build_url = url(server_config()) |
10 | 5 |
|
11 |
| - if doctype == "html": |
12 |
| - mime = "text/html;charset=utf-8" |
13 |
| - elif doctype == "xhtml": |
14 |
| - mime = "application/xhtml+xml" |
15 |
| - doc = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" |
| 6 | +BOILERPLATES = { |
| 7 | + "html": "<!doctype html>\n<meta charset={charset}>\n{src}", |
| 8 | + "xhtml": """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" |
16 | 9 | "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
17 | 10 | <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
18 | 11 | <head>
|
19 | 12 | <title>XHTML might be the future</title>
|
20 | 13 | </head>
|
21 | 14 |
|
22 | 15 | <body>
|
23 |
| - {} |
| 16 | + {src} |
24 | 17 | </body>
|
25 |
| -</html>""".format(doc) |
26 |
| - elif doctype == "xml": |
27 |
| - mime = "text/xml" |
28 |
| - doc = """<?xml version="1.0" encoding="UTF-8"?>{}""".format(doc) |
| 18 | +</html>""", |
| 19 | + "xml": """<?xml version="1.0" encoding="{charset}"?>\n{src}""", |
| 20 | +} |
| 21 | +MIME_TYPES = { |
| 22 | + "html": "text/html", |
| 23 | + "xhtml": "application/xhtml+xml", |
| 24 | + "xml": "text/xml", |
| 25 | +} |
29 | 26 |
|
30 |
| - query = {"doc": doc} |
31 |
| - if mime != "text/html;charset=utf8": |
32 |
| - query["content-type"] = mime |
| 27 | +def inline(src, doctype="html", mime=None, charset=None, **kwargs): |
| 28 | + """ |
| 29 | + Takes a source extract and produces well-formed documents. |
33 | 30 |
|
34 |
| - return build_url("/webdriver/tests/support/inline.py", |
35 |
| - query=urllib.urlencode(query), |
36 |
| - **kwargs) |
| 31 | + Based on the desired document type, the extract is embedded with |
| 32 | + predefined boilerplate in order to produce well-formed documents. |
| 33 | + The media type and character set may also be individually configured. |
37 | 34 |
|
| 35 | + This helper function originally used data URLs, but since these |
| 36 | + are not universally supported (or indeed standardised!) across |
| 37 | + browsers, it now delegates the serving of the document to wptserve. |
| 38 | + This file also acts as a wptserve handler (see the main function |
| 39 | + below) which configures the HTTP response using query parameters. |
| 40 | +
|
| 41 | + This function returns a URL to the wptserve handler, which in turn |
| 42 | + will serve an HTTP response with the requested source extract |
| 43 | + inlined in a well-formed document, and the Content-Type header |
| 44 | + optionally configured using the desired media type and character set. |
| 45 | +
|
| 46 | + Any additional keyword arguments are passed on to the build_url |
| 47 | + function. |
| 48 | + """ |
| 49 | + from .fixtures import server_config, url |
| 50 | + build_url = url(server_config()) |
38 | 51 |
|
39 |
| -def iframe(doc, **kwargs): |
40 |
| - return "<iframe src='%s'></iframe>" % inline(doc, **kwargs) |
| 52 | + if mime is None: |
| 53 | + mime = MIME_TYPES[doctype] |
| 54 | + if charset is None: |
| 55 | + charset = "UTF-8" |
| 56 | + doc = BOILERPLATES[doctype].format(charset=charset, src=src) |
| 57 | + |
| 58 | + query = {"doc": doc, "mime": mime, "charset": charset} |
| 59 | + return build_url( |
| 60 | + "/webdriver/tests/support/inline.py", |
| 61 | + query=urllib.urlencode(query), |
| 62 | + **kwargs) |
| 63 | + |
| 64 | + |
| 65 | +def iframe(src, **kwargs): |
| 66 | + """Inlines document extract as the source document of an <iframe>.""" |
| 67 | + return "<iframe src='{}'></iframe>".format(inline(src, **kwargs)) |
41 | 68 |
|
42 | 69 |
|
43 | 70 | def main(request, response):
|
44 | 71 | doc = request.GET.first("doc", None)
|
45 |
| - content_type = request.GET.first("content-type", "text/html;charset=utf8") |
| 72 | + mime = request.GET.first("mime", None) |
| 73 | + charset = request.GET.first("charset", None) |
| 74 | + |
46 | 75 | if doc is None:
|
47 |
| - rv = 404, [("Content-Type", "text/plain")], "Missing doc parameter in query" |
48 |
| - else: |
49 |
| - response.headers.update([ |
50 |
| - ("Content-Type", content_type), |
51 |
| - ("X-XSS-Protection", "0") |
52 |
| - ]) |
53 |
| - rv = doc |
54 |
| - return rv |
| 76 | + return 404, [("Content-Type", |
| 77 | + "text/plain")], "Missing doc parameter in query" |
| 78 | + |
| 79 | + content_type = [] |
| 80 | + if mime is not None: |
| 81 | + content_type.append(mime) |
| 82 | + if charset is not None: |
| 83 | + content_type.append("charset={}".format(charset)) |
| 84 | + |
| 85 | + headers = {"X-XSS-Protection": "0"} |
| 86 | + if len(content_type) > 0: |
| 87 | + headers["Content-Type"] = ";".join(content_type) |
| 88 | + |
| 89 | + return 200, headers.items(), doc |
0 commit comments