public function testGetClassFromNode() { $this->assertEquals('Overblog\\MediaWiki\\LeafHeading', Node::getClassFromNode(Node::NODE_HEADING)); $this->assertEquals('Overblog\\MediaWiki\\LeafParagraph', Node::getClassFromNode(Node::NODE_PARAGRAPH)); $this->assertEquals('Overblog\\MediaWiki\\LeafPre', Node::getClassFromNode(Node::NODE_PRE)); $this->assertEquals('Overblog\\MediaWiki\\BranchList', Node::getClassFromNode(Node::NODE_LIST)); $this->assertEquals('Overblog\\MediaWiki\\BranchListItem', Node::getClassFromNode(Node::NODE_LIST_ITEM)); }
/** * Class constuctor * * @param mixed $data json StdClass, json string or HTML string */ public function __construct($data) { if (is_object($data) and $data->type === Node::NODE_TYPE_DOCUMENT) { if (isset($data->children) and is_array($data->children)) { foreach ($data->children as $c) { $class = Node::getClassFromNode($c->type); $this->_nodes[] = new $class($c); } } } }
/** * Class constuctor * * @param mixed $data json StdClass, json string or HTML string */ public function __construct($data) { if (is_object($data)) { if (isset($data->children) and is_array($data->children)) { foreach ($data->children as $c) { $class = Node::getClassFromNode($c->type); $this->_nodes[] = new $class($c); } } if (isset($data->content)) { $this->_content = new LeafSimple($data); } if (isset($data->attributes)) { $this->_attributes = $data->attributes; } } }
/** * Class constuctor * * Nested lists are handled quite curiously in MediaWiki in that they are * in no way nested, and that the type of list in the * * @param mixed $data json StdClass, json string or HTML string */ public function __construct($data) { if (is_object($data)) { if (isset($data->children) and is_array($data->children)) { $previousType = null; // Previous top-level list type $currentType = null; // Current top-level list type $currentList = null; // Current top-level list $previousLevel = null; // previous element sub-level $currentSubList = null; // Current sub list foreach ($data->children as $c) { // Détermination du type de la première liste if (!isset($c->attributes) or !isset($c->attributes->styles)) { $c->attributes = new \StdClass(); $c->attributes->styles = array(); $c->attributes->styles[] = 'bullet'; } $currentType = $c->attributes->styles[0]; // Si la nouvelle liste est d'un type différent, on change if ($previousType !== $currentType) { if (!is_null($currentList)) { $class = Node::getClassFromNode($currentList->type); $this->_nodes[] = new $class($currentList); } $currentList = json_decode('{"type":"' . ($currentType === 'number' ? 'listOrdered' : 'listUnordered') . '","children":[]}'); $previousType = $currentType; } if (count($c->attributes->styles) > 1) { // ce n'est pas le même niveau que le précédent. // On crée donc un objet de liste dans lequel on // insère le listItem actuel en supprimant un niveau if ($previousLevel !== $c->attributes->styles) { $currentSubList = json_decode('{"type":"listItem",' . '"children":[{"type":"list"}]}'); $previousLevel = $c->attributes->styles; array_pop($c->attributes->styles); $currentSubList->children[0]->children = array(clone $c); } else { array_pop($c->attributes->styles); $currentSubList->children[0]->children[] = clone $c; } } else { if (!is_null($currentSubList)) { $currentList->children[] = $currentSubList; } $currentList->children[] = clone $c; $currentSubList = null; } } if (!is_null($currentSubList)) { $currentList->children[] = $currentSubList; } if (!is_null($currentList)) { $class = Node::getClassFromNode($currentList->type); $this->_nodes[] = new $class($currentList); } } if (isset($data->attributes)) { $this->_attributes = $data->attributes; } } }