1: | <?php
|
2: |
|
3: | |
4: | |
5: | |
6: | |
7: | |
8: |
|
9: | class HTMLPurifier_ChildDef_Custom extends HTMLPurifier_ChildDef
|
10: | {
|
11: | |
12: | |
13: |
|
14: | public $type = 'custom';
|
15: |
|
16: | |
17: | |
18: |
|
19: | public $allow_empty = false;
|
20: |
|
21: | |
22: | |
23: | |
24: |
|
25: | public $dtd_regex;
|
26: |
|
27: | |
28: | |
29: | |
30: |
|
31: | private $_pcre_regex;
|
32: |
|
33: | |
34: | |
35: |
|
36: | public function __construct($dtd_regex)
|
37: | {
|
38: | $this->dtd_regex = $dtd_regex;
|
39: | $this->_compileRegex();
|
40: | }
|
41: |
|
42: | |
43: | |
44: |
|
45: | protected function _compileRegex()
|
46: | {
|
47: | $raw = str_replace(' ', '', $this->dtd_regex);
|
48: | if ($raw[0] != '(') {
|
49: | $raw = "($raw)";
|
50: | }
|
51: | $el = '[#a-zA-Z0-9_.-]+';
|
52: | $reg = $raw;
|
53: |
|
54: |
|
55: |
|
56: |
|
57: |
|
58: | preg_match_all("/$el/", $reg, $matches);
|
59: | foreach ($matches[0] as $match) {
|
60: | $this->elements[$match] = true;
|
61: | }
|
62: |
|
63: |
|
64: | $reg = preg_replace("/$el/", '(,\\0)', $reg);
|
65: |
|
66: |
|
67: | $reg = preg_replace("/([^,(|]\(+),/", '\\1', $reg);
|
68: |
|
69: |
|
70: | $reg = preg_replace("/,\(/", '(', $reg);
|
71: |
|
72: | $this->_pcre_regex = $reg;
|
73: | }
|
74: |
|
75: | |
76: | |
77: | |
78: | |
79: | |
80: |
|
81: | public function validateChildren($children, $config, $context)
|
82: | {
|
83: | $list_of_children = '';
|
84: | $nesting = 0;
|
85: | foreach ($children as $node) {
|
86: | if (!empty($node->is_whitespace)) {
|
87: | continue;
|
88: | }
|
89: | $list_of_children .= $node->name . ',';
|
90: | }
|
91: |
|
92: | $list_of_children = ',' . rtrim($list_of_children, ',');
|
93: | $okay =
|
94: | preg_match(
|
95: | '/^,?' . $this->_pcre_regex . '$/',
|
96: | $list_of_children
|
97: | );
|
98: | return (bool)$okay;
|
99: | }
|
100: | }
|
101: |
|
102: |
|
103: | |