tags:

views:

182

answers:

2

I use sitemesh for page decoration in my web app.

I have a form where the contents of a textarea field is a complete html page.

The problem is that when Sitemesh parses this page it extracts the title, head and body from my textarea field and decorates the page with it.

<textarea name="page_content">
    <!-- tags below should not be parsed by Sitemesh -->
    <html>
         <head>...</head>
         <body>...</body>
    </html>
</textarea>

It seems that the Sitemesh page parser does care that it has already seen these tags in the page.

Do you have any ideas on how to prevent Sitemesh from parsing the content of the textarea?

A: 

I solved it myself. The solution is self evident when you examine the source code for HTMLPageParser

The trick is to write a custom PageParser that adds a new state with its own set of rules i.e no rules at all:

public class CustomPageParser extends HTMLPageParser {

    @Override
    protected void addUserDefinedRules(State html, PageBuilder page) {
        super.addUserDefinedRules(html, page);
        // Ensure that while in <textarea> tag, none of the other rules kick in.
        State textarea = new State();
        html.addRule(new StateTransitionRule("textarea", textarea));

    }
}
henrik_lundgren
+1  A: 

You can't put <tags> inside a textarea. It is completely invalid. Textarea elements are not ‘CDATA elements’ like <script> and <style>, any < you put inside them is real markup and not a string literal.

In practice browsers will usually let you get away with it (until you try to include another textarea inside, of course), but what you should be writing is:

<textarea name="page_content">
    &lt;html>
        ...
    &lt;/html>
</textarea>
bobince