1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of PHP-Typography. |
4
|
|
|
* |
5
|
|
|
* Copyright 2017-2019 Peter Putzer. |
6
|
|
|
* |
7
|
|
|
* This program is free software; you can redistribute it and/or modify |
8
|
|
|
* it under the terms of the GNU General Public License as published by |
9
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
10
|
|
|
* (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU General Public License along |
18
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc., |
19
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
20
|
|
|
* |
21
|
|
|
* *** |
22
|
|
|
* |
23
|
|
|
* @package mundschenk-at/php-typography |
24
|
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.html |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
namespace PHP_Typography\Fixes\Node_Fixes; |
28
|
|
|
|
29
|
|
|
use PHP_Typography\DOM; |
30
|
|
|
use PHP_Typography\RE; |
31
|
|
|
use PHP_Typography\Settings; |
32
|
|
|
use PHP_Typography\Strings; |
33
|
|
|
use PHP_Typography\U; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Styles initial quotes and guillemets (if enabled). |
37
|
|
|
* |
38
|
|
|
* @author Peter Putzer <[email protected]> |
39
|
|
|
* |
40
|
|
|
* @since 5.0.0 |
41
|
|
|
*/ |
42
|
|
|
class Style_Initial_Quotes_Fix extends Classes_Dependent_Fix { |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* CSS class for single quotes. |
46
|
|
|
* |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
protected $single_quote_class; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* CSS class for double quotes. |
53
|
|
|
* |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
protected $double_quote_class; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Creates a new classes dependent fix. |
60
|
|
|
* |
61
|
|
|
* @param string $css_single Required. |
62
|
|
|
* @param string $css_double Required. |
63
|
|
|
* @param bool $feed_compatible Optional. Default false. |
64
|
|
|
*/ |
65
|
1 |
|
public function __construct( $css_single, $css_double, $feed_compatible = false ) { |
66
|
1 |
|
parent::__construct( [ $css_single, $css_double ], $feed_compatible ); |
67
|
|
|
|
68
|
1 |
|
$this->single_quote_class = $css_single; |
69
|
1 |
|
$this->double_quote_class = $css_double; |
70
|
1 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Apply the fix to a given textnode. |
74
|
|
|
* |
75
|
|
|
* @since 6.0.0 The method was accidentally made public and is now protected. |
76
|
|
|
* |
77
|
|
|
* @param \DOMText $textnode Required. |
78
|
|
|
* @param Settings $settings Required. |
79
|
|
|
* @param bool $is_title Optional. Default false. |
80
|
|
|
*/ |
81
|
10 |
|
protected function apply_internal( \DOMText $textnode, Settings $settings, $is_title = false ) { |
82
|
10 |
|
if ( empty( $settings[ Settings::STYLE_INITIAL_QUOTES ] ) || empty( $settings[ Settings::INITIAL_QUOTE_TAGS ] ) || null !== DOM::get_previous_textnode( $textnode ) ) { |
83
|
5 |
|
return; |
84
|
|
|
} |
85
|
|
|
|
86
|
5 |
|
$node_data = $textnode->data; |
87
|
5 |
|
$f = Strings::functions( $node_data ); |
88
|
5 |
|
$first_character = $f['substr']( $node_data, 0, 1 ); |
89
|
|
|
|
90
|
5 |
|
if ( self::is_single_quote( $first_character ) ) { |
91
|
1 |
|
$span_class = $this->single_quote_class; |
92
|
4 |
|
} elseif ( self::is_double_quote( $first_character ) ) { |
93
|
3 |
|
$span_class = $this->double_quote_class; |
94
|
|
|
} |
95
|
|
|
|
96
|
5 |
|
if ( ! empty( $span_class ) ) { |
97
|
|
|
// Assume page title is <h2>. |
98
|
4 |
|
$block_level_parent = $is_title ? 'h2' : DOM::get_block_parent_name( $textnode ); |
99
|
|
|
|
100
|
4 |
|
if ( ! empty( $block_level_parent ) && isset( $settings[ Settings::INITIAL_QUOTE_TAGS ][ $block_level_parent ] ) ) { |
101
|
3 |
|
$textnode->data = RE::escape_tags( '<span class="' . $span_class . '">' ) . $first_character . RE::escape_tags( '</span>' ) . $f['substr']( $node_data, 1, $f['strlen']( $node_data ) ); |
102
|
|
|
} |
103
|
|
|
} |
104
|
5 |
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Checks if the given string is a "single" quote character. |
108
|
|
|
* |
109
|
|
|
* @param string $quote Required. |
110
|
|
|
* |
111
|
|
|
* @return bool |
112
|
|
|
*/ |
113
|
14 |
|
private static function is_single_quote( $quote ) { |
114
|
14 |
|
return ( "'" === $quote || U::SINGLE_QUOTE_OPEN === $quote || U::SINGLE_LOW_9_QUOTE === $quote || U::SINGLE_ANGLE_QUOTE_OPEN === $quote || U::SINGLE_ANGLE_QUOTE_CLOSE === $quote || ',' === $quote ); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Checks if the given string is a "single" quote character. |
119
|
|
|
* |
120
|
|
|
* @param string $quote Required. |
121
|
|
|
* |
122
|
|
|
* @return bool |
123
|
|
|
*/ |
124
|
14 |
|
private static function is_double_quote( $quote ) { |
125
|
14 |
|
return ( '"' === $quote || U::DOUBLE_QUOTE_OPEN === $quote || U::GUILLEMET_OPEN === $quote || U::GUILLEMET_CLOSE === $quote || U::DOUBLE_LOW_9_QUOTE === $quote ); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|