31 # text with no html simulates an interface message string or a title
32 $textWithNoHtml =
'This could be an article title';
33 $textWithHtmlSm =
'Before <wrap><in>and</in> another <unclose> <in>word</in></wrap>.';
34 $textWithHtmlLg = str_repeat(
43 if ( !$method || $method ===
'validateEmail' ) {
44 $benches[
'Sanitizer::validateEmail (valid)'] =
static function () {
45 Sanitizer::validateEmail(
'user@example.org' );
47 $benches[
'Sanitizer::validateEmail (invalid)'] =
static function () {
48 Sanitizer::validateEmail(
'username@example! org' );
51 if ( !$method || $method ===
'encodeAttribute' ) {
52 $benches[
'Sanitizer::encodeAttribute (simple)'] =
static function () {
53 Sanitizer::encodeAttribute(
'simple' );
55 $benches[
'Sanitizer::encodeAttribute (special)'] =
static function () {
56 Sanitizer::encodeAttribute(
":'\"\n https://example" );
59 if ( !$method || $method ===
'safeEncodeAttribute' ) {
60 $benches[
'Sanitizer::safeEncodeAttribute (simple)'] =
static function () {
61 Sanitizer::safeEncodeAttribute(
'simple' );
63 $benches[
'Sanitizer::safeEncodeAttribute (special)'] =
static function () {
64 Sanitizer::safeEncodeAttribute(
":'\"\n https://example" );
67 if ( !$method || $method ===
'internalRemoveHtmlTags' ) {
68 $tiny = strlen( $textWithNoHtml );
69 $sm = strlen( $textWithHtmlSm );
70 $lg = round( strlen( $textWithHtmlLg ) / 1000 ) .
'K';
71 $benches[
"Sanitizer::internalRemoveHtmlTags (input: $tiny)"] =
static function () use ( $textWithNoHtml ) {
72 Sanitizer::internalRemoveHtmlTags( $textWithNoHtml );
74 $benches[
"Sanitizer::internalRemoveHtmlTags (input: $sm)"] =
static function () use ( $textWithHtmlSm ) {
75 Sanitizer::internalRemoveHtmlTags( $textWithHtmlSm );
77 $benches[
"Sanitizer::internalRemoveHtmlTags (input: $lg)"] =
static function () use ( $textWithHtmlLg ) {
78 Sanitizer::internalRemoveHtmlTags( $textWithHtmlLg );
81 if ( !$method || $method ===
'tidy' ) {
82 # This matches what DISPLAYTITLE was previously doing to sanitize
84 $tiny = strlen( $textWithNoHtml );
85 $sm = strlen( $textWithHtmlSm );
86 $lg = round( strlen( $textWithHtmlLg ) / 1000 ) .
'K';
87 $doit =
static function ( $text ) {
88 return static function () use ( $text ) {
89 $tidy = new \MediaWiki\Tidy\RemexDriver(
90 new \
MediaWiki\Config\ServiceOptions( [ MainConfigNames::TidyConfig ], [
91 MainConfigNames::TidyConfig => [
'pwrap' =>
false ],
93 $textWithTags = $tidy->tidy( $text, Sanitizer::armorFrenchSpaces( ... ) );
94 $textWithTags = Sanitizer::normalizeCharReferences(
95 Sanitizer::internalRemoveHtmlTags( $textWithTags )
99 $benches[
"DISPLAYTITLE tidy (input: $tiny)"] = $doit( $textWithNoHtml );
100 $benches[
"DISPLAYTITLE tidy (input: $sm)"] = $doit( $textWithHtmlSm );
101 $benches[
"DISPLAYTITLE tidy (input: $lg)"] = $doit( $textWithHtmlLg );
103 if ( !$method || $method ===
'removeSomeTags' ) {
104 $tiny = strlen( $textWithNoHtml );
105 $sm = strlen( $textWithHtmlSm );
106 $lg = round( strlen( $textWithHtmlLg ) / 1000 ) .
'K';
107 $benches[
"Sanitizer::removeSomeTags (input: $tiny)"] =
static function () use ( $textWithNoHtml ) {
108 Sanitizer::removeSomeTags( $textWithNoHtml );
110 $benches[
"Sanitizer::removeSomeTags (input: $sm)"] =
static function () use ( $textWithHtmlSm ) {
111 Sanitizer::removeSomeTags( $textWithHtmlSm );
113 $benches[
"Sanitizer::removeSomeTags (input: $lg)"] =
static function () use ( $textWithHtmlLg ) {
114 Sanitizer::removeSomeTags( $textWithHtmlLg );
117 if ( !$method || $method ===
'stripAllTags' ) {
118 $sm = strlen( $textWithHtmlSm );
119 $lg = round( strlen( $textWithHtmlLg ) / 1000 ) .
'K';
120 $benches[
"Sanitizer::stripAllTags (input: $sm)"] =
static function () use ( $textWithHtmlSm ) {
121 Sanitizer::stripAllTags( $textWithHtmlSm );
123 $benches[
"Sanitizer::stripAllTags (input: $lg)"] =
static function () use ( $textWithHtmlLg ) {
124 Sanitizer::stripAllTags( $textWithHtmlLg );
128 $this->
bench( $benches );