Skip to content
Snippets Groups Projects
Unverified Commit 4657f661 authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #3249240 by alexpott, andypost, Wim Leers: HTMLRestrictionsUtilities::...

Issue #3249240 by alexpott, andypost, Wim Leers: HTMLRestrictionsUtilities:: providedElementsAttributes() causes deprecations on PHP 8.1

(cherry picked from commit 65aaec60)
parent 910dedc3
No related branches found
No related tags found
21 merge requests!2496Issue #3222757 by lauriii, Wim Leers, nod_, rachel_norfolk, itmaybejj,...,!2366Issue #3285105 by Daniel Arend,!2304Issue #3258987: Class "Drupal\Core\Utility\Error" not found in _drupal_error_handler_real() due to bug in PHP 8.1.0-8.1.5,!2148Issue #3270899: Remove Color module from core,!2136Issue #3227824: Move the linkset functionality from the decoupled menus contributed module to core's system module,!2071Issue #927570: Setting 403 or 404 handler to a page that redirects leads to endless loop,!1975Issue #3269749: losing query params from user to user/login redirect,!1959Issue #3236497: Allow other modules to opt out of security release message from update_page_top,!1481Issue #3252562: Allow functions that accept no arguments to be used as callable,!1443Issue #3075230: Provide menu link with disable option [Node Add Form],!1387Draft: Resolve #2511878 "Support enclosure field",!1321Issue #3239123: Refactor (if feasible) uses of the jQuery text function to use vanillaJS,!1282Issue #3227824: Add the decoupled menus module to core,!1269Issue #3239134: Refactor (if feasible) uses of the jQuery val function to use VanillaJS,!1262Issue #3239500: Add Array.includes polyfill to support IE11 and Opera Mini,!1229Issue #3225621: Use media query event listener instead of a listener on the resize event,!1159Convert dblog entries into entities,!799Issue #3214332: Preview content is broken in Claro.,!776Resolve #85494 "Use email verification 9.3.x",!558Resolve #3020422 "Toolbar style update",!231Issue #2671162: summary text wysiwyg patch working fine on 9.2.0-dev
......@@ -13,6 +13,8 @@
* Utilities for interacting with HTML restrictions.
*
* @internal
*
* @see \Drupal\filter\Plugin\FilterInterface::getHTMLRestrictions()
*/
final class HTMLRestrictionsUtilities {
......@@ -29,10 +31,13 @@ final class HTMLRestrictionsUtilities {
* Formats HTML elements for display.
*
* @param array $elements
* List of elements to format.
* List of elements to format. The structure is the same as the allowed tags
* array documented in FilterInterface::getHTMLRestrictions().
*
* @return string
* @return string[]
* A formatted list; a string representation of the given HTML elements.
*
* @see \Drupal\filter\Plugin\FilterInterface::getHTMLRestrictions()
*/
public static function toReadableElements(array $elements): array {
$readable = [];
......@@ -77,10 +82,14 @@ public static function allowedElementsStringToPluginElementsArray(string $elemen
* A string of HTML tags, potentially with attributes.
*
* @return array
* An elements array in the structure expected by filter_html.
* An elements array. The structure is the same as the allowed tags array
* documented in FilterInterface::getHTMLRestrictions().
*
* @see \Drupal\ckeditor5\Plugin\CKEditor5PluginManager::WILDCARD_ELEMENT_METHODS
* @see \Drupal\ckeditor5\HTMLRestrictionsUtilities::WILDCARD_ELEMENT_METHODS
* Each key in this array represents a valid wildcard tag.
*
* @see \Drupal\filter\Plugin\Filter\FilterHtml
* @see \Drupal\filter\Plugin\FilterInterface::getHTMLRestrictions()
*/
public static function allowedElementsStringToHtmlFilterArray(string $elements_string): array {
preg_match('/<(\$[A-Z,a-z]*)/', $elements_string, $wildcard_matches);
......@@ -105,7 +114,7 @@ public static function allowedElementsStringToHtmlFilterArray(string $elements_s
if ($node->hasAttributes()) {
foreach ($node->attributes as $attribute_name => $attribute) {
$value = empty($attribute->value) ? TRUE : explode(' ', $attribute->value);
self::providedElementsAttributes($elements, $tag, $attribute_name, $value);
self::addAllowedAttributeToElements($elements, $tag, $attribute_name, $value);
}
}
else {
......@@ -121,10 +130,13 @@ public static function allowedElementsStringToHtmlFilterArray(string $elements_s
* Cleans unwanted artifacts from "allowed HTML" arrays.
*
* @param array $elements
* An array of allowed elements, structured as expected by filter_html.
* An array of allowed elements. The structure is the same as the allowed
* tags array documented in FilterInterface::getHTMLRestrictions().
*
* @return array
* The array without unwanted artifacts.
*
* @see \Drupal\filter\Plugin\FilterInterface::getHTMLRestrictions()
*/
public static function cleanAllowedHtmlArray(array $elements): array {
// When recursively merging elements arrays, unkeyed boolean values can
......@@ -141,21 +153,35 @@ public static function cleanAllowedHtmlArray(array $elements): array {
* Adds allowed attributes to the elements array.
*
* @param array $elements
* The elements array.
* The elements array. The structure is the same as the allowed tags array
* documented in FilterInterface::getHTMLRestrictions().
* @param string $tag
* The tag having its attributes configured.
* @param string $attribute
* The attribute being configured.
* @param array|bool $value
* @param array|true $value
* The attribute config value.
*
* @see \Drupal\filter\Plugin\FilterInterface::getHTMLRestrictions()
*/
public static function providedElementsAttributes(array &$elements, string $tag, string $attribute, $value): void {
$attribute_already_allows_all = isset($elements[$tag][$attribute]) && $elements[$tag][$attribute] === TRUE;
public static function addAllowedAttributeToElements(array &$elements, string $tag, string $attribute, $value): void {
if (isset($elements[$tag][$attribute]) && $elements[$tag][$attribute] === TRUE) {
// There's nothing to change as the tag/attribute combination is already
// set to allow all.
return;
}
if (isset($elements[$tag]) && $elements[$tag] === FALSE) {
// If the tag is already allowed with no attributes then the value will be
// FALSE. We need to convert the value to an empty array so that attribute
// configuration can be added.
$elements[$tag] = [];
}
if ($value === TRUE) {
$elements[$tag][$attribute] = TRUE;
}
elseif (!$attribute_already_allows_all) {
else {
foreach ($value as $attribute_value) {
$elements[$tag][$attribute][$attribute_value] = TRUE;
}
......@@ -165,6 +191,9 @@ public static function providedElementsAttributes(array &$elements, string $tag,
/**
* Compares two HTML restrictions.
*
* The structure of the arrays is the same as the allowed tags array
* documented in FilterInterface::getHTMLRestrictions().
*
* @param array $elements_array_1
* The array to compare from.
* @param array $elements_array_2
......@@ -173,6 +202,8 @@ public static function providedElementsAttributes(array &$elements, string $tag,
* @return array
* Returns an array with all the values in $elements_array_1 that are not
* present in $elements_array_1, including values that are FALSE
*
* @see \Drupal\filter\Plugin\FilterInterface::getHTMLRestrictions()
*/
public static function diffAllowedElements(array $elements_array_1, array $elements_array_2): array {
return array_filter(
......
......@@ -326,10 +326,7 @@ public function getProvidedElements(array $plugin_ids = [], EditorInterface $edi
if (is_array($attribute_value)) {
$attribute_value = array_keys($attribute_value);
}
$element_already_allows_all_values = isset($elements[$wildcard_tag][$attribute_name]) && $elements[$wildcard_tag][$attribute_name] === TRUE;
if (!$element_already_allows_all_values) {
HTMLRestrictionsUtilities::providedElementsAttributes($elements, $wildcard_tag, $attribute_name, $attribute_value);
}
HTMLRestrictionsUtilities::addAllowedAttributeToElements($elements, $wildcard_tag, $attribute_name, $attribute_value);
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment