Kses final class
Minn\Support\Kses · public/minn/src/Minn/Support/Kses.php · 481 lines
The HTML a user without unfiltered_html may store. Tags outside the allowlist are removed and their text kept; attributes outside the tag's list are dropped; URL attributes lose unsafe schemes; style attributes keep only listed properties and no code. Comments keep their text, and block delimiters are written back with their attribute values filtered.
Constants and properties
| const | COMMENT = array ( 'a' => array ( 0 => 'href', 1 => 'title', 2 => 'rel', ), 'abbr' => array ( 0 => 'title', ), 'acronym' => array ( 0 => 'title', ), 'b' => array ( ), 'blockquote' => array ( 0 => 'cite', ), 'cite' => array ( ), 'code' => array ( ), 'del' => array ( 0 => 'datetime', ), 'em' => array ( ), 'i' => array ( ), 'q' => array ( 0 => 'cite', ), 's' => array ( ), 'strike' => array ( ), 'strong' => array ( ), ) | |
| const | URI_ATTRIBUTES = array ( 0 => 'action', 1 => 'archive', 2 => 'background', 3 => 'cite', 4 => 'classid', 5 => 'codebase', 6 => 'data', 7 => 'formaction', 8 => 'href', 9 => 'icon', 10 => 'longdesc', 11 => 'manifest', 12 => 'poster', 13 => 'profile', 14 => 'src', 15 => 'usemap', 16 => 'xmlns', ) | Every attribute the reference treats as holding a URI, so its scheme is judged wherever the attribute is allowed. |
| const | SCHEMES = array ( 0 => 'http', 1 => 'https', 2 => 'ftp', 3 => 'ftps', 4 => 'mailto', 5 => 'news', 6 => 'irc', 7 => 'gopher', 8 => 'nntp', 9 => 'feed', 10 => 'telnet', 11 => 'mms', 12 => 'rtsp', 13 => 'sms', 14 => 'svn', 15 => 'tel', 16 => 'fax', 17 => 'xmpp', 18 => 'webcal', 19 => 'urn', ) | The URI schemes allowed by default. |
| private | REFERENCE = '/&(#[0-9]+|#[xX][0-9a-fA-F]+|[a-zA-Z][a-zA-Z0-9]{0,31});/' | |
| private | STRAY_AMPERSAND = '/&(?!(?:#[0-9]+|#[xX][0-9a-fA-F]+|[a-zA-Z][a-zA-Z0-9]{0,31});)/' | |
| private | MARKUP = array ( '&' => '&', '<' => '<', '>' => '>', '"' => '"', '\'' => ''', ) |
Public
| static | post(string $html): string# | Post content as an author without unfiltered_html may store it. | |||
| static | comment(string $html): string# | A comment, profile or term description as anyone without unfiltered_html may store it: listed attributes only. | |||
| static | sanitize(string $html, Minn\Support\KsesPolicy $policy): string# | The whole pass wp_kses makes with its default hooks: control characters out, references normalized, a "<" that opens no tag escaped, block attribute values filtered the same way, then the tags. | |||
| static | withoutControls(string $html): string# | The control characters kses removes (tab, newline and carriage return stay). | |||
| static | normalizeEntities(string $html): string# | Every "&" made a reference, in one pass: a known name or a code point kses accepts stays one (decimal padded to three digits, hex with a lower-case x), anything else becomes "&". | |||
| static | lessThan(string $html, ?Closure $escape = NULL): string# | A "<" that reaches the next "<" or the end without a ">" is text: the run is escaped the way esc_html escapes it (quotes included).
| |||
| static | escapeHtml(string $text): string# | Text escaped for HTML with references kept: invalid UTF-8 gives nothing, quotes become " and '. | |||
| static | blockAttributes(string $html, Closure $clean): string# | Block markup with every attribute key and string value passed through the same filter, and the delimiters written back in their canonical form ("--->" read as "-->"). Markup without a comment is left alone.
| |||
| static | filter(string $html, Minn\Support\KsesPolicy $policy): string# | The tag pass: each run from "<" to the next ">" (or the end) is a comment, an inert bogus comment ("</" before a non-letter, "<!" before a lower-case letter), or an element judged by the policy; anything else in angle brackets goes. Text between keeps its references, with stray ones escaped. Run alone (wp_kses_split), a last "<" with no ">" still reads as a tag; inside sanitize() the less-than pass has already escaped it. | |||
| static | tag(string $run, Minn\Support\KsesPolicy $policy): string# | One run of the tag pass on its own (wp_kses_split2): a comment, a tag, or, for anything not starting with "<", a lone ">". | |||
| static | element(string $element, string $attributes, Minn\Support\KsesPolicy $policy): string# | An element rebuilt with the attributes the policy allows it (wp_kses_attr), its name as written and an XHTML slash kept; an element the policy does not know keeps none. | |||
| static | attributeList(string $raw): ?array# | The attributes written inside a tag, read the way kses reads them: a name, then "=" and a quoted or bare value, or no value at all; junk between (stray quotes, "=", "/") is skipped; the first of two same-named attributes wins. Null when a quoted value never closes, which costs the tag every attribute. @return array<string, array{name: string, value: ?string, whole: string}>|null | |||
| static | text(string $value): string# | Plain text: tags gone, whitespace collapsed, control characters dropped. | |||
| static | url(string $url): string# | A URL for a stored field: empty when its scheme is not one the reference allows. The scheme is judged as a browser reads it, without the tabs, line breaks and other control characters it drops (java, a tab, then script: is javascript:). | |||
| static | attributeUrl(string $url, array $schemes = self::SCHEMES): string# | A URL inside markup: whatever stands before the first colon, once every character reference and percent escape is decoded as deep as it goes and the invisible characters are dropped, must be an allowed scheme, or it is cut off and the rest is judged again. The reference cuts "?q=a:b" to "b" and "javascript:alert(1)//http://" to "//" the same way. A value with a good scheme is returned as given.
| |||
| static | validCodePoint(int $code): bool# | Whether a code point is one a reference may name (valid_unicode): tab, line feed, carriage return, and the rest of Unicode from space up, except the surrogates and U+FFFE and U+FFFF. | |||
| static | pdfObject(string $url, string $uploadsUrl): bool# | Whether a URL may be an object's data in post content: an http or https URL on the uploads host and port, no credentials, query or fragment, whose path ends in ".pdf". |
Internals 12
Private and protected methods: how the class does its work. Not a contract; they change whenever the code is tidied.
| private | cleanValue(mixed $value, Closure $clean): mixed# |
| |||
| private | run(string $run, Minn\Support\KsesPolicy $policy): string# | One bracketed run as the tag pass keeps it, or nothing. | |||
| private | htmlComment(string $body, Minn\Support\KsesPolicy $policy): string# | A comment kept whole, its body run through the same pass (a ">" in it escaped, tags judged): no "--" inside, an empty one dropped, an unclosed one closed. | |||
| private | deepDecode(string $value): string# | A value with its references and percent escapes decoded until nothing changes, so no encoding hides a scheme. | |||
| private | visible(string $text): string# | The text with whitespace, control characters, no-break spaces, and replacement characters removed. | |||
| private | normalizeText(string $text): string# | Text between tags as the reference stores it: valid references stay references (decimal ones padded to three digits), invalid ones and stray ampersands become "&", and a closing bracket is escaped. | |||
| private | normalizeAttribute(string $value): string# | An attribute value as the reference stores it: valid references become their characters, except that the five markup characters stay escaped (an apostrophe as '); invalid references and stray ampersands become "&"; raw quotes are escaped. | |||
| private | named(string $name): ?string# | What a named reference stands for, or null when the name is not one. | |||
| private | codePoint(string $body): ?int# | A numeric reference's code point, or null when it names no character the reference accepts. | |||
| private | attributes(string $raw, string $tag, Minn\Support\KsesPolicy $policy): string# | The attributes the policy allows, in the order written: a value must meet its rules, a URI keeps only an allowed scheme, srcset and style are judged on their own terms. A tag that loses a required attribute keeps none at all. | |||
| private | rendered(string $name, ?string $value, Minn\Support\KsesPolicy $policy): ?string# | One allowed attribute as stored, or null when its value is judged away entirely. | |||
| private | srcset(string $value): string# | Every candidate must be an absolute URL with a safe scheme, or the attribute goes. |
Nothing matches.