4 * Represents a filter that performs processing on documents. Read-only.
6 abstract class XHTMLCompiler_DOMFilter
extends XHTMLCompiler_Filter
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.
18 * Allocated namespace for the filter, or default 'xc' one.
23 * Lookup of namespace shortcuts to full namespace names.
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() {
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);
48 * Performs common initialization of DOM and XPath
49 * @note This must be called before you can use any of the convenience
52 public function setup($dom) {
54 $this->xpath
= new DOMXPath($dom);
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;
73 * XPath object for the current DOM (private: use query() to use it)
74 * @todo Implement all member functions for this
79 * Current DOMDocument (private: use the instance passed to you via parameter)
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);
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
99 * @param $node Node to remove attribute from
100 * @param $ns Namespace of attribute
101 * @param $name Name of attribute
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)) {
109 $value = $node->getAttribute($name);
110 $node->removeAttribute($name);
113 if (isset($this->nsLookup
[$ns])) $ns = $this->nsLookup
[$ns];
114 $value = $node->getAttributeNS($ns, $name);
115 $node->removeAttributeNS($ns, $name);