From 68ed60a4c87600007942f98174efb82c743315a6 Mon Sep 17 00:00:00 2001 From: Dimitrios Pantazis Date: Fri, 14 Jun 2024 19:40:57 +0300 Subject: [PATCH 1/3] Replace module's type attr --- src/Assets/Assets.php | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/Assets/Assets.php b/src/Assets/Assets.php index dbefe32..4b64e0c 100755 --- a/src/Assets/Assets.php +++ b/src/Assets/Assets.php @@ -459,16 +459,18 @@ public function filter_tag_async_defer( $tag, $handle ) { /** * Filters the Script tags to attach type=module based on the rules we set in our Asset class. * - * @param string $tag Tag we are filtering. + * @since 1.0.0 + * @since TBD + * + * @param string $tag Tag we are filtering. * @param string $handle Which is the ID/Handle of the tag we are about to print. * * @return string Script tag with the type=module - * @since 1.0.0 - * */ public function filter_modify_to_module( $tag, $handle ) { - // Only filter for own own filters. - if ( ! $asset = $this->get( $handle ) ) { + $asset = $this->get( $handle ); + // Only filter for our own filters. + if ( ! $asset ) { return $tag; } @@ -482,16 +484,15 @@ public function filter_modify_to_module( $tag, $handle ) { return $tag; } - // These themes already have the `type='text/javascript'` added by WordPress core. - if ( ! current_theme_supports( 'html5', 'script' ) ) { - $replacement = 'type="module"'; - - return str_replace( "type='text/javascript'", $replacement, $tag ); + // Remove the type attribute if it exists. + preg_match( "/ *type=['\"]{0,1}[^'\"]+['\"]{0,1}/i", $tag, $matches ); + if ( ! empty( $matches ) ) { + $tag = str_replace( $matches[0], '', $tag ); } $replacement = '