Skip to content

Commit

Permalink
Move figure child def to HTML5/ subfolder
Browse files Browse the repository at this point in the history
  • Loading branch information
xemlock committed Apr 15, 2019
1 parent 42e1b20 commit 68ee8cf
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 81 deletions.
86 changes: 6 additions & 80 deletions library/HTMLPurifier/ChildDef/Figure.php
Original file line number Diff line number Diff line change
@@ -1,82 +1,8 @@
<?php

class HTMLPurifier_ChildDef_Figure extends HTMLPurifier_ChildDef
{
public $type = 'figure';

public $elements = array(
'figcaption' => true,
);

protected $allowedElements;

/**
* @param HTMLPurifier_Config $config
* @return array
*/
public function getAllowedElements($config)
{
if (null === $this->allowedElements) {
// Add Flow content to allowed elements to prevent MakeWellFormed
// strategy moving them outside 'figure' element
$def = $config->getHTMLDefinition();

$this->allowedElements = array_merge(
$def->info_content_sets['Flow'],
$this->elements
);
}
return $this->allowedElements;
}

/**
* @param array $children
* @param HTMLPurifier_Config $config
* @param HTMLPurifier_Context $context
* @return array|bool
*/
public function validateChildren($children, $config, $context)
{
$hasFigcaption = false;
$figcaptionPos = -1;

$result = array();

// Content model:
// Either: one figcaption element followed by flow content.
// Or: flow content followed by one figcaption element.
// Or: flow content.

// Scan through children, accept at most one figcaption.
foreach ($children as $node) {
if ($node->name === 'figcaption') {
if (!$hasFigcaption) {
$hasFigcaption = true;
$figcaptionPos = count($result);
$result[] = $node;
}
continue;
}

// Figcaption must be a first or last child of a figure element.
// If it's not first, then we ignore all siblings that come after.
if ($hasFigcaption && $figcaptionPos > 0) {
break;
}

$result[] = $node;
}

$whitespaceOnly = true;
foreach ($result as $node) {
$whitespaceOnly = $whitespaceOnly && !empty($node->is_whitespace);
}

// remove parent node if there are no children or all children are whitespace-only
if (empty($result) || $whitespaceOnly) {
return false;
}

return $result;
}
}
/**
* @deprecated Use {@link HTMLPurifier_ChildDef_HTML5_Figure} class
* @codeCoverageIgnore
*/
class HTMLPurifier_ChildDef_Figure extends HTMLPurifier_ChildDef_HTML5_Figure
{}
82 changes: 82 additions & 0 deletions library/HTMLPurifier/ChildDef/HTML5/Figure.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

class HTMLPurifier_ChildDef_HTML5_Figure extends HTMLPurifier_ChildDef
{
public $type = 'figure';

public $elements = array(
'figcaption' => true,
);

protected $allowedElements;

/**
* @param HTMLPurifier_Config $config
* @return array
*/
public function getAllowedElements($config)
{
if (null === $this->allowedElements) {
// Add Flow content to allowed elements to prevent MakeWellFormed
// strategy moving them outside 'figure' element
$def = $config->getHTMLDefinition();

$this->allowedElements = array_merge(
$def->info_content_sets['Flow'],
$this->elements
);
}
return $this->allowedElements;
}

/**
* @param array $children
* @param HTMLPurifier_Config $config
* @param HTMLPurifier_Context $context
* @return array|bool
*/
public function validateChildren($children, $config, $context)
{
$hasFigcaption = false;
$figcaptionPos = -1;

$result = array();

// Content model:
// Either: one figcaption element followed by flow content.
// Or: flow content followed by one figcaption element.
// Or: flow content.

// Scan through children, accept at most one figcaption.
foreach ($children as $node) {
if ($node->name === 'figcaption') {
if (!$hasFigcaption) {
$hasFigcaption = true;
$figcaptionPos = count($result);
$result[] = $node;
}
continue;
}

// Figcaption must be a first or last child of a figure element.
// If it's not first, then we ignore all siblings that come after.
if ($hasFigcaption && $figcaptionPos > 0) {
break;
}

$result[] = $node;
}

$whitespaceOnly = true;
foreach ($result as $node) {
$whitespaceOnly = $whitespaceOnly && !empty($node->is_whitespace);
}

// remove parent node if there are no children or all children are whitespace-only
if (empty($result) || $whitespaceOnly) {
return false;
}

return $result;
}
}
2 changes: 1 addition & 1 deletion library/HTMLPurifier/HTMLModule/HTML5/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function setup($config)
$this->addElement('hgroup', 'Block', 'Required: h1 | h2 | h3 | h4 | h5 | h6', 'Common');

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

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

0 comments on commit 68ee8cf

Please sign in to comment.