/** * * Escapes an unquoted HTML attribute, or converts an array of such * attributes to a quoted-and-escaped attribute string. * * When converting associative array of HTML attributes to an escaped * attribute string, keys are attribute names, and values are attribute * values. A value of boolean true indicates a minimized attribute. For * example, `['disabled' => 'disabled']` results in `disabled="disabled"`, * but `['disabled' => true]` results in `disabled`. Values of `false` or * `null` will omit the attribute from output. Array values will be * concatenated and space-separated before escaping. * * @param mixed $raw The attribute (or array of attributes) to escaped. * * @return string The escapted attribute string. * */ public function __invoke($raw) { if (!is_array($raw)) { return $this->replace($raw, '/[^a-z0-9,\\.\\-_]/iSu'); } $esc = ''; foreach ($raw as $key => $val) { // do not add null and false values if ($val === null || $val === false) { continue; } // get rid of extra spaces in the key $key = trim($key); // concatenate and space-separate multiple values if (is_array($val)) { $val = implode(' ', $val); } // what kind of attribute representation? if ($val === true) { // minimized $esc .= $this->__invoke($key); } else { // full; because the it is quoted, we can use html ecaping $esc .= $this->__invoke($key) . '="' . $this->html->__invoke($val) . '"'; } // space separator $esc .= ' '; } // done; remove the last space return rtrim($esc); }
public function test__construct() { $escaper = new HtmlEscaper(ENT_NOQUOTES, 'iso-8859-1'); $this->assertSame('iso-8859-1', $escaper->getEncoding()); $this->assertSame(ENT_NOQUOTES, $escaper->getFlags()); }