Skip to content

Commit 68ee8cf

Browse files
committed
Move figure child def to HTML5/ subfolder
1 parent 42e1b20 commit 68ee8cf

File tree

3 files changed

+89
-81
lines changed

3 files changed

+89
-81
lines changed
Lines changed: 6 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,8 @@
11
<?php
22

3-
class HTMLPurifier_ChildDef_Figure extends HTMLPurifier_ChildDef
4-
{
5-
public $type = 'figure';
6-
7-
public $elements = array(
8-
'figcaption' => true,
9-
);
10-
11-
protected $allowedElements;
12-
13-
/**
14-
* @param HTMLPurifier_Config $config
15-
* @return array
16-
*/
17-
public function getAllowedElements($config)
18-
{
19-
if (null === $this->allowedElements) {
20-
// Add Flow content to allowed elements to prevent MakeWellFormed
21-
// strategy moving them outside 'figure' element
22-
$def = $config->getHTMLDefinition();
23-
24-
$this->allowedElements = array_merge(
25-
$def->info_content_sets['Flow'],
26-
$this->elements
27-
);
28-
}
29-
return $this->allowedElements;
30-
}
31-
32-
/**
33-
* @param array $children
34-
* @param HTMLPurifier_Config $config
35-
* @param HTMLPurifier_Context $context
36-
* @return array|bool
37-
*/
38-
public function validateChildren($children, $config, $context)
39-
{
40-
$hasFigcaption = false;
41-
$figcaptionPos = -1;
42-
43-
$result = array();
44-
45-
// Content model:
46-
// Either: one figcaption element followed by flow content.
47-
// Or: flow content followed by one figcaption element.
48-
// Or: flow content.
49-
50-
// Scan through children, accept at most one figcaption.
51-
foreach ($children as $node) {
52-
if ($node->name === 'figcaption') {
53-
if (!$hasFigcaption) {
54-
$hasFigcaption = true;
55-
$figcaptionPos = count($result);
56-
$result[] = $node;
57-
}
58-
continue;
59-
}
60-
61-
// Figcaption must be a first or last child of a figure element.
62-
// If it's not first, then we ignore all siblings that come after.
63-
if ($hasFigcaption && $figcaptionPos > 0) {
64-
break;
65-
}
66-
67-
$result[] = $node;
68-
}
69-
70-
$whitespaceOnly = true;
71-
foreach ($result as $node) {
72-
$whitespaceOnly = $whitespaceOnly && !empty($node->is_whitespace);
73-
}
74-
75-
// remove parent node if there are no children or all children are whitespace-only
76-
if (empty($result) || $whitespaceOnly) {
77-
return false;
78-
}
79-
80-
return $result;
81-
}
82-
}
3+
/**
4+
* @deprecated Use {@link HTMLPurifier_ChildDef_HTML5_Figure} class
5+
* @codeCoverageIgnore
6+
*/
7+
class HTMLPurifier_ChildDef_Figure extends HTMLPurifier_ChildDef_HTML5_Figure
8+
{}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
class HTMLPurifier_ChildDef_HTML5_Figure extends HTMLPurifier_ChildDef
4+
{
5+
public $type = 'figure';
6+
7+
public $elements = array(
8+
'figcaption' => true,
9+
);
10+
11+
protected $allowedElements;
12+
13+
/**
14+
* @param HTMLPurifier_Config $config
15+
* @return array
16+
*/
17+
public function getAllowedElements($config)
18+
{
19+
if (null === $this->allowedElements) {
20+
// Add Flow content to allowed elements to prevent MakeWellFormed
21+
// strategy moving them outside 'figure' element
22+
$def = $config->getHTMLDefinition();
23+
24+
$this->allowedElements = array_merge(
25+
$def->info_content_sets['Flow'],
26+
$this->elements
27+
);
28+
}
29+
return $this->allowedElements;
30+
}
31+
32+
/**
33+
* @param array $children
34+
* @param HTMLPurifier_Config $config
35+
* @param HTMLPurifier_Context $context
36+
* @return array|bool
37+
*/
38+
public function validateChildren($children, $config, $context)
39+
{
40+
$hasFigcaption = false;
41+
$figcaptionPos = -1;
42+
43+
$result = array();
44+
45+
// Content model:
46+
// Either: one figcaption element followed by flow content.
47+
// Or: flow content followed by one figcaption element.
48+
// Or: flow content.
49+
50+
// Scan through children, accept at most one figcaption.
51+
foreach ($children as $node) {
52+
if ($node->name === 'figcaption') {
53+
if (!$hasFigcaption) {
54+
$hasFigcaption = true;
55+
$figcaptionPos = count($result);
56+
$result[] = $node;
57+
}
58+
continue;
59+
}
60+
61+
// Figcaption must be a first or last child of a figure element.
62+
// If it's not first, then we ignore all siblings that come after.
63+
if ($hasFigcaption && $figcaptionPos > 0) {
64+
break;
65+
}
66+
67+
$result[] = $node;
68+
}
69+
70+
$whitespaceOnly = true;
71+
foreach ($result as $node) {
72+
$whitespaceOnly = $whitespaceOnly && !empty($node->is_whitespace);
73+
}
74+
75+
// remove parent node if there are no children or all children are whitespace-only
76+
if (empty($result) || $whitespaceOnly) {
77+
return false;
78+
}
79+
80+
return $result;
81+
}
82+
}

library/HTMLPurifier/HTMLModule/HTML5/Text.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function setup($config)
3232
$this->addElement('hgroup', 'Block', 'Required: h1 | h2 | h3 | h4 | h5 | h6', 'Common');
3333

3434
// https://html.spec.whatwg.org/dev/grouping-content.html#the-figure-element
35-
$this->addElement('figure', 'Block', new HTMLPurifier_ChildDef_Figure(), 'Common');
35+
$this->addElement('figure', 'Block', new HTMLPurifier_ChildDef_HTML5_Figure(), 'Common');
3636
$this->addElement('figcaption', false, 'Flow', 'Common');
3737

3838
// http://developers.whatwg.org/text-level-semantics.html

0 commit comments

Comments
 (0)