Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ExtensionTagHandler
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 5
30
0.00% covered (danger)
0.00%
0 / 1
 sourceToDom
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 processAttributeEmbeddedDom
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 lintHandler
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 domToWikitext
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 diffHandler
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2declare( strict_types = 1 );
3
4namespace Wikimedia\Parsoid\Ext;
5
6use Wikimedia\Parsoid\DOM\DocumentFragment;
7use Wikimedia\Parsoid\DOM\Element;
8
9/**
10 * A Parsoid extension module may register handlers for one or more
11 * extension tags. The only method which is generally
12 * required by all extension tags is `sourceToDom` (but Translate
13 * doesn't even implement that).  All other methods have default do-nothing
14 * implementations; override them iff you wish to implement those
15 * features.  Default implementations consistently return `false`
16 * to indicate not-implemented (in some cases `null` would be a
17 * valid return value, and in other cases `null` would be a likely
18 * "accidental" return value which we'd like to catch and flag).
19 */
20abstract class ExtensionTagHandler {
21
22    /**
23     * Convert an extension tag's content to "prepared and loaded" DOM.
24     *
25     * @param ParsoidExtensionAPI $extApi
26     * @param string $content Extension tag content
27     * @param array $args Extension tag arguments
28     *   The extension tag arguments should be treated as opaque objects
29     *   and any necessary inspection should be handled through the API.
30     * @return DocumentFragment|false|null
31     *   `DocumentFragment` if returning some parsed content
32     *   `false` to fallback to the default handler for the content
33     *   `null` to drop the instance completely
34     * @throws ExtensionError
35     */
36    public function sourceToDom(
37        ParsoidExtensionAPI $extApi, string $content, array $args
38    ) {
39        return false; /* Use default wrapper */
40    }
41
42    /**
43     * Extensions might embed HTML in attributes in their own custom
44     * representation (whether in data-mw or elsewhere).
45     *
46     * Core Parsoid will need a way to traverse such content. This method
47     * is a way for extension tag handlers to provide this functionality.
48     * Parsoid will only call this method if the tag's config sets the
49     * options['wt2html']['embedsDomInAttributes'] property to true.
50     *
51     * @param ParsoidExtensionAPI $extApi
52     * @param Element $elt The node whose data attributes need to be examined
53     * @param callable(DocumentFragment):bool $proc
54     *        The processor that will process the embedded HTML.
55     *        This processor will be provided a DocumentFragment
56     *        and is expected to return true if that fragment was modified.
57     */
58    public function processAttributeEmbeddedDom(
59        ParsoidExtensionAPI $extApi, Element $elt, callable $proc
60    ): void {
61        // Nothing to do by default
62    }
63
64    /**
65     * Lint handler for this extension.
66     *
67     * If the extension has lints it wants to expose, it should use $extApi
68     * to register those lints. Alternatively, the extension might simply
69     * inspect its DOM and invoke the default lint handler on a DOM tree
70     * that it wants inspected. For example, <ref> nodes often only have
71     * a pointer (the id attribute) to its content, and its lint handler would
72     * look up the DOM tree and invoke the default lint handler on that tree.
73     *
74     * @param ParsoidExtensionAPI $extApi
75     * @param Element $rootNode Extension content's root node
76     * @param callable $defaultHandler Default lint handler
77     *    - Default lint handler has signature $defaultHandler( Element $elt ): void
78     * @return bool Return `false` to indicate that this
79     *   extension has no special lint handler (the default lint handler will
80     *   be used.  Return `true` to indicate linting should proceed with the
81     *   next sibling.
82     */
83    public function lintHandler(
84        ParsoidExtensionAPI $extApi, Element $rootNode, callable $defaultHandler
85    ): bool {
86        /* Use default linter */
87        return false;
88    }
89
90    /**
91     * Serialize a DOM node created by this extension to wikitext.
92     *
93     * @param ParsoidExtensionAPI $extApi
94     * @param Element $node A node in a "prepared and loaded" document.
95     * @param bool $wrapperUnmodified
96     * @return string|false Return false to use the default serialization.
97     */
98    public function domToWikitext(
99        ParsoidExtensionAPI $extApi, Element $node, bool $wrapperUnmodified
100    ) {
101        /* Use default serialization */
102        return false;
103    }
104
105    /**
106     * XXX: Experimental
107     *
108     * Call $domDiff on corresponding subtrees of $origNode and $editedNode
109     *
110     * @param ParsoidExtensionAPI $extApi
111     * @param callable $domDiff
112     * @param Element $origNode
113     * @param Element $editedNode
114     * @return bool
115     */
116    public function diffHandler(
117        ParsoidExtensionAPI $extApi, callable $domDiff, Element $origNode,
118        Element $editedNode
119    ): bool {
120        return false;
121    }
122}