Как обработать пользовательские вложенные теги html на диалекте чабреца? - PullRequest
0 голосов
/ 28 марта 2019

В настоящее время я пишу пользовательский диалект с помощью thymeleaf 3.0.11.RELEASE для компонентов Bootstrap.

Процессор пользовательских тегов для модалов довольно сложен.

Структура тегов должна выглядеть следующим образомthis:

<thb:modal id="MyModal" size="default|small|large" title="Model Title" closeButton="true|false">
<!--
    size->          (optional, if not present, take default)
    closeButton->   (optional, if not present, take true)
    role->          (optional, if not present, take diaglog)
    tabindex ->     (optional, in not present, take '-1'
-->
    <thb:modal-body>
        <p>Modal body text goes here.</p>
    </thb:modal-body>

    <thb:modal-footer>
        <thb:button class="btn btn-primary">Save changes</thb:button>
        <thb:button class="btn btn-secondary" data-dismiss="modal">Close</thb:button>
    </thb:modal-footer>
</thb:modal>

Как видите, у меня есть пользовательские вложенные теги: <thb:modal-body> и <thb:modal-footer>.

Как мне их обработать, чтобы они выглядели так:

<div class="modal" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <p>Modal body text goes here.</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-primary">Save changes</button>
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

До сих пор у меня просто есть класс процессора для тега

public class BootstrapModalTagProcessor extends AbstractElementTagProcessor {

    private static String ELEMENT_NAME = "modal";

    public BootstrapModalTagProcessor(final String dialectPrefix, final int precedence) {
        super(TemplateMode.HTML, // This processor will apply only to HTML mode
                dialectPrefix,     // Prefix to be applied to name for matching
                ELEMENT_NAME,      // No tag name: match any tag name
                true,              // No prefix to be applied to tag name
                null,              // Name of the attribute that will be matched
                false,             // Apply dialect prefix to attribute name
                precedence);       // Remove the matched attribute afterwards
    }

    @Override
    protected void doProcess(ITemplateContext context, IProcessableElementTag tag, IElementTagStructureHandler structureHandler) {
        String id = HtmlEscape.escapeHtml5(tag.getAttribute("id").getValue());
        String title = HtmlEscape.escapeHtml5(tag.getAttribute("title").getValue());
        String size = HtmlEscape.escapeHtml5(Optional.ofNullable(tag.getAttribute("size")).map(IAttribute::getValue).orElse("default"));
        String role = HtmlEscape.escapeHtml5(Optional.ofNullable(tag.getAttribute("role")).map(IAttribute::getValue).orElse("dialog"));

        boolean closeButton = Boolean.parseBoolean(
                HtmlEscape.escapeHtml5(
                        Optional.ofNullable(tag.getAttribute("closeButton"))
                                .map(IAttribute::getValue)
                                .orElse("true")
                )
        );

        int tabindex = Integer.parseInt(
                HtmlEscape.escapeHtml5(
                        Optional.ofNullable(tag.getAttribute("tabindex"))
                                .map(IAttribute::getValue)
                                .orElse("-1")
                )
        );

        IModelFactory modelFactory = context.getModelFactory();

        /*
        <div class="modal" tabindex="-1" role="dialog">
        <div class="modal-dialog" role="document">
        <div class="modal-content">
        <div class="modal-header">
         */

        Map<String, String> attributes = Map.of(
                "id", id,
                "title", title,
                "size", size,
                "role", role);

        IModel model = closeButton ? modalWithCloseButton(modelFactory, attributes) : modalWithoutCloseButton(modelFactory, attributes);

        structureHandler.replaceWith(model, true); // true ==> processable by other thymeleaf dialect processors

    }
}

Возможно, в пакете org.thymeleaf.processor есть другой интерфейс, который я мог быпользуетесь?

Спасибо за помощь, ура.

...