elisp libxml-parse-html-region return NIL - elisp

Here my elisp code (test by url "http://httpbin.org/html")
(with-current-buffer (url-retrieve-synchronously my-url)
(goto-char (point-min))
(re-search-forward "^$")
(delete-region (point) (point-min))
(setq dom (libxml-parse-html-region (point-min) (point-max)))
(hoge--log-trace "Current buffer content: %s" (buffer-string))
(hoge--log-trace "DOM is: %s" dom)
)
To parse html I use elisp function "libxml-parse-html-region"
The result is:
Current buffer content:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>Herman Melville - Moby-Dick</h1>
<div>
<p>
Availing himself ...
</p>
</div>
</body>
</html>
DOM is: nil
As you can see the buffer has content, but elisp function libxml-parse-html-region return nill and as result DOM object is NIL. Why?

Related

Jasmine: How to test a user input?

I wrote the following html file:
<!DOCTYPE html> <html lang="en"> <head>
<meta charset="UTF-8">
<title></title> </head> <body>
<h1> HELLO WORLD </h1>
<button type="button"> CLICK HERE</button>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$('button').click(function() {
$('h1').text("Thank you");
var ans = prompt("Please type your age");
if (ans > 80) {
$('h1').text("You're old");
}
})
</script> </body> </html>
I want to test that once the user clicks on the button and gives an age older than 80, the page behaves as expected and the h1-tag text changes to "You're old".
My question is how to write a Jasmine spec to test this feature?
You need to use "Spy" object to intercept "prompt" function and return your own value. After check the caption text. The code may look like ...
deascribe("test inline function:", function() {
it("caption should be 'too old' for age of more than 80", function () {
spyOn(window, 'prompt').and.returnValue(81);
$('button').click();
expect($('h1').text()).toBe("You're old");
});
});

Golang template does not render html

