KaiRo bug 401 - Convert UA tests to using ExtendedDocument, also updating the used...
[php-utility-classes.git] / tests / ua_list.php
index 3c9fa000f5c43257952d3aa1569de84ee9ced07e..5c2e5ceede0db15c1007e7f03961142242694645 100644 (file)
@@ -4,68 +4,76 @@
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 include('../classes/useragent.php-class');
+include('../classes/document.php-class');
 
 // read string from this file
 $uafile = 'ua_list_raw.txt';
 
-print("<!DOCTYPE html>\n");
-print("<html>\n<head>\n");
-print("  <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n");
-print("  <link rel=\"stylesheet\" type=\"text/css\" href=\"test.css\">\n");
-print("  <title>".'User Agents'."</title>\n");
-print("</head>\n<body>\n");
+// Start HTML document as a DOM object.
+extract(ExtendedDocument::initHTML5()); // sets $document, $html, $head, $title, $body
+$document->formatOutput = true; // we want a nice output
 
-print('<h1>User Agents</h1>'."\n");
+$style = $head->appendElement('link');
+$style->setAttribute('rel', 'stylesheet');
+$style->setAttribute('href', 'test.css');
+$title->appendText('User Agents');
+$h1 = $body->appendElement('h1', 'User Agents');
 
 $ualist = is_readable($uafile)?file($uafile):array();
 
 if (count($ualist)) {
-  print('<table class="border" id="ualist">'."\n");
-  print(' <tr>'."\n");
-  print('  <th>User Agent string</th>'."\n");
-  print('  <th>Brand</th>'."\n");
-  print('  <th>Version</th>'."\n");
-  print('  <th>Bot</th>'."\n");
-  print('  <th>Mob</th>'."\n");
-  print('  <th>Engine</th>'."\n");
-  print('  <th>eVer</th>'."\n");
-  print('  <th>OS</th>'."\n");
-  print('  <th>Platform</th>'."\n");
-  print('  <th>Lang</th>'."\n");
-  print(' </tr>'."\n");
+  $tbl = $body->appendElement('table');
+  $tbl->setAttribute('class', 'border');
+  $tbl->setAttribute('id', 'ualist');
+  $thead = $tbl->appendElement('thead');
+  $trow = $thead->appendElement('tr');
+  $trow->appendElement('th', 'User Agent string');
+  $trow->appendElement('th', 'Brand');
+  $trow->appendElement('th', 'Version');
+  $trow->appendElement('th', 'Bot');
+  $trow->appendElement('th', 'Mob');
+  $trow->appendElement('th', 'Engine');
+  $trow->appendElement('th', 'eVer');
+  $trow->appendElement('th', 'OS');
+  $trow->appendElement('th', 'Platform');
+  $trow->appendElement('th', 'Lang');
 
+  $tbody = $tbl->appendElement('tbody');
   foreach ($ualist as $uastring) {
     $uastring = trim($uastring);
     if (substr($uastring, 0, 1) == '#') {
       // comment
-      print(' <tr>'."\n");
-      print('  <td colspan="10" class="comment">'.substr($uastring, 1).'</td>'."\n");
-      print(' </tr>'."\n");
+      $trow = $tbody->appendElement('tr');
+      $cell = $trow->appendElement('td', substr($uastring, 1));
+      $cell->setAttribute('colspan', 10);
+      $cell->setAttribute('class', 'comment');
     }
     else {
       $ua = new userAgent($uastring);
-      print(' <tr>'."\n");
-      print('  <td class="ua">'.$ua->getUAString().'</td>'."\n");
-      print('  <td>'.$ua->getBrand().'</td>'."\n");
-      print('  <td>'.$ua->getVersion().'</td>'."\n");
-      print('  <td>'.($ua->isBot()?'x':'-').'</td>'."\n");
-      print('  <td>'.($ua->isMobile()?'x':'-').'</td>'."\n");
-      print('  <td>'.$ua->getEngine().'</td>'."\n");
-      print('  <td>'.$ua->getEngineVersion().'</td>'."\n");
-      print('  <td>'.$ua->getOS().'</td>'."\n");
-      print('  <td>'.$ua->getPlatform().'</td>'."\n");
-      print('  <td>'.$ua->getLanguage().'</td>'."\n");
-      print(' </tr>'."\n");
+      $trow = $tbody->appendElement('tr');
+      $cell = $trow->appendElement('td', $ua->getUAString());
+      $cell->setAttribute('class', 'ua');
+      $trow->appendElement('td', $ua->getBrand());
+      $trow->appendElement('td', $ua->getVersion());
+      $trow->appendElement('td', ($ua->isBot()?'x':'-'));
+      $trow->appendElement('td', ($ua->isMobile()?'x':'-'));
+      $trow->appendElement('td', $ua->getEngine());
+      $trow->appendElement('td', $ua->getEngineVersion());
+      $trow->appendElement('td', $ua->getOS());
+      $trow->appendElement('td', $ua->getPlatform());
+      $trow->appendElement('td', $ua->getLanguage());
     }
   }
-  print('</table>'."\n");
 }
 else {
-  print('No User Agent strings found in file "'.$uafile.'".'."\n");
+  $body->appendElement('p', 'No User Agent strings found in file "'.$uafile.'".');
 }
 
-print("<div id=\"copyright\">\n");
-print("Page code under Mozilla Public License, code available at <a href=\"https://github.com/KaiRo-at/php-utility-classes\">GitHub</a>.\n");
-print("</div>\n");
-print("</body></html>\n");
+$footer = $body->appendElement('footer', 'Page code under Mozilla Public License, code available at ');
+$footer->setAttribute('id', 'copyright');
+$footer->appendLink('https://github.com/KaiRo-at/php-utility-classes', 'GitHub');
+$footer->appendText('.');
+
+// Send HTML to client.
+print($document->saveHTML());
 ?>