commandbox-cfformat icon indicating copy to clipboard operation
commandbox-cfformat copied to clipboard

Ignoring blocks/tags via settings vs comment

Open bhartsfield opened this issue 3 years ago • 2 comments

I've ran into this a couple of times now. I deal with an app that has a bunch of .cfm files and those cfm files contain <script>...</script> blocks. Cfformat will dutifully format the contents of the script tags as if it were CFML and that can cause javascript errors.

The most recent example was a tinyMCE toolbar setting... which is essentially just an array of comma delimited lists containing pipe and space delimited lists (ugh...). Those can get pretty long and, at some point, cfformat's line or array length settings seem to kick in and break it up into more, shorter lines. Unfortunately, adding a linebreak in the wrong place would break those settings.

Actual example before cfformat

toolbar: [
	"cut copy paste | undo redo | link unlink | image <cfif variables.enableTinyMCEMediaEmbed>media </cfif>hr blockquote charmap nonbreaking | visualchars visualblocks | removeformat | code | preview | fullscreen",
	"formatselect fontselect fontsizeselect | bold italic underline | alignleft aligncenter alignright alignjustify | bullist numlist | outdent indent | forecolor backcolor | <cfif variables.showmergeFields>mergefields</cfif>"
],

After cfformat (line break after the closing cfif)

toolbar: [
	"cut copy paste | undo redo | link unlink | image <cfif variables.enableTinyMCEMediaEmbed>media </cfif>
hr blockquote charmap nonbreaking | visualchars visualblocks | removeformat | code | preview | fullscreen",
	"formatselect fontselect fontsizeselect | bold italic underline | alignleft aligncenter alignright alignjustify | bullist numlist | outdent indent | forecolor backcolor | <cfif variables.showmergeFields>mergefields</cfif>
"
],

Rather than having to wrap all script blocks in cfformat ignore start and end comments (or selectively wrapping things like the above example in those comments), it would be great if you could tell cfformat to ignore certain tags/blocks with a .cfformat.ignore.json file.

A farther reaching approach to solving the same thing might be a setting that allows us to tell cfformat what tags to include (within a .cfm file) based on a regex pattern. i.e. <cf[^>]+>.

Cheers

bhartsfield avatar Mar 09 '22 12:03 bhartsfield

I agree this is a problem, and is one of the reasons my focus with this library was on component files where <script> tags are not likely to be found (though of course it does happen). The mixture of CFML and JavaScript makes things tricky. I am not sure yet which approach to solving this is the most doable. Whatever is done is most likely always going to be imperfect, but I do think things can be improved.

jcberquist avatar Mar 10 '22 17:03 jcberquist

I’d go a little further and say these script blocks shouldn’t exist in the cfm files either… that’s what .js files are for :-) Unfortunately, it’s a scattered legacy monolith.

My original thought was just a setting to ignore script blocks (off by default to keep the current behavior). I only expanded to the regex because I recalled cfformat doing the same to some XML and the regex approach should cover that too.

we’ll just have to be more diligent in code reviews until things get a bit cleaner in the app.

thanks for cfformt!

bhartsfield avatar Mar 11 '22 12:03 bhartsfield