Skip to content

Commit

Permalink
doctype
Browse files Browse the repository at this point in the history
  • Loading branch information
zzgab committed Nov 28, 2015
1 parent 8598976 commit b19d129
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
9 changes: 3 additions & 6 deletions examples/example1-getstarted/template-main.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
<fig:template xmlns:fig="http://figdice.org">
<fig:template xmlns:fig="http://figdice.org" fig:doctype="html">
<!-- Let's make a "fig:*" Root node, so as to mute it automatically from the output. -->
<!-- the root node can carry the fig:doctype attribute, to instruct the engine to
generate a <!DOCTYPE > directive. -->

<!-- Let's use a CDATA sequence in our template, to
produce the DOCTYPE element in the output -->
<![CDATA[
<!DOCTYPE html>
]]>
<html lang="en">
<head>
<!-- You can use any expression, including 'paths and symbols' in the
Expand Down
31 changes: 28 additions & 3 deletions src/figdice/classes/ViewElementTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,18 @@ public function setAttribute($name, $value) {
public function hasAttribute($name) {
return array_key_exists($name, $this->attributes);
}

/**
* Indicates whether the current tag carries the specified attribute within
* the fig: namespace (where "fig:" is soft-coded according the xmlns:fig).
* @param $name
* @return bool
*/
private function hasFigAttribute($name)
{
return $this->hasAttribute($this->view->figNamespace . $name);
}

public function appendChild(ViewElement & $child) {
if(0 < count($this->children)) {
$this->children[count($this->children) - 1]->nextSibling = & $child;
Expand Down Expand Up @@ -329,6 +341,8 @@ private function renderNoMacro($bypassWalk = false) {
//so that its immediate children can run the fig:case attribute form fresh.
$this->caseSwitched = false;



//================================================================
//fig:cond
//(fig:condition is deprecated)
Expand Down Expand Up @@ -417,7 +431,7 @@ private function renderNoMacro($bypassWalk = false) {
$this->parent->runtimeAttributes[$this->attributes['name']] = new Flag();
}
else {
if (isset($this->attributes['value'])) {
if ($this->hasAttribute('value')) {
$value = $this->evaluate($this->attributes['value']);
if (is_string($value)) {
$value = htmlspecialchars($value);
Expand Down Expand Up @@ -450,7 +464,7 @@ private function renderNoMacro($bypassWalk = false) {
//fig:walk
//Loop over evaluated dataset.
if(! $bypassWalk) {
if($this->hasAttribute($this->view->figNamespace . 'walk')) {
if($this->hasFigAttribute('walk')) {
return $this->fig_walk();
}
}
Expand All @@ -459,7 +473,7 @@ private function renderNoMacro($bypassWalk = false) {
//fig:call
//A tag with the fig:call directive is necessarily mute.
//It is used as a placeholder only for the directive.
if(isset($this->attributes[$this->view->figNamespace . 'call'])) {
if($this->hasFigAttribute('call')) {
return $this->fig_call();
}

Expand Down Expand Up @@ -663,6 +677,14 @@ private function renderNoMacro($bypassWalk = false) {
$this->runtimeAttributes = array();
}


//================================================================
// If we're dealing with rootnode, trean doctype directive
// fig:doctype
if ((null == $this->parent) && ($this->hasFigAttribute('doctype'))) {
$result = '<!doctype ' . $this->getFigAttribute('doctype') . '>' . PHP_EOL . $result;
}

return $result;
}

Expand Down Expand Up @@ -728,6 +750,9 @@ public function getAttribute($name, $default = null) {
}
return $default;
}
private function getFigAttribute($name, $default = null) {
return $this->getAttribute($this->view->figNamespace . $name, $default);
}

private function fig_mount() {
$target = $this->getAttribute('target');
Expand Down
19 changes: 19 additions & 0 deletions test/ViewParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,23 @@ public function testWalkOnNonCountableObjectRunsOnArrayWithObject()
$view->mount('obj', $obj);
$this->assertEquals('<test>12</test>', $view->render());
}

public function testDoctype()
{
$view = new View();
$templateSource = <<<ENDXML
<tpl:template xmlns:tpl="http://figdice.org" tpl:doctype="html">
<html></html>
</tpl:template>
ENDXML;
$view->loadString($templateSource);

$expected = <<<EXPECTED
<!doctype html>
<html></html>
EXPECTED;

$this->assertEquals($expected, $view->render());
}
}

0 comments on commit b19d129

Please sign in to comment.