I created a subfolder called 'views' in my web root directory. Within the View folder, I have the static folder which contains the css and js files.
The html pages are rendered when I have the html files in the web root. However they do not render when placed within the views folder. I am using template.ParseGlob to parse the file and ExecuteTemplate to render.
package main
import (
"html/template"
"net/http"
"github.com/gorilla/mux"
)
var router = mux.NewRouter()
var tmpl *template.Template
func init() {
tmpl = template.Must(template.ParseGlob("view/*.html"))
}
func indexPage(w http.ResponseWriter, r *http.Request) {
err := tmpl.ExecuteTemplate(w, "signin", nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func main() {
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
router.PathPrefix("/").Handler(http.StripPrefix("/", http.FileServer(http.Dir("view/"))))
router.HandleFunc("/", indexPage)
http.ListenAndServe(":8091", router)
}
HTML files: Have defined the header and footer in index.html which I refernce in the signin.html file
{{ define "header"}}
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>User Sign in</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="/static/css/bootstrap.min.css">
<link rel="stylesheet" href="/static/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="/static/css/main.css">
<script src="/static/js/vendor/modernizr-2.8.3-respond-1.4.2.min.js"></script>
</head>
<body>
{{end}}
{{define "footer"}}
<footer class="panel-footer"><p>© Company 2016</p></footer>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/vendor/jquery-1.11.2.min.js"><\/script>')</script>
<script src="/static/js/vendor/bootstrap.min.js"></script>
<script src="/static/js/main.js"></script>
<!-- Google Analytics: change UA-XXXXX-X to be your site's ID. -->
<script>
(function(b,o,i,l,e,r){b.GoogleAnalyticsObject=l;b[l]||(b[l]=
function(){(b[l].q=b[l].q||[]).push(arguments)});b[l].l=+new Date;
e=o.createElement(i);r=o.getElementsByTagName(i)[0];
e.src='//www.google-analytics.com/analytics.js';
r.parentNode.insertBefore(e,r)}(window,document,'script','ga'));
ga('create','UA-XXXXX-X','auto');ga('send','pageview');
</script>
</body>
</html>
{{end}}
signin.html file:
{{define "signin"}}
{{template "header" .}}
<h1 class="alert alert-info">Login</h1>
<div class="container">
{{with .Errors.message}}
<div class="alert alert-danger">
{{.}}
</div>
{{end}}
<form method="POST" action="/">
<label class="form-control" for="uname">User Name</label>
<input class="form-control" type="text" id="uname" name="uname">
<label class="form-control" for="password">Password</label>
<input class="form-control" type="password" id="password" name="password">
<button class="btn btn-info" type="submit">Submit</button>
</form>
{{template "footer" .}}
{{end}}
Why is it that this doesn't work when I place the html files in the sub-directory 'views'. The only thing that changes is the argument to parseGlob.
I believe all you need to do is remove this line:
router.PathPrefix("/").Handler(http.StripPrefix("/", http.FileServer(http.Dir("view/"))))
Works for me. Though you need to clean up your html a bit too - I see at least a missing </div>.
Templates are processed on the server and do not need to be served over internet. At the same time this route entry conflicts with the following one (indexPage) which defines another handler for the same route entry ("/"). So when you open it in a browser, server just send template files over internet. While indexPage handler is never called as directory handler is matched first.
Also you talking about "views" folder, but you code says "view" (without 's' at the end). Could be another simple reason.

Input field still accepts digits in Javascript

My code problem is that the input field for user name still accepts digits .
I tried to validate it with match() and (else if) but still not working.
The execution jumps to the (else) line.
please any help will be appreciated.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Functions in JS</title>
<script language="Javascript">
function showName() {
var entered=document.getElementById("name1").value;
var patt1 =/\[0-9]/g;
if( entered !==''){
document.getElementById("demo").innerHTML= entered;
}
else if( entered.match(patt1) ){
document.getElementById("demo").innerHTML="please enter a valid value";
}
else{
document.getElementById("demo").innerHTML="please enter value";
}
}
</script>
</head>
<body>
<form>
<label>Name:</label>
<input type="text" name="name1" id="name1" title="Only Alphabets" >
</form>
<input type="submit" onClick="showName();" >
<p><span id="demo"></span></p>
</body>
</html>
You have to test() your input against the pattern. From w3schools:
The test() method tests for a match in a string.
This method returns true if it finds a match, otherwise it returns
false.
<script language="Javascript">
function showName() {
var entered = document.getElementById("name1").value;
var pattern = /[0-9]+$/;
if(entered == ""){
document.getElementById("demo").innerHTML="Please enter a value";
} else if(pattern.test(entered)){
document.getElementById("demo").innerHTML="Please enter a valid value";
} else {
document.getElementById("demo").innerHTML=entered;
}
}
</script>
In my solution I'm only looking for two exceptions: No input and input contains a number. Everything else is allowed. You might need to change it to your needs :)
function showName() {
var entered=document.getElementById("name1").value;
var patt1 =/[0-9]+$/;
if(entered.match(patt1) ){
document.getElementById("demo").innerHTML='please enter a valid value '
}
else{
document.getElementById("demo").innerHTML= entered;
}
}
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Functions in JS</title>
<script language="Javascript">
</script>
</head>
<body>
<form>
<label>Name:</label>
<input type="text" name="name1" id="name1" title="Only Alphabets" >
</form>
<input type="submit" onClick="showName();" >
<p><span id="demo"></span></p>
https://plnkr.co/edit/j39qy0aXOlfhkCW8upVL?p=preview

D3 example not working

I just copy pasted this from the first example in here:
http://www.recursion.org/d3-for-mere-mortals/
<!DOCTYPE html>
<meta charset="utf-8">
<body>
<!--<script src="d3/d3.v3.js"></script>-->
<script src="/d3/d3.v3.js"></script>
<div>
<script>
var rectDemo = d3.select("#rect-demo").
append("svg:svg").
attr("width", 400).
attr("height", 300);
rectDemo.append("svg:rect").
attr("x", 100).
attr("y", 100).
attr("height", 100).
attr("width", 200);
d3.selectAll("body").append(rectDemo) ;
</script>
</div>
</body>
and it shows nothing on the page. I am sure I am doing something stupid here but this is my first example and can't figure out what is going it....
The script attempts to draw the rectangle in a svg element that's added inside the (already existing) element with id="rect-demo".
Therefore, you need to have an HTML element (for example a <div>) with the appropriate id:
<div id="rect-demo"> </div>
See this live demo: http://jsbin.com/enisen/2/edit

Can I put HTML into a variable?

Using the Sinatra library, I'm trying to condense two functions that display HTML code into a single function. Both these functions differ by only a small amount of HTML.
Here's an example.
def make_start_page()
<<EOS
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<p> Hello </p>
<img src="..." />
</body>
</html>
EOS
end
def make_guess_page()
<<EOS
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<p> Something different </p>
<a href="..." >1</a>
</body>
</html>
EOS
end
In the Ruby function that will call these two functions, I was wondering if it is possible to take the small portion of HTML that differs and pass it to a single, condensed version of these two functions that will display the page.
def handle()
if 1
var = "<p> Hello </p>
<img src="..." />"
elsif 2
var = "<p> Something different </p>
<a href="..." >1</a>"
make_start_guess_page(var)
end
You can interpolate variables in heredoc:
def make_start_page(var)
<<EOS
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
#{var}
</body>
</html>
EOS
end
For example.
There no reason why you could not do that. However if you want to print it, you'll probably have to use functions like String#html_safe in rails, or != in haml

Resources