Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
80.65% |
25 / 31 |
|
66.67% |
6 / 9 |
CRAP | |
0.00% |
0 / 1 |
HtmlPFragment | |
80.65% |
25 / 31 |
|
66.67% |
6 / 9 |
15.42 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
newFromHtmlString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
castFromPFragment | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
isEmpty | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
asHtmlString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
concat | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
4 | |||
toJsonArray | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
newFromJsonArray | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
jsonClassHintFor | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 |
1 | <?php |
2 | declare( strict_types = 1 ); |
3 | |
4 | namespace Wikimedia\Parsoid\Fragments; |
5 | |
6 | use Wikimedia\Parsoid\Core\DomSourceRange; |
7 | use Wikimedia\Parsoid\Ext\ParsoidExtensionAPI; |
8 | |
9 | /** |
10 | * An atomic fragment represented as an HTML string with inline data |
11 | * attributes, not necessarily balanced. |
12 | */ |
13 | class HtmlPFragment extends PFragment { |
14 | |
15 | public const TYPE_HINT = 'html'; |
16 | |
17 | private string $value; |
18 | |
19 | private function __construct( string $html, ?DomSourceRange $srcOffsets = null ) { |
20 | parent::__construct( $srcOffsets ); |
21 | $this->value = $html; |
22 | } |
23 | |
24 | /** |
25 | * Create a new HtmlPFragment from the given HTML string and optional |
26 | * source string. |
27 | */ |
28 | public static function newFromHtmlString( |
29 | string $html, ?DomSourceRange $srcOffsets |
30 | ): HtmlPFragment { |
31 | return new self( $html, $srcOffsets ); |
32 | } |
33 | |
34 | /** |
35 | * Return a new HtmlPFragment corresponding to the given PFragment. |
36 | * If the fragment is not already an HtmlPFragment, this will convert |
37 | * it using PFragment::asHtmlString(). |
38 | */ |
39 | public static function castFromPFragment( |
40 | ParsoidExtensionAPI $ext, PFragment $fragment |
41 | ): HtmlPFragment { |
42 | if ( $fragment instanceof HtmlPFragment ) { |
43 | return $fragment; |
44 | } |
45 | return new self( $fragment->asHtmlString( $ext ), $fragment->srcOffsets ); |
46 | } |
47 | |
48 | /** @inheritDoc */ |
49 | public function isEmpty(): bool { |
50 | return $this->value === ''; |
51 | } |
52 | |
53 | /** @inheritDoc */ |
54 | public function asHtmlString( ParsoidExtensionAPI $ext ): string { |
55 | return $this->value; |
56 | } |
57 | |
58 | /** |
59 | * Return a HtmlPFragment representing the concatenation of the |
60 | * given fragments, as (unbalanced) HTML strings. |
61 | */ |
62 | public static function concat( ParsoidExtensionAPI $ext, PFragment ...$fragments ): self { |
63 | $result = []; |
64 | $isFirst = true; |
65 | $firstDSR = null; |
66 | $lastDSR = null; |
67 | foreach ( $fragments as $f ) { |
68 | if ( !$f->isEmpty() ) { |
69 | if ( $isFirst ) { |
70 | $firstDSR = $f->getSrcOffsets(); |
71 | $isFirst = false; |
72 | } |
73 | $result[] = $f->asHtmlString( $ext ); |
74 | $lastDSR = $f->getSrcOffsets(); |
75 | } |
76 | } |
77 | return new self( |
78 | implode( '', $result ), |
79 | self::joinSourceRange( $firstDSR, $lastDSR ) |
80 | ); |
81 | } |
82 | |
83 | // JsonCodecable implementation |
84 | |
85 | /** @inheritDoc */ |
86 | public function toJsonArray(): array { |
87 | return [ |
88 | self::TYPE_HINT => $this->value, |
89 | ] + parent::toJsonArray(); |
90 | } |
91 | |
92 | /** @inheritDoc */ |
93 | public static function newFromJsonArray( array $json ): self { |
94 | $v = $json[self::TYPE_HINT]; |
95 | return new self( $v, $json['dsr'] ?? null ); |
96 | } |
97 | |
98 | /** @inheritDoc */ |
99 | public static function jsonClassHintFor( string $keyName ) { |
100 | if ( $keyName === self::TYPE_HINT ) { |
101 | return null; // string |
102 | } |
103 | return parent::jsonClassHintFor( $keyName ); |
104 | } |
105 | } |