Implement AbsolutePath and IEConditionalComments filters. Add an xc namespace (urn...
[xhtml-compiler.git] / XHTMLCompiler / DOMFilter.php
blob5c8777cfb3c98af4605b60d4866659e1ab1d101c
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 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
39 abstract public function process(DOMDocument $dom, $page);
41 /**
42 * Performs common initialization of DOM and XPath
44 public function setup($dom) {
45 $this->dom = $dom;
46 $this->xpath = new DOMXPath($dom);
47 // defaults
48 $ns['html'] = "http://www.w3.org/1999/xhtml";
49 $this->ns = $ns['xc'] = "urn:xhtml-compiler";
50 if (isset($this->prefix)) {
51 if ($this->prefix == 'html' || $this->prefix == 'xc') {
52 throw new Exception('Prefix for ' . get_class($this) .
53 'may not be the reserved ' . $this->prefix);
55 $this->ns = $ns[$this->prefix] = "urn:xhtml-compiler:" . $this->name;
57 foreach ($ns as $prefix => $uri) {
58 $this->xpath->registerNamespace($prefix, $uri);
62 /**
63 * XPath object for the current DOM
65 private $xpath;
67 /**
68 * Current DOMDocument
70 private $dom;
72 /**
73 * Querys a DOM with an XPath expression
74 * @param $expr XPath expression to evaluate
75 * @param $context Context node
77 protected function query($expr, $context = false) {
78 if (!$this->dom) throw new Exception('Filter must be setup before using convenience functions');
79 if (!$context) return $this->xpath->query($expr);
80 return $this->xpath->query($expr, $context);
83 /**
84 * Retrieves a namespaced attribute from an element, and then deletes it.
85 * @note This is best for proprietary attributes that, once you grab
86 * their data, they should be removed from the DOM for validation's
87 * sake.
88 * @param $node Node to remove attribute from
89 * @param $ns Namespace of attribute
90 * @param $name Name of attribute
92 protected function confiscateAttr($node, $ns, $name) {
93 $value = $node->getAttributeNS($ns, $name);
94 $node->removeAttributeNS($ns, $name);
95 return $value;