Browse Source

Implement configuration getting

master
J. King 1 year ago
parent
commit
af23cb18ca
  1. 71
      lib/AbstractSanitizer.php

71
lib/AbstractSanitizer.php

@ -510,6 +510,10 @@ abstract class AbstractSanitizer {
// ignore any other value for the elements list (this is invalid)
continue;
}
if (!$list) {
// an empty element list is non-functional, so we can skip the attribute
continue;
}
// create any structures which might be missing
if (!isset($out[$opt])) {
$out[$opt] = [];
@ -531,4 +535,71 @@ abstract class AbstractSanitizer {
$this->config = $out;
}
}
/** Returns a normalized representation of the effective configuration */
public function getConfiguration(): array {
return $this->exportConfiguration($this->config);
}
/** Returns the default configuration */
public function getDefaultConfiguration(): array {
return $this->exportConfiguration(self::DEFAULT_CONF);
}
/** Converts a configuration from the internal representation to the external representation */
protected function exportConfiguration(array $config): array {
$out = [];
// start with the element lists
foreach (["allowElements", "blockElements", "dropElements"] as $opt) {
if (isset($config[$opt])) {
$out[$opt] = [];
ksort($config[$opt]);
foreach ($config[$opt] as $ns => $set) {
ksort($set);
foreach ($set as $el => $t) {
if ($ns === self::HTML_NAMESPACE) {
$out[$opt][] = $el;
} else {
$out[$opt][] = ['name' => $el, 'namespace' => $ns];
}
}
}
}
}
// continue with attribute lists
foreach (["allowAttributes", "dropAttributes"] as $opt) {
if (isset($config[$opt])) {
$out[$opt] = [];
ksort($config[$opt]);
foreach ($config[$opt] as $ns => $set) {
ksort($set);
foreach ($set as $attr => $elems) {
$list = [];
ksort($elems);
foreach ($elems as $eNs => $eSet) {
ksort($eSet);
foreach ($eSet as $el => $t) {
if ($eNs === self::HTML_NAMESPACE) {
$list[] = $el;
} else {
$list[] = ['name' => $el, 'namespace' => $eNs];
}
}
}
if ($ns === self::NULL_NAMESPACE) {
$out[$opt][] = ['name' => $attr, 'elements' => $list];
} else {
$out[$opt][] = ['name' => $attr, 'namespace' => $ns, 'elements' => $list];
}
}
}
}
}
// finally handle the boolean options
foreach (["allowCustomElements", "allowUnknownMarkup", "allowComments", "allowProcessingInstructions", "nullNamespaceAsHtml"] as $opt) {
$out[$opt] = $config[$opt];
}
// return the transformed configuration
return $out;
}
}

Loading…
Cancel
Save