: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* Flexible unit of measure for CSS dimensions.
* Adapted from the `amp.validator.CssLength` class found in `validator.js` from the `ampproject/amphtml` project on
* @link https://github.com/ampproject/amphtml/blob/1911070201440/validator/engine/validator.js#L3351
* @package ampproject/amp-toolbox
// Special attribute values.
* Whether the value or unit is invalid. Note that passing an empty value as `$attr_value` is considered valid.
protected $isValid = false;
* Whether the attribute value is set.
protected $isDefined = false;
* Whether the attribute value is 'auto'. This is a special value that indicates that the value gets derived from
* the context. In practice that's only ever the case for a width.
protected $isAuto = false;
* Whether the attribute value is 'fluid'.
protected $isFluid = false;
* The unit, 'px' being the default in case it's absent.
* Instantiate a CssLength object.
* @param string|null $attrValue Attribute value to be parsed.
public function __construct($attrValue)
if (null === $attrValue) {
$this->attrValue = $attrValue;
* Validate the attribute value.
* @param bool $allowAuto Whether or not to allow the 'auto' value as a value.
* @param bool $allowFluid Whether or not to allow the 'fluid' value as a value.
public function validate($allowAuto, $allowFluid)
if (self::AUTO === $this->attrValue) {
$this->isValid = $allowAuto;
if (self::FLUID === $this->attrValue) {
$this->isValid = $allowFluid;
$pattern = '/^(?<numeral>\d+(?:\.\d+)?)(?<unit>px|em|rem|vh|vw|vmin|vmax)?$/';
if (preg_match($pattern, $this->attrValue, $match)) {
$this->numeral = isset($match['numeral']) ? (float)$match['numeral'] : $this->numeral;
$this->unit = isset($match['unit']) ? $match['unit'] : $this->unit;
* Whether or not the attribute value is valid.
public function isValid()
* Whether the attribute value is set.
public function isDefined()
* Whether the attribute value is 'fluid'.
public function isFluid()
* Whether the attribute value is 'auto'.
* The unit of the attribute.
public function getUnit()
* The numeral of the attribute.
public function getNumeral()