Esempio n. 1
0
 /**
  * \protected
  *
  * Create a new XHTML element.
  *
  * This constructor accepts a variable number of arguments. Each argument
  * is appended as a child node, but the last argument is handled
  * differently. If the last argument is an associative array, its values are
  * used as attribute names and values. If it's not an associative array, it
  * is treated just as the other arguments and appended as a child node.
  *
  * The AnewtXHTMLElement class is abstract and cannot be instantiated
  * directly. Use one of its descendants instead (all XHTML element classes
  * extend AnewtXHTMLElement); there is one class for each XHTML element in
  * the HTML specification.
  *
  * \param $children
  *   One or more child nodes (optional).
  *
  * \param $attributes
  *   Associative array with element attributes (optional).
  */
 function __construct($children = null, $attributes = null)
 {
     assert('!is_null($this->node_name); // node name must be specified');
     parent::__construct($this->node_name);
     $num_args = func_num_args();
     if ($num_args == 0) {
         return;
     }
     $args = func_get_args();
     /* Use last element for attributes, but only if it's an associative
      * array. */
     if (is_assoc_array($args[$num_args - 1])) {
         $attributes = array_pop($args);
         $this->set_attributes($attributes);
     }
     /* Add additional arguments as child nodes. */
     $this->append_children($args);
 }
Esempio n. 2
0
 /**
  * Render this page into XHTML.
  *
  * This methods renders the whole page into a complete XHTML page. Usually
  * you want to use flush() to output the page to the browser.
  *
  * \return
  *   The rendered page as a string.
  *
  * \see AnewtPage::flush
  */
 public function render()
 {
     /* Create basic element nodes */
     $head = new AnewtXMLDomElement('head');
     $body = new AnewtXMLDomElement('body');
     $head->always_render_closing_tag = true;
     $body->always_render_closing_tag = true;
     /* Content-type in meta tag. This must be the first element inside the
      * <head>...</head> element. */
     $head->append_child(ax_meta(array('http-equiv' => 'Content-type', 'content' => $this->build_content_type_charset())));
     /* Base URI */
     $base_uri = $this->_get('base-uri');
     if (!is_null($base_uri)) {
         assert('is_string($base_uri); // base-uri must be a simple string');
         /* Employ a simple heuristic to make sure we use an absolute URI, as
          * is required by the HTML specification. */
         if (!str_contains($base_uri, '://')) {
             $base_uri = AnewtRequest::canonical_base_url() . $base_uri;
         }
         $base = new AnewtXHTMLBase();
         $base->set_attribute('href', $base_uri);
         $head->append_child($base);
     }
     /* Page title (always include, even if empty, since this is required by
      * the HTML specification) */
     $title = $this->_get('title');
     $head->append_child(ax_title($title));
     /* Dublin Core metadata. See http://dublincore.org/documents/dcq-html/ * */
     if ($this->_get('show-dublin-core')) {
         $head->append_child(ax_link(array('rel' => 'schema.DC', 'href' => 'http://purl.org/dc/elements/1.1/')));
         $head->append_child(ax_meta_name_content('DC.language', $this->_get('language')));
         if (!is_null($title)) {
             $head->append_child(ax_meta_name_content('DC.title', $title));
         }
         if ($this->_isset('creator')) {
             $head->append_child(ax_meta_name_content('DC.creator', $this->_get('creator')));
         }
         if ($this->_isset('description')) {
             $head->append_child(ax_meta_name_content('DC.description', $this->_get('description')));
         }
         if ($this->_isset('date')) {
             $date = $this->get('date');
         } else {
             $date = AnewtDateTime::now();
         }
         $head->append_child(ax_meta_name_content('DC.date', AnewtDateTime::date($date)));
     }
     /* Powered by Anewt! */
     $generator = $this->_get('generator');
     if (!is_null($generator)) {
         assert('is_string($generator);');
         $head->append_child(ax_meta_name_content('generator', $generator));
     }
     /* Robots */
     $robots = $this->_get('robots');
     if (!is_null($robots)) {
         assert('is_string($robots);');
         $head->append_child(ax_meta(array('name' => 'robots', 'content' => $robots)));
     }
     /* Links (including stylesheets) and JavaScripts */
     $head->append_children($this->_links);
     $head->append_children($this->_javascripts);
     /* Favicon */
     $favicon = $this->_get('favicon');
     if (!is_null($favicon)) {
         assert('is_string($favicon);');
         $head->append_child(ax_link_favicon($favicon));
     }
     /* Body content */
     if ($this->_get('blocks')) {
         /* This is a page using div blocks */
         if ($this->_content && $this->_content->has_child_nodes()) {
             throw new AnewtException('Pages using blocks should not have content outside blocks.');
         }
         /* The buffer holding all content is either a wrapper div or an
          * invisible fragment (both XML nodes, so the API is the same). */
         if ($this->_get('use-wrapper-div')) {
             $buffer = ax_div_id(null, $this->_get('wrapper-div-id'));
         } else {
             $buffer = ax_fragment();
         }
         /* Add the content */
         foreach ($this->_get('blocks') as $block_name) {
             $block = $this->get_block($block_name);
             $buffer->append_child(ax_div_id($block, $block_name));
             unset($block);
             unset($div);
         }
         $body->append_child($buffer);
     } else {
         /* This page has no blocks, so we use the nodes in _content instead
          * (if any) */
         if ($this->_blocks) {
             throw new AnewtException('Pages not using blocks should not have content in blocks');
         }
         if ($this->_content) {
             $body->append_child($this->_content);
         }
     }
     /* Assemble the top level elements */
     $document = new AnewtXMLDomDocument();
     $document->set_document_type($this->_get('document-type'));
     $document->set_content_type($this->_get('content-type'));
     $document->set_encoding($this->_get('encoding'));
     $html = new AnewtXMLDomElement('html', array('xmlns' => 'http://www.w3.org/1999/xhtml', 'xml:lang' => $this->_get('language'), 'lang' => $this->_get('language')));
     $html->append_child($head);
     $html->append_child($body);
     $document->append_child($html);
     return $document;
 }
Esempio n. 3
0
 /**
  * \private
  *
  * Build an XML element representing this item.
  *
  * This method should not be called directly, it is only used by the
  * AnewtRssChannel class.
  *
  * \return
  *   A XML element representing this item.
  */
 public function _build_element()
 {
     $item = new AnewtXMLDomElement('item');
     /* Loop over all properties of this item */
     foreach ($this->properties as $property => $property_spec) {
         list($tagname, $status, $type) = $property_spec;
         $element = AnewtRssChannel::_build_rss_element($this, $property, $tagname, $status, $type);
         if (is_null($element)) {
             continue;
         }
         $item->append_child($element);
         unset($element);
     }
     return $item;
 }