#42804 closed task (blessed) (fixed)
type is not required in HTML5
Reported by: | sasiddiqui | Owned by: | SergeyBiryukov |
---|---|---|---|
Milestone: | 5.3 | Priority: | low |
Severity: | normal | Version: | |
Component: | Script Loader | Keywords: | needs-testing has-patch commit |
Focuses: | Cc: |
Description
type is no more required in style and script tags in HTML5. Now HTML Validator start throwing warnings on the script and style tags which contains the types.
Attaching a path which fixes the issues for inline and enqueued files (CSS and JS).
Attachments (8)
Change History (54)
#1
@
7 years ago
- Type changed from defect (bug) to enhancement
Hello and welcome to track, sasiddiqui.
Thank your for your suggestion. I agree this would be a nice enhancement. When validating html these warnings comes up. WordPress could do better with making correct html.
Are you able to make a patch with just the changes you propose?
#2
@
7 years ago
- Component changed from General to Script Loader
- Priority changed from normal to low
- Version trunk deleted
Please have a look at https://make.wordpress.org/core/handbook/tutorials/working-with-patches/ in order to create patch files that can be more easily scanned. Uploading whole files is unfortunately not really helpful.
Also, just because type
is not required anymore with HTML5, it doesn't mean every website or teme is using HTML5. In HTML4 and other doc types, the type
attribute is required and will cause validation errors (not just warnings as in your example).
Just removing type
might not the best solution. Maybe we need to use add_theme_support
for this?
#3
@
7 years ago
@knutsp Thanks. @swissspidy Sorry about the improper attachments. Would create a patch and add it again.
Apology for the improper files.
#4
@
7 years ago
- Keywords has-patch needs-testing added
@knutsp @swissspidy Patch has been attached. Let me know if i have done any mistake in it.
@swissspidy As per your concern about the HTML 4 so, i am not sure but i think, mostly themes are working on HTML 5 specially the sites which are working on the latest WordPress Version. It's quite long time when HTML 5 had been introduced.
But, if you guys think that we still need to support the HTML 4 so, we can also add the filter for HTML 4 which doesn't remove the type attr from the script and style tags.
#5
@
7 years ago
- Keywords needs-patch added; has-patch removed
Patch has been attached. Let me know if i have done any mistake in it.
Much better, but the patch should only change the lines in question. Right now, it changes lots of the coding style throughout the files and the patch is very long and difficult to scan because of this. It's recommended not to touch irrelevant code sections.
For now and updated code you can use PHP Code Sniffer with the WP Coding Standards (https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards) to automatically correct coding standards mistakes. The WordPress Git repository contains a phpcs.xml.dist file to automatically configure PHPCS for that.
But, if you guys think that we still need to support the HTML 4 so, we can also add the filter for HTML 4 which doesn't remove the type attr from the script and style tags.
That's what I meant with add_theme_support
. There's precedence for that. See https://developer.wordpress.org/reference/functions/add_theme_support/#html5
#6
follow-up:
↓ 7
@
7 years ago
Something like add_theme_support( 'html5', [ 'script', 'css' ] );
?
We already have add_theme_support( 'html5', [ 'comment-list', 'comment-form', 'search-form', 'gallery', 'caption' ] );
Could this be simplified to just add_theme_support( 'html5' );
to catch all?
#7
in reply to:
↑ 6
;
follow-up:
↓ 8
@
7 years ago
Something like
add_theme_support( 'html5', [ 'script', 'css' ] );
?
Yeah, something like that.
Could this be simplified to just
add_theme_support( 'html5' );
to catch all?
That's how the command worked in the beginning, however it was changed to take an array of features that are supported.
For BC reasons add_theme_support( 'html5' );
is like calling add_theme_support( 'html5', array( 'comment-list', 'comment-form', 'search-form' ) );
. I don't think we can change that. Plus, this would warrant its own ticket.
#8
in reply to:
↑ 7
@
7 years ago
Replying to swissspidy:
For BC reasons
add_theme_support( 'html5' );
is like callingadd_theme_support( 'html5', array( 'comment-list', 'comment-form', 'search-form' ) );
. I don't think we can change that.
Why not?
Own ticket, of course.
#9
@
7 years ago
- Keywords has-patch added; needs-patch removed
Please check this patch: https://core.trac.wordpress.org/attachment/ticket/42804/42804-update1.patch
You need to add the add_theme_support
in functions.php to remove make the code work.
Something like:
add_theme_support( 'html5', array( 'comment-list', 'comment-form', 'search-form', 'script', 'style' ) );
or
add_theme_support( 'html5', array( 'script', 'style' ) );
#13
@
7 years ago
- Milestone changed from Awaiting Review to 5.0
@reeslo No, this was not added in 4.9.2 as you can see from the history above. The ticket wasn't added to the 4.9.x milestone at any point. However, I think we can add this to 5.0.
#18
in reply to:
↑ 17
;
follow-up:
↓ 25
@
6 years ago
Replying to pento:
Has this been fixed yet? I'm getting the warnings in W3C validator.
EDIT: Added the following function to my theme functions.php and the W3C validation errors are gone. Is this the right way to fix the issue?
//Remove JS and CSS types add_action( 'template_redirect', function(){ ob_start( function( $buffer ){ $buffer = str_replace( array( 'type="text/javascript"', "type='text/javascript'", 'type="text/css"', "type='text/css'" ), '', $buffer ); return $buffer; }); });
#19
@
6 years ago
- Milestone changed from 5.1 to Future Release
This patch needs review and a decision.
#20
@
6 years ago
Is there any update when it can be deployed in WordPress Core? I can understand that it doesn't have such testing yet but it would be great if any time or WordPress version can be mentioned.
#21
@
6 years ago
Any chance this will be reviewed and approved soon? Would like to clean up those remaining validation errors and get rid of unnecessary code. Thanks.
#22
@
6 years ago
@waterworks2 We all are waiting for this.. Hopefully, it will be reviewed and available soon.
#24
@
6 years ago
Any chance this will be reviewed and approved soon? Would like to clean up those remaining validation errors and get rid of unnecessary code. Thanks.
#25
in reply to:
↑ 18
@
5 years ago
Replying to gabrieldiggs:
EDIT: Added the following function to my theme functions.php and the W3C validation errors are gone. Is this the right way to fix the issue?
//Remove JS and CSS types add_action( 'template_redirect', function(){ ob_start( function( $buffer ){ $buffer = str_replace( array( 'type="text/javascript"', "type='text/javascript'", 'type="text/css"', "type='text/css'" ), '', $buffer ); return $buffer; }); });
I did something similar: https://www.damiencarbery.com/2018/11/remove-type-from-script-and-style-markup/
I filtered 'script_loader_tag' and 'style_loader_tag' to work on enqueued scripts and styles and used ob_start() and ob_get_contents() on 'wp_head' to process inline scripts and styles.
#28
@
5 years ago
- Milestone changed from Future Release to 5.3
- Owner set to SergeyBiryukov
- Status changed from new to reviewing
#29
follow-up:
↓ 30
@
5 years ago
Hello how do I leave this ticket? I have notifications blocked but I keep getting emails with updates. How do I block those?
#30
in reply to:
↑ 29
@
5 years ago
Replying to gabrieldiggs:
Hello how do I leave this ticket? I have notifications blocked but I keep getting emails with updates. How do I block those?
Sorry for the noise, it's a known problem that's being investigated in #meta3594.
#34
@
5 years ago
- Resolution fixed deleted
- Status changed from closed to reopened
Getting Fatal error: Uncaught Error: Call to undefined function current_theme_supports() in wp-includes\class.wp-scripts.php:145
in load-scripts.php.
load-scripts.php
and load-styles.php
do not load all of WP. They use sctipt-loader only as a whitelist for scripts and styles.
We can add a stub or a function_exists()
for current_theme_supports()
but then this (minor) enhancement becomes somewhat redundant? Also it seems it has effect only when outputting individual scripts and styles in wp-admin, i.e. when SCRIPT_DEBUG
is true.
Also, would have been nice to also remove the <![CDATA[
stuff :)
#37
@
5 years ago
In 42804.2.diff:
- Add
function_exists()
when usingis_admin()
andcurrent_theme_supports()
. At some point WP_Dependencies was a separate "package". It's very unlikely but "something" may still be using it that way... - Remove
<![CDATA[
when outputting HTML 5.0 script tags. Not sure if it triggers validation errors, but is super outdated.
@SergeyBiryukov could you have a look in case I'm missing something and commit plz :)
#41
@
5 years ago
- Keywords needs-dev-note removed
Mentioned in the Miscellaneous Developer Focused Changes dev note for 5.3: https://make.wordpress.org/core/2019/10/15/miscellaneous-developer-focused-changes-in-5-3/
#42
@
5 years ago
HTML5 is definitely industry standard. Do we have any plan to move away from it being the exception?
It's nice that we have a way to remove the declarations but in the end it's just more "onboarding" for new developers.
#45
follow-up:
↓ 46
@
3 years ago
Hi @SergeyBiryukov et al,
I have a plugin which adds a <script>
tag to the footer of every page, and the tag includes the type="module"
attribute.
I've discovered that if I use the "add_theme_support" function to add html5 support for scripts, it not only results in the type="text/javascript"
attribute being removed from all <script>
tags, but the type="module"
attribute is also removed too, which is a problem - this attribute is still needed and still valid under HTML5.
The description of this patch at the top of this page is incorrect to state that "type is no more required" under HTML5 - in fact, only the JavaScript MIME type is no longer required. Other values for type, such as "module", are still valid:
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script
Question: is it possible the patch provided here is also stripping out the type="module"
attribute?
If so, could that please be reversed or fixed, so that this attribute is not removed?
#46
in reply to:
↑ 45
@
3 years ago
Replying to GermanKiwi:
Question: is it possible the patch provided here is also stripping out the
type="module"
attribute?
If so, could that please be reversed or fixed, so that this attribute is not removed?
Hi there, welcome to WordPress Trac!
As far as I can tell, the changes in [46164] should only specifically affect the type="text/javascript"
attribute, but I have not tested them with other values.
Please note that this ticket was closed on a completed milestone and may not receive enough attention for further updates. If the issue is still relevant, could you create a new ticket for investigation? Thanks!
Fixed the type issue with enqueue and inline script tags