true, 'bgsound' => true, 'frame' => true, 'keygen' => true ]; /** * Elements that strip leading newlines * http://www.whatwg.org/specs/web-apps/current-work/multipage/the-end.html#html-fragment-serialization-algorithm */ private const NEWLINE_STRIPPING_ELEMENTS = [ 'pre' => true, 'textarea' => true, 'listing' => true ]; private const ENTITY_ENCODINGS = [ 'single' => [ '<' => '<', '&' => '&', "'" => ''' ], 'double' => [ '<' => '<', '&' => '&', '"' => '"' ], 'xml' => [ '<' => '<', '&' => '&', "\u{0338}" => '̸' ], ]; /** * HTML entity encoder helper. * Only supports the few entities we'll actually need: <&'" * @param string $raw Input string * @param string $encodeChars Set of characters to encode, "single", "double", or "xml" * @return string */ private static function encodeHtmlEntities( string $raw, string $encodeChars ): string { return strtr( $raw, self::ENTITY_ENCODINGS[$encodeChars] ); } /** * Modify the attribute array, replacing data-object-id with JSON * encoded data. This is just a debugging hack, not to be confused with * DOMDataUtils::storeDataAttribs() * * @param Element $node * @param array &$attrs * @param bool $keepTmp * @param bool $storeDiffMark */ private static function dumpDataAttribs( Element $node, array &$attrs, bool $keepTmp, bool $storeDiffMark ) { if ( !isset( $attrs[DOMDataUtils::DATA_OBJECT_ATTR_NAME] ) ) { return; } $codec = DOMDataUtils::getCodec( $node->ownerDocument ); $nd = DOMDataUtils::getNodeData( $node ); $pd = $nd->parsoid_diff ?? null; if ( $pd && $storeDiffMark ) { $attrs['data-parsoid-diff'] = PHPUtils::jsonEncode( $pd ); } $dp = $nd->parsoid; if ( $dp ) { if ( !$keepTmp ) { $dp = clone $dp; // @phan-suppress-next-line PhanTypeObjectUnsetDeclaredProperty unset( $dp->tmp ); } $attrs['data-parsoid'] = $codec->toJsonString( $dp, DOMDataUtils::getCodecHints()['data-parsoid'] ); } $dmw = $nd->mw; if ( $dmw ) { $attrs['data-mw'] = $codec->toJsonString( $dmw, DOMDataUtils::getCodecHints()['data-mw'] ); } unset( $attrs[DOMDataUtils::DATA_OBJECT_ATTR_NAME] ); } /** * Serialize an HTML DOM3 node to XHTML. The XHTML and associated information will be fed * step-by-step to the callback given in $accum. * @param Node $node * @param array $options See {@link XMLSerializer::serialize()} * @param callable $accum function( $bit, $node, $flag ) * - $bit: (string) piece of HTML code * - $node: (Node) ?? * - $flag: (string|null) 'start' or 'end' (??) */ private static function serializeToString( Node $node, array $options, callable $accum ): void { $smartQuote = $options['smartQuote']; $saveData = $options['saveData']; switch ( $node->nodeType ) { case XML_ELEMENT_NODE: DOMUtils::assertElt( $node ); $child = $node->firstChild; $nodeName = DOMCompat::nodeName( $node ); $localName = $node->localName; $accum( '<' . $localName, $node ); $attrs = DOMUtils::attributes( $node ); if ( $saveData ) { self::dumpDataAttribs( $node, $attrs, $options['keepTmp'], $options['storeDiffMark'] ); } foreach ( $attrs as $an => $av ) { if ( $smartQuote // More double quotes than single quotes in value? && substr_count( $av, '"' ) > substr_count( $av, "'" ) ) { // use single quotes $accum( ' ' . $an . "='" . self::encodeHtmlEntities( $av, 'single' ) . "'", $node ); } else { // use double quotes $accum( ' ' . $an . '="' . self::encodeHtmlEntities( $av, 'double' ) . '"', $node ); } } if ( $child || ( !isset( Consts::$HTML['VoidTags'][$nodeName] ) && !isset( self::$alsoSerializeAsVoid[$nodeName] ) ) ) { $accum( '>', $node, 'start' ); // if is cdata child node if ( DOMUtils::isRawTextElement( $node ) ) { // TODO: perform context-sensitive escaping? // Currently this content is not normally part of our DOM, so // no problem. If it was, we'd probably have to do some // tag-specific escaping. Examples: // * < to \u003c in