@@ -22,24 +22,24 @@ |
||
| 22 | 22 | class HTMLPurifier_Filter |
| 23 | 23 | { |
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * Name of the filter for identification purposes |
|
| 27 | - */ |
|
| 28 | - public $name; |
|
| 25 | + /** |
|
| 26 | + * Name of the filter for identification purposes |
|
| 27 | + */ |
|
| 28 | + public $name; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Pre-processor function, handles HTML before HTML Purifier |
|
| 32 | - */ |
|
| 33 | - public function preFilter($html, $config, $context) { |
|
| 34 | - return $html; |
|
| 35 | - } |
|
| 30 | + /** |
|
| 31 | + * Pre-processor function, handles HTML before HTML Purifier |
|
| 32 | + */ |
|
| 33 | + public function preFilter($html, $config, $context) { |
|
| 34 | + return $html; |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Post-processor function, handles HTML after HTML Purifier |
|
| 39 | - */ |
|
| 40 | - public function postFilter($html, $config, $context) { |
|
| 41 | - return $html; |
|
| 42 | - } |
|
| 37 | + /** |
|
| 38 | + * Post-processor function, handles HTML after HTML Purifier |
|
| 39 | + */ |
|
| 40 | + public function postFilter($html, $config, $context) { |
|
| 41 | + return $html; |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | 44 | } |
| 45 | 45 | |
@@ -23,265 +23,265 @@ |
||
| 23 | 23 | class HTMLPurifier_Filter_ExtractStyleBlocks extends HTMLPurifier_Filter |
| 24 | 24 | { |
| 25 | 25 | |
| 26 | - public $name = 'ExtractStyleBlocks'; |
|
| 27 | - private $_styleMatches = array(); |
|
| 28 | - private $_tidy; |
|
| 26 | + public $name = 'ExtractStyleBlocks'; |
|
| 27 | + private $_styleMatches = array(); |
|
| 28 | + private $_tidy; |
|
| 29 | 29 | |
| 30 | - private $_id_attrdef; |
|
| 31 | - private $_class_attrdef; |
|
| 32 | - private $_enum_attrdef; |
|
| 30 | + private $_id_attrdef; |
|
| 31 | + private $_class_attrdef; |
|
| 32 | + private $_enum_attrdef; |
|
| 33 | 33 | |
| 34 | - public function __construct() { |
|
| 35 | - $this->_tidy = new csstidy(); |
|
| 36 | - $this->_id_attrdef = new HTMLPurifier_AttrDef_HTML_ID(true); |
|
| 37 | - $this->_class_attrdef = new HTMLPurifier_AttrDef_CSS_Ident(); |
|
| 38 | - $this->_enum_attrdef = new HTMLPurifier_AttrDef_Enum(array('first-child', 'link', 'visited', 'active', 'hover', 'focus')); |
|
| 39 | - } |
|
| 34 | + public function __construct() { |
|
| 35 | + $this->_tidy = new csstidy(); |
|
| 36 | + $this->_id_attrdef = new HTMLPurifier_AttrDef_HTML_ID(true); |
|
| 37 | + $this->_class_attrdef = new HTMLPurifier_AttrDef_CSS_Ident(); |
|
| 38 | + $this->_enum_attrdef = new HTMLPurifier_AttrDef_Enum(array('first-child', 'link', 'visited', 'active', 'hover', 'focus')); |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Save the contents of CSS blocks to style matches |
|
| 43 | - * @param $matches preg_replace style $matches array |
|
| 44 | - */ |
|
| 45 | - protected function styleCallback($matches) { |
|
| 46 | - $this->_styleMatches[] = $matches[1]; |
|
| 47 | - } |
|
| 41 | + /** |
|
| 42 | + * Save the contents of CSS blocks to style matches |
|
| 43 | + * @param $matches preg_replace style $matches array |
|
| 44 | + */ |
|
| 45 | + protected function styleCallback($matches) { |
|
| 46 | + $this->_styleMatches[] = $matches[1]; |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * Removes inline <style> tags from HTML, saves them for later use |
|
| 51 | - * @todo Extend to indicate non-text/css style blocks |
|
| 52 | - */ |
|
| 53 | - public function preFilter($html, $config, $context) { |
|
| 54 | - $tidy = $config->get('Filter.ExtractStyleBlocks.TidyImpl'); |
|
| 55 | - if ($tidy !== null) $this->_tidy = $tidy; |
|
| 56 | - $html = preg_replace_callback('#<style(?:\s.*)?>(.+)</style>#isU', array($this, 'styleCallback'), $html); |
|
| 57 | - $style_blocks = $this->_styleMatches; |
|
| 58 | - $this->_styleMatches = array(); // reset |
|
| 59 | - $context->register('StyleBlocks', $style_blocks); // $context must not be reused |
|
| 60 | - if ($this->_tidy) { |
|
| 61 | - foreach ($style_blocks as &$style) { |
|
| 62 | - $style = $this->cleanCSS($style, $config, $context); |
|
| 63 | - } |
|
| 64 | - } |
|
| 65 | - return $html; |
|
| 66 | - } |
|
| 49 | + /** |
|
| 50 | + * Removes inline <style> tags from HTML, saves them for later use |
|
| 51 | + * @todo Extend to indicate non-text/css style blocks |
|
| 52 | + */ |
|
| 53 | + public function preFilter($html, $config, $context) { |
|
| 54 | + $tidy = $config->get('Filter.ExtractStyleBlocks.TidyImpl'); |
|
| 55 | + if ($tidy !== null) $this->_tidy = $tidy; |
|
| 56 | + $html = preg_replace_callback('#<style(?:\s.*)?>(.+)</style>#isU', array($this, 'styleCallback'), $html); |
|
| 57 | + $style_blocks = $this->_styleMatches; |
|
| 58 | + $this->_styleMatches = array(); // reset |
|
| 59 | + $context->register('StyleBlocks', $style_blocks); // $context must not be reused |
|
| 60 | + if ($this->_tidy) { |
|
| 61 | + foreach ($style_blocks as &$style) { |
|
| 62 | + $style = $this->cleanCSS($style, $config, $context); |
|
| 63 | + } |
|
| 64 | + } |
|
| 65 | + return $html; |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - /** |
|
| 69 | - * Takes CSS (the stuff found in <style>) and cleans it. |
|
| 70 | - * @warning Requires CSSTidy <http://csstidy.sourceforge.net/> |
|
| 71 | - * @param $css CSS styling to clean |
|
| 72 | - * @param $config Instance of HTMLPurifier_Config |
|
| 73 | - * @param $context Instance of HTMLPurifier_Context |
|
| 74 | - * @return Cleaned CSS |
|
| 75 | - */ |
|
| 76 | - public function cleanCSS($css, $config, $context) { |
|
| 77 | - // prepare scope |
|
| 78 | - $scope = $config->get('Filter.ExtractStyleBlocks.Scope'); |
|
| 79 | - if ($scope !== null) { |
|
| 80 | - $scopes = array_map('trim', explode(',', $scope)); |
|
| 81 | - } else { |
|
| 82 | - $scopes = array(); |
|
| 83 | - } |
|
| 84 | - // remove comments from CSS |
|
| 85 | - $css = trim($css); |
|
| 86 | - if (strncmp('<!--', $css, 4) === 0) { |
|
| 87 | - $css = substr($css, 4); |
|
| 88 | - } |
|
| 89 | - if (strlen($css) > 3 && substr($css, -3) == '-->') { |
|
| 90 | - $css = substr($css, 0, -3); |
|
| 91 | - } |
|
| 92 | - $css = trim($css); |
|
| 93 | - set_error_handler('htmlpurifier_filter_extractstyleblocks_muteerrorhandler'); |
|
| 94 | - $this->_tidy->parse($css); |
|
| 95 | - restore_error_handler(); |
|
| 96 | - $css_definition = $config->getDefinition('CSS'); |
|
| 97 | - $html_definition = $config->getDefinition('HTML'); |
|
| 98 | - $new_css = array(); |
|
| 99 | - foreach ($this->_tidy->css as $k => $decls) { |
|
| 100 | - // $decls are all CSS declarations inside an @ selector |
|
| 101 | - $new_decls = array(); |
|
| 102 | - foreach ($decls as $selector => $style) { |
|
| 103 | - $selector = trim($selector); |
|
| 104 | - if ($selector === '') continue; // should not happen |
|
| 105 | - // Parse the selector |
|
| 106 | - // Here is the relevant part of the CSS grammar: |
|
| 107 | - // |
|
| 108 | - // ruleset |
|
| 109 | - // : selector [ ',' S* selector ]* '{' ... |
|
| 110 | - // selector |
|
| 111 | - // : simple_selector [ combinator selector | S+ [ combinator? selector ]? ]? |
|
| 112 | - // combinator |
|
| 113 | - // : '+' S* |
|
| 114 | - // : '>' S* |
|
| 115 | - // simple_selector |
|
| 116 | - // : element_name [ HASH | class | attrib | pseudo ]* |
|
| 117 | - // | [ HASH | class | attrib | pseudo ]+ |
|
| 118 | - // element_name |
|
| 119 | - // : IDENT | '*' |
|
| 120 | - // ; |
|
| 121 | - // class |
|
| 122 | - // : '.' IDENT |
|
| 123 | - // ; |
|
| 124 | - // attrib |
|
| 125 | - // : '[' S* IDENT S* [ [ '=' | INCLUDES | DASHMATCH ] S* |
|
| 126 | - // [ IDENT | STRING ] S* ]? ']' |
|
| 127 | - // ; |
|
| 128 | - // pseudo |
|
| 129 | - // : ':' [ IDENT | FUNCTION S* [IDENT S*]? ')' ] |
|
| 130 | - // ; |
|
| 131 | - // |
|
| 132 | - // For reference, here are the relevant tokens: |
|
| 133 | - // |
|
| 134 | - // HASH #{name} |
|
| 135 | - // IDENT {ident} |
|
| 136 | - // INCLUDES == |
|
| 137 | - // DASHMATCH |= |
|
| 138 | - // STRING {string} |
|
| 139 | - // FUNCTION {ident}\( |
|
| 140 | - // |
|
| 141 | - // And the lexical scanner tokens |
|
| 142 | - // |
|
| 143 | - // name {nmchar}+ |
|
| 144 | - // nmchar [_a-z0-9-]|{nonascii}|{escape} |
|
| 145 | - // nonascii [\240-\377] |
|
| 146 | - // escape {unicode}|\\[^\r\n\f0-9a-f] |
|
| 147 | - // unicode \\{h}}{1,6}(\r\n|[ \t\r\n\f])? |
|
| 148 | - // ident -?{nmstart}{nmchar*} |
|
| 149 | - // nmstart [_a-z]|{nonascii}|{escape} |
|
| 150 | - // string {string1}|{string2} |
|
| 151 | - // string1 \"([^\n\r\f\\"]|\\{nl}|{escape})*\" |
|
| 152 | - // string2 \'([^\n\r\f\\"]|\\{nl}|{escape})*\' |
|
| 153 | - // |
|
| 154 | - // We'll implement a subset (in order to reduce attack |
|
| 155 | - // surface); in particular: |
|
| 156 | - // |
|
| 157 | - // - No Unicode support |
|
| 158 | - // - No escapes support |
|
| 159 | - // - No string support (by proxy no attrib support) |
|
| 160 | - // - element_name is matched against allowed |
|
| 161 | - // elements (some people might find this |
|
| 162 | - // annoying...) |
|
| 163 | - // - Pseudo-elements one of :first-child, :link, |
|
| 164 | - // :visited, :active, :hover, :focus |
|
| 68 | + /** |
|
| 69 | + * Takes CSS (the stuff found in <style>) and cleans it. |
|
| 70 | + * @warning Requires CSSTidy <http://csstidy.sourceforge.net/> |
|
| 71 | + * @param $css CSS styling to clean |
|
| 72 | + * @param $config Instance of HTMLPurifier_Config |
|
| 73 | + * @param $context Instance of HTMLPurifier_Context |
|
| 74 | + * @return Cleaned CSS |
|
| 75 | + */ |
|
| 76 | + public function cleanCSS($css, $config, $context) { |
|
| 77 | + // prepare scope |
|
| 78 | + $scope = $config->get('Filter.ExtractStyleBlocks.Scope'); |
|
| 79 | + if ($scope !== null) { |
|
| 80 | + $scopes = array_map('trim', explode(',', $scope)); |
|
| 81 | + } else { |
|
| 82 | + $scopes = array(); |
|
| 83 | + } |
|
| 84 | + // remove comments from CSS |
|
| 85 | + $css = trim($css); |
|
| 86 | + if (strncmp('<!--', $css, 4) === 0) { |
|
| 87 | + $css = substr($css, 4); |
|
| 88 | + } |
|
| 89 | + if (strlen($css) > 3 && substr($css, -3) == '-->') { |
|
| 90 | + $css = substr($css, 0, -3); |
|
| 91 | + } |
|
| 92 | + $css = trim($css); |
|
| 93 | + set_error_handler('htmlpurifier_filter_extractstyleblocks_muteerrorhandler'); |
|
| 94 | + $this->_tidy->parse($css); |
|
| 95 | + restore_error_handler(); |
|
| 96 | + $css_definition = $config->getDefinition('CSS'); |
|
| 97 | + $html_definition = $config->getDefinition('HTML'); |
|
| 98 | + $new_css = array(); |
|
| 99 | + foreach ($this->_tidy->css as $k => $decls) { |
|
| 100 | + // $decls are all CSS declarations inside an @ selector |
|
| 101 | + $new_decls = array(); |
|
| 102 | + foreach ($decls as $selector => $style) { |
|
| 103 | + $selector = trim($selector); |
|
| 104 | + if ($selector === '') continue; // should not happen |
|
| 105 | + // Parse the selector |
|
| 106 | + // Here is the relevant part of the CSS grammar: |
|
| 107 | + // |
|
| 108 | + // ruleset |
|
| 109 | + // : selector [ ',' S* selector ]* '{' ... |
|
| 110 | + // selector |
|
| 111 | + // : simple_selector [ combinator selector | S+ [ combinator? selector ]? ]? |
|
| 112 | + // combinator |
|
| 113 | + // : '+' S* |
|
| 114 | + // : '>' S* |
|
| 115 | + // simple_selector |
|
| 116 | + // : element_name [ HASH | class | attrib | pseudo ]* |
|
| 117 | + // | [ HASH | class | attrib | pseudo ]+ |
|
| 118 | + // element_name |
|
| 119 | + // : IDENT | '*' |
|
| 120 | + // ; |
|
| 121 | + // class |
|
| 122 | + // : '.' IDENT |
|
| 123 | + // ; |
|
| 124 | + // attrib |
|
| 125 | + // : '[' S* IDENT S* [ [ '=' | INCLUDES | DASHMATCH ] S* |
|
| 126 | + // [ IDENT | STRING ] S* ]? ']' |
|
| 127 | + // ; |
|
| 128 | + // pseudo |
|
| 129 | + // : ':' [ IDENT | FUNCTION S* [IDENT S*]? ')' ] |
|
| 130 | + // ; |
|
| 131 | + // |
|
| 132 | + // For reference, here are the relevant tokens: |
|
| 133 | + // |
|
| 134 | + // HASH #{name} |
|
| 135 | + // IDENT {ident} |
|
| 136 | + // INCLUDES == |
|
| 137 | + // DASHMATCH |= |
|
| 138 | + // STRING {string} |
|
| 139 | + // FUNCTION {ident}\( |
|
| 140 | + // |
|
| 141 | + // And the lexical scanner tokens |
|
| 142 | + // |
|
| 143 | + // name {nmchar}+ |
|
| 144 | + // nmchar [_a-z0-9-]|{nonascii}|{escape} |
|
| 145 | + // nonascii [\240-\377] |
|
| 146 | + // escape {unicode}|\\[^\r\n\f0-9a-f] |
|
| 147 | + // unicode \\{h}}{1,6}(\r\n|[ \t\r\n\f])? |
|
| 148 | + // ident -?{nmstart}{nmchar*} |
|
| 149 | + // nmstart [_a-z]|{nonascii}|{escape} |
|
| 150 | + // string {string1}|{string2} |
|
| 151 | + // string1 \"([^\n\r\f\\"]|\\{nl}|{escape})*\" |
|
| 152 | + // string2 \'([^\n\r\f\\"]|\\{nl}|{escape})*\' |
|
| 153 | + // |
|
| 154 | + // We'll implement a subset (in order to reduce attack |
|
| 155 | + // surface); in particular: |
|
| 156 | + // |
|
| 157 | + // - No Unicode support |
|
| 158 | + // - No escapes support |
|
| 159 | + // - No string support (by proxy no attrib support) |
|
| 160 | + // - element_name is matched against allowed |
|
| 161 | + // elements (some people might find this |
|
| 162 | + // annoying...) |
|
| 163 | + // - Pseudo-elements one of :first-child, :link, |
|
| 164 | + // :visited, :active, :hover, :focus |
|
| 165 | 165 | |
| 166 | - // handle ruleset |
|
| 167 | - $selectors = array_map('trim', explode(',', $selector)); |
|
| 168 | - $new_selectors = array(); |
|
| 169 | - foreach ($selectors as $sel) { |
|
| 170 | - // split on +, > and spaces |
|
| 171 | - $basic_selectors = preg_split('/\s*([+> ])\s*/', $sel, -1, PREG_SPLIT_DELIM_CAPTURE); |
|
| 172 | - // even indices are chunks, odd indices are |
|
| 173 | - // delimiters |
|
| 174 | - $nsel = null; |
|
| 175 | - $delim = null; // guaranteed to be non-null after |
|
| 176 | - // two loop iterations |
|
| 177 | - for ($i = 0, $c = count($basic_selectors); $i < $c; $i++) { |
|
| 178 | - $x = $basic_selectors[$i]; |
|
| 179 | - if ($i % 2) { |
|
| 180 | - // delimiter |
|
| 181 | - if ($x === ' ') { |
|
| 182 | - $delim = ' '; |
|
| 183 | - } else { |
|
| 184 | - $delim = ' ' . $x . ' '; |
|
| 185 | - } |
|
| 186 | - } else { |
|
| 187 | - // simple selector |
|
| 188 | - $components = preg_split('/([#.:])/', $x, -1, PREG_SPLIT_DELIM_CAPTURE); |
|
| 189 | - $sdelim = null; |
|
| 190 | - $nx = null; |
|
| 191 | - for ($j = 0, $cc = count($components); $j < $cc; $j ++) { |
|
| 192 | - $y = $components[$j]; |
|
| 193 | - if ($j === 0) { |
|
| 194 | - if ($y === '*' || isset($html_definition->info[$y = strtolower($y)])) { |
|
| 195 | - $nx = $y; |
|
| 196 | - } else { |
|
| 197 | - // $nx stays null; this matters |
|
| 198 | - // if we don't manage to find |
|
| 199 | - // any valid selector content, |
|
| 200 | - // in which case we ignore the |
|
| 201 | - // outer $delim |
|
| 202 | - } |
|
| 203 | - } elseif ($j % 2) { |
|
| 204 | - // set delimiter |
|
| 205 | - $sdelim = $y; |
|
| 206 | - } else { |
|
| 207 | - $attrdef = null; |
|
| 208 | - if ($sdelim === '#') { |
|
| 209 | - $attrdef = $this->_id_attrdef; |
|
| 210 | - } elseif ($sdelim === '.') { |
|
| 211 | - $attrdef = $this->_class_attrdef; |
|
| 212 | - } elseif ($sdelim === ':') { |
|
| 213 | - $attrdef = $this->_enum_attrdef; |
|
| 214 | - } else { |
|
| 215 | - throw new HTMLPurifier_Exception('broken invariant sdelim and preg_split'); |
|
| 216 | - } |
|
| 217 | - $r = $attrdef->validate($y, $config, $context); |
|
| 218 | - if ($r !== false) { |
|
| 219 | - if ($r !== true) { |
|
| 220 | - $y = $r; |
|
| 221 | - } |
|
| 222 | - if ($nx === null) { |
|
| 223 | - $nx = ''; |
|
| 224 | - } |
|
| 225 | - $nx .= $sdelim . $y; |
|
| 226 | - } |
|
| 227 | - } |
|
| 228 | - } |
|
| 229 | - if ($nx !== null) { |
|
| 230 | - if ($nsel === null) { |
|
| 231 | - $nsel = $nx; |
|
| 232 | - } else { |
|
| 233 | - $nsel .= $delim . $nx; |
|
| 234 | - } |
|
| 235 | - } else { |
|
| 236 | - // delimiters to the left of invalid |
|
| 237 | - // basic selector ignored |
|
| 238 | - } |
|
| 239 | - } |
|
| 240 | - } |
|
| 241 | - if ($nsel !== null) { |
|
| 242 | - if (!empty($scopes)) { |
|
| 243 | - foreach ($scopes as $s) { |
|
| 244 | - $new_selectors[] = "$s $nsel"; |
|
| 245 | - } |
|
| 246 | - } else { |
|
| 247 | - $new_selectors[] = $nsel; |
|
| 248 | - } |
|
| 249 | - } |
|
| 250 | - } |
|
| 251 | - if (empty($new_selectors)) continue; |
|
| 252 | - $selector = implode(', ', $new_selectors); |
|
| 253 | - foreach ($style as $name => $value) { |
|
| 254 | - if (!isset($css_definition->info[$name])) { |
|
| 255 | - unset($style[$name]); |
|
| 256 | - continue; |
|
| 257 | - } |
|
| 258 | - $def = $css_definition->info[$name]; |
|
| 259 | - $ret = $def->validate($value, $config, $context); |
|
| 260 | - if ($ret === false) unset($style[$name]); |
|
| 261 | - else $style[$name] = $ret; |
|
| 262 | - } |
|
| 263 | - $new_decls[$selector] = $style; |
|
| 264 | - } |
|
| 265 | - $new_css[$k] = $new_decls; |
|
| 266 | - } |
|
| 267 | - // remove stuff that shouldn't be used, could be reenabled |
|
| 268 | - // after security risks are analyzed |
|
| 269 | - $this->_tidy->css = $new_css; |
|
| 270 | - $this->_tidy->import = array(); |
|
| 271 | - $this->_tidy->charset = null; |
|
| 272 | - $this->_tidy->namespace = null; |
|
| 273 | - $css = $this->_tidy->print->plain(); |
|
| 274 | - // we are going to escape any special characters <>& to ensure |
|
| 275 | - // that no funny business occurs (i.e. </style> in a font-family prop). |
|
| 276 | - if ($config->get('Filter.ExtractStyleBlocks.Escaping')) { |
|
| 277 | - $css = str_replace( |
|
| 278 | - array('<', '>', '&'), |
|
| 279 | - array('\3C ', '\3E ', '\26 '), |
|
| 280 | - $css |
|
| 281 | - ); |
|
| 282 | - } |
|
| 283 | - return $css; |
|
| 284 | - } |
|
| 166 | + // handle ruleset |
|
| 167 | + $selectors = array_map('trim', explode(',', $selector)); |
|
| 168 | + $new_selectors = array(); |
|
| 169 | + foreach ($selectors as $sel) { |
|
| 170 | + // split on +, > and spaces |
|
| 171 | + $basic_selectors = preg_split('/\s*([+> ])\s*/', $sel, -1, PREG_SPLIT_DELIM_CAPTURE); |
|
| 172 | + // even indices are chunks, odd indices are |
|
| 173 | + // delimiters |
|
| 174 | + $nsel = null; |
|
| 175 | + $delim = null; // guaranteed to be non-null after |
|
| 176 | + // two loop iterations |
|
| 177 | + for ($i = 0, $c = count($basic_selectors); $i < $c; $i++) { |
|
| 178 | + $x = $basic_selectors[$i]; |
|
| 179 | + if ($i % 2) { |
|
| 180 | + // delimiter |
|
| 181 | + if ($x === ' ') { |
|
| 182 | + $delim = ' '; |
|
| 183 | + } else { |
|
| 184 | + $delim = ' ' . $x . ' '; |
|
| 185 | + } |
|
| 186 | + } else { |
|
| 187 | + // simple selector |
|
| 188 | + $components = preg_split('/([#.:])/', $x, -1, PREG_SPLIT_DELIM_CAPTURE); |
|
| 189 | + $sdelim = null; |
|
| 190 | + $nx = null; |
|
| 191 | + for ($j = 0, $cc = count($components); $j < $cc; $j ++) { |
|
| 192 | + $y = $components[$j]; |
|
| 193 | + if ($j === 0) { |
|
| 194 | + if ($y === '*' || isset($html_definition->info[$y = strtolower($y)])) { |
|
| 195 | + $nx = $y; |
|
| 196 | + } else { |
|
| 197 | + // $nx stays null; this matters |
|
| 198 | + // if we don't manage to find |
|
| 199 | + // any valid selector content, |
|
| 200 | + // in which case we ignore the |
|
| 201 | + // outer $delim |
|
| 202 | + } |
|
| 203 | + } elseif ($j % 2) { |
|
| 204 | + // set delimiter |
|
| 205 | + $sdelim = $y; |
|
| 206 | + } else { |
|
| 207 | + $attrdef = null; |
|
| 208 | + if ($sdelim === '#') { |
|
| 209 | + $attrdef = $this->_id_attrdef; |
|
| 210 | + } elseif ($sdelim === '.') { |
|
| 211 | + $attrdef = $this->_class_attrdef; |
|
| 212 | + } elseif ($sdelim === ':') { |
|
| 213 | + $attrdef = $this->_enum_attrdef; |
|
| 214 | + } else { |
|
| 215 | + throw new HTMLPurifier_Exception('broken invariant sdelim and preg_split'); |
|
| 216 | + } |
|
| 217 | + $r = $attrdef->validate($y, $config, $context); |
|
| 218 | + if ($r !== false) { |
|
| 219 | + if ($r !== true) { |
|
| 220 | + $y = $r; |
|
| 221 | + } |
|
| 222 | + if ($nx === null) { |
|
| 223 | + $nx = ''; |
|
| 224 | + } |
|
| 225 | + $nx .= $sdelim . $y; |
|
| 226 | + } |
|
| 227 | + } |
|
| 228 | + } |
|
| 229 | + if ($nx !== null) { |
|
| 230 | + if ($nsel === null) { |
|
| 231 | + $nsel = $nx; |
|
| 232 | + } else { |
|
| 233 | + $nsel .= $delim . $nx; |
|
| 234 | + } |
|
| 235 | + } else { |
|
| 236 | + // delimiters to the left of invalid |
|
| 237 | + // basic selector ignored |
|
| 238 | + } |
|
| 239 | + } |
|
| 240 | + } |
|
| 241 | + if ($nsel !== null) { |
|
| 242 | + if (!empty($scopes)) { |
|
| 243 | + foreach ($scopes as $s) { |
|
| 244 | + $new_selectors[] = "$s $nsel"; |
|
| 245 | + } |
|
| 246 | + } else { |
|
| 247 | + $new_selectors[] = $nsel; |
|
| 248 | + } |
|
| 249 | + } |
|
| 250 | + } |
|
| 251 | + if (empty($new_selectors)) continue; |
|
| 252 | + $selector = implode(', ', $new_selectors); |
|
| 253 | + foreach ($style as $name => $value) { |
|
| 254 | + if (!isset($css_definition->info[$name])) { |
|
| 255 | + unset($style[$name]); |
|
| 256 | + continue; |
|
| 257 | + } |
|
| 258 | + $def = $css_definition->info[$name]; |
|
| 259 | + $ret = $def->validate($value, $config, $context); |
|
| 260 | + if ($ret === false) unset($style[$name]); |
|
| 261 | + else $style[$name] = $ret; |
|
| 262 | + } |
|
| 263 | + $new_decls[$selector] = $style; |
|
| 264 | + } |
|
| 265 | + $new_css[$k] = $new_decls; |
|
| 266 | + } |
|
| 267 | + // remove stuff that shouldn't be used, could be reenabled |
|
| 268 | + // after security risks are analyzed |
|
| 269 | + $this->_tidy->css = $new_css; |
|
| 270 | + $this->_tidy->import = array(); |
|
| 271 | + $this->_tidy->charset = null; |
|
| 272 | + $this->_tidy->namespace = null; |
|
| 273 | + $css = $this->_tidy->print->plain(); |
|
| 274 | + // we are going to escape any special characters <>& to ensure |
|
| 275 | + // that no funny business occurs (i.e. </style> in a font-family prop). |
|
| 276 | + if ($config->get('Filter.ExtractStyleBlocks.Escaping')) { |
|
| 277 | + $css = str_replace( |
|
| 278 | + array('<', '>', '&'), |
|
| 279 | + array('\3C ', '\3E ', '\26 '), |
|
| 280 | + $css |
|
| 281 | + ); |
|
| 282 | + } |
|
| 283 | + return $css; |
|
| 284 | + } |
|
| 285 | 285 | |
| 286 | 286 | } |
| 287 | 287 | |
@@ -52,7 +52,9 @@ discard block |
||
| 52 | 52 | */ |
| 53 | 53 | public function preFilter($html, $config, $context) { |
| 54 | 54 | $tidy = $config->get('Filter.ExtractStyleBlocks.TidyImpl'); |
| 55 | - if ($tidy !== null) $this->_tidy = $tidy; |
|
| 55 | + if ($tidy !== null) { |
|
| 56 | + $this->_tidy = $tidy; |
|
| 57 | + } |
|
| 56 | 58 | $html = preg_replace_callback('#<style(?:\s.*)?>(.+)</style>#isU', array($this, 'styleCallback'), $html); |
| 57 | 59 | $style_blocks = $this->_styleMatches; |
| 58 | 60 | $this->_styleMatches = array(); // reset |
@@ -101,7 +103,10 @@ discard block |
||
| 101 | 103 | $new_decls = array(); |
| 102 | 104 | foreach ($decls as $selector => $style) { |
| 103 | 105 | $selector = trim($selector); |
| 104 | - if ($selector === '') continue; // should not happen |
|
| 106 | + if ($selector === '') { |
|
| 107 | + continue; |
|
| 108 | + } |
|
| 109 | + // should not happen |
|
| 105 | 110 | // Parse the selector |
| 106 | 111 | // Here is the relevant part of the CSS grammar: |
| 107 | 112 | // |
@@ -248,7 +253,9 @@ discard block |
||
| 248 | 253 | } |
| 249 | 254 | } |
| 250 | 255 | } |
| 251 | - if (empty($new_selectors)) continue; |
|
| 256 | + if (empty($new_selectors)) { |
|
| 257 | + continue; |
|
| 258 | + } |
|
| 252 | 259 | $selector = implode(', ', $new_selectors); |
| 253 | 260 | foreach ($style as $name => $value) { |
| 254 | 261 | if (!isset($css_definition->info[$name])) { |
@@ -257,8 +264,11 @@ discard block |
||
| 257 | 264 | } |
| 258 | 265 | $def = $css_definition->info[$name]; |
| 259 | 266 | $ret = $def->validate($value, $config, $context); |
| 260 | - if ($ret === false) unset($style[$name]); |
|
| 261 | - else $style[$name] = $ret; |
|
| 267 | + if ($ret === false) { |
|
| 268 | + unset($style[$name]); |
|
| 269 | + } else { |
|
| 270 | + $style[$name] = $ret; |
|
| 271 | + } |
|
| 262 | 272 | } |
| 263 | 273 | $new_decls[$selector] = $style; |
| 264 | 274 | } |
@@ -181,14 +181,14 @@ discard block |
||
| 181 | 181 | if ($x === ' ') { |
| 182 | 182 | $delim = ' '; |
| 183 | 183 | } else { |
| 184 | - $delim = ' ' . $x . ' '; |
|
| 184 | + $delim = ' '.$x.' '; |
|
| 185 | 185 | } |
| 186 | 186 | } else { |
| 187 | 187 | // simple selector |
| 188 | 188 | $components = preg_split('/([#.:])/', $x, -1, PREG_SPLIT_DELIM_CAPTURE); |
| 189 | 189 | $sdelim = null; |
| 190 | 190 | $nx = null; |
| 191 | - for ($j = 0, $cc = count($components); $j < $cc; $j ++) { |
|
| 191 | + for ($j = 0, $cc = count($components); $j < $cc; $j++) { |
|
| 192 | 192 | $y = $components[$j]; |
| 193 | 193 | if ($j === 0) { |
| 194 | 194 | if ($y === '*' || isset($html_definition->info[$y = strtolower($y)])) { |
@@ -222,7 +222,7 @@ discard block |
||
| 222 | 222 | if ($nx === null) { |
| 223 | 223 | $nx = ''; |
| 224 | 224 | } |
| 225 | - $nx .= $sdelim . $y; |
|
| 225 | + $nx .= $sdelim.$y; |
|
| 226 | 226 | } |
| 227 | 227 | } |
| 228 | 228 | } |
@@ -230,7 +230,7 @@ discard block |
||
| 230 | 230 | if ($nsel === null) { |
| 231 | 231 | $nsel = $nx; |
| 232 | 232 | } else { |
| 233 | - $nsel .= $delim . $nx; |
|
| 233 | + $nsel .= $delim.$nx; |
|
| 234 | 234 | } |
| 235 | 235 | } else { |
| 236 | 236 | // delimiters to the left of invalid |
@@ -275,7 +275,7 @@ discard block |
||
| 275 | 275 | // that no funny business occurs (i.e. </style> in a font-family prop). |
| 276 | 276 | if ($config->get('Filter.ExtractStyleBlocks.Escaping')) { |
| 277 | 277 | $css = str_replace( |
| 278 | - array('<', '>', '&'), |
|
| 278 | + array('<', '>', '&'), |
|
| 279 | 279 | array('\3C ', '\3E ', '\26 '), |
| 280 | 280 | $css |
| 281 | 281 | ); |
@@ -3,37 +3,37 @@ |
||
| 3 | 3 | class HTMLPurifier_Filter_YouTube extends HTMLPurifier_Filter |
| 4 | 4 | { |
| 5 | 5 | |
| 6 | - public $name = 'YouTube'; |
|
| 7 | - |
|
| 8 | - public function preFilter($html, $config, $context) { |
|
| 9 | - $pre_regex = '#<object[^>]+>.+?'. |
|
| 10 | - 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s'; |
|
| 11 | - $pre_replace = '<span class="youtube-embed">\1</span>'; |
|
| 12 | - return preg_replace($pre_regex, $pre_replace, $html); |
|
| 13 | - } |
|
| 14 | - |
|
| 15 | - public function postFilter($html, $config, $context) { |
|
| 16 | - $post_regex = '#<span class="youtube-embed">((?:v|cp)/[A-Za-z0-9\-_=]+)</span>#'; |
|
| 17 | - return preg_replace_callback($post_regex, array($this, 'postFilterCallback'), $html); |
|
| 18 | - } |
|
| 19 | - |
|
| 20 | - protected function armorUrl($url) { |
|
| 21 | - return str_replace('--', '--', $url); |
|
| 22 | - } |
|
| 23 | - |
|
| 24 | - protected function postFilterCallback($matches) { |
|
| 25 | - $url = $this->armorUrl($matches[1]); |
|
| 26 | - return '<object width="425" height="350" type="application/x-shockwave-flash" '. |
|
| 27 | - 'data="http://www.youtube.com/'.$url.'">'. |
|
| 28 | - '<param name="movie" value="http://www.youtube.com/'.$url.'"></param>'. |
|
| 29 | - '<!--[if IE]>'. |
|
| 30 | - '<embed src="http://www.youtube.com/'.$url.'"'. |
|
| 31 | - 'type="application/x-shockwave-flash"'. |
|
| 32 | - 'wmode="transparent" width="425" height="350" />'. |
|
| 33 | - '<![endif]-->'. |
|
| 34 | - '</object>'; |
|
| 35 | - |
|
| 36 | - } |
|
| 6 | + public $name = 'YouTube'; |
|
| 7 | + |
|
| 8 | + public function preFilter($html, $config, $context) { |
|
| 9 | + $pre_regex = '#<object[^>]+>.+?'. |
|
| 10 | + 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s'; |
|
| 11 | + $pre_replace = '<span class="youtube-embed">\1</span>'; |
|
| 12 | + return preg_replace($pre_regex, $pre_replace, $html); |
|
| 13 | + } |
|
| 14 | + |
|
| 15 | + public function postFilter($html, $config, $context) { |
|
| 16 | + $post_regex = '#<span class="youtube-embed">((?:v|cp)/[A-Za-z0-9\-_=]+)</span>#'; |
|
| 17 | + return preg_replace_callback($post_regex, array($this, 'postFilterCallback'), $html); |
|
| 18 | + } |
|
| 19 | + |
|
| 20 | + protected function armorUrl($url) { |
|
| 21 | + return str_replace('--', '--', $url); |
|
| 22 | + } |
|
| 23 | + |
|
| 24 | + protected function postFilterCallback($matches) { |
|
| 25 | + $url = $this->armorUrl($matches[1]); |
|
| 26 | + return '<object width="425" height="350" type="application/x-shockwave-flash" '. |
|
| 27 | + 'data="http://www.youtube.com/'.$url.'">'. |
|
| 28 | + '<param name="movie" value="http://www.youtube.com/'.$url.'"></param>'. |
|
| 29 | + '<!--[if IE]>'. |
|
| 30 | + '<embed src="http://www.youtube.com/'.$url.'"'. |
|
| 31 | + 'type="application/x-shockwave-flash"'. |
|
| 32 | + 'wmode="transparent" width="425" height="350" />'. |
|
| 33 | + '<![endif]-->'. |
|
| 34 | + '</object>'; |
|
| 35 | + |
|
| 36 | + } |
|
| 37 | 37 | } |
| 38 | 38 | |
| 39 | 39 | // vim: et sw=4 sts=4 |
@@ -18,226 +18,226 @@ |
||
| 18 | 18 | class HTMLPurifier_HTMLModule |
| 19 | 19 | { |
| 20 | 20 | |
| 21 | - // -- Overloadable ---------------------------------------------------- |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * Short unique string identifier of the module |
|
| 25 | - */ |
|
| 26 | - public $name; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * Informally, a list of elements this module changes. Not used in |
|
| 30 | - * any significant way. |
|
| 31 | - */ |
|
| 32 | - public $elements = array(); |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * Associative array of element names to element definitions. |
|
| 36 | - * Some definitions may be incomplete, to be merged in later |
|
| 37 | - * with the full definition. |
|
| 38 | - */ |
|
| 39 | - public $info = array(); |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * Associative array of content set names to content set additions. |
|
| 43 | - * This is commonly used to, say, add an A element to the Inline |
|
| 44 | - * content set. This corresponds to an internal variable $content_sets |
|
| 45 | - * and NOT info_content_sets member variable of HTMLDefinition. |
|
| 46 | - */ |
|
| 47 | - public $content_sets = array(); |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Associative array of attribute collection names to attribute |
|
| 51 | - * collection additions. More rarely used for adding attributes to |
|
| 52 | - * the global collections. Example is the StyleAttribute module adding |
|
| 53 | - * the style attribute to the Core. Corresponds to HTMLDefinition's |
|
| 54 | - * attr_collections->info, since the object's data is only info, |
|
| 55 | - * with extra behavior associated with it. |
|
| 56 | - */ |
|
| 57 | - public $attr_collections = array(); |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Associative array of deprecated tag name to HTMLPurifier_TagTransform |
|
| 61 | - */ |
|
| 62 | - public $info_tag_transform = array(); |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * List of HTMLPurifier_AttrTransform to be performed before validation. |
|
| 66 | - */ |
|
| 67 | - public $info_attr_transform_pre = array(); |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * List of HTMLPurifier_AttrTransform to be performed after validation. |
|
| 71 | - */ |
|
| 72 | - public $info_attr_transform_post = array(); |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * List of HTMLPurifier_Injector to be performed during well-formedness fixing. |
|
| 76 | - * An injector will only be invoked if all of it's pre-requisites are met; |
|
| 77 | - * if an injector fails setup, there will be no error; it will simply be |
|
| 78 | - * silently disabled. |
|
| 79 | - */ |
|
| 80 | - public $info_injector = array(); |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * Boolean flag that indicates whether or not getChildDef is implemented. |
|
| 84 | - * For optimization reasons: may save a call to a function. Be sure |
|
| 85 | - * to set it if you do implement getChildDef(), otherwise it will have |
|
| 86 | - * no effect! |
|
| 87 | - */ |
|
| 88 | - public $defines_child_def = false; |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Boolean flag whether or not this module is safe. If it is not safe, all |
|
| 92 | - * of its members are unsafe. Modules are safe by default (this might be |
|
| 93 | - * slightly dangerous, but it doesn't make much sense to force HTML Purifier, |
|
| 94 | - * which is based off of safe HTML, to explicitly say, "This is safe," even |
|
| 95 | - * though there are modules which are "unsafe") |
|
| 96 | - * |
|
| 97 | - * @note Previously, safety could be applied at an element level granularity. |
|
| 98 | - * We've removed this ability, so in order to add "unsafe" elements |
|
| 99 | - * or attributes, a dedicated module with this property set to false |
|
| 100 | - * must be used. |
|
| 101 | - */ |
|
| 102 | - public $safe = true; |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * Retrieves a proper HTMLPurifier_ChildDef subclass based on |
|
| 106 | - * content_model and content_model_type member variables of |
|
| 107 | - * the HTMLPurifier_ElementDef class. There is a similar function |
|
| 108 | - * in HTMLPurifier_HTMLDefinition. |
|
| 109 | - * @param $def HTMLPurifier_ElementDef instance |
|
| 110 | - * @return HTMLPurifier_ChildDef subclass |
|
| 111 | - */ |
|
| 112 | - public function getChildDef($def) {return false;} |
|
| 113 | - |
|
| 114 | - // -- Convenience ----------------------------------------------------- |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * Convenience function that sets up a new element |
|
| 118 | - * @param $element Name of element to add |
|
| 119 | - * @param $type What content set should element be registered to? |
|
| 120 | - * Set as false to skip this step. |
|
| 121 | - * @param $contents Allowed children in form of: |
|
| 122 | - * "$content_model_type: $content_model" |
|
| 123 | - * @param $attr_includes What attribute collections to register to |
|
| 124 | - * element? |
|
| 125 | - * @param $attr What unique attributes does the element define? |
|
| 126 | - * @note See ElementDef for in-depth descriptions of these parameters. |
|
| 127 | - * @return Created element definition object, so you |
|
| 128 | - * can set advanced parameters |
|
| 129 | - */ |
|
| 130 | - public function addElement($element, $type, $contents, $attr_includes = array(), $attr = array()) { |
|
| 131 | - $this->elements[] = $element; |
|
| 132 | - // parse content_model |
|
| 133 | - list($content_model_type, $content_model) = $this->parseContents($contents); |
|
| 134 | - // merge in attribute inclusions |
|
| 135 | - $this->mergeInAttrIncludes($attr, $attr_includes); |
|
| 136 | - // add element to content sets |
|
| 137 | - if ($type) $this->addElementToContentSet($element, $type); |
|
| 138 | - // create element |
|
| 139 | - $this->info[$element] = HTMLPurifier_ElementDef::create( |
|
| 140 | - $content_model, $content_model_type, $attr |
|
| 141 | - ); |
|
| 142 | - // literal object $contents means direct child manipulation |
|
| 143 | - if (!is_string($contents)) $this->info[$element]->child = $contents; |
|
| 144 | - return $this->info[$element]; |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * Convenience function that creates a totally blank, non-standalone |
|
| 149 | - * element. |
|
| 150 | - * @param $element Name of element to create |
|
| 151 | - * @return Created element |
|
| 152 | - */ |
|
| 153 | - public function addBlankElement($element) { |
|
| 154 | - if (!isset($this->info[$element])) { |
|
| 155 | - $this->elements[] = $element; |
|
| 156 | - $this->info[$element] = new HTMLPurifier_ElementDef(); |
|
| 157 | - $this->info[$element]->standalone = false; |
|
| 158 | - } else { |
|
| 159 | - trigger_error("Definition for $element already exists in module, cannot redefine"); |
|
| 160 | - } |
|
| 161 | - return $this->info[$element]; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * Convenience function that registers an element to a content set |
|
| 166 | - * @param Element to register |
|
| 167 | - * @param Name content set (warning: case sensitive, usually upper-case |
|
| 168 | - * first letter) |
|
| 169 | - */ |
|
| 170 | - public function addElementToContentSet($element, $type) { |
|
| 171 | - if (!isset($this->content_sets[$type])) $this->content_sets[$type] = ''; |
|
| 172 | - else $this->content_sets[$type] .= ' | '; |
|
| 173 | - $this->content_sets[$type] .= $element; |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * Convenience function that transforms single-string contents |
|
| 178 | - * into separate content model and content model type |
|
| 179 | - * @param $contents Allowed children in form of: |
|
| 180 | - * "$content_model_type: $content_model" |
|
| 181 | - * @note If contents is an object, an array of two nulls will be |
|
| 182 | - * returned, and the callee needs to take the original $contents |
|
| 183 | - * and use it directly. |
|
| 184 | - */ |
|
| 185 | - public function parseContents($contents) { |
|
| 186 | - if (!is_string($contents)) return array(null, null); // defer |
|
| 187 | - switch ($contents) { |
|
| 188 | - // check for shorthand content model forms |
|
| 189 | - case 'Empty': |
|
| 190 | - return array('empty', ''); |
|
| 191 | - case 'Inline': |
|
| 192 | - return array('optional', 'Inline | #PCDATA'); |
|
| 193 | - case 'Flow': |
|
| 194 | - return array('optional', 'Flow | #PCDATA'); |
|
| 195 | - } |
|
| 196 | - list($content_model_type, $content_model) = explode(':', $contents); |
|
| 197 | - $content_model_type = strtolower(trim($content_model_type)); |
|
| 198 | - $content_model = trim($content_model); |
|
| 199 | - return array($content_model_type, $content_model); |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - /** |
|
| 203 | - * Convenience function that merges a list of attribute includes into |
|
| 204 | - * an attribute array. |
|
| 205 | - * @param $attr Reference to attr array to modify |
|
| 206 | - * @param $attr_includes Array of includes / string include to merge in |
|
| 207 | - */ |
|
| 208 | - public function mergeInAttrIncludes(&$attr, $attr_includes) { |
|
| 209 | - if (!is_array($attr_includes)) { |
|
| 210 | - if (empty($attr_includes)) $attr_includes = array(); |
|
| 211 | - else $attr_includes = array($attr_includes); |
|
| 212 | - } |
|
| 213 | - $attr[0] = $attr_includes; |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - /** |
|
| 217 | - * Convenience function that generates a lookup table with boolean |
|
| 218 | - * true as value. |
|
| 219 | - * @param $list List of values to turn into a lookup |
|
| 220 | - * @note You can also pass an arbitrary number of arguments in |
|
| 221 | - * place of the regular argument |
|
| 222 | - * @return Lookup array equivalent of list |
|
| 223 | - */ |
|
| 224 | - public function makeLookup($list) { |
|
| 225 | - if (is_string($list)) $list = func_get_args(); |
|
| 226 | - $ret = array(); |
|
| 227 | - foreach ($list as $value) { |
|
| 228 | - if (is_null($value)) continue; |
|
| 229 | - $ret[$value] = true; |
|
| 230 | - } |
|
| 231 | - return $ret; |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - /** |
|
| 235 | - * Lazy load construction of the module after determining whether |
|
| 236 | - * or not it's needed, and also when a finalized configuration object |
|
| 237 | - * is available. |
|
| 238 | - * @param $config Instance of HTMLPurifier_Config |
|
| 239 | - */ |
|
| 240 | - public function setup($config) {} |
|
| 21 | + // -- Overloadable ---------------------------------------------------- |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * Short unique string identifier of the module |
|
| 25 | + */ |
|
| 26 | + public $name; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * Informally, a list of elements this module changes. Not used in |
|
| 30 | + * any significant way. |
|
| 31 | + */ |
|
| 32 | + public $elements = array(); |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * Associative array of element names to element definitions. |
|
| 36 | + * Some definitions may be incomplete, to be merged in later |
|
| 37 | + * with the full definition. |
|
| 38 | + */ |
|
| 39 | + public $info = array(); |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * Associative array of content set names to content set additions. |
|
| 43 | + * This is commonly used to, say, add an A element to the Inline |
|
| 44 | + * content set. This corresponds to an internal variable $content_sets |
|
| 45 | + * and NOT info_content_sets member variable of HTMLDefinition. |
|
| 46 | + */ |
|
| 47 | + public $content_sets = array(); |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Associative array of attribute collection names to attribute |
|
| 51 | + * collection additions. More rarely used for adding attributes to |
|
| 52 | + * the global collections. Example is the StyleAttribute module adding |
|
| 53 | + * the style attribute to the Core. Corresponds to HTMLDefinition's |
|
| 54 | + * attr_collections->info, since the object's data is only info, |
|
| 55 | + * with extra behavior associated with it. |
|
| 56 | + */ |
|
| 57 | + public $attr_collections = array(); |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Associative array of deprecated tag name to HTMLPurifier_TagTransform |
|
| 61 | + */ |
|
| 62 | + public $info_tag_transform = array(); |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * List of HTMLPurifier_AttrTransform to be performed before validation. |
|
| 66 | + */ |
|
| 67 | + public $info_attr_transform_pre = array(); |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * List of HTMLPurifier_AttrTransform to be performed after validation. |
|
| 71 | + */ |
|
| 72 | + public $info_attr_transform_post = array(); |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * List of HTMLPurifier_Injector to be performed during well-formedness fixing. |
|
| 76 | + * An injector will only be invoked if all of it's pre-requisites are met; |
|
| 77 | + * if an injector fails setup, there will be no error; it will simply be |
|
| 78 | + * silently disabled. |
|
| 79 | + */ |
|
| 80 | + public $info_injector = array(); |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * Boolean flag that indicates whether or not getChildDef is implemented. |
|
| 84 | + * For optimization reasons: may save a call to a function. Be sure |
|
| 85 | + * to set it if you do implement getChildDef(), otherwise it will have |
|
| 86 | + * no effect! |
|
| 87 | + */ |
|
| 88 | + public $defines_child_def = false; |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Boolean flag whether or not this module is safe. If it is not safe, all |
|
| 92 | + * of its members are unsafe. Modules are safe by default (this might be |
|
| 93 | + * slightly dangerous, but it doesn't make much sense to force HTML Purifier, |
|
| 94 | + * which is based off of safe HTML, to explicitly say, "This is safe," even |
|
| 95 | + * though there are modules which are "unsafe") |
|
| 96 | + * |
|
| 97 | + * @note Previously, safety could be applied at an element level granularity. |
|
| 98 | + * We've removed this ability, so in order to add "unsafe" elements |
|
| 99 | + * or attributes, a dedicated module with this property set to false |
|
| 100 | + * must be used. |
|
| 101 | + */ |
|
| 102 | + public $safe = true; |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * Retrieves a proper HTMLPurifier_ChildDef subclass based on |
|
| 106 | + * content_model and content_model_type member variables of |
|
| 107 | + * the HTMLPurifier_ElementDef class. There is a similar function |
|
| 108 | + * in HTMLPurifier_HTMLDefinition. |
|
| 109 | + * @param $def HTMLPurifier_ElementDef instance |
|
| 110 | + * @return HTMLPurifier_ChildDef subclass |
|
| 111 | + */ |
|
| 112 | + public function getChildDef($def) {return false;} |
|
| 113 | + |
|
| 114 | + // -- Convenience ----------------------------------------------------- |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * Convenience function that sets up a new element |
|
| 118 | + * @param $element Name of element to add |
|
| 119 | + * @param $type What content set should element be registered to? |
|
| 120 | + * Set as false to skip this step. |
|
| 121 | + * @param $contents Allowed children in form of: |
|
| 122 | + * "$content_model_type: $content_model" |
|
| 123 | + * @param $attr_includes What attribute collections to register to |
|
| 124 | + * element? |
|
| 125 | + * @param $attr What unique attributes does the element define? |
|
| 126 | + * @note See ElementDef for in-depth descriptions of these parameters. |
|
| 127 | + * @return Created element definition object, so you |
|
| 128 | + * can set advanced parameters |
|
| 129 | + */ |
|
| 130 | + public function addElement($element, $type, $contents, $attr_includes = array(), $attr = array()) { |
|
| 131 | + $this->elements[] = $element; |
|
| 132 | + // parse content_model |
|
| 133 | + list($content_model_type, $content_model) = $this->parseContents($contents); |
|
| 134 | + // merge in attribute inclusions |
|
| 135 | + $this->mergeInAttrIncludes($attr, $attr_includes); |
|
| 136 | + // add element to content sets |
|
| 137 | + if ($type) $this->addElementToContentSet($element, $type); |
|
| 138 | + // create element |
|
| 139 | + $this->info[$element] = HTMLPurifier_ElementDef::create( |
|
| 140 | + $content_model, $content_model_type, $attr |
|
| 141 | + ); |
|
| 142 | + // literal object $contents means direct child manipulation |
|
| 143 | + if (!is_string($contents)) $this->info[$element]->child = $contents; |
|
| 144 | + return $this->info[$element]; |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * Convenience function that creates a totally blank, non-standalone |
|
| 149 | + * element. |
|
| 150 | + * @param $element Name of element to create |
|
| 151 | + * @return Created element |
|
| 152 | + */ |
|
| 153 | + public function addBlankElement($element) { |
|
| 154 | + if (!isset($this->info[$element])) { |
|
| 155 | + $this->elements[] = $element; |
|
| 156 | + $this->info[$element] = new HTMLPurifier_ElementDef(); |
|
| 157 | + $this->info[$element]->standalone = false; |
|
| 158 | + } else { |
|
| 159 | + trigger_error("Definition for $element already exists in module, cannot redefine"); |
|
| 160 | + } |
|
| 161 | + return $this->info[$element]; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * Convenience function that registers an element to a content set |
|
| 166 | + * @param Element to register |
|
| 167 | + * @param Name content set (warning: case sensitive, usually upper-case |
|
| 168 | + * first letter) |
|
| 169 | + */ |
|
| 170 | + public function addElementToContentSet($element, $type) { |
|
| 171 | + if (!isset($this->content_sets[$type])) $this->content_sets[$type] = ''; |
|
| 172 | + else $this->content_sets[$type] .= ' | '; |
|
| 173 | + $this->content_sets[$type] .= $element; |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * Convenience function that transforms single-string contents |
|
| 178 | + * into separate content model and content model type |
|
| 179 | + * @param $contents Allowed children in form of: |
|
| 180 | + * "$content_model_type: $content_model" |
|
| 181 | + * @note If contents is an object, an array of two nulls will be |
|
| 182 | + * returned, and the callee needs to take the original $contents |
|
| 183 | + * and use it directly. |
|
| 184 | + */ |
|
| 185 | + public function parseContents($contents) { |
|
| 186 | + if (!is_string($contents)) return array(null, null); // defer |
|
| 187 | + switch ($contents) { |
|
| 188 | + // check for shorthand content model forms |
|
| 189 | + case 'Empty': |
|
| 190 | + return array('empty', ''); |
|
| 191 | + case 'Inline': |
|
| 192 | + return array('optional', 'Inline | #PCDATA'); |
|
| 193 | + case 'Flow': |
|
| 194 | + return array('optional', 'Flow | #PCDATA'); |
|
| 195 | + } |
|
| 196 | + list($content_model_type, $content_model) = explode(':', $contents); |
|
| 197 | + $content_model_type = strtolower(trim($content_model_type)); |
|
| 198 | + $content_model = trim($content_model); |
|
| 199 | + return array($content_model_type, $content_model); |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + /** |
|
| 203 | + * Convenience function that merges a list of attribute includes into |
|
| 204 | + * an attribute array. |
|
| 205 | + * @param $attr Reference to attr array to modify |
|
| 206 | + * @param $attr_includes Array of includes / string include to merge in |
|
| 207 | + */ |
|
| 208 | + public function mergeInAttrIncludes(&$attr, $attr_includes) { |
|
| 209 | + if (!is_array($attr_includes)) { |
|
| 210 | + if (empty($attr_includes)) $attr_includes = array(); |
|
| 211 | + else $attr_includes = array($attr_includes); |
|
| 212 | + } |
|
| 213 | + $attr[0] = $attr_includes; |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + /** |
|
| 217 | + * Convenience function that generates a lookup table with boolean |
|
| 218 | + * true as value. |
|
| 219 | + * @param $list List of values to turn into a lookup |
|
| 220 | + * @note You can also pass an arbitrary number of arguments in |
|
| 221 | + * place of the regular argument |
|
| 222 | + * @return Lookup array equivalent of list |
|
| 223 | + */ |
|
| 224 | + public function makeLookup($list) { |
|
| 225 | + if (is_string($list)) $list = func_get_args(); |
|
| 226 | + $ret = array(); |
|
| 227 | + foreach ($list as $value) { |
|
| 228 | + if (is_null($value)) continue; |
|
| 229 | + $ret[$value] = true; |
|
| 230 | + } |
|
| 231 | + return $ret; |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + /** |
|
| 235 | + * Lazy load construction of the module after determining whether |
|
| 236 | + * or not it's needed, and also when a finalized configuration object |
|
| 237 | + * is available. |
|
| 238 | + * @param $config Instance of HTMLPurifier_Config |
|
| 239 | + */ |
|
| 240 | + public function setup($config) {} |
|
| 241 | 241 | |
| 242 | 242 | } |
| 243 | 243 | |
@@ -109,7 +109,7 @@ |
||
| 109 | 109 | * @param $def HTMLPurifier_ElementDef instance |
| 110 | 110 | * @return HTMLPurifier_ChildDef subclass |
| 111 | 111 | */ |
| 112 | - public function getChildDef($def) {return false;} |
|
| 112 | + public function getChildDef($def) {return false; } |
|
| 113 | 113 | |
| 114 | 114 | // -- Convenience ----------------------------------------------------- |
| 115 | 115 | |
@@ -134,13 +134,17 @@ discard block |
||
| 134 | 134 | // merge in attribute inclusions |
| 135 | 135 | $this->mergeInAttrIncludes($attr, $attr_includes); |
| 136 | 136 | // add element to content sets |
| 137 | - if ($type) $this->addElementToContentSet($element, $type); |
|
| 137 | + if ($type) { |
|
| 138 | + $this->addElementToContentSet($element, $type); |
|
| 139 | + } |
|
| 138 | 140 | // create element |
| 139 | 141 | $this->info[$element] = HTMLPurifier_ElementDef::create( |
| 140 | 142 | $content_model, $content_model_type, $attr |
| 141 | 143 | ); |
| 142 | 144 | // literal object $contents means direct child manipulation |
| 143 | - if (!is_string($contents)) $this->info[$element]->child = $contents; |
|
| 145 | + if (!is_string($contents)) { |
|
| 146 | + $this->info[$element]->child = $contents; |
|
| 147 | + } |
|
| 144 | 148 | return $this->info[$element]; |
| 145 | 149 | } |
| 146 | 150 | |
@@ -168,8 +172,11 @@ discard block |
||
| 168 | 172 | * first letter) |
| 169 | 173 | */ |
| 170 | 174 | public function addElementToContentSet($element, $type) { |
| 171 | - if (!isset($this->content_sets[$type])) $this->content_sets[$type] = ''; |
|
| 172 | - else $this->content_sets[$type] .= ' | '; |
|
| 175 | + if (!isset($this->content_sets[$type])) { |
|
| 176 | + $this->content_sets[$type] = ''; |
|
| 177 | + } else { |
|
| 178 | + $this->content_sets[$type] .= ' | '; |
|
| 179 | + } |
|
| 173 | 180 | $this->content_sets[$type] .= $element; |
| 174 | 181 | } |
| 175 | 182 | |
@@ -183,7 +190,10 @@ discard block |
||
| 183 | 190 | * and use it directly. |
| 184 | 191 | */ |
| 185 | 192 | public function parseContents($contents) { |
| 186 | - if (!is_string($contents)) return array(null, null); // defer |
|
| 193 | + if (!is_string($contents)) { |
|
| 194 | + return array(null, null); |
|
| 195 | + } |
|
| 196 | + // defer |
|
| 187 | 197 | switch ($contents) { |
| 188 | 198 | // check for shorthand content model forms |
| 189 | 199 | case 'Empty': |
@@ -207,8 +217,11 @@ discard block |
||
| 207 | 217 | */ |
| 208 | 218 | public function mergeInAttrIncludes(&$attr, $attr_includes) { |
| 209 | 219 | if (!is_array($attr_includes)) { |
| 210 | - if (empty($attr_includes)) $attr_includes = array(); |
|
| 211 | - else $attr_includes = array($attr_includes); |
|
| 220 | + if (empty($attr_includes)) { |
|
| 221 | + $attr_includes = array(); |
|
| 222 | + } else { |
|
| 223 | + $attr_includes = array($attr_includes); |
|
| 224 | + } |
|
| 212 | 225 | } |
| 213 | 226 | $attr[0] = $attr_includes; |
| 214 | 227 | } |
@@ -222,10 +235,14 @@ discard block |
||
| 222 | 235 | * @return Lookup array equivalent of list |
| 223 | 236 | */ |
| 224 | 237 | public function makeLookup($list) { |
| 225 | - if (is_string($list)) $list = func_get_args(); |
|
| 238 | + if (is_string($list)) { |
|
| 239 | + $list = func_get_args(); |
|
| 240 | + } |
|
| 226 | 241 | $ret = array(); |
| 227 | 242 | foreach ($list as $value) { |
| 228 | - if (is_null($value)) continue; |
|
| 243 | + if (is_null($value)) { |
|
| 244 | + continue; |
|
| 245 | + } |
|
| 229 | 246 | $ret[$value] = true; |
| 230 | 247 | } |
| 231 | 248 | return $ret; |
@@ -7,24 +7,24 @@ |
||
| 7 | 7 | class HTMLPurifier_HTMLModule_Bdo extends HTMLPurifier_HTMLModule |
| 8 | 8 | { |
| 9 | 9 | |
| 10 | - public $name = 'Bdo'; |
|
| 11 | - public $attr_collections = array( |
|
| 12 | - 'I18N' => array('dir' => false) |
|
| 13 | - ); |
|
| 10 | + public $name = 'Bdo'; |
|
| 11 | + public $attr_collections = array( |
|
| 12 | + 'I18N' => array('dir' => false) |
|
| 13 | + ); |
|
| 14 | 14 | |
| 15 | - public function setup($config) { |
|
| 16 | - $bdo = $this->addElement( |
|
| 17 | - 'bdo', 'Inline', 'Inline', array('Core', 'Lang'), |
|
| 18 | - array( |
|
| 19 | - 'dir' => 'Enum#ltr,rtl', // required |
|
| 20 | - // The Abstract Module specification has the attribute |
|
| 21 | - // inclusions wrong for bdo: bdo allows Lang |
|
| 22 | - ) |
|
| 23 | - ); |
|
| 24 | - $bdo->attr_transform_post['required-dir'] = new HTMLPurifier_AttrTransform_BdoDir(); |
|
| 15 | + public function setup($config) { |
|
| 16 | + $bdo = $this->addElement( |
|
| 17 | + 'bdo', 'Inline', 'Inline', array('Core', 'Lang'), |
|
| 18 | + array( |
|
| 19 | + 'dir' => 'Enum#ltr,rtl', // required |
|
| 20 | + // The Abstract Module specification has the attribute |
|
| 21 | + // inclusions wrong for bdo: bdo allows Lang |
|
| 22 | + ) |
|
| 23 | + ); |
|
| 24 | + $bdo->attr_transform_post['required-dir'] = new HTMLPurifier_AttrTransform_BdoDir(); |
|
| 25 | 25 | |
| 26 | - $this->attr_collections['I18N']['dir'] = 'Enum#ltr,rtl'; |
|
| 27 | - } |
|
| 26 | + $this->attr_collections['I18N']['dir'] = 'Enum#ltr,rtl'; |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | 29 | } |
| 30 | 30 | |
@@ -2,24 +2,24 @@ |
||
| 2 | 2 | |
| 3 | 3 | class HTMLPurifier_HTMLModule_CommonAttributes extends HTMLPurifier_HTMLModule |
| 4 | 4 | { |
| 5 | - public $name = 'CommonAttributes'; |
|
| 5 | + public $name = 'CommonAttributes'; |
|
| 6 | 6 | |
| 7 | - public $attr_collections = array( |
|
| 8 | - 'Core' => array( |
|
| 9 | - 0 => array('Style'), |
|
| 10 | - // 'xml:space' => false, |
|
| 11 | - 'class' => 'Class', |
|
| 12 | - 'id' => 'ID', |
|
| 13 | - 'title' => 'CDATA', |
|
| 14 | - ), |
|
| 15 | - 'Lang' => array(), |
|
| 16 | - 'I18N' => array( |
|
| 17 | - 0 => array('Lang'), // proprietary, for xml:lang/lang |
|
| 18 | - ), |
|
| 19 | - 'Common' => array( |
|
| 20 | - 0 => array('Core', 'I18N') |
|
| 21 | - ) |
|
| 22 | - ); |
|
| 7 | + public $attr_collections = array( |
|
| 8 | + 'Core' => array( |
|
| 9 | + 0 => array('Style'), |
|
| 10 | + // 'xml:space' => false, |
|
| 11 | + 'class' => 'Class', |
|
| 12 | + 'id' => 'ID', |
|
| 13 | + 'title' => 'CDATA', |
|
| 14 | + ), |
|
| 15 | + 'Lang' => array(), |
|
| 16 | + 'I18N' => array( |
|
| 17 | + 0 => array('Lang'), // proprietary, for xml:lang/lang |
|
| 18 | + ), |
|
| 19 | + 'Common' => array( |
|
| 20 | + 0 => array('Core', 'I18N') |
|
| 21 | + ) |
|
| 22 | + ); |
|
| 23 | 23 | |
| 24 | 24 | } |
| 25 | 25 | |
@@ -7,31 +7,31 @@ |
||
| 7 | 7 | class HTMLPurifier_HTMLModule_Edit extends HTMLPurifier_HTMLModule |
| 8 | 8 | { |
| 9 | 9 | |
| 10 | - public $name = 'Edit'; |
|
| 11 | - |
|
| 12 | - public function setup($config) { |
|
| 13 | - $contents = 'Chameleon: #PCDATA | Inline ! #PCDATA | Flow'; |
|
| 14 | - $attr = array( |
|
| 15 | - 'cite' => 'URI', |
|
| 16 | - // 'datetime' => 'Datetime', // not implemented |
|
| 17 | - ); |
|
| 18 | - $this->addElement('del', 'Inline', $contents, 'Common', $attr); |
|
| 19 | - $this->addElement('ins', 'Inline', $contents, 'Common', $attr); |
|
| 20 | - } |
|
| 21 | - |
|
| 22 | - // HTML 4.01 specifies that ins/del must not contain block |
|
| 23 | - // elements when used in an inline context, chameleon is |
|
| 24 | - // a complicated workaround to acheive this effect |
|
| 25 | - |
|
| 26 | - // Inline context ! Block context (exclamation mark is |
|
| 27 | - // separator, see getChildDef for parsing) |
|
| 28 | - |
|
| 29 | - public $defines_child_def = true; |
|
| 30 | - public function getChildDef($def) { |
|
| 31 | - if ($def->content_model_type != 'chameleon') return false; |
|
| 32 | - $value = explode('!', $def->content_model); |
|
| 33 | - return new HTMLPurifier_ChildDef_Chameleon($value[0], $value[1]); |
|
| 34 | - } |
|
| 10 | + public $name = 'Edit'; |
|
| 11 | + |
|
| 12 | + public function setup($config) { |
|
| 13 | + $contents = 'Chameleon: #PCDATA | Inline ! #PCDATA | Flow'; |
|
| 14 | + $attr = array( |
|
| 15 | + 'cite' => 'URI', |
|
| 16 | + // 'datetime' => 'Datetime', // not implemented |
|
| 17 | + ); |
|
| 18 | + $this->addElement('del', 'Inline', $contents, 'Common', $attr); |
|
| 19 | + $this->addElement('ins', 'Inline', $contents, 'Common', $attr); |
|
| 20 | + } |
|
| 21 | + |
|
| 22 | + // HTML 4.01 specifies that ins/del must not contain block |
|
| 23 | + // elements when used in an inline context, chameleon is |
|
| 24 | + // a complicated workaround to acheive this effect |
|
| 25 | + |
|
| 26 | + // Inline context ! Block context (exclamation mark is |
|
| 27 | + // separator, see getChildDef for parsing) |
|
| 28 | + |
|
| 29 | + public $defines_child_def = true; |
|
| 30 | + public function getChildDef($def) { |
|
| 31 | + if ($def->content_model_type != 'chameleon') return false; |
|
| 32 | + $value = explode('!', $def->content_model); |
|
| 33 | + return new HTMLPurifier_ChildDef_Chameleon($value[0], $value[1]); |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | 36 | } |
| 37 | 37 | |
@@ -28,7 +28,9 @@ |
||
| 28 | 28 | |
| 29 | 29 | public $defines_child_def = true; |
| 30 | 30 | public function getChildDef($def) { |
| 31 | - if ($def->content_model_type != 'chameleon') return false; |
|
| 31 | + if ($def->content_model_type != 'chameleon') { |
|
| 32 | + return false; |
|
| 33 | + } |
|
| 32 | 34 | $value = explode('!', $def->content_model); |
| 33 | 35 | return new HTMLPurifier_ChildDef_Chameleon($value[0], $value[1]); |
| 34 | 36 | } |
@@ -5,115 +5,115 @@ |
||
| 5 | 5 | */ |
| 6 | 6 | class HTMLPurifier_HTMLModule_Forms extends HTMLPurifier_HTMLModule |
| 7 | 7 | { |
| 8 | - public $name = 'Forms'; |
|
| 9 | - public $safe = false; |
|
| 10 | - |
|
| 11 | - public $content_sets = array( |
|
| 12 | - 'Block' => 'Form', |
|
| 13 | - 'Inline' => 'Formctrl', |
|
| 14 | - ); |
|
| 15 | - |
|
| 16 | - public function setup($config) { |
|
| 17 | - $form = $this->addElement('form', 'Form', |
|
| 18 | - 'Required: Heading | List | Block | fieldset', 'Common', array( |
|
| 19 | - 'accept' => 'ContentTypes', |
|
| 20 | - 'accept-charset' => 'Charsets', |
|
| 21 | - 'action*' => 'URI', |
|
| 22 | - 'method' => 'Enum#get,post', |
|
| 23 | - // really ContentType, but these two are the only ones used today |
|
| 24 | - 'enctype' => 'Enum#application/x-www-form-urlencoded,multipart/form-data', |
|
| 25 | - )); |
|
| 26 | - $form->excludes = array('form' => true); |
|
| 27 | - |
|
| 28 | - $input = $this->addElement('input', 'Formctrl', 'Empty', 'Common', array( |
|
| 29 | - 'accept' => 'ContentTypes', |
|
| 30 | - 'accesskey' => 'Character', |
|
| 31 | - 'alt' => 'Text', |
|
| 32 | - 'checked' => 'Bool#checked', |
|
| 33 | - 'disabled' => 'Bool#disabled', |
|
| 34 | - 'maxlength' => 'Number', |
|
| 35 | - 'name' => 'CDATA', |
|
| 36 | - 'readonly' => 'Bool#readonly', |
|
| 37 | - 'size' => 'Number', |
|
| 38 | - 'src' => 'URI#embedded', |
|
| 39 | - 'tabindex' => 'Number', |
|
| 40 | - 'type' => 'Enum#text,password,checkbox,button,radio,submit,reset,file,hidden,image', |
|
| 41 | - 'value' => 'CDATA', |
|
| 42 | - )); |
|
| 43 | - $input->attr_transform_post[] = new HTMLPurifier_AttrTransform_Input(); |
|
| 44 | - |
|
| 45 | - $this->addElement('select', 'Formctrl', 'Required: optgroup | option', 'Common', array( |
|
| 46 | - 'disabled' => 'Bool#disabled', |
|
| 47 | - 'multiple' => 'Bool#multiple', |
|
| 48 | - 'name' => 'CDATA', |
|
| 49 | - 'size' => 'Number', |
|
| 50 | - 'tabindex' => 'Number', |
|
| 51 | - )); |
|
| 52 | - |
|
| 53 | - $this->addElement('option', false, 'Optional: #PCDATA', 'Common', array( |
|
| 54 | - 'disabled' => 'Bool#disabled', |
|
| 55 | - 'label' => 'Text', |
|
| 56 | - 'selected' => 'Bool#selected', |
|
| 57 | - 'value' => 'CDATA', |
|
| 58 | - )); |
|
| 59 | - // It's illegal for there to be more than one selected, but not |
|
| 60 | - // be multiple. Also, no selected means undefined behavior. This might |
|
| 61 | - // be difficult to implement; perhaps an injector, or a context variable. |
|
| 62 | - |
|
| 63 | - $textarea = $this->addElement('textarea', 'Formctrl', 'Optional: #PCDATA', 'Common', array( |
|
| 64 | - 'accesskey' => 'Character', |
|
| 65 | - 'cols*' => 'Number', |
|
| 66 | - 'disabled' => 'Bool#disabled', |
|
| 67 | - 'name' => 'CDATA', |
|
| 68 | - 'readonly' => 'Bool#readonly', |
|
| 69 | - 'rows*' => 'Number', |
|
| 70 | - 'tabindex' => 'Number', |
|
| 71 | - )); |
|
| 72 | - $textarea->attr_transform_pre[] = new HTMLPurifier_AttrTransform_Textarea(); |
|
| 73 | - |
|
| 74 | - $button = $this->addElement('button', 'Formctrl', 'Optional: #PCDATA | Heading | List | Block | Inline', 'Common', array( |
|
| 75 | - 'accesskey' => 'Character', |
|
| 76 | - 'disabled' => 'Bool#disabled', |
|
| 77 | - 'name' => 'CDATA', |
|
| 78 | - 'tabindex' => 'Number', |
|
| 79 | - 'type' => 'Enum#button,submit,reset', |
|
| 80 | - 'value' => 'CDATA', |
|
| 81 | - )); |
|
| 82 | - |
|
| 83 | - // For exclusions, ideally we'd specify content sets, not literal elements |
|
| 84 | - $button->excludes = $this->makeLookup( |
|
| 85 | - 'form', 'fieldset', // Form |
|
| 86 | - 'input', 'select', 'textarea', 'label', 'button', // Formctrl |
|
| 87 | - 'a', // as per HTML 4.01 spec, this is omitted by modularization |
|
| 88 | - 'isindex', 'iframe' // legacy items |
|
| 89 | - ); |
|
| 90 | - |
|
| 91 | - // Extra exclusion: img usemap="" is not permitted within this element. |
|
| 92 | - // We'll omit this for now, since we don't have any good way of |
|
| 93 | - // indicating it yet. |
|
| 94 | - |
|
| 95 | - // This is HIGHLY user-unfriendly; we need a custom child-def for this |
|
| 96 | - $this->addElement('fieldset', 'Form', 'Custom: (#WS?,legend,(Flow|#PCDATA)*)', 'Common'); |
|
| 97 | - |
|
| 98 | - $label = $this->addElement('label', 'Formctrl', 'Optional: #PCDATA | Inline', 'Common', array( |
|
| 99 | - 'accesskey' => 'Character', |
|
| 100 | - // 'for' => 'IDREF', // IDREF not implemented, cannot allow |
|
| 101 | - )); |
|
| 102 | - $label->excludes = array('label' => true); |
|
| 103 | - |
|
| 104 | - $this->addElement('legend', false, 'Optional: #PCDATA | Inline', 'Common', array( |
|
| 105 | - 'accesskey' => 'Character', |
|
| 106 | - )); |
|
| 107 | - |
|
| 108 | - $this->addElement('optgroup', false, 'Required: option', 'Common', array( |
|
| 109 | - 'disabled' => 'Bool#disabled', |
|
| 110 | - 'label*' => 'Text', |
|
| 111 | - )); |
|
| 112 | - |
|
| 113 | - // Don't forget an injector for <isindex>. This one's a little complex |
|
| 114 | - // because it maps to multiple elements. |
|
| 115 | - |
|
| 116 | - } |
|
| 8 | + public $name = 'Forms'; |
|
| 9 | + public $safe = false; |
|
| 10 | + |
|
| 11 | + public $content_sets = array( |
|
| 12 | + 'Block' => 'Form', |
|
| 13 | + 'Inline' => 'Formctrl', |
|
| 14 | + ); |
|
| 15 | + |
|
| 16 | + public function setup($config) { |
|
| 17 | + $form = $this->addElement('form', 'Form', |
|
| 18 | + 'Required: Heading | List | Block | fieldset', 'Common', array( |
|
| 19 | + 'accept' => 'ContentTypes', |
|
| 20 | + 'accept-charset' => 'Charsets', |
|
| 21 | + 'action*' => 'URI', |
|
| 22 | + 'method' => 'Enum#get,post', |
|
| 23 | + // really ContentType, but these two are the only ones used today |
|
| 24 | + 'enctype' => 'Enum#application/x-www-form-urlencoded,multipart/form-data', |
|
| 25 | + )); |
|
| 26 | + $form->excludes = array('form' => true); |
|
| 27 | + |
|
| 28 | + $input = $this->addElement('input', 'Formctrl', 'Empty', 'Common', array( |
|
| 29 | + 'accept' => 'ContentTypes', |
|
| 30 | + 'accesskey' => 'Character', |
|
| 31 | + 'alt' => 'Text', |
|
| 32 | + 'checked' => 'Bool#checked', |
|
| 33 | + 'disabled' => 'Bool#disabled', |
|
| 34 | + 'maxlength' => 'Number', |
|
| 35 | + 'name' => 'CDATA', |
|
| 36 | + 'readonly' => 'Bool#readonly', |
|
| 37 | + 'size' => 'Number', |
|
| 38 | + 'src' => 'URI#embedded', |
|
| 39 | + 'tabindex' => 'Number', |
|
| 40 | + 'type' => 'Enum#text,password,checkbox,button,radio,submit,reset,file,hidden,image', |
|
| 41 | + 'value' => 'CDATA', |
|
| 42 | + )); |
|
| 43 | + $input->attr_transform_post[] = new HTMLPurifier_AttrTransform_Input(); |
|
| 44 | + |
|
| 45 | + $this->addElement('select', 'Formctrl', 'Required: optgroup | option', 'Common', array( |
|
| 46 | + 'disabled' => 'Bool#disabled', |
|
| 47 | + 'multiple' => 'Bool#multiple', |
|
| 48 | + 'name' => 'CDATA', |
|
| 49 | + 'size' => 'Number', |
|
| 50 | + 'tabindex' => 'Number', |
|
| 51 | + )); |
|
| 52 | + |
|
| 53 | + $this->addElement('option', false, 'Optional: #PCDATA', 'Common', array( |
|
| 54 | + 'disabled' => 'Bool#disabled', |
|
| 55 | + 'label' => 'Text', |
|
| 56 | + 'selected' => 'Bool#selected', |
|
| 57 | + 'value' => 'CDATA', |
|
| 58 | + )); |
|
| 59 | + // It's illegal for there to be more than one selected, but not |
|
| 60 | + // be multiple. Also, no selected means undefined behavior. This might |
|
| 61 | + // be difficult to implement; perhaps an injector, or a context variable. |
|
| 62 | + |
|
| 63 | + $textarea = $this->addElement('textarea', 'Formctrl', 'Optional: #PCDATA', 'Common', array( |
|
| 64 | + 'accesskey' => 'Character', |
|
| 65 | + 'cols*' => 'Number', |
|
| 66 | + 'disabled' => 'Bool#disabled', |
|
| 67 | + 'name' => 'CDATA', |
|
| 68 | + 'readonly' => 'Bool#readonly', |
|
| 69 | + 'rows*' => 'Number', |
|
| 70 | + 'tabindex' => 'Number', |
|
| 71 | + )); |
|
| 72 | + $textarea->attr_transform_pre[] = new HTMLPurifier_AttrTransform_Textarea(); |
|
| 73 | + |
|
| 74 | + $button = $this->addElement('button', 'Formctrl', 'Optional: #PCDATA | Heading | List | Block | Inline', 'Common', array( |
|
| 75 | + 'accesskey' => 'Character', |
|
| 76 | + 'disabled' => 'Bool#disabled', |
|
| 77 | + 'name' => 'CDATA', |
|
| 78 | + 'tabindex' => 'Number', |
|
| 79 | + 'type' => 'Enum#button,submit,reset', |
|
| 80 | + 'value' => 'CDATA', |
|
| 81 | + )); |
|
| 82 | + |
|
| 83 | + // For exclusions, ideally we'd specify content sets, not literal elements |
|
| 84 | + $button->excludes = $this->makeLookup( |
|
| 85 | + 'form', 'fieldset', // Form |
|
| 86 | + 'input', 'select', 'textarea', 'label', 'button', // Formctrl |
|
| 87 | + 'a', // as per HTML 4.01 spec, this is omitted by modularization |
|
| 88 | + 'isindex', 'iframe' // legacy items |
|
| 89 | + ); |
|
| 90 | + |
|
| 91 | + // Extra exclusion: img usemap="" is not permitted within this element. |
|
| 92 | + // We'll omit this for now, since we don't have any good way of |
|
| 93 | + // indicating it yet. |
|
| 94 | + |
|
| 95 | + // This is HIGHLY user-unfriendly; we need a custom child-def for this |
|
| 96 | + $this->addElement('fieldset', 'Form', 'Custom: (#WS?,legend,(Flow|#PCDATA)*)', 'Common'); |
|
| 97 | + |
|
| 98 | + $label = $this->addElement('label', 'Formctrl', 'Optional: #PCDATA | Inline', 'Common', array( |
|
| 99 | + 'accesskey' => 'Character', |
|
| 100 | + // 'for' => 'IDREF', // IDREF not implemented, cannot allow |
|
| 101 | + )); |
|
| 102 | + $label->excludes = array('label' => true); |
|
| 103 | + |
|
| 104 | + $this->addElement('legend', false, 'Optional: #PCDATA | Inline', 'Common', array( |
|
| 105 | + 'accesskey' => 'Character', |
|
| 106 | + )); |
|
| 107 | + |
|
| 108 | + $this->addElement('optgroup', false, 'Required: option', 'Common', array( |
|
| 109 | + 'disabled' => 'Bool#disabled', |
|
| 110 | + 'label*' => 'Text', |
|
| 111 | + )); |
|
| 112 | + |
|
| 113 | + // Don't forget an injector for <isindex>. This one's a little complex |
|
| 114 | + // because it maps to multiple elements. |
|
| 115 | + |
|
| 116 | + } |
|
| 117 | 117 | } |
| 118 | 118 | |
| 119 | 119 | // vim: et sw=4 sts=4 |
@@ -6,25 +6,25 @@ |
||
| 6 | 6 | class HTMLPurifier_HTMLModule_Hypertext extends HTMLPurifier_HTMLModule |
| 7 | 7 | { |
| 8 | 8 | |
| 9 | - public $name = 'Hypertext'; |
|
| 9 | + public $name = 'Hypertext'; |
|
| 10 | 10 | |
| 11 | - public function setup($config) { |
|
| 12 | - $a = $this->addElement( |
|
| 13 | - 'a', 'Inline', 'Inline', 'Common', |
|
| 14 | - array( |
|
| 15 | - // 'accesskey' => 'Character', |
|
| 16 | - // 'charset' => 'Charset', |
|
| 17 | - 'href' => 'URI', |
|
| 18 | - // 'hreflang' => 'LanguageCode', |
|
| 19 | - 'rel' => new HTMLPurifier_AttrDef_HTML_LinkTypes('rel'), |
|
| 20 | - 'rev' => new HTMLPurifier_AttrDef_HTML_LinkTypes('rev'), |
|
| 21 | - // 'tabindex' => 'Number', |
|
| 22 | - // 'type' => 'ContentType', |
|
| 23 | - ) |
|
| 24 | - ); |
|
| 25 | - $a->formatting = true; |
|
| 26 | - $a->excludes = array('a' => true); |
|
| 27 | - } |
|
| 11 | + public function setup($config) { |
|
| 12 | + $a = $this->addElement( |
|
| 13 | + 'a', 'Inline', 'Inline', 'Common', |
|
| 14 | + array( |
|
| 15 | + // 'accesskey' => 'Character', |
|
| 16 | + // 'charset' => 'Charset', |
|
| 17 | + 'href' => 'URI', |
|
| 18 | + // 'hreflang' => 'LanguageCode', |
|
| 19 | + 'rel' => new HTMLPurifier_AttrDef_HTML_LinkTypes('rel'), |
|
| 20 | + 'rev' => new HTMLPurifier_AttrDef_HTML_LinkTypes('rev'), |
|
| 21 | + // 'tabindex' => 'Number', |
|
| 22 | + // 'type' => 'ContentType', |
|
| 23 | + ) |
|
| 24 | + ); |
|
| 25 | + $a->formatting = true; |
|
| 26 | + $a->excludes = array('a' => true); |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | 29 | } |
| 30 | 30 | |