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
|
|
|
* Wraps hanging punctuation in <span class="pull-*"> and <span class="push-*">, if enabled. |
37
|
|
|
* |
38
|
|
|
* @author Peter Putzer <[email protected]> |
39
|
|
|
* |
40
|
|
|
* @since 5.0.0 |
41
|
|
|
*/ |
42
|
|
|
class Style_Hanging_Punctuation_Fix extends Classes_Dependent_Fix { |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* CSS class for single-width punctuation marks. |
46
|
|
|
* |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
protected $push_single_class; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* CSS class for double-width punctuation marks. |
53
|
|
|
* |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
protected $push_double_class; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* CSS class for single-width punctuation marks. |
60
|
|
|
* |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
protected $pull_single_class; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* CSS class for double-width punctuation marks. |
67
|
|
|
* |
68
|
|
|
* @var string |
69
|
|
|
*/ |
70
|
|
|
protected $pull_double_class; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* An array of replacment arrays (indexed by the "$block" flag). |
74
|
|
|
* |
75
|
|
|
* @var array |
76
|
|
|
*/ |
77
|
|
|
protected $replacements; |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
// Hanging punctuation. |
81
|
|
|
const _DOUBLE_HANGING_PUNCTUATION = |
82
|
|
|
'"' . |
83
|
|
|
U::DOUBLE_QUOTE_OPEN . |
84
|
|
|
U::DOUBLE_QUOTE_CLOSE . |
85
|
|
|
U::DOUBLE_LOW_9_QUOTE . |
86
|
|
|
U::DOUBLE_PRIME; // requires modifiers: x (multiline pattern) u (utf8). |
87
|
|
|
|
88
|
|
|
const _SINGLE_HANGING_PUNCTUATION = |
89
|
|
|
"'" . |
90
|
|
|
U::SINGLE_QUOTE_OPEN . |
91
|
|
|
U::SINGLE_QUOTE_CLOSE . |
92
|
|
|
U::SINGLE_LOW_9_QUOTE . |
93
|
|
|
U::SINGLE_PRIME . |
94
|
|
|
U::APOSTROPHE; // requires modifiers: x (multiline pattern) u (utf8). |
95
|
|
|
|
96
|
|
|
// Style hanging punctuation. |
97
|
|
|
const STYLE_DOUBLE = '/(\s)([' . self::_DOUBLE_HANGING_PUNCTUATION . '])(\w+)/S'; |
98
|
|
|
const STYLE_SINGLE = '/(\s)([' . self::_SINGLE_HANGING_PUNCTUATION . '])(\w+)/S'; |
99
|
|
|
const STYLE_INITIAL_DOUBLE = '/(?:\A)([' . self::_DOUBLE_HANGING_PUNCTUATION . '])(\w+)/S'; |
100
|
|
|
const STYLE_INITIAL_SINGLE = '/(?:\A)([' . self::_SINGLE_HANGING_PUNCTUATION . '])(\w+)/S'; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Creates a new classes dependent fix. |
104
|
|
|
* |
105
|
|
|
* @param string $push_single_class Required. |
106
|
|
|
* @param string $push_double_class Required. |
107
|
|
|
* @param string $pull_single_class Required. |
108
|
|
|
* @param string $pull_double_class Required. |
109
|
|
|
* @param bool $feed_compatible Optional. Default false. |
110
|
|
|
*/ |
111
|
1 |
|
public function __construct( $push_single_class, $push_double_class, $pull_single_class, $pull_double_class, $feed_compatible = false ) { |
112
|
1 |
|
parent::__construct( [ $pull_single_class, $pull_double_class ], $feed_compatible ); |
113
|
|
|
|
114
|
1 |
|
$this->push_single_class = $push_single_class; |
115
|
1 |
|
$this->push_double_class = $push_double_class; |
116
|
1 |
|
$this->pull_single_class = $pull_single_class; |
117
|
1 |
|
$this->pull_double_class = $pull_double_class; |
118
|
|
|
|
119
|
1 |
|
$this->replacements = [ |
120
|
|
|
false => [ |
121
|
1 |
|
RE::escape_tags( '$1<span class="' . $this->push_double_class . '"></span>' . U::ZERO_WIDTH_SPACE . '<span class="' . $this->pull_double_class . '">$2</span>$3' ), |
122
|
1 |
|
RE::escape_tags( '$1<span class="' . $this->push_single_class . '"></span>' . U::ZERO_WIDTH_SPACE . '<span class="' . $this->pull_single_class . '">$2</span>$3' ), |
123
|
1 |
|
RE::escape_tags( '<span class="' . $this->push_double_class . '"></span>' . U::ZERO_WIDTH_SPACE . '<span class="' . $this->pull_double_class . '">$1</span>$2' ), |
124
|
1 |
|
RE::escape_tags( '<span class="' . $this->push_single_class . '"></span>' . U::ZERO_WIDTH_SPACE . '<span class="' . $this->pull_single_class . '">$1</span>$2' ), |
125
|
|
|
], |
126
|
|
|
true => [ |
127
|
1 |
|
RE::escape_tags( '$1<span class="' . $this->push_double_class . '"></span>' . U::ZERO_WIDTH_SPACE . '<span class="' . $this->pull_double_class . '">$2</span>$3' ), |
128
|
1 |
|
RE::escape_tags( '$1<span class="' . $this->push_single_class . '"></span>' . U::ZERO_WIDTH_SPACE . '<span class="' . $this->pull_single_class . '">$2</span>$3' ), |
129
|
1 |
|
RE::escape_tags( '<span class="' . $this->pull_double_class . '">$1</span>$2' ), |
130
|
1 |
|
RE::escape_tags( '<span class="' . $this->pull_single_class . '">$1</span>$2' ), |
131
|
|
|
], |
132
|
|
|
]; |
133
|
1 |
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Apply the fix to a given textnode. |
137
|
|
|
* |
138
|
|
|
* @since 6.0.0 The method was accidentally made public and is now protected. |
139
|
|
|
* |
140
|
|
|
* @param \DOMText $textnode Required. |
141
|
|
|
* @param Settings $settings Required. |
142
|
|
|
* @param bool $is_title Optional. Default false. |
143
|
|
|
*/ |
144
|
6 |
|
protected function apply_internal( \DOMText $textnode, Settings $settings, $is_title = false ) { |
145
|
6 |
|
if ( empty( $settings[ Settings::STYLE_HANGING_PUNCTUATION ] ) ) { |
146
|
3 |
|
return; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
// Look for the first textnode. |
150
|
3 |
|
$firstnode = DOM::get_first_textnode( DOM::get_block_parent( $textnode ) ); |
151
|
3 |
|
$block = null === $firstnode || $textnode === $firstnode; |
152
|
|
|
|
153
|
|
|
// Need to get context of adjacent characters outside adjacent inline tags or HTML comment |
154
|
|
|
// if we have adjacent characters add them to the text. |
155
|
3 |
|
$next_character = DOM::get_next_chr( $textnode ); |
156
|
3 |
|
$node_data = "{$textnode->data}$next_character"; // We have no interest in preceeding characters for this fix. |
157
|
3 |
|
$f = Strings::functions( $node_data ); |
158
|
|
|
|
159
|
3 |
|
$node_data = \preg_replace( |
160
|
|
|
[ |
161
|
3 |
|
self::STYLE_DOUBLE . $f['u'], |
162
|
3 |
|
self::STYLE_SINGLE . $f['u'], |
163
|
3 |
|
self::STYLE_INITIAL_DOUBLE . $f['u'], |
164
|
3 |
|
self::STYLE_INITIAL_SINGLE . $f['u'], |
165
|
|
|
], |
166
|
3 |
|
$this->replacements[ $block ], |
167
|
3 |
|
$node_data |
168
|
|
|
); |
169
|
|
|
|
170
|
|
|
// Remove any added characters. |
171
|
3 |
|
$textnode->data = self::remove_adjacent_characters( $node_data, $f['strlen'], $f['substr'], 0, $f['strlen']( $next_character ) ); |
172
|
3 |
|
} |
173
|
|
|
} |
174
|
|
|
|