【问题标题】:enlive remove html tag激活删除html标签
【发布时间】:2013-10-25 14:26:18
【问题描述】:

我有这个 html sn-p。我想解析那个 sn-p 并发出没有 javascript 标签的 html

<html>
    <body>
        <div class="content">lorem ipsum</div>
        <script  src="/js/jquery.js" type="text/javascript"></script>
        <script src="/js/bootstrap.min.js" type="text/javascript"></script>
    </body>
</html>

变成这样

<html>
    <body>
        <div class="content">lorem ipsum</div>
    </body>
</html>

我找不到删除标签的 enlive 辅助函数。

我找到了解决方案,感谢您提供的示例。于是我写了这段代码,js就消失了

(html/deftemplate template-about "../resources/public/build/about/index.html"
    []
    [:script] (fn js-clear [& args] nil)
)

【问题讨论】:

    标签: clojure enlive


    【解决方案1】:

    当我需要有条件地删除渲染页面的一些标签时,我通常的方法是使用nil returning 函数。

    例如

    (html/defsnippet upgrade-plan "page_templates/upgrade-plan.html" [:#upgradePlanSection]
      [pending-invoice ... ]
      ...
      [:#delayedPlanWarning] #(when pending-invoice
                                (html/at %
                                         [:#delayedPlanWarning] (html/remove-attr :style)
                                         [:.messagesText] (html/html-content (tower/t :plans/pending-invoice 
                                                                                    (:id pending-invoice)))))
      ...
    

    在这种特殊情况下,如果pending-invoicenil,则delayedPlanWarning 元素将从呈现的html 中删除,因为函数返回nil

    【讨论】:

      【解决方案2】:

      如果您不介意额外的解析和发射,以下会很好:

      (def orig (html-resource (java.io.StringReader. "<html>
      <body>
          <div class=\"content\">lorem ipsum</div>
          <script  src=\"/js/jquery.js\" type=\"text/javascript\"></script>
          <script src=\"/js/bootstrap.min.js\" type=\"text/javascript\"></script>
      </body>
      </html>")))
      (def stripped (transform orig [(keyword "script")] (substitute "")))
      (apply str (emit* stripped))
      

      【讨论】:

        【解决方案3】:

        删除整个标签时,只需要使用nil作为转换即可。

        (deftemplate tester1
                     (java.io.StringReader. "<html><body><div class=\"content\">...")
                     []
                     [:script] nil)
        
        (template1)
        

        在您的情况下,您可以将 StringReader 替换为您正在使用的资源。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-08-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-03-24
          • 1970-01-01
          • 1970-01-01
          • 2012-02-21
          相关资源
          最近更新 更多