Message Boards

Move all <style> tags from <body> to <head>

Marcin Klinski, modified 3 Years ago.

Move all <style> tags from <body> to <head>

New Member Posts: 12 Join Date: 1/3/20 Recent Posts
Is there some possibility to move all <style> tags from <body> to <head> ?
Maybe using <liferay-util:buffer var="html">  and then:

&lt;% html = html.replaceAll("(?s)<style>.*?</style>", ""); %&gt;

That code remove all styles tags from output HTML but I need to cut and insert that tags in <head> section.
thumbnail
Christoph Rabel, modified 3 Years ago.

RE: Move all <style> tags from <body> to <head>

Liferay Legend Posts: 1554 Join Date: 9/24/09 Recent Posts
First question: Why? Is it really necessary?
Can you identify the places where this happening? Maybe you can "kill them" at the source. There is a tag, that adds things to the head of the page and maybe you can fix it where the tags are added.
If the style tags are inside of portlets then you could intercept them. Liferay renders all portlets and stores them in a buffer. Later, in the theme that buffer content is inserted into the page. You probably could get in there somewhere and modify that buffer or do the replacement. Please note that this will incur some performance penalty.
thumbnail
Olaf Kock, modified 3 Years ago.

RE: Move all <style> tags from <body> to <head>

Liferay Legend Posts: 6403 Join Date: 9/23/08 Recent Posts
Marcin Klinski:

Is there some possibility to move all <style> tags from <body> to <head> ?
Seconding Christoph.
You seem to be on a quest rewriting significant portions of the rendered HTML, and doing so you'll end up with unique opportunities to see failures that nobody else sees, and that nobody else will have a chance to help you on.
You've chosen a platform in order to take away a lot of infrastructure burden. I understand that you personally would have made different choices in various places. But you made the (wise) choice to build on a platform. Now you would have all the time in the world to work on actual business problems, rather than on infrastructure. But you choose not to, and worry about tiny technical beauty aspects.
In case it's not a beauty aspect, and you're on to something big and structurally important, I'd like to see the root problem discussed and fixed, rather than String.replace bandaid applied.