Skip to content

Commit

Permalink
Merge pull request #38 from WebFiori/dev
Browse files Browse the repository at this point in the history
Added Support for Inline JS
  • Loading branch information
usernane authored Sep 10, 2023
2 parents 1fb340b + 67c6e7b commit ec2c836
Show file tree
Hide file tree
Showing 18 changed files with 148 additions and 60 deletions.
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Files and folders here will be not included when creating package
* text eol=crlf
*.php text eol=crlf
*.html text eol=crlf
/tests export-ignore
/.github export-ignore
/.gitignore export-ignore
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/php70.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:

steps:
- name: Clone Repo
uses: actions/checkout@v1
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/php71.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:

steps:
- name: Clone Repo
uses: actions/checkout@v1
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/php72.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:

steps:
- name: Clone Repo
uses: actions/checkout@v1
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/php73.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:

steps:
- name: Clone Repo
uses: actions/checkout@v1
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/php74.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:

steps:
- name: Clone Repo
uses: actions/checkout@v1
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/php80.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:

steps:
- name: Clone Repo
uses: actions/checkout@v1
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/php81.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:

steps:
- name: Clone Repo
uses: actions/checkout@v1
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/php82.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:

steps:
- name: Clone Repo
uses: actions/checkout@v1
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand Down
2 changes: 1 addition & 1 deletion tests/test-templates/php-template-2.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</ul>
<?php
} else {
echo "No posts.\n";
echo "No posts.\r\n";
}
?>
</div>
Expand Down
10 changes: 10 additions & 0 deletions tests/webfiori/test/ui/HTMLTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ public function test00() {
. '<tr><td></td><td></td><td></td><td></td><td></td>'
. '</tr>'
. '</table>', $table->toHTML());
$table->setFirstColCellType('th');
$this->assertEquals('<table border="1" style="border-collapse:collapse;">'
. '<tr>'
. '<th></th><th></th><th></th><th></th><th></th>'
. '</tr>'
. '<tr><td></td><td></td><td></td><td></td><td></td>'
. '</tr>'
. '<tr><td></td><td></td><td></td><td></td><td></td>'
. '</tr>'
. '</table>', $table->toHTML());
$table->setIsQuotedAttribute(false);
}
/**
Expand Down
50 changes: 47 additions & 3 deletions tests/webfiori/test/ui/LoadTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ public function test04() {
public function test05() {
$compiler = new TemplateCompiler(self::TEST_TEMPLATES_PATH.'php-template.php');
$node = $compiler->getCompiled();
$this->assertEquals("<div>\n This is a test on php</div>", $node->toHTML());
$this->assertEquals("<div>\r\n"
. " This is a test on php</div>", $node->toHTML());
}
/**
* @test
Expand Down Expand Up @@ -111,8 +112,11 @@ public function test07() {
'posts' => []
]);
$node = $compiler->getCompiled();
$this->assertEquals("<div>\n No posts.\n</div>", $node->toHTML());
$this->assertEquals("<div>\r\n"
. " No posts.\r\n"
. "</div>", $node->toHTML());
}

/**
* @test
*/
Expand All @@ -130,11 +134,51 @@ public function test08() {
. "<li>Two</li>"
. "<li>Three</li>"
. "</ul>"
. "<div>\n"
. "<div>\r\n"
. " Good Job!"
. "</div>"
. "</div>", $compiler->getCompiled()->toHTML());
}
/**
* @test
*/
public function test09() {
$compiler = new TemplateCompiler('template.html');
$this->assertEquals("<div v-if=\"someVar <= 6 || someVar >= 8 || someVar === 6\">\r\n"
. " <script>\r\n"
. " \r\n"
. " function allIsGood() {\r\n"
. " if (a > 6) {\r\n"
. " alert(\"Oh. A is > 6 but probably < 100.\");\r\n"
. " }\r\n"
. " if (a < 100) {\r\n"
. " alert('Oh. A is < 100.');\r\n"
. " }\r\n"
. " }\r\n"
. " \r\n"
. " </script>\r\n"
. "</div>\r\n", $compiler->getCompiled()->toHTML(true));
}
/**
* @test
*/
public function test10() {
$compiler = new TemplateCompiler('template2.php');
$this->assertEquals("<div v-if=\"someVar <= 6 || someVar >= 8 || someVar === 6\">\r\n"
. " <script>\r\n"
. " \r\n"
. " function allIsGood() {\r\n"
. " if (a > 6) {\r\n"
. " alert(\"Oh. A is > 6 but probably < 100.\");\r\n"
. " }\r\n"
. " if (a < 100) {\r\n"
. " alert('Oh. A is < 100.');\r\n"
. " }\r\n"
. " }\r\n"
. " \r\n"
. " </script>\r\n"
. "</div>\r\n", $compiler->getCompiled()->toHTML(true));
}
/**
* @test
*/
Expand Down
12 changes: 12 additions & 0 deletions tests/webfiori/test/ui/template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div v-if="someVar <= 6 || someVar >= 8 || someVar === 6">
<script>
function allIsGood() {
if (a > 6) {
alert("Oh. A is > 6 but probably < 100.");
}
if (a < 100) {
alert('Oh. A is < 100.');
}
}
</script>
</div>
14 changes: 14 additions & 0 deletions tests/webfiori/test/ui/template2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
?>
<div v-if="someVar <= 6 || someVar >= 8 || someVar === 6">
<script>
function allIsGood() {
if (a > 6) {
alert("Oh. A is > 6 but probably < 100.");
}
if (a < 100) {
alert('Oh. A is < 100.');
}
}
</script>
</div>
4 changes: 2 additions & 2 deletions webfiori/ui/HTMLList.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function addChild($node, $attrsOrChain = [], bool $chainOnParent = false)
if ($node instanceof HTMLNode) {
$li->addChild($node);
} else {
$li->text($node);
$li->text($node.'');
}

