| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\RemexHtml\Serializer; |
| 4 |
|
| 5 |
/** |
| 6 |
* A formatter suitable for pre-sanitized input with ignoreEntities enabled |
| 7 |
* in the Tokenizer. |
| 8 |
*/ |
| 9 |
class FastFormatter implements Formatter { |
| 10 |
/** @inheritDoc */ |
| 11 |
public function __construct( $options = [] ) { |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* @inheritDoc |
| 16 |
*/ |
| 17 |
public function startDocument( $fragmentNamespace, $fragmentName ) { |
| 18 |
if ( $fragmentNamespace === null ) { |
| 19 |
return "<!DOCTYPE html>\n"; |
| 20 |
} else { |
| 21 |
return ''; |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* @inheritDoc |
| 27 |
*/ |
| 28 |
public function doctype( $name, $public, $system ) { |
| 29 |
return ''; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* @inheritDoc |
| 34 |
*/ |
| 35 |
public function characters( SerializerNode $parent, $text, $start, $length ) { |
| 36 |
return substr( $text, $start, $length ); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* @inheritDoc |
| 41 |
*/ |
| 42 |
public function element( SerializerNode $parent, SerializerNode $node, $contents ) { |
| 43 |
$name = $node->name; |
| 44 |
$ret = "<$name"; |
| 45 |
foreach ( $node->attrs->getValues() as $attrName => $value ) { |
| 46 |
$ret .= " $attrName=\"$value\""; |
| 47 |
} |
| 48 |
if ( $contents === null ) { |
| 49 |
$ret .= "/>"; |
| 50 |
} elseif ( isset( $contents[0] ) && $contents[0] === "\n" |
| 51 |
&& in_array( $name, [ 'pre', 'textarea', 'listing' ] ) |
| 52 |
) { |
| 53 |
$ret .= ">\n$contents</$name>"; |
| 54 |
} else { |
| 55 |
$ret .= ">$contents</$name>"; |
| 56 |
} |
| 57 |
return $ret; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* @inheritDoc |
| 62 |
*/ |
| 63 |
public function comment( SerializerNode $parent, $text ) { |
| 64 |
return "<!--$text-->"; |
| 65 |
} |
| 66 |
} |