9
9
use Wikimedia \Parsoid \Core \Sanitizer ;
10
10
use Wikimedia \Parsoid \DOM \Document ;
11
11
use Wikimedia \Parsoid \DOM \Element ;
12
+ use Wikimedia \Parsoid \Ext \ParsoidExtensionAPI ;
12
13
use Wikimedia \Parsoid \Utils \DOMCompat ;
13
14
14
15
class ParsoidPortableInfoboxRenderService {
@@ -41,31 +42,34 @@ public function __construct() {
41
42
* @param array $params
42
43
* @return void
43
44
*/
44
- private function buildParamMap ( array $ params ): void {
45
- // loop over all of the parameters we received and if they have both a value
46
- // and a name, then add them to the array - if they do not have a "wt" value, then the value was
47
- // empty and therefore we should not render this part of the infobox
48
- // At this point, we have what we pretty much have what we pass to the legacy function on line
49
- // 104 of PortableInfoboxParserTagController::class
50
- foreach ( $ params as $ param ) {
51
- if ( isset ( $ param ->k ) && isset ( $ param ->valueWt ) ) {
52
- $ this ->paramMap [ $ param ->k ] = $ param ->valueWt ;
53
- }
54
- }
55
- }
45
+ private function buildParamMap ( ParsoidExtensionAPI $ extApi , array $ params ): void {
46
+ // loop over all of the parameters we received and if they have both a value
47
+ // and a name, then add them to the array - if they do not have a "valueWt" value, then the value was
48
+ // empty and therefore we should not render this part of the infobox
49
+ // At this point, we have what we pretty much have what we pass to the legacy function on line
50
+ // 104 of PortableInfoboxParserTagController::class
51
+ foreach ( $ params as $ param ) {
52
+ if ( isset ( $ param ->k ) && isset ( $ param ->valueWt ) ) {
53
+ $ htmlValue = $ this ->processWikitextToHtml ( $ extApi , $ param ->valueWt );
54
+ $ this ->paramMap [ $ param ->k ] = $ htmlValue ;
55
+ }
56
+ }
57
+ }
58
+
56
59
57
60
/**
58
61
* This function is responsible for rendering the actual infobox to the DOM
59
62
* This is the entrypoint which should be called to render the infobox from the
60
63
* DOMProcessor. This will delegate appropriately
61
64
*/
62
65
public function render (
66
+ ParsoidExtensionAPI $ extApi ,
63
67
Element $ container ,
64
68
Document $ doc ,
65
69
array $ params ,
66
70
string $ parsoidData
67
71
): void {
68
- $ this ->buildParamMap ( $ params );
72
+ $ this ->buildParamMap ( $ extApi , $ params );
69
73
[ $ data , $ attr ] = $ this ->prepareInfobox ( $ parsoidData , $ this ->paramMap ?: [] );
70
74
71
75
$ themes = $ this ->getThemes ( $ attr );
@@ -100,7 +104,6 @@ public function prepareInfobox(
100
104
string $ parsoidData ,
101
105
array $ params ,
102
106
): array {
103
- $ this ->buildParamMap ( $ params );
104
107
105
108
// same as legacy!
106
109
$ infoboxNode = NodeFactory::newFromXML ( $ parsoidData , $ this ->paramMap ?: [] );
@@ -488,4 +491,33 @@ private function createHorizontalGroupData( array $groupData ) {
488
491
return $ horizontalGroupData ;
489
492
}
490
493
494
+ /**
495
+ * A utility function to parse wikitext to HTML which will be passed into the infobox
496
+ * template
497
+ * @param \Wikimedia\Parsoid\Ext\ParsoidExtensionAPI $extApi
498
+ * @param string $wikitext
499
+ * @return string
500
+ */
501
+ private function processWikitextToHtml ( ParsoidExtensionAPI $ extApi , string $ wikitext ): string {
502
+
503
+ $ paramParsed = $ extApi ->wikitextToDOM ( $ wikitext , [
504
+ 'processInNewFrame ' => true ,
505
+ 'parseOpts ' => [ 'context ' => 'inline ' ]
506
+ ], true );
507
+
508
+
509
+ $ htmlContent = '' ;
510
+ // this feels really hacky?!
511
+ if ( $ paramParsed ->hasChildNodes () ) {
512
+
513
+ $ tempDoc = $ paramParsed ->ownerDocument ;
514
+
515
+ foreach ( $ paramParsed ->childNodes as $ childNode ) {
516
+ $ htmlContent .= $ tempDoc ->saveHTML ( $ childNode );
517
+ }
518
+ }
519
+
520
+ return $ htmlContent ;
521
+ }
522
+
491
523
}
0 commit comments