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
 processAttributeEmbeddedHTML
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 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     * @param ParsoidExtensionAPI $extApi
51     * @param Element $elt The node whose data attributes need to be examined
52     * @param Closure $proc The processor that will process the embedded HTML
53     *        Signature: (string) -> string
54     *        This processor will be provided the HTML string as input
55     *        and is expected to return a possibly modified string.
56     */
57    public function processAttributeEmbeddedHTML(
58        ParsoidExtensionAPI $extApi, Element $elt, Closure $proc
59    ): void {
60        // Nothing to do by default
61    }
62
63    /**
64     * Lint handler for this extension.
65     *
66     * If the extension has lints it wants to expose, it should use $extApi
67     * to register those lints. Alternatively, the extension might simply
68     * inspect its DOM and invoke the default lint handler on a DOM tree
69     * that it wants inspected. For example, <ref> nodes often only have
70     * a pointer (the id attribute) to its content, and its lint handler would
71     * look up the DOM tree and invoke the default lint handler on that tree.
72     *
73     * @param ParsoidExtensionAPI $extApi
74     * @param Element $rootNode Extension content's root node
75     * @param callable $defaultHandler Default lint handler
76     *    - Default lint handler has signature $defaultHandler( Element $elt ): void
77     * @return bool Return `false` to indicate that this
78     *   extension has no special lint handler (the default lint handler will
79     *   be used.  Return `true` to indicate linting should proceed with the
80     *   next sibling.
81     */
82    public function lintHandler(
83        ParsoidExtensionAPI $extApi, Element $rootNode, callable $defaultHandler
84    ) {
85        /* Use default linter */
86        return false;
87    }
88
89    /**
90     * Serialize a DOM node created by this extension to wikitext.
91     * @param ParsoidExtensionAPI $extApi
92     * @param Element $node
93     * @param bool $wrapperUnmodified
94     * @return string|false Return false to use the default serialization.
95     */
96    public function domToWikitext(
97        ParsoidExtensionAPI $extApi, Element $node, bool $wrapperUnmodified
98    ) {
99        /* Use default serialization */
100        return false;
101    }
102
103    /**
104     * XXX: Experimental
105     *
106     * Call $domDiff on corresponding substrees of $origNode and $editedNode
107     *
108     * @param ParsoidExtensionAPI $extApi
109     * @param callable $domDiff
110     * @param Element $origNode
111     * @param Element $editedNode
112     * @return bool
113     */
114    public function diffHandler(
115        ParsoidExtensionAPI $extApi, callable $domDiff, Element $origNode,
116        Element $editedNode
117    ): bool {
118        return false;
119    }
120}