tagHandler.php Still WIP

This commit is contained in:
2018-11-19 16:56:32 +01:00
parent 4d44b671a4
commit 0c1d9f24ed

View File

@ -4,47 +4,97 @@
*/ */
class tag { class tag {
private $DOM = "<div>Please, edit it !</div>"; private $DOM;
private $attr = array(); private $doc;
public function __construct(array $attributes = array(), String $DOMContent= "") { public function __construct(DOMDocument $doc, DOMElement $DOMContent) {
$this->attr = array_merge($this->attr, $attributes); $this->doc = $doc;
$this->DOM = $DOMContent; $this->DOM = $DOMContent;
} }
private function process() { public function getDoc() {
return $this->doc;
}
public function getDOM() {
return $this->DOM; return $this->DOM;
} }
public function render() { public function render() {
return $this->process(); return $this->DOM;
} }
} }
$post = array(
'title'=> "test",
'url'=> "pokemon",
'content'=> "<p>pouet</p>"
);
$posts = array(
$post,
$post,
$post,
$post,
);
class bold extends tag {
public function render() {
class loop extends tag { //recuperation de la balise de base (<tag type="bold">pouet</tag>)
private function process() { $pok = $this->getDOM();
return "<div>pokemon</div>"; //recuperation du document (necessaire a la création de balises
$doc = $this->getDoc();
//creation de la balise "div"
$res = $doc->createElement("div");
//creation du texte et assignation du texte se trouvant dans la balise de base
$text = $doc->createTextNode($pok->textContent);
//on rajoute a notre balise div notre texte
$res->appendChild($text);
//on rajoute a la balise div du style pour le mettre en gras
$res->setAttribute("style", "font-weight: bold");
//on retourne la div
return $res;
} }
} }
$tag = new tag();
$loop = new loop(); class article extends tag {
echo $tag->render(); public function render() {
echo $loop->render(); $pok = $this->getDOM();
?>
<!DOCTYPE html> $doc = $this->getDoc();
<html>
<head> var_dump($pok->getElementsByTagName("test"));
<meta charset="utf-8" /> }
<meta http-equiv="X-UA-Compatible" content="IE=edge"> }
<title>Page Title</title> //testing purpose
<meta name="viewport" content="width=device-width, initial-scale=1"> $content = file_get_contents("./test.html");
</head>
<body> function loadTags($ctnt) {
<tag type="tag"></tag> $dom = new DOMDocument();
<tag type="loop"></tag> libxml_use_internal_errors(true);
</body> $dom->loadHTML($ctnt);
</html> libxml_clear_errors();
$list = $dom->getElementsByTagName("tag");
//list compoenents and load content
foreach($list as $lst) {
$tgs = $lst->getAttribute("type");
//echo $tgs;
$tg = new $tgs($dom, $lst);
//add to parent the result
$lst->parentNode->appendChild($tg->render());
}
//remove all tag components
while($list->length >= 1) {
$list[0]->parentNode->removeChild($list[0]);
}
$res = $dom->saveHTML();
echo $res;
}
loadTags($content);
?>