Search in sources :

Example 6 with MarkupElement

use of org.apache.wicket.markup.MarkupElement in project wicket by apache.

the class StyleAndScriptIdentifierTest method postProcess.

/**
 * https://issues.apache.org/jira/browse/WICKET-4453
 *
 * This test wraps rawMarkup in org.apache.wicket.util.string.JavaScriptUtils#SCRIPT_CONTENT_PREFIX
 * twice - once in Markup.of() and second in the explicit call to StyleAndScriptIdentifier.postProcess().
 * The second time it realizes that the element body is already wrapped and skips it.
 */
@Test
public void postProcess() {
    String rawMarkup = "<script>someJS()</script>";
    Markup createMarkupElementsMarkup = Markup.of(rawMarkup);
    Markup markup = new Markup(createMarkupElementsMarkup.getMarkupResourceStream());
    for (MarkupElement markupElement : createMarkupElementsMarkup) {
        markup.addMarkupElement(markupElement);
    }
    StyleAndScriptIdentifier filter = new StyleAndScriptIdentifier();
    filter.postProcess(markup);
    assertEquals("<script>\n/*<![CDATA[*/\nsomeJS()\n/*]]>*/\n</script>", markup.toString(true));
}
Also used : Markup(org.apache.wicket.markup.Markup) MarkupElement(org.apache.wicket.markup.MarkupElement) Test(org.junit.Test)

Example 7 with MarkupElement

use of org.apache.wicket.markup.MarkupElement in project wicket by apache.

the class Component method internalRender.

/**
 * Performs a render of this component as part of a Page level render process.
 */
private void internalRender() {
    // Make sure there is a markup available for the Component
    IMarkupFragment markup = getMarkup();
    if (markup == null) {
        throw new MarkupNotFoundException("Markup not found for Component: " + toString());
    }
    // MarkupStream is an Iterator for the markup
    MarkupStream markupStream = new MarkupStream(markup);
    MarkupElement elem = markup.get(0);
    if (elem instanceof ComponentTag) {
        // Guarantee that the markupStream is set and determineVisibility not yet tested
        // See WICKET-2049
        ((ComponentTag) elem).onBeforeRender(this, markupStream);
    }
    // and the isVisible property.
    if (determineVisibility()) {
        setFlag(FLAG_HAS_BEEN_RENDERED, true);
        // Rendering is beginning
        if (log.isDebugEnabled()) {
            log.debug("Begin render {}", this);
        }
        try {
            notifyBehaviorsComponentBeforeRender();
            onRender();
            notifyBehaviorsComponentRendered();
            // Component has been rendered
            rendered();
        } catch (RuntimeException ex) {
            onException(ex);
        }
        if (log.isDebugEnabled()) {
            log.debug("End render {}", this);
        }
    } else // elem is null when rendering a page
    if ((elem != null) && (elem instanceof ComponentTag)) {
        if (getFlag(FLAG_PLACEHOLDER)) {
            renderPlaceholderTag((ComponentTag) elem, getResponse());
        }
    }
}
Also used : ComponentTag(org.apache.wicket.markup.ComponentTag) MarkupStream(org.apache.wicket.markup.MarkupStream) MarkupNotFoundException(org.apache.wicket.markup.MarkupNotFoundException) IMarkupFragment(org.apache.wicket.markup.IMarkupFragment) MarkupElement(org.apache.wicket.markup.MarkupElement)

Example 8 with MarkupElement

use of org.apache.wicket.markup.MarkupElement in project wicket by apache.

the class BorderBehavior method beforeRender.

