Skip to content

Commit 15463b5

Browse files
committed
Add support for HAST@2.0.0 doctype node
1 parent f92d2ff commit 15463b5

File tree

4 files changed

+130
-4
lines changed

4 files changed

+130
-4
lines changed

lib/doctype.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* @author Titus Wormer
3+
* @copyright 2016 Titus Wormer
4+
* @license MIT
5+
* @module hast:to-html:doctype
6+
* @fileoverview Stringify doctypes.
7+
*/
8+
9+
'use strict';
10+
11+
/* eslint-env commonjs */
12+
13+
/* Expose. */
14+
module.exports = doctype;
15+
16+
/**
17+
* Stringify a doctype `node`.
18+
*
19+
* @param {Object} ctx - Stringify configuration.
20+
* @param {HastDoctype} node - Node.
21+
* @return {string} - Stringified `node`.
22+
*/
23+
function doctype(ctx, node) {
24+
var pub = node.public;
25+
var sys = node.system;
26+
var val = '<!DOCTYPE';
27+
28+
if (!node.name) {
29+
return val + '>';
30+
}
31+
32+
val += ' ' + node.name;
33+
34+
if (pub != null) {
35+
val += ' PUBLIC ' + smart(pub);
36+
} else if (sys != null) {
37+
val += ' SYSTEM';
38+
}
39+
40+
if (sys != null) {
41+
val += ' ' + smart(sys);
42+
}
43+
44+
return val + '>';
45+
}
46+
47+
function smart(value) {
48+
var quote = value.indexOf('"') === -1 ? '"' : '\'';
49+
return quote + value + quote;
50+
}

lib/one.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@ var handlers = {};
2222
handlers.root = require('./all');
2323
handlers.text = require('./text');
2424
handlers.element = require('./element');
25-
handlers.directive = require('./directive');
25+
handlers.doctype = require('./doctype');
2626
handlers.comment = require('./comment');
27-
handlers.characterData = require('./character-data');
2827
handlers.raw = require('./raw');
2928

29+
/* Deprecated */
30+
handlers.directive = require('./directive');
31+
handlers.characterData = require('./character-data');
32+
3033
/**
3134
* Stringify `node`.
3235
*

test/doctype.js

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* @author Titus Wormer
3+
* @copyright 2016 Titus Wormer
4+
* @license MIT
5+
* @module hast-util-to-html
6+
* @fileoverview Test suite for `hast-util-to-html`.
7+
*/
8+
9+
'use strict';
10+
11+
/* eslint-env node */
12+
13+
/* Dependencies. */
14+
var test = require('tape');
15+
var u = require('unist-builder');
16+
var to = require('..');
17+
18+
/* Tests. */
19+
test('`text`', function (t) {
20+
t.deepEqual(
21+
to(u('doctype')),
22+
'<!DOCTYPE>',
23+
'should stringify doctypes without `name`'
24+
);
25+
26+
t.deepEqual(
27+
to(u('doctype', {name: 'html'})),
28+
'<!DOCTYPE html>',
29+
'should stringify doctypes with `name`'
30+
);
31+
32+
t.deepEqual(
33+
to(u('doctype', {
34+
name: 'html',
35+
public: '-//W3C//DTD XHTML 1.0 Transitional//EN'
36+
})),
37+
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">',
38+
'should stringify doctypes with a public identifier'
39+
);
40+
41+
t.deepEqual(
42+
to(u('doctype', {
43+
name: 'html',
44+
system: 'about:legacy-compat'
45+
})),
46+
'<!DOCTYPE html SYSTEM "about:legacy-compat">',
47+
'should stringify doctypes with a system identifier'
48+
);
49+
50+
t.deepEqual(
51+
to(u('doctype', {
52+
name: 'html',
53+
public: '-//W3C//DTD HTML 4.01//',
54+
system: 'http://www.w3.org/TR/html4/strict.dtd'
55+
})),
56+
'<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//" "http://www.w3.org/TR/html4/strict.dtd">',
57+
'should stringify doctypes with both identifiers'
58+
);
59+
60+
t.deepEqual(
61+
to(u('doctype', {
62+
name: 'html',
63+
system: 'taco"'
64+
})),
65+
'<!DOCTYPE html SYSTEM \'taco"\'>',
66+
'should quote smartly'
67+
);
68+
69+
t.end();
70+
});

test/index.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@
1414
require('./core');
1515
require('./root');
1616
require('./comment');
17-
require('./character-data');
18-
require('./directive');
17+
require('./doctype');
1918
require('./text');
2019
require('./raw');
2120
require('./element');
2221
require('./attribute');
2322
require('./omission');
2423
require('./omission-opening');
2524
require('./omission-closing');
25+
26+
/* Legacy. */
27+
require('./character-data');
28+
require('./directive');

0 commit comments

Comments
 (0)