Remove trailing whitespace.
[xhtml-compiler.git] / XHTMLCompiler / DOMFilter.php
blob8621004b95fcaa166225c1a2a0ebe4fda0766837
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 * Lookup of namespace shortcuts to full namespace names.
25 protected $nsLookup;
27 /**
28 * List of attributes the extension adds to the generic xc namespace,
29 * helps prevent naming conflicts.
31 protected $xcAttr = array();
32 public function getXCAttributesDefined() {
33 return $this->xcAttr;
37 /**
38 * Defines a filter that processes a DOMDocument.
39 * @note Return is not used as objects are passed "by reference", and
40 * thus edits we make will be globally reflected.
41 * @param $dom DOMDocument to process
42 * @param $page XHTMLCompiler_Page object representing the context
43 * @param $manager Currently running XHTMLCompiler_FilterManager
45 abstract public function process(DOMDocument $dom, $page, $manager);
47 /**
48 * Performs common initialization of DOM and XPath
49 * @note This must be called before you can use any of the convenience
50 * functions.
52 public function setup($dom) {
53 $this->dom = $dom;
54 $this->xpath = new DOMXPath($dom);
55 // defaults
56 $ns['html'] = "http://www.w3.org/1999/xhtml";
57 $ns['xml'] = "http://www.w3.org/XML/1998/namespace";
58 $this->ns = $ns['xc'] = "urn:xhtml-compiler";
59 if (isset($this->prefix)) {
60 if ($this->prefix == 'html' || $this->prefix == 'xc') {
61 throw new Exception('Prefix for ' . get_class($this) .
62 'may not be the reserved ' . $this->prefix);
64 $this->ns = $ns[$this->prefix] = "urn:xhtml-compiler:" . $this->name;
66 foreach ($ns as $prefix => $uri) {
67 $this->xpath->registerNamespace($prefix, $uri);
69 $this->nsLookup = $ns;
72 /**
73 * XPath object for the current DOM (private: use query() to use it)
74 * @todo Implement all member functions for this
76 private $xpath;
78 /**
79 * Current DOMDocument (private: use the instance passed to you via parameter)
81 private $dom;
83 /**
84 * Querys a DOM with an XPath expression
85 * @param $expr XPath expression to evaluate
86 * @param $context Context node
88 protected function query($expr, $context = false) {
89 if (!$this->dom) throw new Exception('Filter must be setup before using convenience functions');
90 if (!$context) return $this->xpath->query($expr);
91 return $this->xpath->query($expr, $context);
94 /**
95 * Retrieves a namespaced attribute from an element, and then deletes it.
96 * @note This is best for proprietary attributes that, once you grab
97 * their data, they should be removed from the DOM for validation's
98 * sake.
99 * @param $node Node to remove attribute from
100 * @param $ns Namespace of attribute
101 * @param $name Name of attribute
102 * or
103 * @param $node Node to remove attribute from
104 * @param $name Name of attribute
106 protected function confiscateAttr($node, $ns_or_name, $name = null) {
107 if (is_null($name)) {
108 $name = $ns_or_name;
109 $value = $node->getAttribute($name);
110 $node->removeAttribute($name);
111 } else {
112 $ns = $ns_or_name;
113 if (isset($this->nsLookup[$ns])) $ns = $this->nsLookup[$ns];
114 $value = $node->getAttributeNS($ns, $name);
115 $node->removeAttributeNS($ns, $name);
117 return $value;