Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ParsoidUtils
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 3
90
0.00% covered (danger)
0.00%
0 / 1
 createLangFragment
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 createLangAttribute
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 addAttributesToNode
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2
3namespace Kartographer;
4
5use Wikimedia\Bcp47Code\Bcp47CodeValue;
6use Wikimedia\Parsoid\DOM\DocumentFragment;
7use Wikimedia\Parsoid\DOM\Element;
8use Wikimedia\Parsoid\Ext\ParsoidExtensionAPI;
9
10/**
11 * @license MIT
12 */
13class ParsoidUtils {
14
15    /**
16     * Creates an internationalized Parsoid fragment. If $language is not provided, the returned
17     * fragment is generated in the page language.
18     */
19    public static function createLangFragment(
20        string $msgKey, array $params, ParsoidExtensionAPI $extApi, ?string $language
21    ): DocumentFragment {
22        if ( $language === null ) {
23            return $extApi->createPageContentI18nFragment( $msgKey, $params );
24        }
25        return $extApi->createLangI18nFragment( new Bcp47CodeValue( $language ), $msgKey, $params );
26    }
27
28    /**
29     * Creates an internationalized Parsoid attribute on the provided element. If $language is
30     * not provided, the created attribute is generated in the page language.
31     */
32    public static function createLangAttribute(
33        Element $element, string $name, string $msgKey, array $params, ParsoidExtensionAPI $extApi, ?string $language
34    ): void {
35        if ( $language === null ) {
36            $extApi->addPageContentI18nAttribute( $element, $name, $msgKey, $params );
37        } else {
38            $extApi->addLangI18nAttribute( $element, new Bcp47CodeValue( $language ), $name, $msgKey, $params );
39        }
40    }
41
42    public static function addAttributesToNode( array $attrs, Element $node ): void {
43        foreach ( $attrs as $k => $v ) {
44            if ( $v === null ) {
45                continue;
46            }
47            if ( $k === 'class' && is_array( $v ) ) {
48                $node->setAttribute( $k, implode( ' ', $v ) );
49            } else {
50                $node->setAttribute( $k, $v );
51            }
52        }
53    }
54
55}