/** * Check if infobox (div element or table which contains 'infobox' string in class attribute) * exists in first article section, and extract it from this section * * @param Parser $parser Parser instance * @param integer $section number of section in article text * @param string $content reference to section content * @param boolean $showEditLinks should add edit link * @return bool */ public static function onParserSectionCreate($parser, $section, &$content, $showEditLinks) { // skip if we're not parsing for venus if (!F::app()->checkSkin('venus')) { return true; } try { if (self::isInfoboxInFirstSection($parser, $section, $content)) { $infoboxExtractor = new InfoboxExtractor($content); $dom = $infoboxExtractor->getDOMDocument(); $nodes = $infoboxExtractor->getInfoboxNodes(); $node = $nodes->item(0); if ($node instanceof DOMElement) { $body = $dom->documentElement->firstChild; // replace extracted infobox with a dummy element to prevent newlines from creating empty paragraphs (CON-2166) // <table infobox-placeholder="1"></table> $placeholder = $dom->createElement('table'); $placeholder->setAttribute('infobox-placeholder', 'true'); $node->parentNode->insertBefore($placeholder, $node); // perform a magic around infobox wrapper $node = $infoboxExtractor->clearInfoboxStyles($node); $infoboxWrapper = $infoboxExtractor->wrapInfobox($node, 'infoboxWrapper', 'infobox-wrapper'); $infoboxContainer = $infoboxExtractor->wrapInfobox($infoboxWrapper, 'infoboxContainer', 'infobox-container'); // move infobox to the beginning of article content $infoboxExtractor->insertNode($body, $infoboxContainer, true); $content = $dom->saveHTML(); $parser->getOutput()->addModules('ext.wikia.venus.article.infobox'); } } } catch (DOMException $e) { // log exceptions WikiaLogger::instance()->error(__METHOD__, ['exception' => $e]); } return true; }
/** * @dataProvider testWrapInfoboxDataProvider */ public function testWrapInfobox($html, $attributes, $expectedHtml) { $infoboxExractor = new InfoboxExtractor($html); $dom = $infoboxExractor->getDOMDocument(); $infobox = $dom->documentElement->firstChild->firstChild; $infoboxContainer = $infoboxExractor->wrapInfobox($infobox, $attributes['id'], $attributes['class']); $nodeHtml = $dom->saveHTML($infoboxContainer); $this->assertEquals($nodeHtml, $expectedHtml); }