|
| 1 | +/** |
| 2 | + * @author Titus Wormer |
| 3 | + * @copyright 2016 Titus Wormer |
| 4 | + * @license MIT |
| 5 | + * @module hast-util-assert |
| 6 | + * @fileoverview Assert `hast` nodes. |
| 7 | + */ |
| 8 | + |
| 9 | +'use strict'; |
| 10 | + |
| 11 | +/* Dependencies. */ |
| 12 | +var assert = require('assert'); |
| 13 | +var zwitch = require('zwitch'); |
| 14 | +var mapz = require('mapz'); |
| 15 | +var unist = require('unist-util-assert'); |
| 16 | + |
| 17 | +/* Construct. */ |
| 18 | +var hast = zwitch('type'); |
| 19 | + |
| 20 | +/* Expose. */ |
| 21 | +module.exports = exports = unist.wrap(hast); |
| 22 | + |
| 23 | +exports.parent = unist.wrap(parent); |
| 24 | +exports.text = unist.text; |
| 25 | +exports.void = unist.void; |
| 26 | +exports.wrap = unist.wrap; |
| 27 | +exports.all = mapz(exports, {key: 'children', indices: false}); |
| 28 | + |
| 29 | +/* Core interface. */ |
| 30 | +hast.invalid = hast.unknown = unknown; |
| 31 | + |
| 32 | +/* Per-type handling. */ |
| 33 | +hast.handlers = { |
| 34 | + root: unist.wrap(root), |
| 35 | + element: unist.wrap(element), |
| 36 | + doctype: unist.wrap(doctype), |
| 37 | + comment: exports.text, |
| 38 | + text: exports.text |
| 39 | +}; |
| 40 | + |
| 41 | +function unknown(node, ancestor) { |
| 42 | + unist(node, ancestor); |
| 43 | +} |
| 44 | + |
| 45 | +function parent(node) { |
| 46 | + unist.parent(node); |
| 47 | + exports.all(node); |
| 48 | +} |
| 49 | + |
| 50 | +function root(node, ancestor) { |
| 51 | + parent(node); |
| 52 | + |
| 53 | + assert.equal(ancestor, undefined, '`root` should not have a parent'); |
| 54 | +} |
| 55 | + |
| 56 | +function element(node) { |
| 57 | + parent(node); |
| 58 | + |
| 59 | + assert.equal(typeof node.tagName, 'string', '`element` should have a `tagName`'); |
| 60 | + assert.notEqual(node.tagName, '', '`element.tagName` should not be empty'); |
| 61 | +} |
| 62 | + |
| 63 | +function doctype(node) { |
| 64 | + unist.void(node); |
| 65 | + |
| 66 | + assert.equal(typeof node.name, 'string', '`doctype` should have a `name`'); |
| 67 | + |
| 68 | + if (node.public != null) { |
| 69 | + assert.equal(typeof node.public, 'string', '`doctype.public` should be `string`'); |
| 70 | + } |
| 71 | + |
| 72 | + if (node.system != null) { |
| 73 | + assert.equal(typeof node.system, 'string', '`doctype.system` should be `string`'); |
| 74 | + } |
| 75 | +} |
0 commit comments