-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move figure child def to HTML5/ subfolder
- Loading branch information
Showing
3 changed files
with
89 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters