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; |
29
|
|
|
|
30
|
|
|
use PHP_Typography\Settings\Dash_Style; |
31
|
|
|
use PHP_Typography\Settings\Quote_Style; |
32
|
|
|
use PHP_Typography\Settings\Quotes; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Store settings for the PHP_Typography class. |
36
|
|
|
* |
37
|
|
|
* @author Peter Putzer <[email protected]> |
38
|
|
|
* |
39
|
|
|
* @since 4.0.0 |
40
|
|
|
* @since 6.5.0 The protected property $no_break_narrow_space has been deprecated. |
41
|
|
|
*/ |
42
|
|
|
class Settings implements \ArrayAccess, \JsonSerializable { |
43
|
|
|
|
44
|
|
|
// General attributes. |
45
|
|
|
const IGNORE_TAGS = 'ignoreTags'; |
46
|
|
|
const IGNORE_CLASSES = 'ignoreClasses'; |
47
|
|
|
const IGNORE_IDS = 'ignoreIDs'; |
48
|
|
|
|
49
|
|
|
// Smart characters. |
50
|
|
|
const SMART_QUOTES = 'smartQuotes'; |
51
|
|
|
const SMART_QUOTES_EXCEPTIONS = 'smartQuotesExceptions'; |
52
|
|
|
const SMART_DASHES = 'smartDashes'; |
53
|
|
|
const SMART_ELLIPSES = 'smartEllipses'; |
54
|
|
|
const SMART_DIACRITICS = 'smartDiacritics'; |
55
|
|
|
const DIACRITIC_LANGUAGE = 'diacriticLanguage'; |
56
|
|
|
const DIACRITIC_WORDS = 'diacriticWords'; |
57
|
|
|
const DIACRITIC_REPLACEMENT_DATA = 'diacriticReplacement'; |
58
|
|
|
const DIACRITIC_CUSTOM_REPLACEMENTS = 'diacriticCustomReplacements'; |
59
|
|
|
const SMART_MARKS = 'smartMarks'; |
60
|
|
|
const SMART_ORDINAL_SUFFIX = 'smartOrdinalSuffix'; |
61
|
|
|
const SMART_ORDINAL_SUFFIX_ROMAN_NUMERALS = 'smartOrdinalSuffixRomanNumerals'; |
62
|
|
|
const SMART_MATH = 'smartMath'; |
63
|
|
|
const SMART_FRACTIONS = 'smartFractions'; |
64
|
|
|
const SMART_EXPONENTS = 'smartExponents'; |
65
|
|
|
const SMART_AREA_UNITS = 'smartAreaVolumeUnits'; |
66
|
|
|
|
67
|
|
|
// Smart spacing. |
68
|
|
|
const SINGLE_CHARACTER_WORD_SPACING = 'singleCharacterWordSpacing'; |
69
|
|
|
const FRACTION_SPACING = 'fractionSpacing'; |
70
|
|
|
const UNIT_SPACING = 'unitSpacing'; |
71
|
|
|
const UNITS = 'units'; |
72
|
|
|
const NUMBERED_ABBREVIATION_SPACING = 'numberedAbbreviationSpacing'; |
73
|
|
|
const FRENCH_PUNCTUATION_SPACING = 'frenchPunctuationSpacing'; |
74
|
|
|
const DASH_SPACING = 'dashSpacing'; |
75
|
|
|
const DEWIDOW = 'dewidow'; |
76
|
|
|
const DEWIDOW_MAX_LENGTH = 'dewidowMaxLength'; |
77
|
|
|
const DEWIDOW_MAX_PULL = 'dewidowMaxPull'; |
78
|
|
|
const DEWIDOW_WORD_NUMBER = 'dewidowWordNumber'; |
79
|
|
|
const HYPHEN_HARD_WRAP = 'hyphenHardWrap'; |
80
|
|
|
const URL_WRAP = 'urlWrap'; |
81
|
|
|
const URL_MIN_AFTER_WRAP = 'urlMinAfterWrap'; |
82
|
|
|
const EMAIL_WRAP = 'emailWrap'; |
83
|
|
|
const SPACE_COLLAPSE = 'spaceCollapse'; |
84
|
|
|
|
85
|
|
|
// Character styling. |
86
|
|
|
const STYLE_AMPERSANDS = 'styleAmpersands'; |
87
|
|
|
const STYLE_CAPS = 'styleCaps'; |
88
|
|
|
const STYLE_INITIAL_QUOTES = 'styleInitialQuotes'; |
89
|
|
|
const INITIAL_QUOTE_TAGS = 'initialQuoteTags'; |
90
|
|
|
const STYLE_NUMBERS = 'styleNumbers'; |
91
|
|
|
const STYLE_HANGING_PUNCTUATION = 'styleHangingPunctuation'; |
92
|
|
|
|
93
|
|
|
// Hyphenation. |
94
|
|
|
const HYPHENATION = 'hyphenation'; |
95
|
|
|
const HYPHENATION_LANGUAGE = 'hyphenLanguage'; |
96
|
|
|
const HYPHENATION_MIN_LENGTH = 'hyphenMinLength'; |
97
|
|
|
const HYPHENATION_MIN_BEFORE = 'hyphenMinBefore'; |
98
|
|
|
const HYPHENATION_MIN_AFTER = 'hyphenMinAfter'; |
99
|
|
|
const HYPHENATION_CUSTOM_EXCEPTIONS = 'hyphenationCustomExceptions'; |
100
|
|
|
const HYPHENATE_HEADINGS = 'hyphenateTitle'; |
101
|
|
|
const HYPHENATE_ALL_CAPS = 'hyphenateAllCaps'; |
102
|
|
|
const HYPHENATE_TITLE_CASE = 'hyphenateTitleCase'; |
103
|
|
|
const HYPHENATE_COMPOUNDS = 'hyphenateCompounds'; |
104
|
|
|
|
105
|
|
|
// Parser error handling. |
106
|
|
|
const PARSER_ERRORS_IGNORE = 'parserErrorsIgnore'; |
107
|
|
|
const PARSER_ERRORS_HANDLER = 'parserErrorsHandler'; |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* The current no-break narrow space character. |
111
|
|
|
* |
112
|
|
|
* @deprecated 6.5.0 |
113
|
|
|
* |
114
|
|
|
* @var string |
115
|
|
|
*/ |
116
|
|
|
protected $no_break_narrow_space; |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Primary quote style. |
120
|
|
|
* |
121
|
|
|
* @var Quotes |
122
|
|
|
*/ |
123
|
|
|
protected $primary_quote_style; |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Secondary quote style. |
127
|
|
|
* |
128
|
|
|
* @var Quotes |
129
|
|
|
*/ |
130
|
|
|
protected $secondary_quote_style; |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* A regex pattern for custom units (or the empty string). |
134
|
|
|
* |
135
|
|
|
* @var string |
136
|
|
|
*/ |
137
|
|
|
protected $custom_units = ''; |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* A hashmap of settings for the various typographic options. |
141
|
|
|
* |
142
|
|
|
* @var array |
143
|
|
|
*/ |
144
|
|
|
protected $data = []; |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* The current dash style. |
148
|
|
|
* |
149
|
|
|
* @var Settings\Dashes |
150
|
|
|
*/ |
151
|
|
|
protected $dash_style; |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* The Unicode character mapping (some characters still have compatibility issues). |
155
|
|
|
* |
156
|
|
|
* @since 6.5.0 |
157
|
|
|
* |
158
|
|
|
* @var string[] |
159
|
|
|
*/ |
160
|
|
|
protected $unicode_mapping; |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* An array containing just remapped characters (for optimization). |
164
|
|
|
* |
165
|
|
|
* @since 6.5.0 |
166
|
|
|
* |
167
|
|
|
* @var string[] |
168
|
|
|
*/ |
169
|
|
|
protected $remapped_characters; |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Sets up a new Settings object. |
173
|
|
|
* |
174
|
|
|
* @since 6.0.0 If $set_defaults is `false`, the settings object is not fully |
175
|
|
|
* initialized unless `set_smart_quotes_primary`, |
176
|
|
|
* `set_smart_quotes_secondary`, `set_smart_dashes_style` and |
177
|
|
|
* `set_true_no_break_narrow_space` are called explicitly. |
178
|
|
|
* @since 6.5.0 A (partial) character mapping can be given to remap certain |
179
|
|
|
* characters. |
180
|
|
|
* |
181
|
|
|
* @param bool $set_defaults Optional. If true, set default values for various properties. Default true. |
182
|
|
|
* @param string[] $mapping Optional. Unicode characters to remap. The default maps the narrow no-break space to the normal NO-BREAK SPACE and the apostrophe to the RIGHT SINGLE QUOTATION MARK. |
183
|
|
|
*/ |
184
|
1 |
|
public function __construct( $set_defaults = true, array $mapping = [ U::NO_BREAK_NARROW_SPACE => U::NO_BREAK_SPACE, U::APOSTROPHE => U::SINGLE_QUOTE_CLOSE ] ) { // phpcs:ignore WordPress.Arrays.ArrayDeclarationSpacing |
185
|
1 |
|
if ( $set_defaults ) { |
186
|
1 |
|
$this->set_defaults(); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
// Merge default character mapping with given mapping. |
190
|
1 |
|
$this->unicode_mapping = $mapping; |
191
|
|
|
|
192
|
|
|
// Keep backwards compatibility. |
193
|
1 |
|
if ( isset( $this->unicode_mapping[ U::NO_BREAK_NARROW_SPACE ] ) ) { |
194
|
|
|
/* @scrutinizer ignore-deprecated */ |
195
|
1 |
|
$this->no_break_narrow_space = $this->unicode_mapping[ U::NO_BREAK_NARROW_SPACE ]; |
196
|
|
|
} |
197
|
1 |
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Provides access to named settings (object syntax). |
201
|
|
|
* |
202
|
|
|
* @param string $key The settings key. |
203
|
|
|
* |
204
|
|
|
* @return mixed |
205
|
|
|
*/ |
206
|
1 |
|
public function &__get( $key ) { |
207
|
1 |
|
return $this->data[ $key ]; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Changes a named setting (object syntax). |
212
|
|
|
* |
213
|
|
|
* @param string $key The settings key. |
214
|
|
|
* @param mixed $value The settings value. |
215
|
|
|
*/ |
216
|
1 |
|
public function __set( $key, $value ) { |
217
|
1 |
|
$this->data[ $key ] = $value; |
218
|
1 |
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Checks if a named setting exists (object syntax). |
222
|
|
|
* |
223
|
|
|
* @param string $key The settings key. |
224
|
|
|
*/ |
225
|
1 |
|
public function __isset( $key ) { |
226
|
1 |
|
return isset( $this->data[ $key ] ); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Unsets a named setting. |
231
|
|
|
* |
232
|
|
|
* @param string $key The settings key. |
233
|
|
|
*/ |
234
|
1 |
|
public function __unset( $key ) { |
235
|
1 |
|
unset( $this->data[ $key ] ); |
236
|
1 |
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Changes a named setting (array syntax). |
240
|
|
|
* |
241
|
|
|
* @param string $offset The settings key. |
242
|
|
|
* @param mixed $value The settings value. |
243
|
|
|
*/ |
244
|
1 |
|
public function offsetSet( $offset, $value ) { |
245
|
1 |
|
if ( ! empty( $offset ) ) { |
246
|
1 |
|
$this->data[ $offset ] = $value; |
247
|
|
|
} |
248
|
1 |
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Checks if a named setting exists (array syntax). |
252
|
|
|
* |
253
|
|
|
* @param string $offset The settings key. |
254
|
|
|
*/ |
255
|
1 |
|
public function offsetExists( $offset ) { |
256
|
1 |
|
return isset( $this->data[ $offset ] ); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Unsets a named setting (array syntax). |
261
|
|
|
* |
262
|
|
|
* @param string $offset The settings key. |
263
|
|
|
*/ |
264
|
1 |
|
public function offsetUnset( $offset ) { |
265
|
1 |
|
unset( $this->data[ $offset ] ); |
266
|
1 |
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Provides access to named settings (array syntax). |
270
|
|
|
* |
271
|
|
|
* @param string $offset The settings key. |
272
|
|
|
* |
273
|
|
|
* @return mixed |
274
|
|
|
*/ |
275
|
1 |
|
public function offsetGet( $offset ) { |
276
|
1 |
|
return isset( $this->data[ $offset ] ) ? $this->data[ $offset ] : null; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Provides a JSON serialization of the settings. |
281
|
|
|
* |
282
|
|
|
* @return mixed |
283
|
|
|
*/ |
284
|
1 |
|
public function jsonSerialize() { |
285
|
1 |
|
return \array_merge( |
286
|
1 |
|
$this->data, |
287
|
|
|
[ |
288
|
1 |
|
'unicode_mapping' => $this->unicode_mapping, |
289
|
1 |
|
'primary_quotes' => "{$this->primary_quote_style->open()}|{$this->primary_quote_style->close()}", |
290
|
1 |
|
'secondary_quotes' => "{$this->secondary_quote_style->open()}|{$this->secondary_quote_style->close()}", |
291
|
1 |
|
'dash_style' => "{$this->dash_style->interval_dash()}|{$this->dash_style->interval_space()}|{$this->dash_style->parenthetical_dash()}|{$this->dash_style->parenthetical_space()}", |
292
|
1 |
|
'custom_units' => $this->custom_units, |
293
|
|
|
] |
294
|
|
|
); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Remaps a unicode character to another one. |
299
|
|
|
* |
300
|
|
|
* @since 6.5.0 |
301
|
|
|
* |
302
|
|
|
* @param string $char The remapped character. |
303
|
|
|
* @param string $new_char The character to actually use. |
304
|
|
|
*/ |
305
|
1 |
|
public function remap_character( $char, $new_char ) { |
306
|
1 |
|
if ( $char !== $new_char ) { |
307
|
1 |
|
$this->unicode_mapping[ $char ] = $new_char; |
308
|
|
|
} else { |
309
|
1 |
|
unset( $this->unicode_mapping[ $char ] ); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
// Compatibility with the old way of setting the no-break narrow space. |
313
|
1 |
|
if ( U::NO_BREAK_NARROW_SPACE === $char ) { |
314
|
|
|
/* @scrutinizer ignore-deprecated */ |
315
|
1 |
|
$this->no_break_narrow_space = $new_char; |
316
|
|
|
} |
317
|
1 |
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Remaps one or more strings. |
321
|
|
|
* |
322
|
|
|
* @since 6.5.0 |
323
|
|
|
* |
324
|
|
|
* @param string|string[] $input The input string(s). |
325
|
|
|
* |
326
|
|
|
* @return string|string[] |
327
|
|
|
*/ |
328
|
4 |
|
public function apply_character_mapping( $input ) { |
329
|
|
|
|
330
|
|
|
// Nothing for us to do. |
331
|
4 |
|
if ( empty( $input ) || empty( $this->unicode_mapping ) ) { |
332
|
1 |
|
return $input; |
333
|
|
|
} |
334
|
|
|
|
335
|
3 |
|
$native_array = \is_array( $input ); |
336
|
3 |
|
$data = (array) $input; |
337
|
|
|
|
338
|
3 |
|
foreach ( $data as $key => $string ) { |
339
|
3 |
|
$data[ $key ] = \strtr( $string, $this->unicode_mapping ); |
340
|
|
|
} |
341
|
|
|
|
342
|
3 |
|
return $native_array ? $data : $data[0]; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* Retrieves the current non-breaking narrow space character (either the |
347
|
|
|
* regular non-breaking space or the the true non-breaking narrow space  ). |
348
|
|
|
* |
349
|
|
|
* @deprecated 6.5.0 Use U::NO_BREAK_NARROW_SPACE instead and let Settings::apply_character_mapping() do the rest. |
350
|
|
|
* |
351
|
|
|
* @return string |
352
|
|
|
*/ |
353
|
1 |
|
public function no_break_narrow_space() { |
354
|
1 |
|
return /* @scrutinizer ignore-deprecated */$this->no_break_narrow_space; |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* Retrieves the primary (double) quote style. |
359
|
|
|
* |
360
|
|
|
* @return Quotes |
361
|
|
|
*/ |
362
|
1 |
|
public function primary_quote_style() { |
363
|
1 |
|
return $this->primary_quote_style; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* Retrieves the secondary (single) quote style. |
368
|
|
|
* |
369
|
|
|
* @return Quotes |
370
|
|
|
*/ |
371
|
1 |
|
public function secondary_quote_style() { |
372
|
1 |
|
return $this->secondary_quote_style; |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* Retrieves the dash style. |
377
|
|
|
* |
378
|
|
|
* @return Settings\Dashes |
379
|
|
|
*/ |
380
|
1 |
|
public function dash_style() { |
381
|
1 |
|
return $this->dash_style; |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* Retrieves the custom units pattern. |
386
|
|
|
* |
387
|
|
|
* @return string The pattern is suitable for inclusion into a regular expression. |
388
|
|
|
*/ |
389
|
1 |
|
public function custom_units() { |
390
|
1 |
|
return $this->custom_units; |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* (Re)set various options to their default values. |
395
|
|
|
*/ |
396
|
1 |
|
public function set_defaults() { |
397
|
|
|
// General attributes. |
398
|
1 |
|
$this->set_tags_to_ignore(); |
399
|
1 |
|
$this->set_classes_to_ignore(); |
400
|
1 |
|
$this->set_ids_to_ignore(); |
401
|
|
|
|
402
|
|
|
// Smart characters. |
403
|
1 |
|
$this->set_smart_quotes(); |
404
|
1 |
|
$this->set_smart_quotes_primary(); |
405
|
1 |
|
$this->set_smart_quotes_secondary(); |
406
|
1 |
|
$this->set_smart_quotes_exceptions(); |
407
|
1 |
|
$this->set_smart_dashes(); |
408
|
1 |
|
$this->set_smart_dashes_style(); |
409
|
1 |
|
$this->set_smart_ellipses(); |
410
|
1 |
|
$this->set_smart_diacritics(); |
411
|
1 |
|
$this->set_diacritic_language(); |
412
|
1 |
|
$this->set_diacritic_custom_replacements(); |
413
|
1 |
|
$this->set_smart_marks(); |
414
|
1 |
|
$this->set_smart_ordinal_suffix(); |
415
|
1 |
|
$this->set_smart_ordinal_suffix_match_roman_numerals(); |
416
|
1 |
|
$this->set_smart_math(); |
417
|
1 |
|
$this->set_smart_fractions(); |
418
|
1 |
|
$this->set_smart_exponents(); |
419
|
1 |
|
$this->set_smart_area_units(); |
420
|
|
|
|
421
|
|
|
// Smart spacing. |
422
|
1 |
|
$this->set_single_character_word_spacing(); |
423
|
1 |
|
$this->set_fraction_spacing(); |
424
|
1 |
|
$this->set_unit_spacing(); |
425
|
1 |
|
$this->set_french_punctuation_spacing(); |
426
|
1 |
|
$this->set_units(); |
427
|
1 |
|
$this->set_dash_spacing(); |
428
|
1 |
|
$this->set_dewidow(); |
429
|
1 |
|
$this->set_max_dewidow_length(); |
430
|
1 |
|
$this->set_max_dewidow_pull(); |
431
|
1 |
|
$this->set_dewidow_word_number(); |
432
|
1 |
|
$this->set_wrap_hard_hyphens(); |
433
|
1 |
|
$this->set_url_wrap(); |
434
|
1 |
|
$this->set_email_wrap(); |
435
|
1 |
|
$this->set_min_after_url_wrap(); |
436
|
1 |
|
$this->set_space_collapse(); |
437
|
|
|
/* @scrutinizer ignore-deprecated */ |
438
|
1 |
|
$this->set_true_no_break_narrow_space(); |
439
|
|
|
|
440
|
|
|
// Character styling. |
441
|
1 |
|
$this->set_style_ampersands(); |
442
|
1 |
|
$this->set_style_caps(); |
443
|
1 |
|
$this->set_style_initial_quotes(); |
444
|
1 |
|
$this->set_style_numbers(); |
445
|
1 |
|
$this->set_style_hanging_punctuation(); |
446
|
1 |
|
$this->set_initial_quote_tags(); |
447
|
|
|
|
448
|
|
|
// Hyphenation. |
449
|
1 |
|
$this->set_hyphenation(); |
450
|
1 |
|
$this->set_hyphenation_language(); |
451
|
1 |
|
$this->set_min_length_hyphenation(); |
452
|
1 |
|
$this->set_min_before_hyphenation(); |
453
|
1 |
|
$this->set_min_after_hyphenation(); |
454
|
1 |
|
$this->set_hyphenate_headings(); |
455
|
1 |
|
$this->set_hyphenate_all_caps(); |
456
|
1 |
|
$this->set_hyphenate_title_case(); |
457
|
1 |
|
$this->set_hyphenate_compounds(); |
458
|
1 |
|
$this->set_hyphenation_exceptions(); |
459
|
|
|
|
460
|
|
|
// Parser error handling. |
461
|
1 |
|
$this->set_ignore_parser_errors(); |
462
|
1 |
|
} |
463
|
|
|
|
464
|
|
|
/** |
465
|
|
|
* Enable lenient parser error handling (HTML is "best guess" if enabled). |
466
|
|
|
* |
467
|
|
|
* @param bool $on Optional. Default false. |
468
|
|
|
*/ |
469
|
1 |
|
public function set_ignore_parser_errors( $on = false ) { |
470
|
1 |
|
$this->data[ self::PARSER_ERRORS_IGNORE ] = $on; |
471
|
1 |
|
} |
472
|
|
|
|
473
|
|
|
/** |
474
|
|
|
* Sets an optional handler for parser errors. Invalid callbacks will be silently ignored. |
475
|
|
|
* |
476
|
|
|
* @since 6.0.0. callable type is enforced via typehinting. |
477
|
|
|
* |
478
|
|
|
* @param callable|null $handler Optional. A callable that takes an array of error strings as its parameter. Default null. |
479
|
|
|
*/ |
480
|
2 |
|
public function set_parser_errors_handler( callable $handler = null ) { |
481
|
2 |
|
$this->data[ self::PARSER_ERRORS_HANDLER ] = $handler; |
482
|
2 |
|
} |
483
|
|
|
|
484
|
|
|
/** |
485
|
|
|
* Enable usage of true "no-break narrow space" ( ) instead of the normal no-break space ( ). |
486
|
|
|
* |
487
|
|
|
* @deprecated 6.5.0 Use ::remap_character() instead. |
488
|
|
|
* |
489
|
|
|
* @param bool $on Optional. Default false. |
490
|
|
|
*/ |
491
|
1 |
|
public function set_true_no_break_narrow_space( $on = false ) { |
492
|
|
|
|
493
|
1 |
|
if ( $on ) { |
494
|
1 |
|
$this->remap_character( U::NO_BREAK_NARROW_SPACE, U::NO_BREAK_NARROW_SPACE ); |
495
|
|
|
} else { |
496
|
1 |
|
$this->remap_character( U::NO_BREAK_NARROW_SPACE, U::NO_BREAK_SPACE ); |
497
|
|
|
} |
498
|
1 |
|
} |
499
|
|
|
|
500
|
|
|
/** |
501
|
|
|
* Sets tags for which the typography of their children will be left untouched. |
502
|
|
|
* |
503
|
|
|
* @param string|array $tags A comma separated list or an array of tag names. |
504
|
|
|
*/ |
505
|
1 |
|
public function set_tags_to_ignore( $tags = [ 'code', 'head', 'kbd', 'object', 'option', 'pre', 'samp', 'script', 'noscript', 'noembed', 'select', 'style', 'textarea', 'title', 'var', 'math' ] ) { |
506
|
|
|
// Ensure that we pass only lower-case tag names to XPath. |
507
|
1 |
|
$tags = array_filter( array_map( 'strtolower', Strings::maybe_split_parameters( $tags ) ), 'ctype_alnum' ); |
508
|
|
|
|
509
|
1 |
|
$this->data[ self::IGNORE_TAGS ] = array_unique( array_merge( $tags, array_flip( DOM::inappropriate_tags() ) ) ); |
510
|
1 |
|
} |
511
|
|
|
|
512
|
|
|
/** |
513
|
|
|
* Sets classes for which the typography of their children will be left untouched. |
514
|
|
|
* |
515
|
|
|
* @param string|array $classes A comma separated list or an array of class names. |
516
|
|
|
*/ |
517
|
1 |
|
public function set_classes_to_ignore( $classes = [ 'vcard', 'noTypo' ] ) { |
518
|
1 |
|
$this->data[ self::IGNORE_CLASSES ] = Strings::maybe_split_parameters( $classes ); |
519
|
1 |
|
} |
520
|
|
|
|
521
|
|
|
/** |
522
|
|
|
* Sets IDs for which the typography of their children will be left untouched. |
523
|
|
|
* |
524
|
|
|
* @param string|array $ids A comma separated list or an array of tag names. |
525
|
|
|
*/ |
526
|
1 |
|
public function set_ids_to_ignore( $ids = [] ) { |
527
|
1 |
|
$this->data[ self::IGNORE_IDS ] = Strings::maybe_split_parameters( $ids ); |
528
|
1 |
|
} |
529
|
|
|
|
530
|
|
|
/** |
531
|
|
|
* Enables/disables typographic quotes. |
532
|
|
|
* |
533
|
|
|
* @param bool $on Optional. Default true. |
534
|
|
|
*/ |
535
|
1 |
|
public function set_smart_quotes( $on = true ) { |
536
|
1 |
|
$this->data[ self::SMART_QUOTES ] = $on; |
537
|
1 |
|
} |
538
|
|
|
|
539
|
|
|
/** |
540
|
|
|
* Sets the style for primary ('double') quotemarks. |
541
|
|
|
* |
542
|
|
|
* Allowed values for $style: |
543
|
|
|
* "doubleCurled" => "“foo”", |
544
|
|
|
* "doubleCurledReversed" => "”foo”", |
545
|
|
|
* "doubleLow9" => "„foo”", |
546
|
|
|
* "doubleLow9Reversed" => "„foo“", |
547
|
|
|
* "singleCurled" => "‘foo’", |
548
|
|
|
* "singleCurledReversed" => "’foo’", |
549
|
|
|
* "singleLow9" => "‚foo’", |
550
|
|
|
* "singleLow9Reversed" => "‚foo‘", |
551
|
|
|
* "doubleGuillemetsFrench" => "« foo »", |
552
|
|
|
* "doubleGuillemets" => "«foo»", |
553
|
|
|
* "doubleGuillemetsReversed" => "»foo«", |
554
|
|
|
* "singleGuillemets" => "‹foo›", |
555
|
|
|
* "singleGuillemetsReversed" => "›foo‹", |
556
|
|
|
* "cornerBrackets" => "「foo」", |
557
|
|
|
* "whiteCornerBracket" => "『foo』" |
558
|
|
|
* |
559
|
|
|
* @param Quotes|string $style Optional. A Quotes instance or a quote style constant. Defaults to 'doubleCurled'. |
560
|
|
|
* |
561
|
|
|
* @throws \DomainException Thrown if $style constant is invalid. |
562
|
|
|
*/ |
563
|
3 |
|
public function set_smart_quotes_primary( $style = Quote_Style::DOUBLE_CURLED ) { |
564
|
3 |
|
$this->primary_quote_style = $this->get_quote_style( $style ); |
565
|
2 |
|
} |
566
|
|
|
|
567
|
|
|
/** |
568
|
|
|
* Sets the style for secondary ('single') quotemarks. |
569
|
|
|
* |
570
|
|
|
* Allowed values for $style: |
571
|
|
|
* "doubleCurled" => "“foo”", |
572
|
|
|
* "doubleCurledReversed" => "”foo”", |
573
|
|
|
* "doubleLow9" => "„foo”", |
574
|
|
|
* "doubleLow9Reversed" => "„foo“", |
575
|
|
|
* "singleCurled" => "‘foo’", |
576
|
|
|
* "singleCurledReversed" => "’foo’", |
577
|
|
|
* "singleLow9" => "‚foo’", |
578
|
|
|
* "singleLow9Reversed" => "‚foo‘", |
579
|
|
|
* "doubleGuillemetsFrench" => "« foo »", |
580
|
|
|
* "doubleGuillemets" => "«foo»", |
581
|
|
|
* "doubleGuillemetsReversed" => "»foo«", |
582
|
|
|
* "singleGuillemets" => "‹foo›", |
583
|
|
|
* "singleGuillemetsReversed" => "›foo‹", |
584
|
|
|
* "cornerBrackets" => "「foo」", |
585
|
|
|
* "whiteCornerBracket" => "『foo』" |
586
|
|
|
* |
587
|
|
|
* @param Quotes|string $style Optional. A Quotes instance or a quote style constant. Defaults to 'singleCurled'. |
588
|
|
|
* |
589
|
|
|
* @throws \DomainException Thrown if $style constant is invalid. |
590
|
|
|
*/ |
591
|
3 |
|
public function set_smart_quotes_secondary( $style = Quote_Style::SINGLE_CURLED ) { |
592
|
3 |
|
$this->secondary_quote_style = $this->get_quote_style( $style ); |
593
|
2 |
|
} |
594
|
|
|
|
595
|
|
|
/** |
596
|
|
|
* Retrieves a Quotes instance from a given style. |
597
|
|
|
* |
598
|
|
|
* @param Quotes|string $style A Quotes instance or a quote style constant. |
599
|
|
|
* |
600
|
|
|
* @throws \DomainException Thrown if $style constant is invalid. |
601
|
|
|
* |
602
|
|
|
* @return Quotes |
603
|
|
|
*/ |
604
|
6 |
|
protected function get_quote_style( $style ) { |
605
|
6 |
|
return $this->get_style( $style, Quotes::class, [ Quote_Style::class, 'get_styled_quotes' ], 'quote' ); |
606
|
|
|
} |
607
|
|
|
|
608
|
|
|
/** |
609
|
|
|
* Sets the list of exceptional words for smart quotes replacement. Mainly, |
610
|
|
|
* this is used for contractions beginning with an apostrophe. |
611
|
|
|
* |
612
|
|
|
* @param string[] $exceptions Optional. An array of replacements indexed by the ”non-smart" form. |
613
|
|
|
* Default a list of English words beginning with an apostrophy. |
614
|
|
|
*/ |
615
|
1 |
|
public function set_smart_quotes_exceptions( $exceptions = [ |
616
|
|
|
"'tain't" => U::APOSTROPHE . 'tain' . U::APOSTROPHE . 't', |
617
|
|
|
"'twere" => U::APOSTROPHE . 'twere', |
618
|
|
|
"'twas" => U::APOSTROPHE . 'twas', |
619
|
|
|
"'tis" => U::APOSTROPHE . 'tis', |
620
|
|
|
"'til" => U::APOSTROPHE . 'til', |
621
|
|
|
"'bout" => U::APOSTROPHE . 'bout', |
622
|
|
|
"'nuff" => U::APOSTROPHE . 'nuff', |
623
|
|
|
"'round" => U::APOSTROPHE . 'round', |
624
|
|
|
"'cause" => U::APOSTROPHE . 'cause', |
625
|
|
|
"'splainin" => U::APOSTROPHE . 'splainin', |
626
|
|
|
"'em'" => U::APOSTROPHE . 'em', |
627
|
|
|
] ) { |
628
|
1 |
|
$this->data[ self::SMART_QUOTES_EXCEPTIONS ] = [ |
629
|
1 |
|
'patterns' => \array_keys( $exceptions ), |
630
|
1 |
|
'replacements' => \array_values( $exceptions ), |
631
|
|
|
]; |
632
|
1 |
|
} |
633
|
|
|
|
634
|
|
|
/** |
635
|
|
|
* Retrieves an object from a given style. |
636
|
|
|
* |
637
|
|
|
* @param object|string $style A style object instance or a style constant. |
638
|
|
|
* @param string $expected_class A class name. |
639
|
|
|
* @param callable $get_style A function that returns a style object from a given style constant. |
640
|
|
|
* @param string $description Style description for the exception message. |
641
|
|
|
* |
642
|
|
|
* @throws \DomainException Thrown if $style constant is invalid. |
643
|
|
|
* |
644
|
|
|
* @return object An instance of $expected_class. |
645
|
|
|
*/ |
646
|
9 |
|
protected function get_style( $style, $expected_class, callable $get_style, $description ) { |
647
|
9 |
|
if ( $style instanceof $expected_class ) { |
648
|
3 |
|
$object = $style; |
649
|
|
|
} else { |
650
|
6 |
|
$object = $get_style( $style, $this ); |
651
|
|
|
} |
652
|
|
|
|
653
|
9 |
|
if ( ! \is_object( $object ) || ! $object instanceof $expected_class ) { |
654
|
3 |
|
throw new \DomainException( "Invalid $description style $style." ); |
655
|
|
|
} |
656
|
|
|
|
657
|
6 |
|
return $object; |
658
|
|
|
} |
659
|
|
|
|
660
|
|
|
/** |
661
|
|
|
* Enables/disables replacement of "a--a" with En Dash " -- " and "---" with Em Dash. |
662
|
|
|
* |
663
|
|
|
* @param bool $on Optional. Default true. |
664
|
|
|
*/ |
665
|
1 |
|
public function set_smart_dashes( $on = true ) { |
666
|
1 |
|
$this->data[ self::SMART_DASHES ] = $on; |
667
|
1 |
|
} |
668
|
|
|
|
669
|
|
|
/** |
670
|
|
|
* Sets the typographical conventions used by smart_dashes. |
671
|
|
|
* |
672
|
|
|
* Allowed values for $style: |
673
|
|
|
* - "traditionalUS" |
674
|
|
|
* - "international" |
675
|
|
|
* |
676
|
|
|
* @param string|Settings\Dashes $style Optional. Default Dash_Style::TRADITIONAL_US. |
677
|
|
|
* |
678
|
|
|
* @throws \DomainException Thrown if $style constant is invalid. |
679
|
|
|
*/ |
680
|
3 |
|
public function set_smart_dashes_style( $style = Dash_Style::TRADITIONAL_US ) { |
681
|
3 |
|
$this->dash_style = $this->get_style( $style, Settings\Dashes::class, [ Dash_Style::class, 'get_styled_dashes' ], 'dash' ); |
682
|
2 |
|
} |
683
|
|
|
|
684
|
|
|
/** |
685
|
|
|
* Enables/disables replacement of "..." with "…". |
686
|
|
|
* |
687
|
|
|
* @param bool $on Optional. Default true. |
688
|
|
|
*/ |
689
|
1 |
|
public function set_smart_ellipses( $on = true ) { |
690
|
1 |
|
$this->data[ self::SMART_ELLIPSES ] = $on; |
691
|
1 |
|
} |
692
|
|
|
|
693
|
|
|
/** |
694
|
|
|
* Enables/disables replacement "creme brulee" with "crème brûlée". |
695
|
|
|
* |
696
|
|
|
* @param bool $on Optional. Default true. |
697
|
|
|
*/ |
698
|
1 |
|
public function set_smart_diacritics( $on = true ) { |
699
|
1 |
|
$this->data[ self::SMART_DIACRITICS ] = $on; |
700
|
1 |
|
} |
701
|
|
|
|
702
|
|
|
/** |
703
|
|
|
* Sets the language used for diacritics replacements. |
704
|
|
|
* |
705
|
|
|
* @param string $lang Has to correspond to a filename in 'diacritics'. Optional. Default 'en-US'. |
706
|
|
|
*/ |
707
|
1 |
|
public function set_diacritic_language( $lang = 'en-US' ) { |
708
|
1 |
|
if ( isset( $this->data[ self::DIACRITIC_LANGUAGE ] ) && $this->data[ self::DIACRITIC_LANGUAGE ] === $lang ) { |
709
|
1 |
|
return; |
710
|
|
|
} |
711
|
|
|
|
712
|
1 |
|
$this->data[ self::DIACRITIC_LANGUAGE ] = $lang; |
713
|
1 |
|
$language_file_name = \dirname( __FILE__ ) . '/diacritics/' . $lang . '.json'; |
714
|
|
|
|
715
|
1 |
|
if ( \file_exists( $language_file_name ) ) { |
716
|
1 |
|
$diacritics_file = \json_decode( \file_get_contents( $language_file_name ), true ); |
717
|
1 |
|
$this->data[ self::DIACRITIC_WORDS ] = $diacritics_file['diacritic_words']; |
718
|
|
|
} else { |
719
|
1 |
|
unset( $this->data[ self::DIACRITIC_WORDS ] ); |
720
|
|
|
} |
721
|
|
|
|
722
|
1 |
|
$this->update_diacritics_replacement_arrays(); |
723
|
1 |
|
} |
724
|
|
|
|
725
|
|
|
/** |
726
|
|
|
* Sets up custom diacritics replacements. |
727
|
|
|
* |
728
|
|
|
* @param string|array $custom_replacements An array formatted [needle=>replacement, needle=>replacement...], |
729
|
|
|
* or a string formatted `"needle"=>"replacement","needle"=>"replacement",... |
730
|
|
|
*/ |
731
|
6 |
|
public function set_diacritic_custom_replacements( $custom_replacements = [] ) { |
732
|
6 |
|
if ( ! \is_array( $custom_replacements ) ) { |
733
|
3 |
|
$custom_replacements = $this->parse_diacritics_replacement_string( $custom_replacements ); |
734
|
|
|
} |
735
|
|
|
|
736
|
6 |
|
$this->data[ self::DIACRITIC_CUSTOM_REPLACEMENTS ] = self::array_map_assoc( |
737
|
|
|
function( $key, $replacement ) { |
738
|
5 |
|
$key = \strip_tags( \trim( $key ) ); |
739
|
5 |
|
$replacement = \strip_tags( \trim( $replacement ) ); |
740
|
|
|
|
741
|
5 |
|
if ( ! empty( $key ) && ! empty( $replacement ) ) { |
742
|
3 |
|
return [ $key => $replacement ]; |
743
|
|
|
} else { |
744
|
2 |
|
return []; |
745
|
|
|
} |
746
|
6 |
|
}, |
747
|
|
|
$custom_replacements |
748
|
|
|
); |
749
|
|
|
|
750
|
6 |
|
$this->update_diacritics_replacement_arrays(); |
751
|
6 |
|
} |
752
|
|
|
|
753
|
|
|
/** |
754
|
|
|
* Parses a custom diacritics replacement string into an array. |
755
|
|
|
* |
756
|
|
|
* @param string $custom_replacements A string formatted `"needle"=>"replacement","needle"=>"replacement",... |
757
|
|
|
* |
758
|
|
|
* @return array |
759
|
|
|
*/ |
760
|
3 |
|
private function parse_diacritics_replacement_string( $custom_replacements ) { |
761
|
3 |
|
return self::array_map_assoc( |
762
|
|
|
function( $key, $replacement ) { |
763
|
|
|
// Account for single and double quotes in keys in and values, discard everything else. |
764
|
3 |
|
if ( \preg_match( '/(?<kquo>"|\')(?<key>(?:(?!\k<kquo>).)+)\k<kquo>\s*=>\s*(?<rquo>"|\')(?<replacement>(?:(?!\k<rquo>).)+)\k<rquo>/', $replacement, $match ) ) { |
765
|
2 |
|
$key = $match['key']; |
766
|
2 |
|
$replacement = $match['replacement']; |
767
|
|
|
|
768
|
2 |
|
return [ $key => $replacement ]; |
769
|
|
|
} |
770
|
|
|
|
771
|
1 |
|
return []; |
772
|
3 |
|
}, |
773
|
|
|
/** RE correct. @scrutinizer ignore-type */ |
774
|
3 |
|
\preg_split( '/,/', $custom_replacements, -1, PREG_SPLIT_NO_EMPTY ) |
775
|
|
|
); |
776
|
|
|
} |
777
|
|
|
|
778
|
|
|
/** |
779
|
|
|
* Provides an array_map implementation with control over resulting array's keys. |
780
|
|
|
* |
781
|
|
|
* Based on https://gist.github.com/jasand-pereza/84ecec7907f003564584. |
782
|
|
|
* |
783
|
|
|
* @since 6.0.0 |
784
|
|
|
* |
785
|
|
|
* @param callable $callback A callback function that needs to return [ $key => $value ] pairs. |
786
|
|
|
* @param array $array The array. |
787
|
|
|
* |
788
|
|
|
* @return array |
789
|
|
|
*/ |
790
|
2 |
|
protected static function array_map_assoc( callable $callback, array $array ) { |
791
|
2 |
|
$new = []; |
792
|
|
|
|
793
|
2 |
|
foreach ( $array as $k => $v ) { |
794
|
2 |
|
$u = $callback( $k, $v ); |
795
|
|
|
|
796
|
2 |
|
if ( ! empty( $u ) ) { |
797
|
1 |
|
$new[ \key( $u ) ] = \current( $u ); |
798
|
|
|
} |
799
|
|
|
} |
800
|
|
|
|
801
|
2 |
|
return $new; |
802
|
|
|
} |
803
|
|
|
|
804
|
|
|
/** |
805
|
|
|
* Update the pattern and replacement arrays in $settings['diacriticReplacement']. |
806
|
|
|
* |
807
|
|
|
* Should be called whenever a new diacritics replacement language is selected or |
808
|
|
|
* when the custom replacements are updated. |
809
|
|
|
*/ |
810
|
7 |
|
private function update_diacritics_replacement_arrays() { |
811
|
7 |
|
$patterns = []; |
812
|
7 |
|
$replacements = []; |
813
|
|
|
|
814
|
7 |
|
if ( ! empty( $this->data[ self::DIACRITIC_CUSTOM_REPLACEMENTS ] ) ) { |
815
|
3 |
|
$this->parse_diacritics_rules( $this->data[ self::DIACRITIC_CUSTOM_REPLACEMENTS ], $patterns, $replacements ); |
816
|
|
|
} |
817
|
7 |
|
if ( ! empty( $this->data[ self::DIACRITIC_WORDS ] ) ) { |
818
|
1 |
|
$this->parse_diacritics_rules( $this->data[ self::DIACRITIC_WORDS ], $patterns, $replacements ); |
819
|
|
|
} |
820
|
|
|
|
821
|
7 |
|
$this->data[ self::DIACRITIC_REPLACEMENT_DATA ] = [ |
822
|
7 |
|
'patterns' => $patterns, |
823
|
7 |
|
'replacements' => $replacements, |
824
|
|
|
]; |
825
|
7 |
|
} |
826
|
|
|
|
827
|
|
|
/** |
828
|
|
|
* Parse an array of diacritics rules. |
829
|
|
|
* |
830
|
|
|
* @param array $diacritics_rules The rules ( $word => $replacement ). |
831
|
|
|
* @param array $patterns Resulting patterns. Passed by reference. |
832
|
|
|
* @param array $replacements Resulting replacements. Passed by reference. |
833
|
|
|
*/ |
834
|
4 |
|
private function parse_diacritics_rules( array $diacritics_rules, array &$patterns, array &$replacements ) { |
835
|
|
|
|
836
|
4 |
|
foreach ( $diacritics_rules as $needle => $replacement ) { |
837
|
4 |
|
$patterns[] = '/\b(?<!\w[' . U::NO_BREAK_SPACE . U::SOFT_HYPHEN . '])' . $needle . '\b(?![' . U::NO_BREAK_SPACE . U::SOFT_HYPHEN . ']\w)/u'; |
838
|
4 |
|
$replacements[ $needle ] = $replacement; |
839
|
|
|
} |
840
|
4 |
|
} |
841
|
|
|
|
842
|
|
|
/** |
843
|
|
|
* Enables/disables replacement of (r) (c) (tm) (sm) (p) (R) (C) (TM) (SM) (P) with ® © ™ ℠ ℗. |
844
|
|
|
* |
845
|
|
|
* @param bool $on Optional. Default true. |
846
|
|
|
*/ |
847
|
1 |
|
public function set_smart_marks( $on = true ) { |
848
|
1 |
|
$this->data[ self::SMART_MARKS ] = $on; |
849
|
1 |
|
} |
850
|
|
|
|
851
|
|
|
/** |
852
|
|
|
* Enables/disables proper mathematical symbols. |
853
|
|
|
* |
854
|
|
|
* @param bool $on Optional. Default true. |
855
|
|
|
*/ |
856
|
1 |
|
public function set_smart_math( $on = true ) { |
857
|
1 |
|
$this->data[ self::SMART_MATH ] = $on; |
858
|
1 |
|
} |
859
|
|
|
|
860
|
|
|
/** |
861
|
|
|
* Enables/disables replacement of 2^2 with 2<sup>2</sup> |
862
|
|
|
* |
863
|
|
|
* @param bool $on Optional. Default true. |
864
|
|
|
*/ |
865
|
1 |
|
public function set_smart_exponents( $on = true ) { |
866
|
1 |
|
$this->data[ self::SMART_EXPONENTS ] = $on; |
867
|
1 |
|
} |
868
|
|
|
|
869
|
|
|
/** |
870
|
|
|
* Enables/disables replacement of 1/4 with <sup>1</sup>⁄<sub>4</sub>. |
871
|
|
|
* |
872
|
|
|
* @param bool $on Optional. Default true. |
873
|
|
|
*/ |
874
|
1 |
|
public function set_smart_fractions( $on = true ) { |
875
|
1 |
|
$this->data[ self::SMART_FRACTIONS ] = $on; |
876
|
1 |
|
} |
877
|
|
|
|
878
|
|
|
/** |
879
|
|
|
* Enables/disables replacement of 1st with 1<sup>st</sup>. |
880
|
|
|
* |
881
|
|
|
* @param bool $on Optional. Default true. |
882
|
|
|
*/ |
883
|
1 |
|
public function set_smart_ordinal_suffix( $on = true ) { |
884
|
1 |
|
$this->data[ self::SMART_ORDINAL_SUFFIX ] = $on; |
885
|
1 |
|
} |
886
|
|
|
|
887
|
|
|
/** |
888
|
|
|
* Enables/disables replacement of XXe with XX<sup>e</sup>. |
889
|
|
|
* |
890
|
|
|
* @since 6.5.0 |
891
|
|
|
* |
892
|
|
|
* @param bool $on Optional. Default false. |
893
|
|
|
*/ |
894
|
1 |
|
public function set_smart_ordinal_suffix_match_roman_numerals( $on = false ) { |
895
|
1 |
|
$this->data[ self::SMART_ORDINAL_SUFFIX_ROMAN_NUMERALS ] = $on; |
896
|
1 |
|
} |
897
|
|
|
|
898
|
|
|
/** |
899
|
|
|
* Enables/disables replacement of m2 with m³ and m3 with m³. |
900
|
|
|
* |
901
|
|
|
* @param bool $on Optional. Default true. |
902
|
|
|
*/ |
903
|
1 |
|
public function set_smart_area_units( $on = true ) { |
904
|
1 |
|
$this->data[ self::SMART_AREA_UNITS ] = $on; |
905
|
1 |
|
} |
906
|
|
|
|
907
|
|
|
/** |
908
|
|
|
* Enables/disables forcing single character words to next line with the insertion of . |
909
|
|
|
* |
910
|
|
|
* @param bool $on Optional. Default true. |
911
|
|
|
*/ |
912
|
1 |
|
public function set_single_character_word_spacing( $on = true ) { |
913
|
1 |
|
$this->data[ self::SINGLE_CHARACTER_WORD_SPACING ] = $on; |
914
|
1 |
|
} |
915
|
|
|
|
916
|
|
|
/** |
917
|
|
|
* Enables/disables fraction spacing. |
918
|
|
|
* |
919
|
|
|
* @param bool $on Optional. Default true. |
920
|
|
|
*/ |
921
|
1 |
|
public function set_fraction_spacing( $on = true ) { |
922
|
1 |
|
$this->data[ self::FRACTION_SPACING ] = $on; |
923
|
1 |
|
} |
924
|
|
|
|
925
|
|
|
/** |
926
|
|
|
* Enables/disables keeping units and values together with the insertion of . |
927
|
|
|
* |
928
|
|
|
* @param bool $on Optional. Default true. |
929
|
|
|
*/ |
930
|
1 |
|
public function set_unit_spacing( $on = true ) { |
931
|
1 |
|
$this->data[ self::UNIT_SPACING ] = $on; |
932
|
1 |
|
} |
933
|
|
|
|
934
|
|
|
/** |
935
|
|
|
* Enables/disables numbered abbreviations like "ISO 9000" together with the insertion of . |
936
|
|
|
* |
937
|
|
|
* @param bool $on Optional. Default true. |
938
|
|
|
*/ |
939
|
1 |
|
public function set_numbered_abbreviation_spacing( $on = true ) { |
940
|
1 |
|
$this->data[ self::NUMBERED_ABBREVIATION_SPACING ] = $on; |
941
|
1 |
|
} |
942
|
|
|
|
943
|
|
|
/** |
944
|
|
|
* Enables/disables extra whitespace before certain punction marks, as is the French custom. |
945
|
|
|
* |
946
|
|
|
* @since 6.0.0 The default value is now `false`.` |
947
|
|
|
* |
948
|
|
|
* @param bool $on Optional. Default false. |
949
|
|
|
*/ |
950
|
1 |
|
public function set_french_punctuation_spacing( $on = false ) { |
951
|
1 |
|
$this->data[ self::FRENCH_PUNCTUATION_SPACING ] = $on; |
952
|
1 |
|
} |
953
|
|
|
|
954
|
|
|
/** |
955
|
|
|
* Sets the list of units to keep together with their values. |
956
|
|
|
* |
957
|
|
|
* @param string|array $units A comma separated list or an array of units. |
958
|
|
|
*/ |
959
|
1 |
|
public function set_units( $units = [] ) { |
960
|
1 |
|
$this->data[ self::UNITS ] = Strings::maybe_split_parameters( $units ); |
961
|
1 |
|
$this->custom_units = $this->update_unit_pattern( $this->data[ self::UNITS ] ); |
962
|
1 |
|
} |
963
|
|
|
|
964
|
|
|
/** |
965
|
|
|
* Update pattern for matching custom units. |
966
|
|
|
* |
967
|
|
|
* @since 6.4.0 Visibility changed to protected, return value added. |
968
|
|
|
* |
969
|
|
|
* @param array $units An array of unit names. |
970
|
|
|
* |
971
|
|
|
* @return string |
972
|
|
|
*/ |
973
|
2 |
|
protected function update_unit_pattern( array $units ) { |
974
|
|
|
// Update unit regex pattern. |
975
|
2 |
|
foreach ( $units as $index => $unit ) { |
976
|
2 |
|
$units[ $index ] = \preg_quote( $unit, '/' ); |
977
|
|
|
} |
978
|
|
|
|
979
|
2 |
|
$custom_units = \implode( '|', $units ); |
980
|
2 |
|
$custom_units .= ! empty( $custom_units ) ? '|' : ''; |
981
|
|
|
|
982
|
2 |
|
return $custom_units; |
983
|
|
|
} |
984
|
|
|
|
985
|
|
|
/** |
986
|
|
|
* Enables/disables wrapping of Em and En dashes are in thin spaces. |
987
|
|
|
* |
988
|
|
|
* @param bool $on Optional. Default true. |
989
|
|
|
*/ |
990
|
1 |
|
public function set_dash_spacing( $on = true ) { |
991
|
1 |
|
$this->data[ self::DASH_SPACING ] = $on; |
992
|
1 |
|
} |
993
|
|
|
|
994
|
|
|
/** |
995
|
|
|
* Enables/disables removal of extra whitespace characters. |
996
|
|
|
* |
997
|
|
|
* @param bool $on Optional. Default true. |
998
|
|
|
*/ |
999
|
1 |
|
public function set_space_collapse( $on = true ) { |
1000
|
1 |
|
$this->data[ self::SPACE_COLLAPSE ] = $on; |
1001
|
1 |
|
} |
1002
|
|
|
|
1003
|
|
|
/** |
1004
|
|
|
* Enables/disables widow handling. |
1005
|
|
|
* |
1006
|
|
|
* @param bool $on Optional. Default true. |
1007
|
|
|
*/ |
1008
|
1 |
|
public function set_dewidow( $on = true ) { |
1009
|
1 |
|
$this->data[ self::DEWIDOW ] = $on; |
1010
|
1 |
|
} |
1011
|
|
|
|
1012
|
|
|
/** |
1013
|
|
|
* Sets the maximum length of widows that will be protected. |
1014
|
|
|
* |
1015
|
|
|
* @param int $length Defaults to 5. Trying to set the value to less than 2 resets the length to the default. |
1016
|
|
|
*/ |
1017
|
1 |
|
public function set_max_dewidow_length( $length = 5 ) { |
1018
|
1 |
|
$length = ( $length > 1 ) ? $length : 5; |
1019
|
|
|
|
1020
|
1 |
|
$this->data[ self::DEWIDOW_MAX_LENGTH ] = $length; |
1021
|
1 |
|
} |
1022
|
|
|
|
1023
|
|
|
/** |
1024
|
|
|
* Sets the maximum number of words considered for dewidowing. |
1025
|
|
|
* |
1026
|
|
|
* @param int $number Defaults to 1. Only 1, 2 and 3 are valid. |
1027
|
|
|
*/ |
1028
|
1 |
|
public function set_dewidow_word_number( $number = 1 ) { |
1029
|
1 |
|
$number = ( $number > 3 || $number < 1 ) ? 1 : $number; |
1030
|
|
|
|
1031
|
1 |
|
$this->data[ self::DEWIDOW_WORD_NUMBER ] = $number; |
1032
|
1 |
|
} |
1033
|
|
|
|
1034
|
|
|
/** |
1035
|
|
|
* Sets the maximum length of pulled text to keep widows company. |
1036
|
|
|
* |
1037
|
|
|
* @param int $length Defaults to 5. Trying to set the value to less than 2 resets the length to the default. |
1038
|
|
|
*/ |
1039
|
1 |
|
public function set_max_dewidow_pull( $length = 5 ) { |
1040
|
1 |
|
$length = ( $length > 1 ) ? $length : 5; |
1041
|
|
|
|
1042
|
1 |
|
$this->data[ self::DEWIDOW_MAX_PULL ] = $length; |
1043
|
1 |
|
} |
1044
|
|
|
|
1045
|
|
|
/** |
1046
|
|
|
* Enables/disables wrapping at internal hard hyphens with the insertion of a zero-width-space. |
1047
|
|
|
* |
1048
|
|
|
* @param bool $on Optional. Default true. |
1049
|
|
|
*/ |
1050
|
1 |
|
public function set_wrap_hard_hyphens( $on = true ) { |
1051
|
1 |
|
$this->data[ self::HYPHEN_HARD_WRAP ] = $on; |
1052
|
1 |
|
} |
1053
|
|
|
|
1054
|
|
|
/** |
1055
|
|
|
* Enables/disables wrapping of urls. |
1056
|
|
|
* |
1057
|
|
|
* @param bool $on Optional. Default true. |
1058
|
|
|
*/ |
1059
|
1 |
|
public function set_url_wrap( $on = true ) { |
1060
|
1 |
|
$this->data[ self::URL_WRAP ] = $on; |
1061
|
1 |
|
} |
1062
|
|
|
|
1063
|
|
|
/** |
1064
|
|
|
* Enables/disables wrapping of email addresses. |
1065
|
|
|
* |
1066
|
|
|
* @param bool $on Optional. Default true. |
1067
|
|
|
*/ |
1068
|
1 |
|
public function set_email_wrap( $on = true ) { |
1069
|
1 |
|
$this->data[ self::EMAIL_WRAP ] = $on; |
1070
|
1 |
|
} |
1071
|
|
|
|
1072
|
|
|
/** |
1073
|
|
|
* Sets the minimum character requirement after an URL wrapping point. |
1074
|
|
|
* |
1075
|
|
|
* @param int $length Defaults to 5. Trying to set the value to less than 1 resets the length to the default. |
1076
|
|
|
*/ |
1077
|
1 |
|
public function set_min_after_url_wrap( $length = 5 ) { |
1078
|
1 |
|
$length = ( $length > 0 ) ? $length : 5; |
1079
|
|
|
|
1080
|
1 |
|
$this->data[ self::URL_MIN_AFTER_WRAP ] = $length; |
1081
|
1 |
|
} |
1082
|
|
|
|
1083
|
|
|
/** |
1084
|
|
|
* Enables/disables wrapping of ampersands in <span class="amp">. |
1085
|
|
|
* |
1086
|
|
|
* @param bool $on Optional. Default true. |
1087
|
|
|
*/ |
1088
|
1 |
|
public function set_style_ampersands( $on = true ) { |
1089
|
1 |
|
$this->data[ self::STYLE_AMPERSANDS ] = $on; |
1090
|
1 |
|
} |
1091
|
|
|
|
1092
|
|
|
/** |
1093
|
|
|
* Enables/disables wrapping caps in <span class="caps">. |
1094
|
|
|
* |
1095
|
|
|
* @param bool $on Optional. Default true. |
1096
|
|
|
*/ |
1097
|
1 |
|
public function set_style_caps( $on = true ) { |
1098
|
1 |
|
$this->data[ self::STYLE_CAPS ] = $on; |
1099
|
1 |
|
} |
1100
|
|
|
|
1101
|
|
|
/** |
1102
|
|
|
* Enables/disables wrapping of initial quotes in <span class="quo"> or <span class="dquo">. |
1103
|
|
|
* |
1104
|
|
|
* @param bool $on Optional. Default true. |
1105
|
|
|
*/ |
1106
|
1 |
|
public function set_style_initial_quotes( $on = true ) { |
1107
|
1 |
|
$this->data[ self::STYLE_INITIAL_QUOTES ] = $on; |
1108
|
1 |
|
} |
1109
|
|
|
|
1110
|
|
|
/** |
1111
|
|
|
* Enables/disables wrapping of numbers in <span class="numbers">. |
1112
|
|
|
* |
1113
|
|
|
* @param bool $on Optional. Default true. |
1114
|
|
|
*/ |
1115
|
1 |
|
public function set_style_numbers( $on = true ) { |
1116
|
1 |
|
$this->data[ self::STYLE_NUMBERS ] = $on; |
1117
|
1 |
|
} |
1118
|
|
|
|
1119
|
|
|
/** |
1120
|
|
|
* Enables/disables wrapping of punctiation and wide characters in <span class="pull-*">. |
1121
|
|
|
* |
1122
|
|
|
* @param bool $on Optional. Default true. |
1123
|
|
|
*/ |
1124
|
1 |
|
public function set_style_hanging_punctuation( $on = true ) { |
1125
|
1 |
|
$this->data[ self::STYLE_HANGING_PUNCTUATION ] = $on; |
1126
|
1 |
|
} |
1127
|
|
|
|
1128
|
|
|
/** |
1129
|
|
|
* Sets the list of tags where initial quotes and guillemets should be styled. |
1130
|
|
|
* |
1131
|
|
|
* @param string|array $tags A comma separated list or an array of tag names. |
1132
|
|
|
*/ |
1133
|
1 |
|
public function set_initial_quote_tags( $tags = [ 'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'blockquote', 'li', 'dd', 'dt' ] ) { |
1134
|
|
|
// Make array if handed a list of tags as a string. |
1135
|
1 |
|
if ( ! \is_array( $tags ) ) { |
1136
|
1 |
|
$tags = \preg_split( '/[^a-z0-9]+/', $tags, -1, PREG_SPLIT_NO_EMPTY ); |
1137
|
|
|
} |
1138
|
|
|
|
1139
|
|
|
// Store the tag array inverted (with the tagName as its index for faster lookup). |
1140
|
1 |
|
$this->data[ self::INITIAL_QUOTE_TAGS ] = \array_change_key_case( \array_flip( /** Array. @scrutinizer ignore-type */ $tags ), CASE_LOWER ); |
1141
|
1 |
|
} |
1142
|
|
|
|
1143
|
|
|
/** |
1144
|
|
|
* Enables/disables hyphenation. |
1145
|
|
|
* |
1146
|
|
|
* @param bool $on Optional. Default true. |
1147
|
|
|
*/ |
1148
|
1 |
|
public function set_hyphenation( $on = true ) { |
1149
|
1 |
|
$this->data[ self::HYPHENATION ] = $on; |
1150
|
1 |
|
} |
1151
|
|
|
|
1152
|
|
|
/** |
1153
|
|
|
* Sets the hyphenation pattern language. |
1154
|
|
|
* |
1155
|
|
|
* @param string $lang Has to correspond to a filename in 'lang'. Optional. Default 'en-US'. |
1156
|
|
|
*/ |
1157
|
8 |
|
public function set_hyphenation_language( $lang = 'en-US' ) { |
1158
|
8 |
|
if ( isset( $this->data[ self::HYPHENATION_LANGUAGE ] ) && $this->data[ self::HYPHENATION_LANGUAGE ] === $lang ) { |
1159
|
3 |
|
return; // Bail out, no need to do anything. |
1160
|
|
|
} |
1161
|
|
|
|
1162
|
8 |
|
$this->data[ self::HYPHENATION_LANGUAGE ] = $lang; |
1163
|
8 |
|
} |
1164
|
|
|
|
1165
|
|
|
/** |
1166
|
|
|
* Sets the minimum length of a word that may be hyphenated. |
1167
|
|
|
* |
1168
|
|
|
* @param int $length Defaults to 5. Trying to set the value to less than 2 resets the length to the default. |
1169
|
|
|
*/ |
1170
|
1 |
|
public function set_min_length_hyphenation( $length = 5 ) { |
1171
|
1 |
|
$length = ( $length > 1 ) ? $length : 5; |
1172
|
|
|
|
1173
|
1 |
|
$this->data[ self::HYPHENATION_MIN_LENGTH ] = $length; |
1174
|
1 |
|
} |
1175
|
|
|
|
1176
|
|
|
/** |
1177
|
|
|
* Sets the minimum character requirement before a hyphenation point. |
1178
|
|
|
* |
1179
|
|
|
* @param int $length Defaults to 3. Trying to set the value to less than 1 resets the length to the default. |
1180
|
|
|
*/ |
1181
|
1 |
|
public function set_min_before_hyphenation( $length = 3 ) { |
1182
|
1 |
|
$length = ( $length > 0 ) ? $length : 3; |
1183
|
|
|
|
1184
|
1 |
|
$this->data[ self::HYPHENATION_MIN_BEFORE ] = $length; |
1185
|
1 |
|
} |
1186
|
|
|
|
1187
|
|
|
/** |
1188
|
|
|
* Sets the minimum character requirement after a hyphenation point. |
1189
|
|
|
* |
1190
|
|
|
* @param int $length Defaults to 2. Trying to set the value to less than 1 resets the length to the default. |
1191
|
|
|
*/ |
1192
|
1 |
|
public function set_min_after_hyphenation( $length = 2 ) { |
1193
|
1 |
|
$length = ( $length > 0 ) ? $length : 2; |
1194
|
|
|
|
1195
|
1 |
|
$this->data[ self::HYPHENATION_MIN_AFTER ] = $length; |
1196
|
1 |
|
} |
1197
|
|
|
|
1198
|
|
|
/** |
1199
|
|
|
* Enables/disables hyphenation of titles and headings. |
1200
|
|
|
* |
1201
|
|
|
* @param bool $on Optional. Default true. |
1202
|
|
|
*/ |
1203
|
1 |
|
public function set_hyphenate_headings( $on = true ) { |
1204
|
1 |
|
$this->data[ self::HYPHENATE_HEADINGS ] = $on; |
1205
|
1 |
|
} |
1206
|
|
|
|
1207
|
|
|
/** |
1208
|
|
|
* Enables/disables hyphenation of words set completely in capital letters. |
1209
|
|
|
* |
1210
|
|
|
* @param bool $on Optional. Default true. |
1211
|
|
|
*/ |
1212
|
1 |
|
public function set_hyphenate_all_caps( $on = true ) { |
1213
|
1 |
|
$this->data[ self::HYPHENATE_ALL_CAPS ] = $on; |
1214
|
1 |
|
} |
1215
|
|
|
|
1216
|
|
|
/** |
1217
|
|
|
* Enables/disables hyphenation of words starting with a capital letter. |
1218
|
|
|
* |
1219
|
|
|
* @param bool $on Optional. Default true. |
1220
|
|
|
*/ |
1221
|
1 |
|
public function set_hyphenate_title_case( $on = true ) { |
1222
|
1 |
|
$this->data[ self::HYPHENATE_TITLE_CASE ] = $on; |
1223
|
1 |
|
} |
1224
|
|
|
|
1225
|
|
|
/** |
1226
|
|
|
* Enables/disables hyphenation of compound words (e.g. "editor-in-chief"). |
1227
|
|
|
* |
1228
|
|
|
* @param bool $on Optional. Default true. |
1229
|
|
|
*/ |
1230
|
1 |
|
public function set_hyphenate_compounds( $on = true ) { |
1231
|
1 |
|
$this->data[ self::HYPHENATE_COMPOUNDS ] = $on; |
1232
|
1 |
|
} |
1233
|
|
|
|
1234
|
|
|
/** |
1235
|
|
|
* Sets custom word hyphenations. |
1236
|
|
|
* |
1237
|
|
|
* @param string|array $exceptions An array of words with all hyphenation points marked with a hard hyphen (or a string list of such words). |
1238
|
|
|
* In the latter case, only alphanumeric characters and hyphens are recognized. The default is empty. |
1239
|
|
|
*/ |
1240
|
2 |
|
public function set_hyphenation_exceptions( $exceptions = [] ) { |
1241
|
2 |
|
$this->data[ self::HYPHENATION_CUSTOM_EXCEPTIONS ] = Strings::maybe_split_parameters( $exceptions ); |
1242
|
2 |
|
} |
1243
|
|
|
|
1244
|
|
|
/** |
1245
|
|
|
* Retrieves a unique hash value for the current settings. |
1246
|
|
|
* |
1247
|
|
|
* @since 5.2.0 The new parameter $raw_output has been added. |
1248
|
|
|
* |
1249
|
|
|
* @param int $max_length Optional. The maximum number of bytes returned (0 for unlimited). Default 16. |
1250
|
|
|
* @param bool $raw_output Optional. Wether to return raw binary data for the hash. Default true. |
1251
|
|
|
* |
1252
|
|
|
* @return string A binary hash value for the current settings limited to $max_length. |
1253
|
|
|
*/ |
1254
|
1 |
|
public function get_hash( $max_length = 16, $raw_output = true ) { |
1255
|
1 |
|
$hash = \md5( \json_encode( $this ), $raw_output ); |
1256
|
|
|
|
1257
|
1 |
|
if ( $max_length < \strlen( $hash ) && $max_length > 0 ) { |
1258
|
1 |
|
$hash = \substr( $hash, 0, $max_length ); |
1259
|
|
|
} |
1260
|
|
|
|
1261
|
1 |
|
return $hash; |
1262
|
|
|
} |
1263
|
|
|
} |
1264
|
|
|
|