a03c95f5c8ca64f2a5635beb71525f6253b73d22
[yaffs-website] / web / core / modules / filter / src / Plugin / Filter / FilterCaption.php
1 <?php
2
3 namespace Drupal\filter\Plugin\Filter;
4
5 use Drupal\Component\Utility\Html;
6 use Drupal\Component\Utility\Xss;
7 use Drupal\filter\FilterProcessResult;
8 use Drupal\filter\Plugin\FilterBase;
9 use Drupal\filter\Render\FilteredMarkup;
10
11 /**
12  * Provides a filter to caption elements.
13  *
14  * When used in combination with the filter_align filter, this must run last.
15  *
16  * @Filter(
17  *   id = "filter_caption",
18  *   title = @Translation("Caption images"),
19  *   description = @Translation("Uses a <code>data-caption</code> attribute on <code>&lt;img&gt;</code> tags to caption images."),
20  *   type = Drupal\filter\Plugin\FilterInterface::TYPE_TRANSFORM_REVERSIBLE
21  * )
22  */
23 class FilterCaption extends FilterBase {
24
25   /**
26    * {@inheritdoc}
27    */
28   public function process($text, $langcode) {
29     $result = new FilterProcessResult($text);
30
31     if (stristr($text, 'data-caption') !== FALSE) {
32       $dom = Html::load($text);
33       $xpath = new \DOMXPath($dom);
34       foreach ($xpath->query('//*[@data-caption]') as $node) {
35         // Read the data-caption attribute's value, then delete it.
36         $caption = Html::escape($node->getAttribute('data-caption'));
37         $node->removeAttribute('data-caption');
38
39         // Sanitize caption: decode HTML encoding, limit allowed HTML tags; only
40         // allow inline tags that are allowed by default, plus <br>.
41         $caption = Html::decodeEntities($caption);
42         $caption = FilteredMarkup::create(Xss::filter($caption, ['a', 'em', 'strong', 'cite', 'code', 'br']));
43
44         // The caption must be non-empty.
45         if (mb_strlen($caption) === 0) {
46           continue;
47         }
48
49         // Given the updated node and caption: re-render it with a caption, but
50         // bubble up the value of the class attribute of the captioned element,
51         // this allows it to collaborate with e.g. the filter_align filter.
52         $tag = $node->tagName;
53         $classes = $node->getAttribute('class');
54         $node->removeAttribute('class');
55         $node = ($node->parentNode->tagName === 'a') ? $node->parentNode : $node;
56         $filter_caption = [
57           '#theme' => 'filter_caption',
58           // We pass the unsanitized string because this is a text format
59           // filter, and after filtering, we always assume the output is safe.
60           // @see \Drupal\filter\Element\ProcessedText::preRenderText()
61           '#node' => FilteredMarkup::create($node->C14N()),
62           '#tag' => $tag,
63           '#caption' => $caption,
64           '#classes' => $classes,
65         ];
66         $altered_html = \Drupal::service('renderer')->render($filter_caption);
67
68         // Load the altered HTML into a new DOMDocument and retrieve the element.
69         $updated_nodes = Html::load($altered_html)->getElementsByTagName('body')
70           ->item(0)
71           ->childNodes;
72
73         foreach ($updated_nodes as $updated_node) {
74           // Import the updated node from the new DOMDocument into the original
75           // one, importing also the child nodes of the updated node.
76           $updated_node = $dom->importNode($updated_node, TRUE);
77           $node->parentNode->insertBefore($updated_node, $node);
78         }
79         // Finally, remove the original data-caption node.
80         $node->parentNode->removeChild($node);
81       }
82
83       $result->setProcessedText(Html::serialize($dom))
84         ->addAttachments([
85           'library' => [
86             'filter/caption',
87           ],
88         ]);
89     }
90
91     return $result;
92   }
93
94   /**
95    * {@inheritdoc}
96    */
97   public function tips($long = FALSE) {
98     if ($long) {
99       return $this->t('
100         <p>You can caption images, videos, blockquotes, and so on. Examples:</p>
101         <ul>
102             <li><code>&lt;img src="" data-caption="This is a caption" /&gt;</code></li>
103             <li><code>&lt;video src="" data-caption="The Drupal Dance" /&gt;</code></li>
104             <li><code>&lt;blockquote data-caption="Dries Buytaert"&gt;Drupal is awesome!&lt;/blockquote&gt;</code></li>
105             <li><code>&lt;code data-caption="Hello world in JavaScript."&gt;alert("Hello world!");&lt;/code&gt;</code></li>
106         </ul>');
107     }
108     else {
109       return $this->t('You can caption images (<code>data-caption="Text"</code>), but also videos, blockquotes, and so on.');
110     }
111   }
112
113 }