Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
ExtensionTagHandler
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 6
42
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
 processAttributeEmbeddedHTML
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 Closure;
7use Wikimedia\Parsoid\DOM\DocumentFragment;
8use Wikimedia\Parsoid\DOM\Element;
9
10/**
11 * A Parsoid extension module may register handlers for one or more
12 * extension tags. The only method which is generally
13 * required by all extension tags is `sourceToDom` (but Translate
14 * doesn't even implement that).  All other methods have default do-nothing
15 * implementations; override them iff you wish to implement those
16 * features.  Default implementations consistently return `false`
17 * to indicate not-implemented (in some cases `null` would be a
18 * valid return value, and in other cases `null` would be a likely
19 * "accidental" return value which we'd like to catch and flag).
20 */
21abstract class ExtensionTagHandler {
22
23    /**
24     * Convert an extension tag's content to "prepared and loaded" DOM.
25     * @param ParsoidExtensionAPI $extApi
26     * @param string $src Extension tag content
27     * @param array $extArgs 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     */
35    public function sourceToDom(
36        ParsoidExtensionAPI $extApi, string $src, array $extArgs
37    ) {
38        return false; /* Use default wrapper */
39    }
40
41    /**
42     * Extensions might embed HTML in attributes in their own custom
43     * representation (whether in data-mw or elsewhere).
44     *
45     * Core Parsoid will need a way to traverse such content. This method
46     * is a way for extension tag handlers to provide this functionality.
47     * Parsoid will only call this method if the tag's config sets the
48     * options['wt2html']['embedsHTMLInAttributes'] property to true.
49     *
50     * Override this method if your element embeds content as an HTML string;
51     * however it is recommended to embed content directly as a
52     * DocumentFragment and override ::processAttributeEmbeddedDom() instead.
53     *
54     * @param ParsoidExtensionAPI $extApi
55     * @param Element $elt The node whose data attributes need to be examined
56     * @param Closure $proc The processor that will process the embedded HTML
57     *        Signature: (string) -> string
58     *        This processor will be provided the HTML string as input
59     *        and is expected to return a possibly modified string.
60     */
61    public function processAttributeEmbeddedHTML(
62        ParsoidExtensionAPI $extApi, Element $elt, Closure $proc
63    ): void {
64        // Nothing to do by default
65    }
66
67    /**
68     * Extensions might embed HTML in attributes in their own custom
69     * representation (whether in data-mw or elsewhere).
70     *
71     * Core Parsoid will need a way to traverse such content. This method
72     * is a way for extension tag handlers to provide this functionality.
73     * Parsoid will only call this method if the tag's config sets the
74     * options['wt2html']['embedsDomInAttributes'] property to true.
75     *
76     * Override this method if your element embeds
77     * content directly as a DocumentFragment, which is recommended.
78     * If your element embeds content as a serialized HTML string,
79     * then ::processAttributeEmbeddedHTML() may be more convenient.
80     *
81     * @param ParsoidExtensionAPI $extApi
82     * @param Element $elt The node whose data attributes need to be examined
83     * @param callable(DocumentFragment):bool $proc
84     *        The processor that will process the embedded HTML.
85     *        This processor will be provided a DocumentFragment
86     *        and is expected to return true if that fragment was modified.
87     */
88    public function processAttributeEmbeddedDom(
89        ParsoidExtensionAPI $extApi, Element $elt, callable $proc
90    ): void {
91        // Nothing to do by default
92    }
93
94    /**
95     * Lint handler for this extension.
96     *
97     * If the extension has lints it wants to expose, it should use $extApi
98     * to register those lints. Alternatively, the extension might simply
99     * inspect its DOM and invoke the default lint handler on a DOM tree
100     * that it wants inspected. For example, <ref> nodes often only have
101     * a pointer (the id attribute) to its content, and its lint handler would
102     * look up the DOM tree and invoke the default lint handler on that tree.
103     *
104     * @param ParsoidExtensionAPI $extApi
105     * @param Element $rootNode Extension content's root node
106     * @param callable $defaultHandler Default lint handler
107     *    - Default lint handler has signature $defaultHandler( Element $elt ): void
108     * @return bool Return `false` to indicate that this
109     *   extension has no special lint handler (the default lint handler will
110     *   be used.  Return `true` to indicate linting should proceed with the
111     *   next sibling.
112     */
113    public function lintHandler(
114        ParsoidExtensionAPI $extApi, Element $rootNode, callable $defaultHandler
115    ) {
116        /* Use default linter */
117        return false;
118    }
119
120    /**
121     * Serialize a DOM node created by this extension to wikitext.
122     * @param ParsoidExtensionAPI $extApi
123     * @param Element $node A node in a "prepared and loaded" document.
124     * @param bool $wrapperUnmodified
125     * @return string|false Return false to use the default serialization.
126     */
127    public function domToWikitext(
128        ParsoidExtensionAPI $extApi, Element $node, bool $wrapperUnmodified
129    ) {
130        /* Use default serialization */
131        return false;
132    }
133
134    /**
135     * XXX: Experimental
136     *
137     * Call $domDiff on corresponding substrees of $origNode and $editedNode
138     *
139     * @param ParsoidExtensionAPI $extApi
140     * @param callable $domDiff
141     * @param Element $origNode
142     * @param Element $editedNode
143     * @return bool
144     */
145    public function diffHandler(
146        ParsoidExtensionAPI $extApi, callable $domDiff, Element $origNode,
147        Element $editedNode
148    ): bool {
149        return false;
150    }
151}