source: sandbox/filemanager/tp/dompdf/include/frame_tree.cls.php @ 1575

Revision 1575, 6.1 KB checked in by amuller, 14 years ago (diff)

Ticket #597 - Implentação, melhorias do modulo gerenciador de arquivos

Line 
1<?php
2/**
3 * DOMPDF - PHP5 HTML to PDF renderer
4 *
5 * File: $RCSfile: frame_tree.cls.php,v $
6 * Created on: 2004-06-02
7 *
8 * Copyright (c) 2004 - Benj Carson <benjcarson@digitaljunkies.ca>
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this library in the file LICENSE.LGPL; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23 * 02111-1307 USA
24 *
25 * Alternatively, you may distribute this software under the terms of the
26 * PHP License, version 3.0 or later.  A copy of this license should have
27 * been distributed with this file in the file LICENSE.PHP .  If this is not
28 * the case, you can obtain a copy at http://www.php.net/license/3_0.txt.
29 *
30 * The latest version of DOMPDF might be available at:
31 * http://www.digitaljunkies.ca/dompdf
32 *
33 * @link http://www.digitaljunkies.ca/dompdf
34 * @copyright 2004 Benj Carson
35 * @author Benj Carson <benjcarson@digitaljunkies.ca>
36 * @package dompdf
37 * @version 0.5.1
38 */
39
40/* $Id: frame_tree.cls.php,v 1.10 2006/07/07 21:31:03 benjcarson Exp $ */
41
42/**
43 * Represents an entire document as a tree of frames
44 *
45 * The Frame_Tree consists of {@link Frame} objects each tied to specific
46 * DomNode objects in a specific DomDocument.  The Frame_Tree has the same
47 * structure as the DomDocument, but adds additional capabalities for
48 * styling and layout.
49 *
50 * @package dompdf
51 * @access protected
52 */
53class Frame_Tree {
54   
55  /**
56   * Tags to ignore while parsing the tree
57   *
58   * @var array
59   */
60  static protected $_HIDDEN_TAGS = array("area", "base", "basefont", "head", "style",
61                                         "meta", "title", "colgroup",
62                                         "noembed", "noscript", "param", "#comment"); 
63  /**
64   * The main DomDocument
65   *
66   * @see http://ca2.php.net/manual/en/ref.dom.php
67   * @var DomDocument
68   */
69  protected $_dom;
70
71  /**
72   * The root node of the FrameTree.
73   *
74   * @var Frame
75   */
76  protected $_root;
77
78  /**
79   * A mapping of {@link Frame} objects to DomNode objects
80   *
81   * @var array
82   */
83  protected $_registry;
84 
85
86  /**
87   * Class constructor
88   *
89   * @param DomDocument $dom the main DomDocument object representing the current html document
90   */
91  function __construct(DomDocument $dom) {
92    $this->_dom = $dom;
93    $this->_root = null;
94    $this->_registry = array();
95  }
96
97  /**
98   * Returns the DomDocument object representing the curent html document
99   *
100   * @return DomDocument
101   */
102  function get_dom() { return $this->_dom; }
103
104  /**
105   * Returns the root frame of the tree
106   *
107   * @return Frame
108   */
109  function get_root() { return $this->_root; }
110
111  /**
112   * Returns a specific frame given its id
113   *
114   * @param string $id
115   * @return Frame
116   */
117  function get_frame($id) { return isset($this->_registry[$id]) ? $this->_registry[$id] : null; }
118
119  /**
120   * Returns a post-order iterator for all frames in the tree
121   *
122   * @return FrameTreeList
123   */
124  function get_frames() { return new FrameTreeList($this->_root); }
125     
126  /**
127   * Builds the tree
128   */
129  function build_tree() {
130    $html = $this->_dom->getElementsByTagName("html")->item(0);
131    if ( is_null($html) )
132      $html = $this->_dom->firstChild;
133
134    if ( is_null($html) )
135      throw new DOMPDF_Exception("Requested HTML document contains no data.");
136
137    $this->_root = $this->_build_tree_r($html);
138
139  }
140
141  /**
142   * Recursively adds {@link Frame} objects to the tree
143   *
144   * Recursively build a tree of Frame objects based on a dom tree.
145   * No layout information is calculated at this time, although the
146   * tree may be adjusted (i.e. nodes and frames for generated content
147   * and images may be created).
148   *
149   * @param DomNode $node the current DomNode being considered
150   * @return Frame
151   */
152  protected function _build_tree_r(DomNode $node) {
153   
154    $frame = new Frame($node);
155    $id = $frame->get_id();
156    $this->_registry[ $id ] = $frame;
157   
158    if ( !$node->hasChildNodes() )
159      return $frame;
160
161    // Fixes 'cannot access undefined property for object with
162    // overloaded access', fix by Stefan radulian
163    // <stefan.radulian@symbion.at>   
164    //foreach ($node->childNodes as $child) {
165
166    // Store the children in an array so that the tree can be modified
167    $children = array();
168    for ($i = 0; $i < $node->childNodes->length; $i++)
169      $children[] = $node->childNodes->item($i);
170
171    foreach ($children as $child) {
172      // Skip non-displaying nodes
173      if ( in_array( mb_strtolower($child->nodeName), self::$_HIDDEN_TAGS) )  {
174        if ( mb_strtolower($child->nodeName) != "head" &&
175             mb_strtolower($child->nodeName) != "style" )
176          $child->parentNode->removeChild($child);
177        continue;
178      }
179
180      // Skip empty text nodes
181      if ( $child->nodeName == "#text" && $child->nodeValue == "" ) {
182        $child->parentNode->removeChild($child);
183        continue;
184      }
185     
186      // Add a container frame for images
187      if ( $child->nodeName == "img" ) {
188        $img_node = $child->ownerDocument->createElement("img_inner");
189     
190        // Move attributes to inner node       
191        foreach ( $child->attributes as $attr => $attr_node ) {
192          // Skip style, but move all other attributes
193          if ( $attr == "style" )
194            continue;
195       
196          $img_node->setAttribute($attr, $attr_node->value);
197        }
198
199        foreach ( $child->attributes as $attr => $node ) {
200          if ( $attr == "style" )
201            continue;
202          $child->removeAttribute($attr);
203        }
204
205        $child->appendChild($img_node);
206      }
207   
208      $frame->append_child($this->_build_tree_r($child), false);
209
210    }
211   
212    return $frame;
213  }
214}
215
216?>
Note: See TracBrowser for help on using the repository browser.