Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 15 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| DOMUtils | |
0.00% |
0 / 15 |
|
0.00% |
0 / 2 |
6 | |
0.00% |
0 / 1 |
| coerceName | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
2 | |||
| uncoerceName | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | |
| 4 | namespace Wikimedia\RemexHtml\DOM; |
| 5 | |
| 6 | use Wikimedia\RemexHtml\HTMLData; |
| 7 | |
| 8 | class DOMUtils { |
| 9 | /** |
| 10 | * Replace unsupported characters with a code of the form U123456. |
| 11 | * |
| 12 | * @param string $name |
| 13 | * @return string |
| 14 | */ |
| 15 | public static function coerceName( $name ) { |
| 16 | $coercedName = |
| 17 | mb_encode_numericentity( mb_substr( $name, 0, 1, 'UTF-8' ), |
| 18 | HTMLData::NAME_START_CHAR_CONV_TABLE, 'UTF-8', true ) . |
| 19 | mb_encode_numericentity( mb_substr( $name, 1, null, 'UTF-8' ), |
| 20 | HTMLData::NAME_CHAR_CONV_TABLE, 'UTF-8', true ); |
| 21 | $coercedName = preg_replace_callback( '/&#x([0-9A-F]*);/', |
| 22 | static function ( $m ) { |
| 23 | return 'U' . str_pad( $m[1], 6, '0', STR_PAD_LEFT ); |
| 24 | }, |
| 25 | $coercedName ); |
| 26 | return $coercedName; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Invert the encoding produced by coerceName() |
| 31 | * |
| 32 | * @param string $name |
| 33 | * @return string |
| 34 | */ |
| 35 | public static function uncoerceName( $name ) { |
| 36 | return mb_decode_numericentity( |
| 37 | preg_replace( '/U([0-9A-F]{6})/', '&#x\1;', $name ), |
| 38 | [ 0, 0x10ffff, 0, 0xffffff ], |
| 39 | 'UTF-8' ); |
| 40 | } |
| 41 | } |