summaryrefslogtreecommitdiff
path: root/src/HTML5/Serializer/OutputRules.php
blob: 3db857b47c03d24fd63b90ef1b96ab05004d09fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
namespace HTML5\Serializer;

use \HTML5\Elements;

class OutputRules {

  protected $traverser;
  protected $encode = FALSE;
  protected $out;

  const DOCTYPE = '<!DOCTYPE html>';

  public function __construct($traverser, $output, $options = array()) {
    $this->traverser = $traverser;

    if (isset($options['encode'])) {
      $this->encode = $options['encode'];
    }

    $this->out = $output;

  }

  public function document($dom) {
    $this->doctype();
    $this->traverser->node($dom->documentElement);
    $this->nl();
  }

  protected function doctype() {
    $this->wr(self::DOCTYPE);
    $this->nl();
  }

  public function element($ele) {
    $name = $ele->tagName;

    // Per spec:
    // If the element has a declared namespace in the HTML, MathML or
    // SVG namespaces, we use the lname instead of the tagName.
    if ($this->traverser->isLocalElement($ele)) {
      $name = $ele->localName;
    }

    $this->openTag($ele);

    // Handle children.
    if ($ele->hasChildNodes()) {
      $this->traverser->children($ele->childNodes);
    }

    // If not unary, add a closing tag.
    if (!Elements::isA($name, Elements::VOID_TAG)) {
      $this->closeTag($ele);
    }
  }

  /**
   * Write a text node.
   *
   * @param \DOMText $ele 
   *   The text node to write.
   */
  public function text($ele) {
    if (isset($ele->parentNode) && Elements::isA($ele->parentNode->tagName, Elements::TEXT_RAW)) {
      $this->wr($ele->data);
      return;
    }

    // FIXME: This probably needs some flags set.
    $this->wr($this->enc($ele->data));

  }

  public function cdata($ele) {
    $this->wr('<![CDATA[')->wr($ele->data)->wr(']]>');
  }

  public function comment($ele) {
    $this->wr('<!--')->wr($ele->data)->wr('-->');
  }

  public function processorInstruction($ele) {
    $this->wr('<?')->wr($ele->target)->wr(' ')->wr($ele->data)->wr(' ?>');
  }

  /**
   * Write the opening tag.
   *
   * Tags for HTML, MathML, and SVG are in the local name. Otherwise, use the
   * qualified name (8.3).
   * 
   * @param \DOMNode $ele
   *   The element being written.
   */
  protected function openTag($ele) {
    // FIXME: Needs support for SVG, MathML, and namespaced XML.
    $this->wr('<')->wr($ele->tagName);
    $this->attrs($ele);
    $this->wr('>');
  }

  protected function attrs($ele) {
    // FIXME: Needs support for xml, xmlns, xlink, and namespaced elements.
    if (!$ele->hasAttributes()) {
      return $this;
    }

    // TODO: Currently, this always writes name="value", and does not do
    // value-less attributes.
    $map = $ele->attributes;
    $len = $map->length;
    for ($i = 0; $i < $len; ++$i) {
      $node = $map->item($i);
      $val = $this->enc($node->value);

      // XXX: The spec says that we need to ensure that anything in
      // the XML, XMLNS, or XLink NS's should use the canonical
      // prefix. It seems that DOM does this for us already, but there
      // may be exceptions.
      $this->wr(' ')->wr($node->name)->wr('="')->wr($val)->wr('"');
    }
  }

  /**
   * Write the closing tag.
   * 
   * Tags for HTML, MathML, and SVG are in the local name. Otherwise, use the
   * qualified name (8.3).
   *
   * @param \DOMNode $ele
   *   The element being written.
   */
  protected function closeTag($ele) {
    // FIXME: Needs support for SVG, MathML, and namespaced XML.
    $this->wr('</')->wr($ele->tagName)->wr('>');
  }

  /**
   * Write to the output.
   *
   * @param string $text
   *   The string to put into the output.
   *
   * @return HTML5\Serializer\Traverser
   *   $this so it can be used in chaining.
   */
  protected function wr($text) {
    fwrite($this->out, $text);
    return $this;
  }

  /**
   * Write a new line character.
   *
   * @return HTML5\Serializer\Traverser
   *   $this so it can be used in chaining.
   */
  protected function nl() {
    fwrite($this->out, PHP_EOL);
    return $this;
  }

  /**
   * Encode text.
   *
   * True encoding will turn all named character references into their entities.
   * This includes such characters as +.# and many other common ones. By default
   * encoding here will just escape &'<>".
   *
   * Note, PHP 5.4+ has better html5 encoding.
   *
   * @todo Use the Entities class in php 5.3 to have html5 entities.
   *
   * @param string $text
   *   text to encode.
   *
   * @return string
   *   The encoded text.
   */
  protected function enc($text) {
    $flags = ENT_QUOTES;

    // Escape rather than encode all entities.
    if (!$this->encode) {
      return htmlspecialchars($text, $flags, 'UTF-8');
    }

    // If we are in PHP 5.4+ we can use the native html5 entity functionality.
    if (defined('ENT_HTML5')) {
      $flags = ENT_HTML5 | ENT_SUBSTITUTE | ENT_QUOTES;
      $ret = htmlentities($text, $flags, 'UTF-8', FALSE);
    }
    // If a version earlier than 5.4 html5 entities are not entirely handled.
    // This manually handles them.
    else {
      $ret = strtr($text, \HTML5\Serializer\HTML5Entities::$map);
    }
    return $ret;
  }

}