enlive删除html标签
问题描述:
我有这个html代码片段。我想分析该片段,并发出HTML没有JavaScript代码enlive删除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)
)
答
我通常的做法,当我需要有条件地删除呈现的页面的一些标签是使用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-invoice
的nil
delayedPlanWarning
元件从呈现的HTML去除,因为该函数返回nil
。
答
如果你不介意额外的解析和发射,下面会做很好:
(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))
答
当取下整个标签,你只需要使用nil
为您的转型。
(deftemplate tester1
(java.io.StringReader. "<html><body><div class=\"content\">...")
[]
[:script] nil)
(template1)
对于您的情况,您将用正在使用的资源替换StringReader
。
非常感谢 – calvin91