Source

botocss-preparse /

Filename Size Date modified Message
lib/net/sourceforge/cssbox/jstyleparser
src
26 B
1.4 KB
4.6 KB
1.1 KB
5.6 KB

Botocss

Pronounced 'botox'. Injects CSS into your HTML markup for sending via email. Guaranteed to take you back 10 years.

Usage

  1. Include Botocss in your project, either by downloading the JAR or using the Maven dependency:

    <dependency>
        <groupId>com.atlassian.botocss</groupId>
        <artifactId>botocss</artifactId>
        <version>1.3</version>
    </dependency>
    

    This dependency is hosted in the atlassian-contrib Maven repository.

  2. Use the Botocss.inject() method to inject CSS into your markup.

    // apply external stylesheet
    String markup = "<html><body><p>Hello, world!</p></body></html>";
    String styles = "p { margin: 10px; }";
    String output = Botocss.inject(markup, styles);
    
    // Botocss also applies internal styles
    String markupWithStyles = "<html>" +
        "<head><style>p { margin: 10px; }</style></head>" +
        "<body><p>Hello, world!</p></body>" + 
        "</html>";
    String output = Botocss.inject(markup);
    

Notes

Styles included in the document take precedence over those that appear in external stylesheets, as they would in CSS. Style blocks in the markup are left there for user agents that support them.

Styles are appended in order to the @style attribute on any matching elements. Botocss does not currently remove redundant styles, it relies on the user agent to disregard them.

Most common CSS selectors are supported: descendent/child/sibling element selectors (a b, a > b, a + b), id, class and attribute selectors (#id, .class, a[href]), and so on. The jsoup documentation has a complete list of supported selectors.

Unsupported selectors include :first-child and :last-child pseudo-selectors, and pseudo-selectors like :hover and :active which can't be applied via inline styles. Unsupported selectors are ignored during processing.

You can enable INFO or DEBUG logging for detailed information about the processing. The log category name is 'com.atlassian.botocss'.

Specificity is not currently respected. The order that styles appear in the stylesheet or document are the order they are applied. Any !important declarations are not respected either.

The HTML parsing and manipulation requires storing the HTML document in memory. Be aware that loading very large HTML documents may require a significant amount of memory. You probably don't want to send a massive chunk of HTML via email anyway.

Dependencies

Botocss bundles the jStyleparser library which is not available in a Maven repository and is licensed under the LGPL v3.

Botocss depends the following libraries at runtime, as noted in the project's POM:

  • jsoup - HTML parsing and manipulation - MIT license
  • slf4j-api (and a compatible sfl4j implementation at runtime) - MIT license
  • antlr-runtime (required by jStyleparser) - BSD license.

License

Botocss is licensed under the BSD license as follows. Note that the Botocss distribution contains the jStyleparser library which is LGPL-licensed as listed above.

Copyright (c) 2011 Atlassian. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  • The names of contributors may not be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.