Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
HtmlTags
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 4
30
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 createAllowedHTMLTags
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
2
 getAllowedHTMLTags
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 checkAllowedHTMLTags
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21namespace MediaWiki\Linter;
22
23class HtmlTags {
24    /**
25     * @var array|null
26     */
27    private static $allowedHtmlTags = null;
28
29    /**
30     * @param SpecialLintErrors|LintErrorsPager $parent
31     */
32    public function __construct( $parent ) {
33        // reuse the allowed html tags array once constructed
34        if ( self::$allowedHtmlTags === null ) {
35            $tagOptionAll = $parent->msg( 'linter-form-tag-option-all' )->escaped();
36            self::$allowedHtmlTags = $this->createAllowedHTMLTags();
37
38            // prepend the translatable word for 'all' to the associative array
39            self::$allowedHtmlTags = array_merge( [ $tagOptionAll => 'all' ], self::$allowedHtmlTags );
40        }
41    }
42
43    /**
44     * Create an associative array out of all valid and deprecated HTML tags
45     * @return array
46     */
47    private function createAllowedHTMLTags(): array {
48        $allowedHtmlTags = [
49            'a',
50            'abbr',
51            'b', 'bdi', 'bdo', 'big', 'blockquote', 'br',
52            'caption', 'center', 'cite', 'code',
53            'data', 'dd', 'del', 'dfn', 'div', 'dl', 'dt',
54            'em',
55            'font',
56            'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr',
57            'i', 'ins',
58            'kbd',
59            'li',
60            'mark',
61            'ol',
62            'p', 'pre',
63            'q',
64            'rb', 'rp', 'rt', 'rtc', 'ruby',
65            's', 'samp', 'small', 'span', 'strike', 'strong', 'sub', 'sup',
66            'table', 'td', 'th', 'time', 'tr', 'tt',
67            'u', 'ul',
68            'var',
69            'wbr',
70        ];
71        // create an associative array where allowed tags are set as both keys and values used
72        // to create UI drop down list in SpecialLintErrors.php and for user search URL string
73        // tag validation for a SQL query in the LinterErrorsPager.php code.
74        return array_combine( $allowedHtmlTags, $allowedHtmlTags );
75    }
76
77    /**
78     * @return array
79     */
80    public function getAllowedHTMLTags(): array {
81        return self::$allowedHtmlTags;
82    }
83
84    /**
85     * @param string $tag
86     * @return bool
87     */
88    public function checkAllowedHTMLTags( string $tag ): bool {
89        return in_array( $tag, self::$allowedHtmlTags, true );
90    }
91
92}