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 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
45 public function setup($dom) {
47 $this->xpath
= new DOMXPath($dom);
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);
64 * XPath object for the current DOM
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);
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
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);