1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of PHP-Typography. |
4
|
|
|
* |
5
|
|
|
* Copyright 2014-2019 Peter Putzer. |
6
|
|
|
* Copyright 2009-2011 KINGdesk, LLC. |
7
|
|
|
* |
8
|
|
|
* This program is free software; you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU General Public License as published by |
10
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
11
|
|
|
* (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* This program is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU General Public License along |
19
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc., |
20
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
21
|
|
|
* |
22
|
|
|
* *** |
23
|
|
|
* |
24
|
|
|
* @package mundschenk-at/php-typography |
25
|
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.html |
26
|
|
|
*/ |
27
|
|
|
|
28
|
|
|
namespace PHP_Typography\Fixes\Node_Fixes; |
29
|
|
|
|
30
|
|
|
use PHP_Typography\DOM; |
31
|
|
|
use PHP_Typography\RE; |
32
|
|
|
use PHP_Typography\Settings; |
33
|
|
|
use PHP_Typography\Strings; |
34
|
|
|
use PHP_Typography\U; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Collapse spaces (if enabled). |
38
|
|
|
* |
39
|
|
|
* @author Peter Putzer <[email protected]> |
40
|
|
|
* |
41
|
|
|
* @since 5.0.0 |
42
|
|
|
*/ |
43
|
|
|
class Space_Collapse_Fix extends Abstract_Node_Fix { |
44
|
|
|
|
45
|
|
|
const COLLAPSE_NORMAL_SPACES = '/[' . RE::NORMAL_SPACES . ']+/Sxu'; |
46
|
|
|
const COLLAPSE_NON_BREAKABLE_SPACES = '/(?:[' . RE::NORMAL_SPACES . ']|' . RE::HTML_SPACES . ')*' . U::NO_BREAK_SPACE . '(?:[' . RE::NORMAL_SPACES . ']|' . RE::HTML_SPACES . ')*/Sxu'; |
47
|
|
|
const COLLAPSE_OTHER_SPACES = '/(?:[' . RE::NORMAL_SPACES . '])*(' . RE::HTML_SPACES . ')(?:[' . RE::NORMAL_SPACES . ']|' . RE::HTML_SPACES . ')*/Sxu'; |
48
|
|
|
const COLLAPSE_SPACES_AT_START_OF_BLOCK = '/\A(?:[' . RE::NORMAL_SPACES . ']|' . RE::HTML_SPACES . ')+/Sxu'; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Apply the fix to a given textnode. |
52
|
|
|
* |
53
|
|
|
* @param \DOMText $textnode Required. |
54
|
|
|
* @param Settings $settings Required. |
55
|
|
|
* @param bool $is_title Optional. Default false. |
56
|
|
|
*/ |
57
|
6 |
|
public function apply( \DOMText $textnode, Settings $settings, $is_title = false ) { |
58
|
6 |
|
if ( empty( $settings[ Settings::SPACE_COLLAPSE ] ) ) { |
59
|
3 |
|
return; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// Cache textnode content. |
63
|
3 |
|
$node_data = $textnode->data; |
64
|
|
|
|
65
|
|
|
// Replace spaces. |
66
|
3 |
|
$node_data = \preg_replace( |
67
|
|
|
[ |
68
|
|
|
// Normal spacing. |
69
|
3 |
|
self::COLLAPSE_NORMAL_SPACES, |
70
|
|
|
// Non-breakable space get's priority. If non-breakable space exists in a string of spaces, it collapses to a single non-breakable space. |
71
|
3 |
|
self::COLLAPSE_NON_BREAKABLE_SPACES, |
72
|
|
|
// For any other spaceing, replace with the first occurance of an unusual space character. |
73
|
3 |
|
self::COLLAPSE_OTHER_SPACES, |
74
|
|
|
], |
75
|
|
|
[ // @codeCoverageIgnoreStart |
76
|
|
|
' ', |
77
|
|
|
U::NO_BREAK_SPACE, |
78
|
|
|
'$1', |
79
|
|
|
], // @codeCoverageIgnoreEnd |
80
|
3 |
|
$node_data |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
// Remove all spacing at beginning of block level elements. |
84
|
3 |
|
if ( null === DOM::get_previous_textnode( $textnode ) ) { |
85
|
3 |
|
$node_data = \preg_replace( self::COLLAPSE_SPACES_AT_START_OF_BLOCK, '', $node_data ); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// Restore textnode content. |
89
|
3 |
|
$textnode->data = $node_data; |
90
|
3 |
|
} |
91
|
|
|
} |
92
|
|
|
|