return parent::addChild($li, $attrsOrChain, $chainOnParent);
Expand Down Expand Up @@ -110,7 +110,7 @@ public function addListItem($listItemBody, bool $escHtmlEntities = true) : HTMLL
if ($listItemBody instanceof HTMLNode) {
$li->addChild($listItemBody);
} else {
$li->text($listItemBody,$escHtmlEntities);
$li->text($listItemBody.'',$escHtmlEntities);
}
$this->addChild($li);
}
Expand Down
36 changes: 18 additions & 18 deletions webfiori/ui/HTMLNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ class HTMLNode implements Countable, Iterator {
*
*/
private $htmlString;
private $isEsc;
private static $IsFormatted = false;
/**
* A global static variable to decide if attributes values
Expand All @@ -153,7 +154,6 @@ class HTMLNode implements Countable, Iterator {
*
*/
private $isVoid;
private $isEsc;
/**
* The name of the tag (such as 'div')
* @var string
Expand Down Expand Up @@ -246,6 +246,7 @@ public function __construct(string $name = 'div', array $attrs = []) {

if ($nameUpper == self::TEXT_NODE || $nameUpper == self::COMMENT_NODE) {
$this->name = $nameUpper;

if ($nameUpper == self::COMMENT_NODE) {
$this->isEsc = false;
}
Expand Down Expand Up @@ -1061,19 +1062,6 @@ public function getName() {
public function getNodeName() : string {
return $this->name;
}
/**
* Sets the value of the property which is used to check if the text
* on the body of the node will be escaped or not if it has HTML entities.
*
* This only applies to text node.
*
* @param bool $esc
*/
public function setEscapeEntities(bool $esc) {
if ($this->isTextNode()) {
$this->isEsc = $esc;
}
}
/**
* Returns the parent node.
*
Expand Down Expand Up @@ -1782,7 +1770,7 @@ public function section($title, int $headingLvl = 1, array $attributes = []) : H
if ($title instanceof HTMLNode) {
$heading->addChild($title);
} else {
$heading->text($title, false);
$heading->text($title.'', false);
}
$section = new HTMLNode('section', $attributes);
$section->addChild($heading);
Expand Down Expand Up @@ -1890,6 +1878,19 @@ public function setClassName(string $val, bool $override = true) : HTMLNode {

return $this;
}
/**
* Sets the value of the property which is used to check if the text
* on the body of the node will be escaped or not if it has HTML entities.
*
* This only applies to text node.
*
* @param bool $esc
*/
public function setEscapeEntities(bool $esc) {
if ($this->isTextNode()) {
$this->isEsc = $esc;
}
}
/**
* Sets the value of the attribute 'id' of the node.
*
Expand Down Expand Up @@ -1975,9 +1976,11 @@ public function setNodeName(string $name) : bool {

if (($this->isTextNode() && $uName == self::COMMENT_NODE) || ($this->isComment() && $uName == self::TEXT_NODE)) {
$this->name = $uName;

if ($uName == self::COMMENT_NODE) {
$this->isEsc = false;
}

return true;
} else {
return false;
Expand Down Expand Up @@ -2095,9 +2098,6 @@ public function setTabIndex(int $val) : HTMLNode {
*/
public function setText(string $text, bool $escHtmlEntities = true) : HTMLNode {
if ($this->isTextNode() || $this->isComment()) {



if ($this->isComment()) {
$text = str_replace('<!--', ' --', str_replace('-->', '-- ', $text));
} else {
Expand Down
Loading

0 comments on commit ec2c836

Please sign in to comment.