I have this html snippet. I want parse that snippet and emit html without javascript tags
<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>
become this
<html>
<body>
<div class="content">lorem ipsum</div>
</body>
</html>
I couldn't found enlive helper function to remove tags.
I have found the solution thanks for the example. So I write this code and the js disappeared
(html/deftemplate template-about "../resources/public/build/about/index.html"
[]
[:script] (fn js-clear [& args] nil)
)
My usual approach when I need to conditionally remove some tags of the rendered page is to use a nil returning function.
For instance
(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)))))
...
In that particular case if pending-invoice is nil the delayedPlanWarning element is removed from the rendered html since the function returns nil.
If you don't mind the extra parsing and emitting, the following would do nicely:
(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))
When removing whole tags, you only need to use nil as your transformation.
(deftemplate tester1
(java.io.StringReader. "<html><body><div class=\"content\">...")
[]
[:script] nil)
(template1)
In your case, you would replace the StringReader with the resource you are using.
Related
I need to remove the first element from a list (the head) and store the value. How would i go about doing that? Im trying to create a stack in sml and making the pop method
In Standard ML lists are equivalent to stacks. You can use hd to get the first element, and you can use tl to get the remaining stack. But hd and tl are partial functions that will fail if the stack is empty. A safer alternative is to use the 'a option type:
fun pop [] = NONE
| pop (top::stack) = SOME (top, stack)
Demonstrating its use:
- pop [1,2,3];
> val it = SOME(1, [2, 3]) : (int * int list) option
It sounds like you are at a level of learning where a tutorial or a textbook better suits your needs.
If you want to remove first element from an array means check below,
HTML
<!DOCTYPE html>
<html data-ng-app="demo">
<head>
<script data-require="angular.js#1.1.5" data-semver="1.1.5" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body>
<div data-ng-controller="DemoController">
<ul>
<li data-ng-repeat="item in items">
{{item}}
<button data-ng-click="removeItem($index)">Remove</button>
</li>
</ul>
<input data-ng-model="newItem"><button data-ng-click="addItem(newItem)">Add</button>
</div>
</body>
</html>
Javascript
"use strict";
var demo = angular.module("demo", []);
function DemoController($scope){
$scope.items = [
"potatoes",
"tomatoes",
"flour",
"sugar",
"salt"
];
$scope.addItem = function(item){
$scope.items.push(item);
$scope.newItem = null;
}
$scope.removeItem = function(index){
$scope.items.splice(index, 1);
}
}
How would I render an async script tag in hiccup?
<script src="demo_async.js" async></script>
I think you should just code it as
(use 'hiccup.core)
(html
[:script {:src "demo_async.js" :async "async"} ] )
=> <script src="demo_async.js" async="async" ></script>
Reference: http://www.w3schools.com/tags/tag_script.asp
Could anyone tell me how to produce a right-click event handler in Clojure? I am familiar with ":on-click" for simple clicks but not right or double clicks. Can't seem to find any helpful resources online. Thanks!
Frequently in ClojureScript the Google Closure library (Event Handling | Closure Library | Google Developers) is used instead of raw JS. The events (Closure Library API Documentation - JavaScript) namespace contains the goog.events.EventType enumeration which specifies each individual event type:
(ns test.core
(:require [goog.dom :as dom]
[goog.events :as events]))
(letfn [(menu-listener [event]
(.log js/console (str "contextmenu " (.-button event))))
(click-listener [event]
(let [btn (.-button event)
msg (if (= btn 2) "Right-click" (str "Button " btn))]
(.log js/console msg)))]
(events/listen (dom/getElement "click-target") "contextmenu" menu-listener)
(events/listen (dom/getElement "click-target") "click" click-listener))
;; src/test/core.cljs
.
<!DOCTYPE html>
<html>
<head>
<title>contextmenu</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<p id="click-target">Right click on me</p>
<script src="out/test.js" type="text/javascript"></script>
</body>
</html>
<!-- index.html -->
Observe:
A right-click (button 2) fires the contextmenu listener. The click listener doesn't get to see it (even if there is no contextmenu listener).
A second right-click will dismiss the context menu but neither listener is fired.
Using om I got right click as context-menu event. Button number is 2 for right button:
{:onContextMenu (fn [e]
(prn e (.-button e)))}
or in plain html+cljs:
<div id="btn">Click me</div>
(.addEventListener (.getElementById js/document "btn")
"contextmenu" (fn [e] (prn e (.-button e))))
https://developer.mozilla.org/en/docs/Web/API/MouseEvent
I'm new to Enlive. I found that I can iterate with clone-for, however, it works for single element. I want to generate a list of a pair of elements like the following:
<div>
item 1<br>
item 2<br>
...
</div>
I tried to select <a> and use clone-for, but end with following result:
<div>
item 1item 2......<br>
</div>
What do I do to repeat <a> with <br> in each iteration?
I think fragments will work in this case.
Try something along these lines:
(html/sniptest "<div>Label<br/></div>" {[:a] [:br]}
(clone-for [{label :label url :url} [{:label "Google" :url "http://www.google.com" }
{:label "Stack Overflow" :url "http://www.stackoverflow.com"}]]
[:a] (do-> (content label)
(set-attr :href url)))))
;; =>
<div>
Google<br />
Stack Overflow<br />
</div>
If you always want the full content of the div to be cloned (not just the fragment :a -> :br) then you can use first-child and last-child. Just change the {[:a] [:br]} selector above to {[:div first-child] [:div last-child]}.
This is my nhaml code
^ var title=""
!!! XML
!!! Strict
%html{xmlns="http://www.w3.org/1999/xhtml"}
%head
%title
Nhaml Master #{title}
_styles
%body
.page
%h1 = "hello world"
_
_scripts
The resulting HTML renders the last tags as such:
</div>
</body>
<script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.3.2.min.js" type="text/javascript">
</script>
</html>
As _scripts is still at the indentation level of %body +1, why does it close body down before rendering _scripts?
Does it work if you move the scripts above the partial? If so, then the partial may be to blame.
%h1 = "hello world"
_scripts
_