Search in sources :

Example 16 with IStandaloneElementTag

use of org.thymeleaf.model.IStandaloneElementTag in project thymeleaf-tests by thymeleaf.

the class ReplaceWithProcessableProcessingInstructionProcessor method doProcess.

@Override
protected void doProcess(final ITemplateContext context, final IProcessingInstruction processingInstruction, final IProcessingInstructionStructureHandler structureHandler) {
    final IModelFactory modelFactory = context.getModelFactory();
    final IStandaloneElementTag tag = modelFactory.createStandaloneElementTag("replaced", "th:text", "one", false, true);
    final IModel model = modelFactory.createModel(tag);
    structureHandler.replaceWith(model, true);
}
Also used : IModel(org.thymeleaf.model.IModel) IModelFactory(org.thymeleaf.model.IModelFactory) IStandaloneElementTag(org.thymeleaf.model.IStandaloneElementTag)

Example 17 with IStandaloneElementTag

use of org.thymeleaf.model.IStandaloneElementTag in project thymeleaf-tests by thymeleaf.

the class ReplaceWithProcessableTextProcessor method doProcess.

@Override
protected void doProcess(final ITemplateContext context, final IText text, final ITextStructureHandler structureHandler) {
    if (EngineEventUtils.isWhitespace(text) || text.getText().equals("one")) {
        return;
    }
    final IModelFactory modelFactory = context.getModelFactory();
    final IStandaloneElementTag tag = modelFactory.createStandaloneElementTag("replaced", "th:text", "one", false, true);
    final IModel model = modelFactory.createModel(tag);
    structureHandler.replaceWith(model, true);
}
Also used : IModel(org.thymeleaf.model.IModel) IModelFactory(org.thymeleaf.model.IModelFactory) IStandaloneElementTag(org.thymeleaf.model.IStandaloneElementTag)

Example 18 with IStandaloneElementTag

use of org.thymeleaf.model.IStandaloneElementTag in project thymeleaf by thymeleaf.

the class StandaloneElementTag method asEngineStandaloneElementTag.

// Meant to be called only from within the engine
static StandaloneElementTag asEngineStandaloneElementTag(final IStandaloneElementTag standaloneElementTag) {
    if (standaloneElementTag instanceof StandaloneElementTag) {
        return (StandaloneElementTag) standaloneElementTag;
    }
    final IAttribute[] originalAttributeArray = standaloneElementTag.getAllAttributes();
    final Attributes attributes;
    if (originalAttributeArray == null || originalAttributeArray.length == 0) {
        attributes = null;
    } else {
        // We will perform a deep cloning of the attributes into objects of the Attribute class, so that
        // we make sure absolutely all Attributes in the new event are under the engine's control
        final Attribute[] newAttributeArray = new Attribute[originalAttributeArray.length];
        for (int i = 0; i < originalAttributeArray.length; i++) {
            final IAttribute originalAttribute = originalAttributeArray[i];
            newAttributeArray[i] = new Attribute(originalAttribute.getAttributeDefinition(), originalAttribute.getAttributeCompleteName(), originalAttribute.getOperator(), originalAttribute.getValue(), originalAttribute.getValueQuotes(), originalAttribute.getTemplateName(), originalAttribute.getLine(), originalAttribute.getCol());
        }
        final String[] newInnerWhiteSpaces;
        if (newAttributeArray.length == 1) {
            newInnerWhiteSpaces = Attributes.DEFAULT_WHITE_SPACE_ARRAY;
        } else {
            newInnerWhiteSpaces = new String[newAttributeArray.length];
            Arrays.fill(newInnerWhiteSpaces, Attributes.DEFAULT_WHITE_SPACE);
        }
        attributes = new Attributes(newAttributeArray, newInnerWhiteSpaces);
    }
    return new StandaloneElementTag(standaloneElementTag.getTemplateMode(), standaloneElementTag.getElementDefinition(), standaloneElementTag.getElementCompleteName(), attributes, standaloneElementTag.isSynthetic(), standaloneElementTag.isMinimized(), standaloneElementTag.getTemplateName(), standaloneElementTag.getLine(), standaloneElementTag.getCol());
}
Also used : IAttribute(org.thymeleaf.model.IAttribute) IStandaloneElementTag(org.thymeleaf.model.IStandaloneElementTag) IAttribute(org.thymeleaf.model.IAttribute)

Example 19 with IStandaloneElementTag

use of org.thymeleaf.model.IStandaloneElementTag in project thymeleaf-tests by thymeleaf.

