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\Settings; |
31
|
|
|
use PHP_Typography\DOM; |
32
|
|
|
use PHP_Typography\U; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Prevents values being split from their units (if enabled). |
36
|
|
|
* |
37
|
|
|
* @author Peter Putzer <[email protected]> |
38
|
|
|
* |
39
|
|
|
* @since 5.0.0 |
40
|
|
|
*/ |
41
|
|
|
class Unit_Spacing_Fix extends Simple_Regex_Replacement_Fix { |
42
|
|
|
|
43
|
|
|
const REPLACEMENT = '$1' . U::NO_BREAK_NARROW_SPACE . '$2'; |
44
|
|
|
const REGEX = '/(\d\.?)\s(' . self::_STANDARD_UNITS . ')' . self::WORD_BOUNDARY . '/Sxu'; |
45
|
|
|
|
46
|
|
|
const _STANDARD_UNITS = ' |
47
|
|
|
### Temporal units |
48
|
|
|
(?:ms|s|secs?|mins?|hrs?)\.?| |
49
|
|
|
milliseconds?|seconds?|minutes?|hours?|days?|years?|decades?|century|centuries|millennium|millennia| |
50
|
|
|
|
51
|
|
|
### Imperial units |
52
|
|
|
(?:in|ft|yd|mi)\.?| |
53
|
|
|
(?:ac|ha|oz|pt|qt|gal|lb|st)\.? |
54
|
|
|
s\.f\.|sf|s\.i\.|si|square[ ]feet|square[ ]foot| |
55
|
|
|
inch|inches|foot|feet|yards?|miles?|acres?|hectares?|ounces?|pints?|quarts?|gallons?|pounds?|stones?| |
56
|
|
|
|
57
|
|
|
### Metric units (with prefixes) |
58
|
|
|
(?:p|µ|[mcdhkMGT])? |
59
|
|
|
(?:[mgstAKNJWCVFSTHBL]|mol|cd|rad|Hz|Pa|Wb|lm|lx|Bq|Gy|Sv|kat|Ω)| |
60
|
|
|
(?:nano|micro|milli|centi|deci|deka|hecto|kilo|mega|giga|tera)? |
61
|
|
|
(?:liters?|meters?|grams?|newtons?|pascals?|watts?|joules?|amperes?)| |
62
|
|
|
|
63
|
|
|
### Computers units (KB, Kb, TB, Kbps) |
64
|
|
|
[kKMGT]?(?:[oBb]|[oBb]ps|flops)| |
65
|
|
|
|
66
|
|
|
### Money |
67
|
|
|
¢|M?(?:£|¥|€|\$)| |
68
|
|
|
|
69
|
|
|
### Other units |
70
|
|
|
°[CF]? | |
71
|
|
|
%|pi|M?px|em|en|[NSEOW]|[NS][EOW]|mbar |
72
|
|
|
'; // required modifiers: x (multiline pattern), u (unicode). |
73
|
|
|
|
74
|
|
|
// (?=\p{^L})|\z) is used instead of \b because otherwise the special symbols ($, € etc.) would not match properly (they are not word characters). |
75
|
|
|
const WORD_BOUNDARY = '(?:(?=\p{^L})|\z)'; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Creates a new fix object. |
79
|
|
|
* |
80
|
|
|
* @param bool $feed_compatible Optional. Default false. |
81
|
|
|
*/ |
82
|
28 |
|
public function __construct( $feed_compatible = false ) { |
83
|
28 |
|
parent::__construct( self::REGEX, self::REPLACEMENT, Settings::UNIT_SPACING, $feed_compatible ); |
84
|
28 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Apply the fix to a given textnode. |
88
|
|
|
* |
89
|
|
|
* @param \DOMText $textnode Required. |
90
|
|
|
* @param Settings $settings Required. |
91
|
|
|
* @param bool $is_title Optional. Default false. |
92
|
|
|
*/ |
93
|
28 |
|
public function apply( \DOMText $textnode, Settings $settings, $is_title = false ) { |
94
|
|
|
// Update regex with custom units. |
95
|
28 |
|
$this->regex = "/(\d\.?)\s({$settings->custom_units()}" . self::_STANDARD_UNITS . ')' . self::WORD_BOUNDARY . '/Sxu'; |
96
|
|
|
|
97
|
28 |
|
parent::apply( $textnode, $settings, $is_title ); |
98
|
28 |
|
} |
99
|
|
|
} |
100
|
|
|
|