Completed
Push — master ( 552173...c8702c )
by Tobias
12:00 queued 43s
created

Tooltip::buildHtmlElements()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 19
cts 19
cp 1
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 18
nc 2
nop 2
crap 2
1
<?php
2
/**
3
 * Contains the component class for rendering a tooltip.
4
 *
5
 * @copyright (C) 2018, Tobias Oetterer, Paderborn University
6
 * @license       https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License, version 3 (or later)
7
 *
8
 * This file is part of the MediaWiki extension BootstrapComponents.
9
 * The BootstrapComponents extension is free software: you can redistribute it
10
 * and/or modify it under the terms of the GNU General Public License as published
11
 * by the Free Software Foundation, either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * The BootstrapComponents extension is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
21
 *
22
 * @file
23
 * @ingroup       BootstrapComponents
24
 * @author        Tobias Oetterer
25
 */
26
27
namespace BootstrapComponents\Components;
28
29
use BootstrapComponents\AbstractComponent;
30
use \Html;
31
32
/**
33
 * Class Tooltip
34
 *
35
 * Class for component 'tooltip'
36
 *
37
 * @see   https://github.com/oetterer/BootstrapComponents/blob/master/docs/components.md#Tooltip
38
 * @since 1.0
39
 */
40
class Tooltip extends AbstractComponent {
41
	/**
42
	 * @inheritdoc
43
	 *
44
	 * @param string $input
45
	 */
46 5
	public function placeMe( $input ) {
47 5
		if ( empty( $input ) ) {
48 2
			return $this->getParserOutputHelper()->renderErrorMessage( 'bootstrap-components-tooltip-target-missing' );
49
		}
50 4
		$tooltip = $this->getValueFor( 'text' );
51 4
		if ( empty( $tooltip ) ) {
52 2
			return $this->getParserOutputHelper()->renderErrorMessage( 'bootstrap-components-tooltip-content-missing' );
53
		}
54 3
		list( $tag, $input, $attributes ) = $this->buildHtmlElements( $input, (string)$tooltip );
55
56
		return [
57 3
				Html::rawElement(
58 3
				$tag,
59 3
				$attributes,
60
				$input
61 3
			),
62 3
			'isHTML'  => true,
63 3
			'noparse' => true,
64 3
		];
65
	}
66
67
	/**
68
	 * @param string $input
69
	 * @param string $tooltip
70
	 *
71
	 * @return array $tag, $text, $attributes
72
	 */
73 3
	private function buildHtmlElements( $input, $tooltip ) {
74 3
		list ( $class, $style ) = $this->processCss( [ 'bootstrap-tooltip' ], [] );
75
76 3
		list ( $input, $target ) = $this->stripLinksFrom( $input, '' );
77
78
		$attributes = [
79 3
			'class'          => $this->arrayToString( $class, ' ' ),
80 3
			'style'          => $this->arrayToString( $style, ';' ),
81 3
			'id'             => $this->getId(),
82 3
		];
83 3
		if ( empty( $target ) ) {
84
			// this is the normal tooltip process
85 3
			$attributes = array_merge(
86 3
				$attributes,
87
				[
88 3
					'data-toggle'    => 'tooltip',
89 3
					'title'          => $tooltip,
90 3
					'data-placement' => $this->getValueFor( 'placement' ),
91
				]
92 3
			);
93 3
			$tag = "span";
94 3
		} else {
95 1
			$attributes['href'] = $target;
96 1
			$tag = "a";
97
		}
98 3
		return [ $tag, $input, $attributes ];
99
	}
100
101
	/**
102
	 * @param string $text
103
	 * @param string $target
104
	 *
105
	 * @return string[]
106
	 */
107 3
	private function stripLinksFrom( $text, $target ) {
108 3
		if ( preg_match( '~<a.+href=.([^>]+Special:Upload[^"]+)[^>]*>(.+)</a>~', $text, $matches ) ) {
109
			// we have an non existing image as text, return image name as text and upload url as target
110
			// since $text was already parsed and html_encoded and Html::rawElement will do this again,
111
			// we need to decode the html special characters in target aka $matches[1]
112 1
			return [ $matches[2], htmlspecialchars_decode( $matches[1] ) ];
113
		}
114 3
		return [ preg_replace( '~^(.*)(<a.+href=[^>]+>)(.+)(</a>)(.*)$~ms', '\1\3\5', $text ), $target ];
115
	}
116
}