Skip to content

Commit f6c34e4

Browse files
Constantin Krügeruniqueck
authored andcommitted
Fix: return empty title if no head title element is found (#16)
1 parent d85cd2f commit f6c34e4

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

lib/html/htmlPage.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ class HtmlPage {
2121
}
2222

2323
getTitle () {
24-
return this.content.querySelector('html head title').innerHTML
24+
const title = this.content.querySelector('html head title')
25+
if (title) {
26+
return title.innerHTML.trim()
27+
} else {
28+
return ''
29+
}
2530
}
2631

2732
getAllIds () {

test/node-unit/htmlPage.spec.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/* global describe it */
2+
'use strict'
3+
4+
const HtmlPage = require('../../lib/html/htmlPage')
5+
const chai = require('chai')
6+
const expect = chai.expect
7+
const HTMLParser = require('node-html-parser')
8+
9+
describe('htmlPage', () => {
10+
describe('title', () => {
11+
it('missing', () => {
12+
const htmlPage = new HtmlPage({
13+
content: HTMLParser.parse(`<!DOCTYPE html>
14+
<meta charset="utf-8">
15+
<script>location="url/to/page.html"</script>
16+
<meta http-equiv="refresh" content="0; url=url/to/page.html">
17+
<meta name="robots" content="noindex">
18+
<title>Redirect Notice</title>
19+
<h1>Redirect Notice</h1>
20+
<p>The page you requested has been relocated to <a href="url/to/page.html">url/to/page.html</a>.</p>
21+
`
22+
)
23+
})
24+
expect(htmlPage.getTitle()).eq('')
25+
})
26+
it('exists', () => {
27+
const htmlPage = new HtmlPage({
28+
content: HTMLParser.parse(`<!DOCTYPE html>
29+
<html lang="en">
30+
<head>
31+
<meta charset="utf-8">
32+
<meta name="viewport" content="width=device-width,initial-scale=1">
33+
<title>Long Title with spaces</title>
34+
<meta name="generator" content="Antora 3.1.9">
35+
<link rel="stylesheet" href="../../antora/css/site.css">
36+
<link rel="stylesheet" href="../../antora/css/custom.css">
37+
<link rel="stylesheet" href="../../antora/css/font-awesome.min.css">
38+
<link rel="stylesheet" href="../../antora/css/extra.css">
39+
<script>var uiRootPath = '../../antora'</script>
40+
</head>
41+
<body></body>
42+
</html>
43+
`
44+
)
45+
})
46+
expect(htmlPage.getTitle()).eq('Long Title with spaces')
47+
})
48+
it('long title with spaces around', () => {
49+
const htmlPage = new HtmlPage({
50+
content: HTMLParser.parse(`<!DOCTYPE html>
51+
<html lang="en">
52+
<head>
53+
<meta charset="utf-8">
54+
<meta name="viewport" content="width=device-width,initial-scale=1">
55+
<title> Long Title with spaces around </title>
56+
<meta name="generator" content="Antora 3.1.9">
57+
<link rel="stylesheet" href="../../antora/css/site.css">
58+
<link rel="stylesheet" href="../../antora/css/custom.css">
59+
<link rel="stylesheet" href="../../antora/css/font-awesome.min.css">
60+
<link rel="stylesheet" href="../../antora/css/extra.css">
61+
<script>var uiRootPath = '../../antora'</script>
62+
</head>
63+
<body></body>
64+
</html>
65+
`
66+
)
67+
})
68+
expect(htmlPage.getTitle()).eq('Long Title with spaces around')
69+
})
70+
})
71+
})

0 commit comments

Comments
 (0)