44 # text with no html simulates an interface message string or a title
45 $textWithNoHtml =
'This could be an article title';
46 $textWithHtmlSm =
'Before <wrap><in>and</in> another <unclose> <in>word</in></wrap>.';
47 $textWithHtmlLg = str_repeat(
56 if ( !$method || $method ===
'validateEmail' ) {
57 $benches[
'Sanitizer::validateEmail (valid)'] =
static function () {
58 Sanitizer::validateEmail(
'user@example.org' );
60 $benches[
'Sanitizer::validateEmail (invalid)'] =
static function () {
61 Sanitizer::validateEmail(
'username@example! org' );
64 if ( !$method || $method ===
'encodeAttribute' ) {
65 $benches[
'Sanitizer::encodeAttribute (simple)'] =
static function () {
66 Sanitizer::encodeAttribute(
'simple' );
68 $benches[
'Sanitizer::encodeAttribute (special)'] =
static function () {
69 Sanitizer::encodeAttribute(
":'\"\n https://example" );
72 if ( !$method || $method ===
'safeEncodeAttribute' ) {
73 $benches[
'Sanitizer::safeEncodeAttribute (simple)'] =
static function () {
74 Sanitizer::safeEncodeAttribute(
'simple' );
76 $benches[
'Sanitizer::safeEncodeAttribute (special)'] =
static function () {
77 Sanitizer::safeEncodeAttribute(
":'\"\n https://example" );
80 if ( !$method || $method ===
'internalRemoveHtmlTags' ) {
81 $tiny = strlen( $textWithNoHtml );
82 $sm = strlen( $textWithHtmlSm );
83 $lg = round( strlen( $textWithHtmlLg ) / 1000 ) .
'K';
84 $benches[
"Sanitizer::internalRemoveHtmlTags (input: $tiny)"] =
static function () use ( $textWithNoHtml ) {
85 Sanitizer::internalRemoveHtmlTags( $textWithNoHtml );
87 $benches[
"Sanitizer::internalRemoveHtmlTags (input: $sm)"] =
static function () use ( $textWithHtmlSm ) {
88 Sanitizer::internalRemoveHtmlTags( $textWithHtmlSm );
90 $benches[
"Sanitizer::internalRemoveHtmlTags (input: $lg)"] =
static function () use ( $textWithHtmlLg ) {
91 Sanitizer::internalRemoveHtmlTags( $textWithHtmlLg );
94 if ( !$method || $method ===
'tidy' ) {
95 # This matches what DISPLAYTITLE was previously doing to sanitize
97 $tiny = strlen( $textWithNoHtml );
98 $sm = strlen( $textWithHtmlSm );
99 $lg = round( strlen( $textWithHtmlLg ) / 1000 ) .
'K';
100 $doit =
static function ( $text ) {
101 return static function () use ( $text ) {
102 $tidy = new \MediaWiki\Tidy\RemexDriver(
103 new \
MediaWiki\Config\ServiceOptions( [ MainConfigNames::TidyConfig ], [
104 MainConfigNames::TidyConfig => [
'pwrap' =>
false ],
106 $textWithTags = $tidy->tidy( $text, [ Sanitizer::class,
'armorFrenchSpaces' ] );
107 $textWithTags = Sanitizer::normalizeCharReferences(
108 Sanitizer::internalRemoveHtmlTags( $textWithTags )
112 $benches[
"DISPLAYTITLE tidy (input: $tiny)"] = $doit( $textWithNoHtml );
113 $benches[
"DISPLAYTITLE tidy (input: $sm)"] = $doit( $textWithHtmlSm );
114 $benches[
"DISPLAYTITLE tidy (input: $lg)"] = $doit( $textWithHtmlLg );
116 if ( !$method || $method ===
'removeSomeTags' ) {
117 $tiny = strlen( $textWithNoHtml );
118 $sm = strlen( $textWithHtmlSm );
119 $lg = round( strlen( $textWithHtmlLg ) / 1000 ) .
'K';
120 $benches[
"Sanitizer::removeSomeTags (input: $tiny)"] =
static function () use ( $textWithNoHtml ) {
121 Sanitizer::removeSomeTags( $textWithNoHtml );
123 $benches[
"Sanitizer::removeSomeTags (input: $sm)"] =
static function () use ( $textWithHtmlSm ) {
124 Sanitizer::removeSomeTags( $textWithHtmlSm );
126 $benches[
"Sanitizer::removeSomeTags (input: $lg)"] =
static function () use ( $textWithHtmlLg ) {
127 Sanitizer::removeSomeTags( $textWithHtmlLg );
130 if ( !$method || $method ===
'stripAllTags' ) {
131 $sm = strlen( $textWithHtmlSm );
132 $lg = round( strlen( $textWithHtmlLg ) / 1000 ) .
'K';
133 $benches[
"Sanitizer::stripAllTags (input: $sm)"] =
static function () use ( $textWithHtmlSm ) {
134 Sanitizer::stripAllTags( $textWithHtmlSm );
136 $benches[
"Sanitizer::stripAllTags (input: $lg)"] =
static function () use ( $textWithHtmlLg ) {
137 Sanitizer::stripAllTags( $textWithHtmlLg );
141 $this->
bench( $benches );