From 517bb62c4c0f1cc2362328c858f09bde061c3894 Mon Sep 17 00:00:00 2001 From: Mats Mikkel Rummelhoff Date: Tue, 7 Aug 2018 11:10:07 +0200 Subject: [PATCH] Fixes an issue where Retcon could throw an exception if given a NULL value instead of a string. Bump to 2.0.7 --- CHANGELOG.md | 4 ++ composer.json | 2 +- src/models/RetconSettings.php | 17 +++++++-- src/services/RetconService.php | 68 +++++++++++++++++----------------- 4 files changed, 52 insertions(+), 39 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e4414b..e31a905 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Retcon Changelog +## 2.0.7 - 2018-08-07 +### Fixed +- Fixes an issue where Retcon could throw an exception if given a NULL value instead of a string (e.g. if a Redactor field had been added to a Field Layout, without re-saving the entries) + ## 2.0.6 - 2018-08-05 ### Fixed - Fixes an issue where Retcon would attempt to use Imager for transforms, even if Imager was not installed or deactivated diff --git a/composer.json b/composer.json index 911e293..0b17df6 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "mmikkel/retcon", "description": "Powerful Twig filters for mutating and querying HTML", "type": "craft-plugin", - "version": "2.0.6", + "version": "2.0.7", "keywords": [ "craft", "cms", diff --git a/src/models/RetconSettings.php b/src/models/RetconSettings.php index 43fbad1..c46e9f8 100644 --- a/src/models/RetconSettings.php +++ b/src/models/RetconSettings.php @@ -10,12 +10,12 @@ class RetconSettings extends Model { /** - * @var string + * @var string|null */ public $baseTransformPath = '@webroot'; /** - * @var string + * @var string|null */ public $baseTransformUrl = '@web'; @@ -42,9 +42,18 @@ public function __construct($config = []) public function init() { parent::init(); + $this->useImager = $this->useImager && Craft::$app->getPlugins()->getPlugin('imager'); - $this->baseTransformPath = Craft::getAlias($this->baseTransformPath); - $this->baseTransformUrl = Craft::getAlias($this->baseTransformUrl); + + $baseTransformPath = Craft::getAlias($this->baseTransformPath); + if ($baseTransformPath) { + $this->baseTransformPath = $baseTransformPath; + } + + $baseTransformUrl = Craft::getAlias($this->baseTransformUrl); + if ($baseTransformUrl) { + $this->baseTransformUrl = $baseTransformUrl; + } } } diff --git a/src/services/RetconService.php b/src/services/RetconService.php index 11e0334..7bbb5f8 100644 --- a/src/services/RetconService.php +++ b/src/services/RetconService.php @@ -40,15 +40,15 @@ class RetconService extends Component { /** - * @param string $html + * @param string|null $html * @param $args * @return mixed * @throws Exception */ - public function retcon(string $html, ...$args) + public function retcon($html, ...$args) { - if (!$html) { + if (!$html = (string)$html) { return TemplateHelper::raw(''); } @@ -79,7 +79,7 @@ public function retcon(string $html, ...$args) * transform * Applies an image transform to all images (or all nodes matching the passed selector(s)) * - * @param string $html + * @param string|null $html * @param string|array $transform * @param string|array $selector * @param array $imagerTransformDefaults @@ -87,10 +87,10 @@ public function retcon(string $html, ...$args) * @return \Twig_Markup * @throws \craft\errors\AssetTransformException */ - public function transform(string $html, $transform, $selector = 'img', array $imagerTransformDefaults = [], array $imagerConfigOverrides = []) + public function transform($html, $transform, $selector = 'img', array $imagerTransformDefaults = [], array $imagerConfigOverrides = []) { - if (!$html) { + if (!$html = (string)$html) { return TemplateHelper::raw(''); } @@ -138,7 +138,7 @@ public function transform(string $html, $transform, $selector = 'img', array $im * srcset * Creates a srcset attribute for all images (or all nodes matching the selector(s) passed) with the proper transforms * - * @param string $html + * @param string|null $html * @param string|array $transforms * @param string|array $selector * @param string $sizes @@ -147,10 +147,10 @@ public function transform(string $html, $transform, $selector = 'img', array $im * @param array $configOverrides * @return \Twig_Markup */ - public function srcset(string $html, $transforms, $selector = 'img', $sizes = '100w', $base64src = false, $transformDefaults = [], $configOverrides = []) + public function srcset($html, $transforms, $selector = 'img', $sizes = '100w', $base64src = false, $transformDefaults = [], $configOverrides = []) { - if (!$html) { + if (!$html = (string)$html) { return TemplateHelper::raw(''); } @@ -231,16 +231,16 @@ public function srcset(string $html, $transforms, $selector = 'img', $sizes = '1 * lazy * Prepares all images (or all nodes matching the selector(s) passed) by swapping out the `src` attribute with a base64 encoded, transparent SVG. The original source will be retained in a data attribute * - * @param string $html + * @param string|null $html * @param string|array $selector * @param string $className * @param string $attributeName * @return \Twig_Markup */ - public function lazy(string $html, $selector = 'img', string $className = 'lazyload', string $attributeName = 'src') + public function lazy($html, $selector = 'img', string $className = 'lazyload', string $attributeName = 'src') { - if (!$html) { + if (!$html = (string)$html) { return TemplateHelper::raw(''); } @@ -274,16 +274,16 @@ public function lazy(string $html, $selector = 'img', string $className = 'lazyl * autoAlt * Attempts to auto-generate alternative text for all images (or all elements matching the $selector attribute). * - * @param string $html + * @param string|null $html * @param string|array $selector * @param string $field * @param bool $overwrite * @return \Twig_Markup */ - public function autoAlt(string $html, $selector = 'img', string $field = 'title', bool $overwrite = false) + public function autoAlt($html, $selector = 'img', string $field = 'title', bool $overwrite = false) { - if (!$html) { + if (!$html = (string)$html) { return TemplateHelper::raw(''); } @@ -318,16 +318,16 @@ public function autoAlt(string $html, $selector = 'img', string $field = 'title' * attr * Adds (to) or replaces one or many attributes for one or many selectors * - * @param string $html + * @param string|null $html * @param string|array $selector * @param array $attributes * @param bool $overwrite * @return \Twig_Markup */ - public function attr(string $html, $selector, array $attributes, bool $overwrite = true) + public function attr($html, $selector, array $attributes, bool $overwrite = true) { - if (!$html) { + if (!$html = (string)$html) { return TemplateHelper::raw(''); } @@ -364,7 +364,7 @@ public function attr(string $html, $selector, array $attributes, bool $overwrite * renameAttr * Renames attributes for matching selector(s) * - * @param string $html + * @param string|null $html * @param string|array $selector * @param array $attributes * @return \Twig_Markup @@ -372,7 +372,7 @@ public function attr(string $html, $selector, array $attributes, bool $overwrite public function renameAttr($html, $selector, array $attributes) { - if (!$html) { + if (!$html = (string)$html) { return TemplateHelper::raw(''); } @@ -399,14 +399,14 @@ public function renameAttr($html, $selector, array $attributes) * remove * Remove all elements matching given selector(s) * - * @param string $html + * @param string|null $html * @param string|array $selector * @return \Twig_Markup */ public function remove($html, $selector) { - if (!$html) { + if (!$html = (string)$html) { return TemplateHelper::raw(''); } @@ -426,14 +426,14 @@ public function remove($html, $selector) * only * Remove everything except nodes matching given selector(s) * - * @param string $html + * @param string|null $html * @param string|array $selector * @return \Twig_Markup */ public function only($html, $selector) { - if (!$html) { + if (!$html = (string)$html) { return TemplateHelper::raw(''); } @@ -464,7 +464,7 @@ public function only($html, $selector) * change * Changes tag type/name for given selector(s). Can also remove tags (whilst retaining their contents) by passing `false` for the $toTag parameter * - * @param string $html + * @param string|null $html * @param string|array $selector * @param string|bool $toTag * @return \Twig_Markup @@ -472,7 +472,7 @@ public function only($html, $selector) public function change($html, $selector, $toTag) { - if (!$html) { + if (!$html = (string)$html) { return TemplateHelper::raw(''); } @@ -516,7 +516,7 @@ public function change($html, $selector, $toTag) * wrap * Wraps all nodes matching the given selector(s) in a container * - * @param string $html + * @param string|null $html * @param string|array $selector * @param string $container * @return \Twig_Markup @@ -524,7 +524,7 @@ public function change($html, $selector, $toTag) public function wrap($html, $selector, $container) { - if (!$html) { + if (!$html = (string)$html) { return TemplateHelper::raw(''); } @@ -561,14 +561,14 @@ public function wrap($html, $selector, $container) * unwrap * Removes the parent of all nodes matching given selector(s), retaining all child nodes * - * @param string $html + * @param string|null $html * @param string|array $selector * @return \Twig_Markup */ public function unwrap($html, $selector) { - if (!$html) { + if (!$html = (string)$html) { return TemplateHelper::raw(''); } @@ -601,7 +601,7 @@ public function unwrap($html, $selector) * inject * Injects string value (could be HTML!) into all nodes matching given selector(s) * - * @param string $html + * @param string|null $html * @param string|array $selector * @param string $toInject * @param bool $overwrite @@ -610,7 +610,7 @@ public function unwrap($html, $selector) public function inject($html, $selector, $toInject, $overwrite = false) { - if (!$html) { + if (!$html = (string)$html) { return TemplateHelper::raw(''); } @@ -665,14 +665,14 @@ public function inject($html, $selector, $toInject, $overwrite = false) * removeEmpty * Removes empty nodes matching given selector(s), or all empty nodes if no selector * - * @param string $html + * @param string|null $html * @param string|array $selector * @return \Twig_Markup */ public function removeEmpty($html, $selector = null) { - if (!$html) { + if (!$html = (string)$html) { return TemplateHelper::raw(''); }