Skip to content

Commit

Permalink
Hello world
Browse files Browse the repository at this point in the history
  • Loading branch information
heapoverride committed Jun 11, 2021
1 parent 31ecd44 commit ceb5648
Showing 1 changed file with 63 additions and 9 deletions.
72 changes: 63 additions & 9 deletions modules-disabled/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,21 @@ class Element {
private $nobody = false;
private $before = null;
private $after = null;
private $depth = 0;

function __construct($name) {
$this->name = strtolower($name);
}

private function indent($depth) {
if ($depth === 0) return "";
$str = "";
for ($i = 0; $i<$depth; $i++) {
$str .= " ";
}
return $str;
}

/**
* Set element that appears before this element
* @param Element|string $element
Expand Down Expand Up @@ -51,6 +61,14 @@ function setText($text) {
return $this;
}

/**
* Get if this element is a text element
* @return bool
*/
function isText() {
return $this instanceof Text || $this->text !== null;
}

/**
* Set all child elements
* @param mixed[] $children
Expand All @@ -76,9 +94,12 @@ function setChildren($children) {
*/
function add($element) {
if ($element instanceof Element) {
$element->depth = $this->depth + 1;
$this->children[] = $element;
} else {
$this->children[] = new Text(strval($element));
$text = new Text(strval($element));
$text->depth = $this->depth + 1;
$this->children[] = $text;
}
return $this;
}
Expand Down Expand Up @@ -118,26 +139,46 @@ function setHasBody($hasBody) {
return $this;
}

/**
* Get if this element is empty
* @return bool
*/
function isEmpty() {
return count($this->children) === 0 && ($this->text === null || strlen($this->text) === 0);
}

/**
* Get if this element has children
* @return bool
*/
function hasChildren() {
return !$this->isText() && count($this->children) > 0;
}

/**
* Print the HTML source code
* @param bool $return Set to True to return the generated code instead of writing it to response body
* @param bool $return Set to `true` to return the generated code instead of writing it to response body
* @param bool $format Set to `true` to format the generated HTML source code
* @return string
*/
function html($return = false) {
function html($return = false, $format = false) {
$html = [];

$indent = $format ? $this->indent($this->depth) : "";
$expand = $format ? (count($this->children) >= 2 || count($this->children) == 1 && !$this->children[0]->isText()) : false;

// before
if ($this->before !== null) {
if ($this->before instanceof Element) {
$html[] = $this->before->html();
$html[] = $this->before->html(true, $format);
} else {
$html[] = strval($this->before);
}
}

if ($this->text === null) {
// opening tag
$html[] = "<".$this->name;
$html[] = $indent."<".$this->name;

// attributes
if (count($this->attributes) !== 0) {
Expand All @@ -160,6 +201,7 @@ function html($return = false) {
}

if (!$this->nobody) $html[] = ">";
if ($format && $expand) $html[] = "\n";
} else {
// text
$html[] = htmlspecialchars($this->text);
Expand All @@ -170,23 +212,26 @@ function html($return = false) {
if (is_string($child)) {
$html[] = htmlspecialchars($child);
} else if ($child instanceof Element) {
$html[] = $child->html(true);
$child->depth = $this->depth + 1;
$html[] = $child->html(true, $format);
}
}

if ($this->text === null) {
if (!$this->nobody) {
// closing tag
if ($expand) $html[] = $indent;
$html[] = "</".$this->name.">";
} else {
$html[] = "/>";
$html[] = " />";
}
if ($format) $html[] = "\n";
}

// after
if ($this->after !== null) {
if ($this->after instanceof Element) {
$html[] = $this->after->html();
$html[] = $this->after->html(true, $format);
} else {
$html[] = strval($this->after);
}
Expand Down Expand Up @@ -221,7 +266,7 @@ function setText($text) {
class Html extends Element {
function __construct() {
parent::__construct("html");
parent::setBefore("<!DOCTYPE html>");
parent::setBefore("<!DOCTYPE html>\n");
}
}

Expand Down Expand Up @@ -862,6 +907,15 @@ function __construct() {
}
}

/**
* Label element
*/
class Label extends Element {
function __construct() {
parent::__construct("label");
}
}

/**
* Sub element (subscripted text)
*/
Expand Down

0 comments on commit ceb5648

Please sign in to comment.