/** * 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 testInsertNodeDataProvider */ public function testInsertNode($content, $prepend, $expectedHTML) { $nodeHtml = ''; $infoboxExtractor = new InfoboxExtractor($content); $dom = $infoboxExtractor->getDOMDocument(); $nodes = $infoboxExtractor->getInfoboxNodes(); if ($nodes->length) { $infobox = $nodes->item(0); $parent = $dom->documentElement->firstChild; $infoboxExtractor->insertNode($parent, $infobox, $prepend); if ($prepend === true) { $nodeHtml = trim($dom->saveHTML($parent->firstChild)); } elseif ($prepend === false) { $nodeHtml = trim($dom->saveHTML($parent->lastChild)); } } $this->assertEquals($expectedHTML, $nodeHtml); }