Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can fit the following Java code in your grails application.</p> <pre><code> import java.io.IOException; import java.util.StringTokenizer; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; public class AutomaticCssInliner { public static void main(String[] args) throws IOException { final String style = "style"; final String html = "&lt;html&gt;" + "&lt;body&gt; &lt;style&gt;" + "body{background:#FFC} \n p{background:red}" + "body, p{font-weight:bold} &lt;/style&gt;" + "&lt;p&gt;...&lt;/p&gt; &lt;/body&gt; &lt;/html&gt;"; // Document doc = Jsoup.connect("http://mypage.com/inlineme.php").get(); Document doc = Jsoup.parse(html); Elements els = doc.select(style);// to get all the style elements for (Element e : els) { String styleRules = e.getAllElements().get(0).data().replaceAll( "\n", "").trim(), delims = "{}"; StringTokenizer st = new StringTokenizer(styleRules, delims); while (st.countTokens() &gt; 1) { String selector = st.nextToken(), properties = st.nextToken(); Elements selectedElements = doc.select(selector); for (Element selElem : selectedElements) { String oldProperties = selElem.attr(style); selElem.attr(style, oldProperties.length() &gt; 0 ? concatenateProperties( oldProperties, properties) : properties); } } e.remove(); } System.out.println(doc);// now we have the result html without the // styles tags, and the inline css in each // element } private static String concatenateProperties(String oldProp, String newProp) { oldProp = oldProp.trim(); if (!newProp.endsWith(";")) newProp += ";"; return newProp + oldProp; // The existing (old) properties should take precedence. } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload