|
| 1 | +/** |
| 2 | + * @author Titus Wormer |
| 3 | + * @copyright 2016 Titus Wormer |
| 4 | + * @license MIT |
| 5 | + * @module mdast-util-assert |
| 6 | + * @fileoverview Assert `unist` nodes. |
| 7 | + */ |
| 8 | + |
| 9 | +'use strict'; |
| 10 | + |
| 11 | +/* eslint-env commonjs */ |
| 12 | +/* eslint-disable babel/new-cap */ |
| 13 | + |
| 14 | +/* Dependencies. */ |
| 15 | +var assert = require('assert'); |
| 16 | +var array = require('x-is-array'); |
| 17 | +var zwitch = require('zwitch'); |
| 18 | +var mapz = require('mapz'); |
| 19 | +var unist = require('unist-util-assert'); |
| 20 | + |
| 21 | +/* Construct. */ |
| 22 | +var mdast = zwitch('type'); |
| 23 | + |
| 24 | +/* Expose. */ |
| 25 | +module.exports = exports = unist.wrap(mdast); |
| 26 | + |
| 27 | +exports.parent = unist.wrap(parent); |
| 28 | +exports.text = unist.text; |
| 29 | +exports.void = unist.void; |
| 30 | +exports.wrap = unist.wrap; |
| 31 | +exports.all = mapz(exports, {key: 'children', indices: false}); |
| 32 | + |
| 33 | +/* Core interface. */ |
| 34 | +mdast.invalid = mdast.unknown = unknown; |
| 35 | + |
| 36 | +/* Per-type handling. */ |
| 37 | +mdast.handlers = { |
| 38 | + root: unist.wrap(root), |
| 39 | + paragraph: exports.parent, |
| 40 | + blockquote: exports.parent, |
| 41 | + tableRow: exports.parent, |
| 42 | + tableCell: exports.parent, |
| 43 | + strong: exports.parent, |
| 44 | + emphasis: exports.parent, |
| 45 | + delete: exports.parent, |
| 46 | + listItem: unist.wrap(listItem), |
| 47 | + footnote: exports.parent, |
| 48 | + heading: unist.wrap(heading), |
| 49 | + text: exports.text, |
| 50 | + inlineCode: exports.text, |
| 51 | + yaml: exports.text, |
| 52 | + code: unist.wrap(code), |
| 53 | + thematicBreak: exports.void, |
| 54 | + break: exports.void, |
| 55 | + list: unist.wrap(list), |
| 56 | + footnoteDefinition: unist.wrap(footnoteDefinition), |
| 57 | + definition: unist.wrap(definition), |
| 58 | + link: unist.wrap(link), |
| 59 | + image: unist.wrap(image), |
| 60 | + linkReference: unist.wrap(linkReference), |
| 61 | + imageReference: unist.wrap(imageReference), |
| 62 | + footnoteReference: unist.wrap(footnoteReference), |
| 63 | + table: unist.wrap(table), |
| 64 | + html: exports.text |
| 65 | +}; |
| 66 | + |
| 67 | +function unknown(node, ancestor) { |
| 68 | + unist(node, ancestor); |
| 69 | +} |
| 70 | + |
| 71 | +function parent(node) { |
| 72 | + unist.parent(node); |
| 73 | + exports.all(node); |
| 74 | +} |
| 75 | + |
| 76 | +function root(node, ancestor) { |
| 77 | + parent(node); |
| 78 | + |
| 79 | + assert.equal(ancestor, undefined, '`root` should not have a parent'); |
| 80 | +} |
| 81 | + |
| 82 | +function list(node) { |
| 83 | + parent(node); |
| 84 | + |
| 85 | + if (node.loose != null) { |
| 86 | + assert.equal(typeof node.loose, 'boolean', '`loose` must be `boolean`'); |
| 87 | + } |
| 88 | + |
| 89 | + if (node.ordered != null) { |
| 90 | + assert.equal(typeof node.ordered, 'boolean', '`ordered` must be `boolean`'); |
| 91 | + } |
| 92 | + |
| 93 | + if (!node.ordered) { |
| 94 | + assert.ok(node.start == null, 'unordered lists must not have `start`'); |
| 95 | + } else if (node.start != null) { |
| 96 | + assert.equal(typeof node.start, 'number', 'ordered lists must have `start`'); |
| 97 | + assert.ok(node.start >= 0, '`start` must be gte `0`'); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +function listItem(node) { |
| 102 | + parent(node); |
| 103 | + |
| 104 | + if (node.loose != null) { |
| 105 | + assert.equal(typeof node.loose, 'boolean', '`loose` must be `boolean`'); |
| 106 | + } |
| 107 | + |
| 108 | + if (node.checked != null) { |
| 109 | + assert.equal(typeof node.checked, 'boolean', '`checked` must be `boolean`'); |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +function heading(node) { |
| 114 | + parent(node); |
| 115 | + |
| 116 | + assert.ok(node.depth > 0, '`depth` should be gte `1`'); |
| 117 | + assert.ok(node.depth <= 6, '`depth` should be lte `6`'); |
| 118 | +} |
| 119 | + |
| 120 | +function code(node) { |
| 121 | + unist.text(node); |
| 122 | + |
| 123 | + if (node.lang != null) { |
| 124 | + assert.equal(typeof node.lang, 'string', '`lang` must be `string`'); |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +function footnoteDefinition(node) { |
| 129 | + parent(node); |
| 130 | + |
| 131 | + assert.equal(typeof node.identifier, 'string', '`footnoteDefinition` must have `identifier`'); |
| 132 | +} |
| 133 | + |
| 134 | +function definition(node) { |
| 135 | + unist.void(node); |
| 136 | + |
| 137 | + assert.equal(typeof node.identifier, 'string', '`identifier` must be `string`'); |
| 138 | + |
| 139 | + if (node.url != null) { |
| 140 | + assert.equal(typeof node.url, 'string', '`url` must be `string`'); |
| 141 | + } |
| 142 | + |
| 143 | + if (node.title != null) { |
| 144 | + assert.equal(typeof node.title, 'string', '`title` must be `string`'); |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +function link(node) { |
| 149 | + parent(node); |
| 150 | + |
| 151 | + if (node.url != null) { |
| 152 | + assert.equal(typeof node.url, 'string', '`url` must be `string`'); |
| 153 | + } |
| 154 | + |
| 155 | + if (node.title != null) { |
| 156 | + assert.equal(typeof node.title, 'string', '`title` must be `string`'); |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +function image(node) { |
| 161 | + unist.void(node); |
| 162 | + |
| 163 | + if (node.url != null) { |
| 164 | + assert.equal(typeof node.url, 'string', '`url` must be `string`'); |
| 165 | + } |
| 166 | + |
| 167 | + if (node.alt != null) { |
| 168 | + assert.equal(typeof node.alt, 'string', '`alt` must be `string`'); |
| 169 | + } |
| 170 | + |
| 171 | + if (node.title != null) { |
| 172 | + assert.equal(typeof node.title, 'string', '`title` must be `string`'); |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +function linkReference(node) { |
| 177 | + parent(node); |
| 178 | + |
| 179 | + assert.equal(typeof node.identifier, 'string', '`identifier` must be `string`'); |
| 180 | + |
| 181 | + if (node.referenceType != null) { |
| 182 | + assert.notEqual( |
| 183 | + ['shortcut', 'collapsed', 'full'].indexOf(node.referenceType), |
| 184 | + -1, |
| 185 | + '`referenceType` must be `shortcut`, `collapsed`, or `full`' |
| 186 | + ); |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +function imageReference(node) { |
| 191 | + unist.void(node); |
| 192 | + |
| 193 | + assert.equal( |
| 194 | + typeof node.identifier, |
| 195 | + 'string', |
| 196 | + '`identifier` must be `string`' |
| 197 | + ); |
| 198 | + |
| 199 | + if (node.alt != null) { |
| 200 | + assert.equal(typeof node.alt, 'string', '`alt` must be `string`'); |
| 201 | + } |
| 202 | + |
| 203 | + if (node.referenceType != null) { |
| 204 | + assert.notEqual( |
| 205 | + ['shortcut', 'collapsed', 'full'].indexOf(node.referenceType), |
| 206 | + -1, |
| 207 | + '`referenceType` must be `shortcut`, `collapsed`, or `full`' |
| 208 | + ); |
| 209 | + } |
| 210 | +} |
| 211 | + |
| 212 | +function footnoteReference(node) { |
| 213 | + unist.void(node); |
| 214 | + |
| 215 | + assert.equal(typeof node.identifier, 'string', '`identifier` must be `string`'); |
| 216 | +} |
| 217 | + |
| 218 | +function table(node) { |
| 219 | + var align; |
| 220 | + var val; |
| 221 | + var length; |
| 222 | + var index; |
| 223 | + |
| 224 | + parent(node); |
| 225 | + |
| 226 | + align = node.align; |
| 227 | + |
| 228 | + if (align != null) { |
| 229 | + assert.ok(array(align), '`align` must be `array`'); |
| 230 | + |
| 231 | + length = align.length; |
| 232 | + index = -1; |
| 233 | + |
| 234 | + while (++index < length) { |
| 235 | + val = align[index]; |
| 236 | + |
| 237 | + if (val != null) { |
| 238 | + assert.notEqual( |
| 239 | + ['left', 'right', 'center'].indexOf(val), |
| 240 | + -1, |
| 241 | + 'each align in table must be `null, \'left\', \'right\', \'center\'`' |
| 242 | + ); |
| 243 | + } |
| 244 | + } |
| 245 | + } |
| 246 | +} |
0 commit comments