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