Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 129877

Summary: [formatting] HTML cleanup document should offer compress empty element tags
Product: [WebTools] WTP Source Editing Reporter: Amy Wu <for.work.things>
Component: wst.htmlAssignee: Ian Tewksbury <itewksbu>
Status: RESOLVED FIXED QA Contact: Nitin Dahyabhai <thatnitind>
Severity: enhancement    
Priority: P3 CC: nsand.dev, thatnitind
Version: 1.5Keywords: helpwanted
Target Milestone: 3.2 M5Flags: nsand.dev: review+
Hardware: PC   
OS: Windows XP   
Whiteboard:
Attachments:
Description Flags
Enhancment Patch
none
Enhancment and JUnits Patch
none
Enhancment and JUnits Patch Update 1
none
Enhancment and JUnits Patch Update 2 (Version 1)
none
Enhancment and JUnits Patch Update 2 (Version 2)
none
Enhancment and JUnits Patch Update 3 (Version 1)
none
Enhancment and JUnits Patch Update 3 (Version 2)
none
Enhancement and JUnits Patch Update 4 nsand.dev: iplog+

Description Amy Wu CLA 2006-02-28 23:35:22 EST
xml cleanup document has option to compress empty element tags
html cleanup document should offer that option as well
Comment 1 Nitin Dahyabhai CLA 2007-09-13 04:34:27 EDT
Considering for 3.0, although there may not be time.
Comment 2 Amy Wu CLA 2008-10-27 04:27:49 EDT
reassigning to inbox
Comment 3 Nick Sandonato CLA 2009-12-14 15:40:09 EST
Care should be taken when considering that in html, but not xhtml, some tags can simply be:

<br>
<li>
Comment 4 Ian Tewksbury CLA 2009-12-14 15:56:49 EST
Then maybe this should wait for Bug 258676 so that there is an XHTML content type that I can use to differentiate between XHTML and HTML.
Comment 5 Ian Tewksbury CLA 2009-12-16 15:32:10 EST
Created attachment 154593 [details]
Enhancment Patch

It turns out HTML already had the preference for this there was just no UI for it.  So this patch copies the UI implementation for the tag compression from the CleanupDialogXML and puts it into the CleanupDialogHTML and then duplicates the actual logic for compressing tags from the org.eclipse.wst.xml.core.internal.cleanup.ElementNodeCleanupHandler#compressEmptyElementTag and puts it org.eclipse.wst.html.core.internal.cleanup.ElementNodeCleanupHandler as well.  That's all it took to get the magic working.
Comment 6 Ian Tewksbury CLA 2009-12-16 15:33:31 EST
P.S. working on some JUnits
Comment 7 Ian Tewksbury CLA 2009-12-17 08:37:18 EST
Created attachment 154654 [details]
Enhancment and JUnits Patch

Same as the last patch only this time has some added junits.
Comment 8 Ian Tewksbury CLA 2009-12-18 08:18:27 EST
Created attachment 154767 [details]
Enhancment and JUnits Patch Update 1

Sometimes helps if after writing awesome JUnits you hook them into the JUnit suite....
Comment 9 Nick Sandonato CLA 2009-12-22 16:51:47 EST
Hey, Ian. While I think overall the patch is good, I've noticed that <div> tags in particular do not like to be compressed. I think this may have to do with isContainer() returning true for ElementImpl. Can this be an additional check before compressing the tags?
Comment 10 Ian Tewksbury CLA 2009-12-23 10:15:54 EST
(In reply to comment #9)
> Hey, Ian. While I think overall the patch is good, I've noticed that <div> tags
> in particular do not like to be compressed. I think this may have to do with
> isContainer() returning true for ElementImpl. Can this be an additional check
> before compressing the tags?

Do you mean DIV tags should not be compressed, or they should be and for you they are not being compressed?

I ask because when I run it on a document with empty div tags they all get compressed.  But if you were meaning to say some tags should be compressed and those are the ones whose "isContainer" returns true then that functionality could easily be added.
Comment 11 Ian Tewksbury CLA 2009-12-23 10:22:24 EST
(In reply to comment #10)
> (In reply to comment #9)
> > Hey, Ian. While I think overall the patch is good, I've noticed that <div> tags
> > in particular do not like to be compressed. I think this may have to do with
> > isContainer() returning true for ElementImpl. Can this be an additional check
> > before compressing the tags?
> 
> Do you mean DIV tags should not be compressed, or they should be and for you
> they are not being compressed?
> 
> I ask because when I run it on a document with empty div tags they all get
> compressed.  But if you were meaning to say some tags should be compressed and
> those are the ones whose "isContainer" returns true then that functionality
> could easily be added.

Nevermind, i now noticed that <div/> is reported with a "no end tag" error.  I'll track down the exact circumstances that say a tag can not be compressed and add that as a condition.
Comment 12 Ian Tewksbury CLA 2009-12-23 10:51:46 EST
Created attachment 154974 [details]
Enhancment and JUnits Patch Update 2 (Version 1)

There are handy helper utility methods in org.eclipse.wst.html.core.internal.validate.CMUtil for checking to see if a CMElementDeclaration for a IDOMElement says its allowed to omit its end tag.

This version of the patch copies the two needed helper methods from CMUtil and puts them in ElementNodeCleanupHandler becuase currently CMUtil is package protected.  I will attach another patch that makes CMUtil public so ElementNodeCleanupHandler can access it.

I can see a valid argument for both solutions so I will let you pick your favorite.
Comment 13 Ian Tewksbury CLA 2009-12-23 10:53:13 EST
Created attachment 154975 [details]
Enhancment and JUnits Patch Update 2 (Version 2)

Same functionality as the "version 1" patch except this one makes CMUtil public so ElementNodeCleanupHandler can access its helper methods.  Less duplicated code this way but it also means making CMUtil public.  So as I said in comment 12 I can arguments for both solutions.
Comment 14 Ian Tewksbury CLA 2009-12-23 11:01:02 EST
Created attachment 154976 [details]
Enhancment and JUnits Patch Update 3 (Version 1)

Same as patch described in comment #12 except I moved the model query call inside the if for checking if the tag is even one to be considered for compressing.  This saves on some execution time.
Comment 15 Ian Tewksbury CLA 2009-12-23 11:01:48 EST
Created attachment 154977 [details]
Enhancment and JUnits Patch Update 3 (Version 2)

Same as patch described in comment #13 except I moved the model query call inside the if for checking if the tag is even one to be considered for compressing.  This saves on some execution time.
Comment 16 Nick Sandonato CLA 2010-01-04 14:20:29 EST
Hey Ian, I think CMUtil.isEndTagOmissible() is being used incorrectly to determine if a tag can be changed from <tag></tag> to <tag />. My understanding of isEndTagOmissible() is that it allows for things like
<br>, <meta>, <li> etc. All of these can be written as such, with no closing or self-closing tag.

I think what you may need to do is if the document is not XHTML, then check if the Element is a container. If it's not a container, then I think you can compress it.
Comment 17 Ian Tewksbury CLA 2010-01-05 15:23:29 EST
Created attachment 155369 [details]
Enhancement and JUnits Patch Update 4

Changed logic to if the tag is either an XHTML tag or not a container then it will be compressed.
Comment 18 Nick Sandonato CLA 2010-01-08 14:35:00 EST
Changes checked in. Thanks, Ian.