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 * List of attributes the extension adds to the generic xc namespace,
24 * helps prevent naming conflicts.
26 protected $xcAttr = array();
27 public function getXCAttributesDefined() {
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);
43 * Performs common initialization of DOM and XPath
44 * @note This must be called before you can use any of the convenience
47 public function setup($dom) {
49 $this->xpath
= new DOMXPath($dom);
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);
66 * XPath object for the current DOM (private: use query() to use it)
67 * @todo Implement all member functions for this
72 * Current DOMDocument (private: use the instance passed to you via parameter)
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);
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
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);