friendica/vendor/ezyang/htmlpurifier/library/HTMLPurifier/AttrTransform/ImgSpace.php

62 lines
1.4 KiB
PHP
Raw Normal View History

2010-09-09 03:14:17 +00:00
<?php
/**
* Pre-transform that changes deprecated hspace and vspace attributes to CSS
*/
2016-02-09 10:06:17 +00:00
class HTMLPurifier_AttrTransform_ImgSpace extends HTMLPurifier_AttrTransform
{
/**
* @type string
*/
2010-09-09 03:14:17 +00:00
protected $attr;
2016-02-09 10:06:17 +00:00
/**
* @type array
*/
2010-09-09 03:14:17 +00:00
protected $css = array(
'hspace' => array('left', 'right'),
'vspace' => array('top', 'bottom')
);
2016-02-09 10:06:17 +00:00
/**
* @param string $attr
*/
public function __construct($attr)
{
2010-09-09 03:14:17 +00:00
$this->attr = $attr;
if (!isset($this->css[$attr])) {
trigger_error(htmlspecialchars($attr) . ' is not valid space attribute');
}
}
2016-02-09 10:06:17 +00:00
/**
* @param array $attr
* @param HTMLPurifier_Config $config
* @param HTMLPurifier_Context $context
* @return array
*/
public function transform($attr, $config, $context)
{
if (!isset($attr[$this->attr])) {
return $attr;
}
2010-09-09 03:14:17 +00:00
$width = $this->confiscateAttr($attr, $this->attr);
// some validation could happen here
2016-02-09 10:06:17 +00:00
if (!isset($this->css[$this->attr])) {
return $attr;
}
2010-09-09 03:14:17 +00:00
$style = '';
foreach ($this->css[$this->attr] as $suffix) {
$property = "margin-$suffix";
$style .= "$property:{$width}px;";
}
$this->prependCSS($attr, $style);
return $attr;
}
}
// vim: et sw=4 sts=4