Commit | Line | Data |
---|---|---|
7f254ad8 AE |
1 | <?php\r |
2 | \r | |
3 | class HTMLPurifier_DoctypeRegistry\r | |
4 | {\r | |
5 | \r | |
6 | /**\r | |
7 | * Hash of doctype names to doctype objects\r | |
8 | */\r | |
9 | protected $doctypes;\r | |
10 | \r | |
11 | /**\r | |
12 | * Lookup table of aliases to real doctype names\r | |
13 | */\r | |
14 | protected $aliases;\r | |
15 | \r | |
16 | /**\r | |
17 | * Registers a doctype to the registry\r | |
18 | * @note Accepts a fully-formed doctype object, or the\r | |
19 | * parameters for constructing a doctype object\r | |
20 | * @param $doctype Name of doctype or literal doctype object\r | |
21 | * @param $modules Modules doctype will load\r | |
22 | * @param $modules_for_modes Modules doctype will load for certain modes\r | |
23 | * @param $aliases Alias names for doctype\r | |
24 | * @return Editable registered doctype\r | |
25 | */\r | |
26 | public function register($doctype, $xml = true, $modules = array(),\r | |
27 | $tidy_modules = array(), $aliases = array(), $dtd_public = null, $dtd_system = null\r | |
28 | ) {\r | |
29 | if (!is_array($modules)) $modules = array($modules);\r | |
30 | if (!is_array($tidy_modules)) $tidy_modules = array($tidy_modules);\r | |
31 | if (!is_array($aliases)) $aliases = array($aliases);\r | |
32 | if (!is_object($doctype)) {\r | |
33 | $doctype = new HTMLPurifier_Doctype(\r | |
34 | $doctype, $xml, $modules, $tidy_modules, $aliases, $dtd_public, $dtd_system\r | |
35 | );\r | |
36 | }\r | |
37 | $this->doctypes[$doctype->name] = $doctype;\r | |
38 | $name = $doctype->name;\r | |
39 | // hookup aliases\r | |
40 | foreach ($doctype->aliases as $alias) {\r | |
41 | if (isset($this->doctypes[$alias])) continue;\r | |
42 | $this->aliases[$alias] = $name;\r | |
43 | }\r | |
44 | // remove old aliases\r | |
45 | if (isset($this->aliases[$name])) unset($this->aliases[$name]);\r | |
46 | return $doctype;\r | |
47 | }\r | |
48 | \r | |
49 | /**\r | |
50 | * Retrieves reference to a doctype of a certain name\r | |
51 | * @note This function resolves aliases\r | |
52 | * @note When possible, use the more fully-featured make()\r | |
53 | * @param $doctype Name of doctype\r | |
54 | * @return Editable doctype object\r | |
55 | */\r | |
56 | public function get($doctype) {\r | |
57 | if (isset($this->aliases[$doctype])) $doctype = $this->aliases[$doctype];\r | |
58 | if (!isset($this->doctypes[$doctype])) {\r | |
59 | trigger_error('Doctype ' . htmlspecialchars($doctype) . ' does not exist', E_USER_ERROR);\r | |
60 | $anon = new HTMLPurifier_Doctype($doctype);\r | |
61 | return $anon;\r | |
62 | }\r | |
63 | return $this->doctypes[$doctype];\r | |
64 | }\r | |
65 | \r | |
66 | /**\r | |
67 | * Creates a doctype based on a configuration object,\r | |
68 | * will perform initialization on the doctype\r | |
69 | * @note Use this function to get a copy of doctype that config\r | |
70 | * can hold on to (this is necessary in order to tell\r | |
71 | * Generator whether or not the current document is XML\r | |
72 | * based or not).\r | |
73 | */\r | |
74 | public function make($config) {\r | |
75 | return clone $this->get($this->getDoctypeFromConfig($config));\r | |
76 | }\r | |
77 | \r | |
78 | /**\r | |
79 | * Retrieves the doctype from the configuration object\r | |
80 | */\r | |
81 | public function getDoctypeFromConfig($config) {\r | |
82 | // recommended test\r | |
83 | $doctype = $config->get('HTML.Doctype');\r | |
84 | if (!empty($doctype)) return $doctype;\r | |
85 | $doctype = $config->get('HTML.CustomDoctype');\r | |
86 | if (!empty($doctype)) return $doctype;\r | |
87 | // backwards-compatibility\r | |
88 | if ($config->get('HTML.XHTML')) {\r | |
89 | $doctype = 'XHTML 1.0';\r | |
90 | } else {\r | |
91 | $doctype = 'HTML 4.01';\r | |
92 | }\r | |
93 | if ($config->get('HTML.Strict')) {\r | |
94 | $doctype .= ' Strict';\r | |
95 | } else {\r | |
96 | $doctype .= ' Transitional';\r | |
97 | }\r | |
98 | return $doctype;\r | |
99 | }\r | |
100 | \r | |
101 | }\r | |
102 | \r | |
103 | // vim: et sw=4 sts=4\r |