Hi. It seems there is a problem with javascript filtering. Steps to reproduce:
1.- New Drupal 8.1.10 installation with standard profile (also fails with 8.2.x tested in simplytest.me).
2.- Edit Basic HTML filter and adds <script type> to allowed HTML tags.
3.- Add a new basic page node, and set the following code in HTML Source mode:
<script type="text/javascript">
document.write('<p>This is a <a href="http://www.drupal.org/">test</a></p>');
document.write('<p>This is a <a href="http://www.drupal.org/">test</a></p>');
</script>
4.- After saving the node, Drupal generates the HTML you can see in the attached screenshot.
| Comment | File | Size | Author |
|---|---|---|---|
| #17 | fix_script_parsing_2822525-17.patch | 2.57 KB | stevenlafl |
| #15 | fix_script_parsing_2822525-15-test-only.patch | 852 bytes | Anonymous (not verified) |
| Captura de pantalla 2016-10-27 a las 12.15.09.png | 281.46 KB | skuark |
Comments
Comment #2
skuark commentedComment #3
cilefen commentedThe basic HTML is a text format, the settings within it are filters. The "Limit allowed HTML tags and correct faulty HTML", FilterHtml, is the filter you are dealing with.
I have no idea why you would want to add script to the allowed tags. But, understand that the entered text is filtered for a number of malformations beyond simply the allowed tags. Is there a reason why the CDATA comments are a problem?
Comment #4
skuark commentedSorry, I said it wrong. Indeed, I was speaking about a Filter Html problem.
Do you mean I have to include the JS code inside a
CDATAcomment, like this?I've tried it with the same result as before. The HTML filter breaks the code in the same way. And I have to add the
<script type>to the allowed tags. Without adding it, the previous code disappears completly after saving the nodeAnyway, I don't know if you are referring to this, or to the
CDATAcomments generated by Drupal that appears in the screenshot. But it is not the problem. If you look at the screenshot, you can see the following JS code, instead of the previous JS:Notice the missing closing
</a>and</p>tags. That's the problem I was referencing.Comment #5
skuark commentedComment #6
cilefen commentedJust to be clear: letting people type inline JavaScript into body text is a bad idea, even when that person is a trusted administrator.
Comment #7
skuark commentedI agree it's a bad idea, but unfortunately it's a client requirement in our latest project. More specifically, we need to support legacy html with tons of script tags, migrated from a Drupal 6.
Anyway, I submitted that issue because, in spite of being a bad practice, I understand it's not working as expected.
Comment #8
droplet commentedTested. No Problems. Make sure not the CKEditor messed your JS code.
Comment #9
cilefen commentedGood point.
Comment #10
Anonymous (not verified) commentedIt is very old problem of libxml2. It have bad parsing tag inside
<script>. You can read more by query "DOMDocument::loadHTML() Unexpected end tag in Entity". Drupal not showing error, because it run with ignore regime:Comment #11
skuark commentedThanks. I've tested without CKEditor, and got the same problem. You can see the followed steps in this gif I've recorded: https://dl.dropboxusercontent.com/u/2430190/filter.gif
Thanks @vaplas. I'll be watching to #1333730: [Meta] PHP DOM (libxml2) misinterprets HTML5.
Comment #12
droplet commentedSorry, overlooked your point
Comment #13
Anonymous (not verified) commented@skuark, it related issue, but it will not help you now.
PS. Amazing gif!!! We believe you, just do not understand correctly first). And way to reproduce it:
Comment #14
Anonymous (not verified) commentedOk, this patch solves the problem of incorrect parsing
<script>through a stubs. It does not look good, but who knows better solution, while we using libxml?)Comment #15
Anonymous (not verified) commentedNit improvement in performance + test
Comment #16
skuark commented@vaplas Great! Your patch works for me :-)
Thanks a lot!
Comment #17
stevenlafl commentedI've simply made this compatible with PHP 7.2 which deprecates create_function.
Comment #18
stevenlafl commentedComment #23
gordon commentedI have looked at this in relation to CDATA issues. I believe that this is "Works as intended". We really do not want Drupal playing with imbedded js. This is an advanced feature and could break so easy.
If you do need to to fix this, then create a custom filter which will do these corrections for you. This doesn't need to be in resolved in core when this will be something that will be individual for sites,and the fact that custom filters can be created to resolve these issues.
Lastly if this is content being migrated and this is a common issue (i.e. the code was just cut and paste from node to node) then if could be fixed during the migration.
Comment #26
andypostI bet it could be closed as filter system upgraded to HTML5