Migrate index.xhtml and comparison.xhtml to use centralized entities for current...
[xhtml-compiler.git] / XHTMLCompiler / DOMFilter.php
blob98e947a7a4a2b18cf7dc82598d5809a65a44c0c1
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
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
45 public function setup($dom) {
46 $this->dom = $dom;
47 $this->xpath = new DOMXPath($dom);
48 // defaults
49 $ns['html'] = "http://www.w3.org/1999/xhtml";
50 $this->ns = $ns['xc'] = "urn:xhtml-compiler";
51 if (isset($this->prefix)) {
52 if ($this->prefix == 'html' || $this->prefix == 'xc') {
53 throw new Exception('Prefix for ' . get_class($this) .
54 'may not be the reserved ' . $this->prefix);
56 $this->ns = $ns[$this->prefix] = "urn:xhtml-compiler:" . $this->name;
58 foreach ($ns as $prefix => $uri) {
59 $this->xpath->registerNamespace($prefix, $uri);
63 /**
64 * XPath object for the current DOM
66 private $xpath;
68 /**
69 * Current DOMDocument
71 private $dom;
73 /**
74 * Querys a DOM with an XPath expression
75 * @param $expr XPath expression to evaluate
76 * @param $context Context node
78 protected function query($expr, $context = false) {
79 if (!$this->dom) throw new Exception('Filter must be setup before using convenience functions');
80 if (!$context) return $this->xpath->query($expr);
81 return $this->xpath->query($expr, $context);
84 /**
85 * Retrieves a namespaced attribute from an element, and then deletes it.
86 * @note This is best for proprietary attributes that, once you grab
87 * their data, they should be removed from the DOM for validation's
88 * sake.
89 * @param $node Node to remove attribute from
90 * @param $ns Namespace of attribute
91 * @param $name Name of attribute
93 protected function confiscateAttr($node, $ns, $name) {
94 $value = $node->getAttributeNS($ns, $name);
95 $node->removeAttributeNS($ns, $name);
96 return $value;