@Override
public void beforeRender(final Component component) {
    final MarkupStream stream = getMarkupStream(component);
    final Response response = component.getResponse();
    stream.setCurrentIndex(0);
    boolean insideBorderMarkup = false;
    while (stream.isCurrentIndexInsideTheStream()) {
        MarkupElement elem = stream.get();
        stream.next();
        if (elem instanceof WicketTag) {
            WicketTag wTag = (WicketTag) elem;
            if (!insideBorderMarkup) {
                if (wTag.isBorderTag() && wTag.isOpen()) {
                    insideBorderMarkup = true;
                    continue;
                } else {
                    throw new WicketRuntimeException("Unexpected tag encountered in markup of component border " + getClass().getName() + ". Tag: " + wTag.toString() + ", expected tag: <wicket:border>");
                }
            } else {
                if (wTag.isBodyTag()) {
                    break;
                } else {
                    throw new WicketRuntimeException("Unexpected tag encountered in markup of component border " + getClass().getName() + ". Tag: " + wTag.toString() + ", expected tag: <wicket:body> or </wicket:body>");
                }
            }
        }
        if (insideBorderMarkup) {
            response.write(elem.toCharSequence());
        }
    }
    if (!stream.isCurrentIndexInsideTheStream()) {
        throw new WicketRuntimeException("Markup for component border " + getClass().getName() + " ended prematurely, was expecting </wicket:border>");
    }
}
Also used : Response(org.apache.wicket.request.Response) WicketTag(org.apache.wicket.markup.WicketTag) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) MarkupStream(org.apache.wicket.markup.MarkupStream) MarkupElement(org.apache.wicket.markup.MarkupElement)

Example 9 with MarkupElement

use of org.apache.wicket.markup.MarkupElement in project wicket by apache.

the class OpenCloseTagExpander method nextElement.

@Override
public MarkupElement nextElement() throws ParseException {
    // Did we hold back an elem? Than return that first
    if (next != null) {
        MarkupElement rtn = next;
        next = null;
        return rtn;
    }
    return super.nextElement();
}
Also used : MarkupElement(org.apache.wicket.markup.MarkupElement)

Example 10 with MarkupElement

use of org.apache.wicket.markup.MarkupElement in project wicket by apache.

the class StyleAndScriptIdentifier method postProcess.

@Override
public void postProcess(Markup markup) {
    for (int i = 0; i < markup.size(); i++) {
        MarkupElement elem = markup.get(i);
        if (elem instanceof ComponentTag) {
            ComponentTag open = (ComponentTag) elem;
            if (shouldProcess(open)) {
                if (open.isOpen() && ((i + 2) < markup.size())) {
                    MarkupElement body = markup.get(i + 1);
                    MarkupElement tag2 = markup.get(i + 2);
                    if ((body instanceof RawMarkup) && (tag2 instanceof ComponentTag)) {
                        ComponentTag close = (ComponentTag) tag2;
                        if (close.closes(open)) {
                            String text = body.toString().trim();
                            if (shouldWrapInCdata(text)) {
                                text = JavaScriptUtils.SCRIPT_CONTENT_PREFIX + body.toString() + JavaScriptUtils.SCRIPT_CONTENT_SUFFIX;
                                markup.replace(i + 1, new RawMarkup(text));
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : RawMarkup(org.apache.wicket.markup.RawMarkup) ComponentTag(org.apache.wicket.markup.ComponentTag) MarkupElement(org.apache.wicket.markup.MarkupElement)

Aggregations

MarkupElement (org.apache.wicket.markup.MarkupElement)14 ComponentTag (org.apache.wicket.markup.ComponentTag)8 WicketTag (org.apache.wicket.markup.WicketTag)6 MarkupStream (org.apache.wicket.markup.MarkupStream)5 IMarkupFragment (org.apache.wicket.markup.IMarkupFragment)3 Test (org.junit.Test)3 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)2 Markup (org.apache.wicket.markup.Markup)2 MarkupException (org.apache.wicket.markup.MarkupException)2 MarkupNotFoundException (org.apache.wicket.markup.MarkupNotFoundException)2 Response (org.apache.wicket.request.Response)2 Component (org.apache.wicket.Component)1 MarkupContainer (org.apache.wicket.MarkupContainer)1 MarkupFragment (org.apache.wicket.markup.MarkupFragment)1 RawMarkup (org.apache.wicket.markup.RawMarkup)1 BorderMarkupSourcingStrategy (org.apache.wicket.markup.html.panel.BorderMarkupSourcingStrategy)1 AbstractMarkupFilter (org.apache.wicket.markup.parser.AbstractMarkupFilter)1