Jump to content

MarkupRegions is stripping out HTML comments


gebeer
 Share

Recommended Posts

Hello,

MarkupRegions is stripping out HTML comments by default. I just discovered that while converting a rather old project to use MarkupRegions. In that project HTML comments are required for some knockout.js code to work. And I was scratching my head for quite a while until I recognized that those comments got removed. This behaviour is nowhere being mentioned in the documentation, other than in the code itself at https://github.com/processwire/processwire/blob/3acd7709c1cfc1817579db00c2f608235bdfb1e7/wire/core/WireMarkupRegions.php#L89. And an old forum post mentions it. 

How can I tell MarkupRegions that my markup is required to stay exactly the same or in other words, where can I pass options ("exact" =>true) to MarkupRegions find() to change the default behaviour?

Link to comment
Share on other sites

23 hours ago, AndZyk said:

I couldn't find anything in the documentation, but in the core file it seems that comments starting with <!--# don't get stripped out:

https://github.com/processwire/processwire/blob/3acd7709c1cfc1817579db00c2f608235bdfb1e7/wire/core/WireMarkupRegions.php#L844

<!--# Comment -->

I tried it and it seems to work.

Regards, Andreas

This only helps in some cases. In my case I need the comment to read 
 

<!-- ko if: properties.ms == "pro" -->

for knockout.js to recognise it. So the other syntax would break my JS.

But thanks for pointing that out.

Link to comment
Share on other sites

16 minutes ago, bernhard said:

Maybe you can trick it by using this?

<!--# <!-- ko if: properties.ms == "pro" -->

 

Unfortunately this is not working. And it looks ugly and feels so wrong ;-)
But thank you for the suggestion.

  • Like 1
Link to comment
Share on other sites

5 hours ago, gebeer said:

And it looks ugly and feels so wrong ?

I totally agree ? 

Another suggestion could be to add a textarea to define custom comment tags that should not get removed, or maybe a regex or such, eg

<!-- ko (having a space after ko)
Link to comment
Share on other sites

9 hours ago, bernhard said:

I totally agree ? 

Another suggestion could be to add a textarea to define custom comment tags that should not get removed, or maybe a regex or such, eg

<!-- ko (having a space after ko)

Thanks again for the suggestion. I really don't want to spend more time for finding a workaround. I'll be waiting until we get some feedback in the feature request and a clean way to avoid stripping comments. In the meantime, I fell back to good old if statements for including the necessary HTML blocks.

Link to comment
Share on other sites

5 hours ago, gebeer said:

I really don't want to spend more time for finding a workaround. I'll be waiting until we get some feedback in the feature request and a clean way to avoid stripping comments.

My suggestion was a suggestion for that feature request, not a workaround ? 

Link to comment
Share on other sites

32 minutes ago, bernhard said:

My suggestion was a suggestion for that feature request, not a workaround ? 

Oh, I see. The feature request is about improving documentation and giving developers a way to disable stripping of comments.

Link to comment
Share on other sites

3 minutes ago, gebeer said:

and giving developers a way to disable stripping of comments.

And exactly that was my suggestion: Ask Ryan not to make something hard-coded or opinionated like a toggle to either strip all <!--# comments --> or to leave all <!--# comments --> as is, but ask him to make that definable by the user, so that he/she can for example tell markupregions to leave all comments that start with " ko " as is, so that knockout comments stay untouched and all other comments still get removed.

That way you would not have to rewrite your codebase and you can use markupregions with any external tool that relies on a custom comments format.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...