代码之家  ›  专栏  ›  技术社区  ›  gavenkoa

Thymleaf 3、attoparser、CDATA和带有脚本标记的HTML模板

  •  0
  • gavenkoa  · 技术社区  · 7 年前

    我努力在SpringBoot1.3.5中传递HTML模板,它依赖于旧的Thymeleaf2.x系列 script 标签:

    <script type="text/template" id="catTmpl">
      <![CDATA[
      <b><%=name%></b>
      ]]>
    </script>
    

    这导致了错误:

    org.xml.sax.SAXParseException: The content of elements must consist of well-formed character data or markup.
    

    undescore.js 就我而言):

    _.template($("#catTmpl").html())
    

    我看到一些博客,发现Thymleaf 3使用了不同的解析器。选中的:

    $ gradle dependencies
    |    |    +--- org.thymeleaf:thymeleaf:3.0.6.RELEASE
    |    |    |    +--- org.attoparser:attoparser:2.0.4.RELEASE
    

    该解析器假设 包含CDATA和以上代码在没有CDATA的情况下可以正常工作。

    是什么 attoparser

    Thymeleaf 3准备好通过传递HTML模板了吗 (或HTML 5 template )标签没有CDATA废话?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Community CDub    4 年前

    attoparser http://www.attoparser.org/ 从项目主页:

    • 要快。与最快的标准解析器一样快。在许多情况下,速度更快。
    • 简化解析体验。通过消除在许多情况下都不需要担心的验证或实体解析。

    如邮件列表所示 阿托解析器 设计用于使用Thymeleaf。

    它能够解析HTML(因此无需关闭 <p> as say this blog post 解析器接受 <div/> <div></div> ,Thymeleaf 3不要。小心!

    根据 bug Modify the way is considered CDATA #9 阿托解析器 解释 script

    默认情况下,所有这些备选方案都不应被视为CDATA(除非它们显式包含在CDATA部分中),因此最好只在以下有关类型的情况下考虑CDATA的内容:

    • 无类型
    • 类型是以下类型之一:javascript、ecmascript、text/javascript、text/ecmascript、application/javascript、application/ecmascript