Improve documentation.
[xhtml-compiler.git] / XHTMLCompiler / DOMFilter.php
blobc0424e1d633759676aeec30b90a42faa73326cdc
1 <?php
3 /**
4 * Represents a filter that performs processing on documents. Read-only.
5 */
6 abstract class XHTMLCompiler_DOMFilter extends XHTMLCompiler_Filter
9 /**
10 * If set, DOMFilter will allocate a namespace for the filter
11 * and assign it to this prefix.
12 * @warning May not be 'html' or 'xc', because these are already
13 * allocated for the core namespaces.
15 protected $prefix;
17 /**
18 * Allocated namespace for the filter, or default 'xc' one.
20 protected $ns;
22 /**
23 * List of attributes the extension adds to the generic xc namespace,
24 * helps prevent naming conflicts.
26 protected $xcAttr = array();
27 public function getXCAttributesDefined() {
28 return $this->xcAttr;
32 /**
33 * Defines a filter that processes a DOMDocument.
34 * @note Return is not used as objects are passed "by reference", and
35 * thus edits we make will be globally reflected.
36 * @param $dom DOMDocument to process
37 * @param $page XHTMLCompiler_Page object representing the context
38 * @param $manager Currently running XHTMLCompiler_FilterManager
40 abstract public function process(DOMDocument $dom, $page, $manager);
42 /**
43 * Performs common initialization of DOM and XPath
44 * @note This must be called before you can use any of the convenience
45 * functions.
47 public function setup($dom) {
48 $this->dom = $dom;
49 $this->xpath = new DOMXPath($dom);
50 // defaults
51 $ns['html'] = "http://www.w3.org/1999/xhtml";
52 $this->ns = $ns['xc'] = "urn:xhtml-compiler";
53 if (isset($this->prefix)) {
54 if ($this->prefix == 'html' || $this->prefix == 'xc') {
55 throw new Exception('Prefix for ' . get_class($this) .
56 'may not be the reserved ' . $this->prefix);
58 $this->ns = $ns[$this->prefix] = "urn:xhtml-compiler:" . $this->name;
60 foreach ($ns as $prefix => $uri) {
61 $this->xpath->registerNamespace($prefix, $uri);
65 /**
66 * XPath object for the current DOM (private: use query() to use it)
67 * @todo Implement all member functions for this
69 private $xpath;
71 /**
72 * Current DOMDocument (private: use the instance passed to you via parameter)
74 private $dom;
76 /**
77 * Querys a DOM with an XPath expression
78 * @param $expr XPath expression to evaluate
79 * @param $context Context node
81 protected function query($expr, $context = false) {
82 if (!$this->dom) throw new Exception('Filter must be setup before using convenience functions');
83 if (!$context) return $this->xpath->query($expr);
84 return $this->xpath->query($expr, $context);
87 /**
88 * Retrieves a namespaced attribute from an element, and then deletes it.
89 * @note This is best for proprietary attributes that, once you grab
90 * their data, they should be removed from the DOM for validation's
91 * sake.
92 * @param $node Node to remove attribute from
93 * @param $ns Namespace of attribute
94 * @param $name Name of attribute
96 protected function confiscateAttr($node, $ns, $name) {
97 $value = $node->getAttributeNS($ns, $name);
98 $node->removeAttributeNS($ns, $name);
99 return $value;