From 286891418ebaa9e079f9a9b5980dfefd98ee1de4 Mon Sep 17 00:00:00 2001 From: reisir Date: Sat, 30 Dec 2023 15:11:31 +0200 Subject: [PATCH 1/6] fix rendering issue, format php, clean up code, declare dependencies --- blueprints.yaml | 5 +- shortcodes/DataTablesScript.php | 8 +-- shortcodes/DataTablesShortcode.php | 88 ++++++++++++------------- templates/partials/datatables.html.twig | 13 ++-- 4 files changed, 58 insertions(+), 56 deletions(-) diff --git a/blueprints.yaml b/blueprints.yaml index ae0a49e..56e78dd 100644 --- a/blueprints.yaml +++ b/blueprints.yaml @@ -1,5 +1,5 @@ name: Datatables -version: 1.0.4 +version: 1.0.3 description: shortcode to embed DataTables jquery plugin icon: plug author: @@ -12,7 +12,8 @@ docs: https://github.com/finanalyst/grav-plugin-datatables/blob/master/README.md license: MIT dependencies: - - shortcode-core + - { name: shortcode-core } + - { name: error } form: validation: strict diff --git a/shortcodes/DataTablesScript.php b/shortcodes/DataTablesScript.php index 0e04d6b..b2d531f 100644 --- a/shortcodes/DataTablesScript.php +++ b/shortcodes/DataTablesScript.php @@ -6,14 +6,14 @@ class DataTablesScript extends Shortcode { public function init() { - $this->shortcode->getHandlers()->add('dt-script', function(ShortcodeInterface $sc) { + $this->shortcode->getHandlers()->add('dt-script', function (ShortcodeInterface $sc) { $content = trim($sc->getContent()); - while ( preg_match('/\<[^>]*\>/', $content, $matches)) { // strip tags from both ends + while (preg_match('/\<[^>]*\>/', $content, $matches)) { // strip tags from both ends $tag = strlen($matches[0]); $len = strlen($content); - $content = substr($content,$tag,$len-$tag-$tag-1); + $content = substr($content, $tag, $len - $tag - $tag - 1); } - if ( isset($this->grav['datatables'] )) $this->grav['datatables'] .= $content; + if (isset($this->grav['datatables'])) $this->grav['datatables'] .= $content; return ''; }); } diff --git a/shortcodes/DataTablesShortcode.php b/shortcodes/DataTablesShortcode.php index e9420c2..11658bf 100644 --- a/shortcodes/DataTablesShortcode.php +++ b/shortcodes/DataTablesShortcode.php @@ -1,5 +1,7 @@ shortcode->getHandlers()->add('datatables', function(ShortcodeInterface $sc) { + $this->shortcode->getHandlers()->add('datatables', function (ShortcodeInterface $sc) { $content = $sc->getContent(); $parameters = $sc->getParameters(); $id = ''; - $res = preg_match('/\]*?\>(.*)\<\/table[^>]*\>/ims',$content); - // does table have a table attached? - if ( $res === FALSE or $res == 0) { - // error some where - return $this->twig->processTemplate('partials/datatables-error.html.twig', - [ 'message' => 'Shortcode content does not appear to have a valid <table>...</table> element. Got instead:', - 'content' => $content - ] ); - } else { - $res = preg_match('/(\]*)id="(.*?)"(.*\>)/ims',$content,$matches); - if ( $res == 1 ) { - $this->grav['debugger']->addMessage('found id'); - if ( ! $matches[2] || preg_match( '/\s/',$matches[2]) == 1 ) { - // id either has spaces - illegal - or is null, so strip output - $content = $matches[1] . $matches[3]; - } else { - $id = $matches[2]; - } - } + + $hasTable = preg_match('/\]*?\>(.*)\<\/table[^>]*\>/ims', $content); + if ($hasTable === FALSE or $hasTable == 0) { + return htmlspecialchars_decode($this->twig->processTemplate( + 'partials/datatables-error.html.twig', + [ + 'message' => 'Shortcode content does not appear to have a valid <table>...</table> element. Got instead:', + 'content' => $content + ] + )); } - if ( ! $id ) { - if ( isset( $params['grav-id'])) { - $id = trim($params['grav-id']); - unset($params['grav-id']); - if (preg_match('/\s/')) { $id = '';} // ignore an illegal id + + $hasIdAttribute = preg_match('/(\]*)id="(.*?)"(.*\>)/ims', $content, $matches); + if ($hasIdAttribute == 1) { + $invalidId = (!$matches[2] || preg_match('/\s/', $matches[2]) == 1); + if ($invalidId) { + $content = $matches[1] . $matches[3]; + } else { + $id = $matches[2]; } - // this occurs if content has twig->processTemplate('partials/datatables.html.twig', - [ - 'id' => $id, - 'content' => $content, - 'options' => $options, - 'snippet' => $this->grav['datatables'] - ]); - $this->grav['datatables'] = ''; // clear snippet for next table invocation - return $output; + + $options = preg_replace(['/"true"/i', '/"false"/i'], ['true', 'false'], json_encode($parameters)); + $output = $this->twig->processTemplate( + 'partials/datatables.html.twig', + [ + 'id' => $id, + 'content' => $content, + 'options' => $options, + 'snippet' => $this->grav['datatables'] + ] + ); + $output = htmlspecialchars_decode($output); + $this->grav['datatables'] = ''; + return $output; }); } } diff --git a/templates/partials/datatables.html.twig b/templates/partials/datatables.html.twig index 3d1c4bf..4a923d4 100644 --- a/templates/partials/datatables.html.twig +++ b/templates/partials/datatables.html.twig @@ -1,10 +1,11 @@ {{ content }} + From 49ffdc3b4df1222e0885da8af3b45166c5fb3013 Mon Sep 17 00:00:00 2001 From: reisir Date: Sat, 30 Dec 2023 16:02:08 +0200 Subject: [PATCH 2/6] handle invalid id from shortcode parameters --- shortcodes/DataTablesShortcode.php | 33 +++++++++++++++++------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/shortcodes/DataTablesShortcode.php b/shortcodes/DataTablesShortcode.php index 11658bf..002655e 100644 --- a/shortcodes/DataTablesShortcode.php +++ b/shortcodes/DataTablesShortcode.php @@ -25,27 +25,32 @@ public function init() )); } + // Get id from html and remove the id attribute $hasIdAttribute = preg_match('/(\]*)id="(.*?)"(.*\>)/ims', $content, $matches); if ($hasIdAttribute == 1) { - $invalidId = (!$matches[2] || preg_match('/\s/', $matches[2]) == 1); - if ($invalidId) { - $content = $matches[1] . $matches[3]; - } else { - $id = $matches[2]; - } + $content = $matches[1] . $matches[3]; + $id = $matches[2]; } - if (!$id) { - $id = trim($parameters['id'] ?? $parameters['grav-id'] ?? ''); - unset($parameters['id']); - unset($parameters['grav-id']); - if (!$id) $id = Utils::generateRandomString(10); - $pos = stripos('twig->processTemplate( 'partials/datatables.html.twig', [ From 1eeb516f6846622018a7c91b46a0b0d40fe9e6e4 Mon Sep 17 00:00:00 2001 From: reisir Date: Sat, 30 Dec 2023 16:11:46 +0200 Subject: [PATCH 3/6] I might be stupid --- shortcodes/DataTablesShortcode.php | 5 ++--- templates/partials/datatables-error.html.twig | 8 ++------ templates/partials/datatables.html.twig | 2 +- 3 files changed, 5 insertions(+), 10 deletions(-) diff --git a/shortcodes/DataTablesShortcode.php b/shortcodes/DataTablesShortcode.php index 002655e..43535e3 100644 --- a/shortcodes/DataTablesShortcode.php +++ b/shortcodes/DataTablesShortcode.php @@ -16,13 +16,13 @@ public function init() $hasTable = preg_match('/\]*?\>(.*)\<\/table[^>]*\>/ims', $content); if ($hasTable === FALSE or $hasTable == 0) { - return htmlspecialchars_decode($this->twig->processTemplate( + return $this->twig->processTemplate( 'partials/datatables-error.html.twig', [ 'message' => 'Shortcode content does not appear to have a valid <table>...</table> element. Got instead:', 'content' => $content ] - )); + ); } // Get id from html and remove the id attribute @@ -60,7 +60,6 @@ public function init() 'snippet' => $this->grav['datatables'] ] ); - $output = htmlspecialchars_decode($output); $this->grav['datatables'] = ''; return $output; }); diff --git a/templates/partials/datatables-error.html.twig b/templates/partials/datatables-error.html.twig index 4e9f6c4..742716b 100644 --- a/templates/partials/datatables-error.html.twig +++ b/templates/partials/datatables-error.html.twig @@ -1,6 +1,2 @@ -
-{{message}} -
-
-{{content}} -
+
{{ message }}
+
{{ content|raw }}
diff --git a/templates/partials/datatables.html.twig b/templates/partials/datatables.html.twig index 4a923d4..e846f7d 100644 --- a/templates/partials/datatables.html.twig +++ b/templates/partials/datatables.html.twig @@ -1,4 +1,4 @@ -{{ content }} +{{ content|raw }}