the class StandaloneElementTagTest method testHtmlStandaloneElementAttrManagement.

@Test
public void testHtmlStandaloneElementAttrManagement() {
    IStandaloneElementTag tag;
    tag = computeHtmlTag("<input>");
    Assert.assertEquals("<input>", tag.toString());
    tag = computeHtmlTag("<input type=\"text\">");
    Assert.assertEquals("<input type=\"text\">", tag.toString());
    tag = computeHtmlTag("<input type=\"text\"   value='hello!!!'>");
    Assert.assertEquals("<input type=\"text\"   value='hello!!!'>", tag.toString());
    tag.getAttributes().removeAttribute("type");
    Assert.assertEquals("<input value='hello!!!'>", tag.toString());
    tag.getAttributes().removeAttribute("value");
    Assert.assertEquals("<input>", tag.toString());
    tag = computeHtmlTag("<input type=\"text\"   value='hello!!!'    >");
    Assert.assertEquals("<input type=\"text\"   value='hello!!!'    >", tag.toString());
    tag.getAttributes().removeAttribute(null, "type");
    Assert.assertEquals("<input value='hello!!!'    >", tag.toString());
    tag.getAttributes().removeAttribute(null, "value");
    Assert.assertEquals("<input    >", tag.toString());
    tag = computeHtmlTag("<input type=\"text\"   value='hello!!!'    ba >");
    Assert.assertEquals("<input type=\"text\"   value='hello!!!'    ba >", tag.toString());
    tag.getAttributes().setAttribute("value", "bye! :(");
    Assert.assertEquals("<input type=\"text\"   value='bye! :('    ba >", tag.toString());
    tag.getAttributes().setAttribute("type", "one");
    Assert.assertEquals("<input type=\"one\"   value='bye! :('    ba >", tag.toString());
    tag.getAttributes().setAttribute("ba", "two");
    Assert.assertEquals("<input type=\"one\"   value='bye! :('    ba=\"two\" >", tag.toString());
    tag.getAttributes().setAttribute("ba", "three", ElementAttributes.ValueQuotes.SINGLE);
    Assert.assertEquals("<input type=\"one\"   value='bye! :('    ba='three' >", tag.toString());
    tag.getAttributes().setAttribute("ba", "four", ElementAttributes.ValueQuotes.NONE);
    Assert.assertEquals("<input type=\"one\"   value='bye! :('    ba=four >", tag.toString());
    tag.getAttributes().setAttribute("ba", "five");
    Assert.assertEquals("<input type=\"one\"   value='bye! :('    ba=five >", tag.toString());
    tag.getAttributes().setAttribute("ba", null);
    Assert.assertEquals("<input type=\"one\"   value='bye! :('    ba >", tag.toString());
    tag.getAttributes().setAttribute("ba", "six");
    Assert.assertEquals("<input type=\"one\"   value='bye! :('    ba=six >", tag.toString());
    tag = computeHtmlTag("<input type=\"text\"   value='hello!!!'    ba=twenty >");
    tag.getAttributes().setAttribute("ba", "thirty");
    Assert.assertEquals("<input type=\"text\"   value='hello!!!'    ba=thirty >", tag.toString());
    tag = computeHtmlTag("<input type=\"text\"   value='hello!!!'    ba=twenty ><p id='one'/>");
    Assert.assertEquals("<p id='one'/>", tag.toString());
}
Also used : IStandaloneElementTag(org.thymeleaf.model.IStandaloneElementTag) Test(org.junit.Test)

Aggregations

IStandaloneElementTag (org.thymeleaf.model.IStandaloneElementTag)19 IModelFactory (org.thymeleaf.model.IModelFactory)13 IModel (org.thymeleaf.model.IModel)12 Test (org.junit.Test)4 TemplateProcessingException (org.thymeleaf.exceptions.TemplateProcessingException)1 IAttribute (org.thymeleaf.model.IAttribute)1 ICloseElementTag (org.thymeleaf.model.ICloseElementTag)1 IOpenElementTag (org.thymeleaf.model.IOpenElementTag)1 IProcessableElementTag (org.thymeleaf.model.IProcessableElementTag)1 ITemplateEvent (org.thymeleaf.model.ITemplateEvent)1 IText (org.thymeleaf.model.IText)1 IElementModelProcessor (org.thymeleaf.processor.element.IElementModelProcessor)1 IElementProcessor (org.thymeleaf.processor.element.IElementProcessor)1 IElementTagProcessor (org.thymeleaf.processor.element.IElementTagProcessor)1