Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 63 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
PageBundle | |
0.00% |
0 / 63 |
|
0.00% |
0 / 6 |
272 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
toHtml | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
validate | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
42 | |||
responseData | |
0.00% |
0 / 29 |
|
0.00% |
0 / 1 |
6 | |||
apply | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
30 | |||
encodeForHeadElement | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | declare( strict_types = 1 ); |
3 | |
4 | namespace Wikimedia\Parsoid\Core; |
5 | |
6 | use Composer\Semver\Semver; |
7 | use Wikimedia\Parsoid\DOM\Document; |
8 | use Wikimedia\Parsoid\DOM\Element; |
9 | use Wikimedia\Parsoid\DOM\Node; |
10 | use Wikimedia\Parsoid\Utils\ContentUtils; |
11 | use Wikimedia\Parsoid\Utils\DOMCompat; |
12 | use Wikimedia\Parsoid\Utils\DOMDataUtils; |
13 | use Wikimedia\Parsoid\Utils\DOMUtils; |
14 | use Wikimedia\Parsoid\Utils\PHPUtils; |
15 | |
16 | /** |
17 | * PORT-FIXME: This is just a placeholder for data that was previously passed |
18 | * to entrypoint in JavaScript. Who will construct these objects and whether |
19 | * this is the correct interface is yet to be determined. |
20 | */ |
21 | class PageBundle { |
22 | /** @var string */ |
23 | public $html; |
24 | |
25 | /** @var ?array */ |
26 | public $parsoid; |
27 | |
28 | /** @var ?array */ |
29 | public $mw; |
30 | |
31 | /** @var ?string */ |
32 | public $version; |
33 | |
34 | /** @var ?array */ |
35 | public $headers; |
36 | |
37 | /** @var string|null */ |
38 | public $contentmodel; |
39 | |
40 | /** |
41 | * @param string $html |
42 | * @param ?array $parsoid |
43 | * @param ?array $mw |
44 | * @param ?string $version |
45 | * @param ?array $headers |
46 | * @param ?string $contentmodel |
47 | */ |
48 | public function __construct( |
49 | string $html, ?array $parsoid = null, ?array $mw = null, |
50 | ?string $version = null, ?array $headers = null, |
51 | ?string $contentmodel = null |
52 | ) { |
53 | $this->html = $html; |
54 | $this->parsoid = $parsoid; |
55 | $this->mw = $mw; |
56 | $this->version = $version; |
57 | $this->headers = $headers; |
58 | $this->contentmodel = $contentmodel; |
59 | } |
60 | |
61 | public function toHtml(): string { |
62 | $doc = DOMUtils::parseHTML( $this->html ); |
63 | self::apply( $doc, $this ); |
64 | return ContentUtils::toXML( $doc ); |
65 | } |
66 | |
67 | /** |
68 | * Check if this pagebundle is valid. |
69 | * @param string $contentVersion Document content version to validate against. |
70 | * @param ?string &$errorMessage Error message will be returned here. |
71 | * @return bool |
72 | */ |
73 | public function validate( |
74 | string $contentVersion, ?string &$errorMessage = null |
75 | ) { |
76 | if ( !$this->parsoid || !isset( $this->parsoid['ids'] ) ) { |
77 | $errorMessage = 'Invalid data-parsoid was provided.'; |
78 | return false; |
79 | } elseif ( Semver::satisfies( $contentVersion, '^999.0.0' ) |
80 | && ( !$this->mw || !isset( $this->mw['ids'] ) ) |
81 | ) { |
82 | $errorMessage = 'Invalid data-mw was provided.'; |
83 | return false; |
84 | } |
85 | return true; |
86 | } |
87 | |
88 | /** |
89 | * @return array |
90 | */ |
91 | public function responseData() { |
92 | $responseData = [ |
93 | 'contentmodel' => $this->contentmodel ?? '', |
94 | 'html' => [ |
95 | 'headers' => array_merge( [ |
96 | 'content-type' => 'text/html; charset=utf-8; ' |
97 | . 'profile="https://www.mediawiki.org/wiki/Specs/HTML/' |
98 | . $this->version . '"', |
99 | ], $this->headers ?? [] ), |
100 | 'body' => $this->html, |
101 | ], |
102 | 'data-parsoid' => [ |
103 | 'headers' => [ |
104 | 'content-type' => 'application/json; charset=utf-8; ' |
105 | . 'profile="https://www.mediawiki.org/wiki/Specs/data-parsoid/' |
106 | . $this->version . '"', |
107 | ], |
108 | 'body' => $this->parsoid, |
109 | ], |
110 | ]; |
111 | if ( Semver::satisfies( $this->version, '^999.0.0' ) ) { |
112 | $responseData['data-mw'] = [ |
113 | 'headers' => [ |
114 | 'content-type' => 'application/json; charset=utf-8; ' . |
115 | 'profile="https://www.mediawiki.org/wiki/Specs/data-mw/' . |
116 | $this->version . '"', |
117 | ], |
118 | 'body' => $this->mw, |
119 | ]; |
120 | } |
121 | return $responseData; |
122 | } |
123 | |
124 | /** |
125 | * Applies the `data-*` attributes JSON structure to the document. |
126 | * Leaves `id` attributes behind -- they are used by citation code to |
127 | * extract `<ref>` body from the DOM. |
128 | * |
129 | * @param Document $doc doc |
130 | * @param PageBundle $pb page bundle |
131 | */ |
132 | public static function apply( Document $doc, PageBundle $pb ): void { |
133 | DOMUtils::visitDOM( |
134 | DOMCompat::getBody( $doc ), |
135 | static function ( Node $node ) use ( &$pb ): void { |
136 | if ( $node instanceof Element ) { |
137 | $id = $node->getAttribute( 'id' ) ?? ''; |
138 | if ( isset( $pb->parsoid['ids'][$id] ) ) { |
139 | DOMDataUtils::setJSONAttribute( |
140 | $node, 'data-parsoid', $pb->parsoid['ids'][$id] |
141 | ); |
142 | } |
143 | if ( isset( $pb->mw['ids'][$id] ) ) { |
144 | // Only apply if it isn't already set. This means |
145 | // earlier applications of the pagebundle have higher |
146 | // precedence, inline data being the highest. |
147 | if ( !$node->hasAttribute( 'data-mw' ) ) { |
148 | DOMDataUtils::setJSONAttribute( |
149 | $node, 'data-mw', $pb->mw['ids'][$id] |
150 | ); |
151 | } |
152 | } |
153 | } |
154 | } |
155 | ); |
156 | } |
157 | |
158 | /** |
159 | * Encode some of these properties for emitting in the <heaad> element of a doc |
160 | * @return string |
161 | */ |
162 | public function encodeForHeadElement(): string { |
163 | return PHPUtils::jsonEncode( [ 'parsoid' => $this->parsoid ?? [], 'mw' => $this->mw ?? [] ] ); |
164 | } |
165 | } |