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