12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019 |
- 'use strict';
-
- var typeOf = require('kind-of');
- var utils = module.exports;
-
- /**
- * Returns true if the given value is a node.
- *
- * ```js
- * var Node = require('snapdragon-node');
- * var node = new Node({type: 'foo'});
- * console.log(utils.isNode(node)); //=> true
- * console.log(utils.isNode({})); //=> false
- * ```
- * @param {Object} `node` Instance of [snapdragon-node][]
- * @returns {Boolean}
- * @api public
- */
-
- utils.isNode = function(node) {
- return typeOf(node) === 'object' && node.isNode === true;
- };
-
- /**
- * Emit an empty string for the given `node`.
- *
- * ```js
- * // do nothing for beginning-of-string
- * snapdragon.compiler.set('bos', utils.noop);
- * ```
- * @param {Object} `node` Instance of [snapdragon-node][]
- * @returns {undefined}
- * @api public
- */
-
- utils.noop = function(node) {
- append(this, '', node);
- };
-
- /**
- * Appdend `node.val` to `compiler.output`, exactly as it was created
- * by the parser.
- *
- * ```js
- * snapdragon.compiler.set('text', utils.identity);
- * ```
- * @param {Object} `node` Instance of [snapdragon-node][]
- * @returns {undefined}
- * @api public
- */
-
- utils.identity = function(node) {
- append(this, node.val, node);
- };
-
- /**
- * Previously named `.emit`, this method appends the given `val`
- * to `compiler.output` for the given node. Useful when you know
- * what value should be appended advance, regardless of the actual
- * value of `node.val`.
- *
- * ```js
- * snapdragon.compiler
- * .set('i', function(node) {
- * this.mapVisit(node);
- * })
- * .set('i.open', utils.append('<i>'))
- * .set('i.close', utils.append('</i>'))
- * ```
- * @param {Object} `node` Instance of [snapdragon-node][]
- * @returns {Function} Returns a compiler middleware function.
- * @api public
- */
-
- utils.append = function(val) {
- return function(node) {
- append(this, val, node);
- };
- };
-
- /**
- * Used in compiler middleware, this onverts an AST node into
- * an empty `text` node and deletes `node.nodes` if it exists.
- * The advantage of this method is that, as opposed to completely
- * removing the node, indices will not need to be re-calculated
- * in sibling nodes, and nothing is appended to the output.
- *
- * ```js
- * utils.toNoop(node);
- * // convert `node.nodes` to the given value instead of deleting it
- * utils.toNoop(node, []);
- * ```
- * @param {Object} `node` Instance of [snapdragon-node][]
- * @param {Array} `nodes` Optionally pass a new `nodes` value, to replace the existing `node.nodes` array.
- * @api public
- */
-
- utils.toNoop = function(node, nodes) {
- if (nodes) {
- node.nodes = nodes;
- } else {
- delete node.nodes;
- node.type = 'text';
- node.val = '';
- }
- };
-
- /**
- * Visit `node` with the given `fn`. The built-in `.visit` method in snapdragon
- * automatically calls registered compilers, this allows you to pass a visitor
- * function.
- *
- * ```js
- * snapdragon.compiler.set('i', function(node) {
- * utils.visit(node, function(childNode) {
- * // do stuff with "childNode"
- * return childNode;
- * });
- * });
- * ```
- * @param {Object} `node` Instance of [snapdragon-node][]
- * @param {Function} `fn`
- * @return {Object} returns the node after recursively visiting all child nodes.
- * @api public
- */
-
- utils.visit = function(node, fn) {
- assert(utils.isNode(node), 'expected node to be an instance of Node');
- assert(isFunction(fn), 'expected a visitor function');
- fn(node);
- return node.nodes ? utils.mapVisit(node, fn) : node;
- };
-
- /**
- * Map [visit](#visit) the given `fn` over `node.nodes`. This is called by
- * [visit](#visit), use this method if you do not want `fn` to be called on
- * the first node.
- *
- * ```js
- * snapdragon.compiler.set('i', function(node) {
- * utils.mapVisit(node, function(childNode) {
- * // do stuff with "childNode"
- * return childNode;
- * });
- * });
- * ```
- * @param {Object} `node` Instance of [snapdragon-node][]
- * @param {Object} `options`
- * @param {Function} `fn`
- * @return {Object} returns the node
- * @api public
- */
-
- utils.mapVisit = function(node, fn) {
- assert(utils.isNode(node), 'expected node to be an instance of Node');
- assert(isArray(node.nodes), 'expected node.nodes to be an array');
- assert(isFunction(fn), 'expected a visitor function');
-
- for (var i = 0; i < node.nodes.length; i++) {
- utils.visit(node.nodes[i], fn);
- }
- return node;
- };
-
- /**
- * Unshift an `*.open` node onto `node.nodes`.
- *
- * ```js
- * var Node = require('snapdragon-node');
- * snapdragon.parser.set('brace', function(node) {
- * var match = this.match(/^{/);
- * if (match) {
- * var parent = new Node({type: 'brace'});
- * utils.addOpen(parent, Node);
- * console.log(parent.nodes[0]):
- * // { type: 'brace.open', val: '' };
- *
- * // push the parent "brace" node onto the stack
- * this.push(parent);
- *
- * // return the parent node, so it's also added to the AST
- * return brace;
- * }
- * });
- * ```
- * @param {Object} `node` Instance of [snapdragon-node][]
- * @param {Function} `Node` (required) Node constructor function from [snapdragon-node][].
- * @param {Function} `filter` Optionaly specify a filter function to exclude the node.
- * @return {Object} Returns the created opening node.
- * @api public
- */
-
- utils.addOpen = function(node, Node, val, filter) {
- assert(utils.isNode(node), 'expected node to be an instance of Node');
- assert(isFunction(Node), 'expected Node to be a constructor function');
-
- if (typeof val === 'function') {
- filter = val;
- val = '';
- }
-
- if (typeof filter === 'function' && !filter(node)) return;
- var open = new Node({ type: node.type + '.open', val: val});
- var unshift = node.unshift || node.unshiftNode;
- if (typeof unshift === 'function') {
- unshift.call(node, open);
- } else {
- utils.unshiftNode(node, open);
- }
- return open;
- };
-
- /**
- * Push a `*.close` node onto `node.nodes`.
- *
- * ```js
- * var Node = require('snapdragon-node');
- * snapdragon.parser.set('brace', function(node) {
- * var match = this.match(/^}/);
- * if (match) {
- * var parent = this.parent();
- * if (parent.type !== 'brace') {
- * throw new Error('missing opening: ' + '}');
- * }
- *
- * utils.addClose(parent, Node);
- * console.log(parent.nodes[parent.nodes.length - 1]):
- * // { type: 'brace.close', val: '' };
- *
- * // no need to return a node, since the parent
- * // was already added to the AST
- * return;
- * }
- * });
- * ```
- * @param {Object} `node` Instance of [snapdragon-node][]
- * @param {Function} `Node` (required) Node constructor function from [snapdragon-node][].
- * @param {Function} `filter` Optionaly specify a filter function to exclude the node.
- * @return {Object} Returns the created closing node.
- * @api public
- */
-
- utils.addClose = function(node, Node, val, filter) {
- assert(utils.isNode(node), 'expected node to be an instance of Node');
- assert(isFunction(Node), 'expected Node to be a constructor function');
-
- if (typeof val === 'function') {
- filter = val;
- val = '';
- }
-
- if (typeof filter === 'function' && !filter(node)) return;
- var close = new Node({ type: node.type + '.close', val: val});
- var push = node.push || node.pushNode;
- if (typeof push === 'function') {
- push.call(node, close);
- } else {
- utils.pushNode(node, close);
- }
- return close;
- };
-
- /**
- * Wraps the given `node` with `*.open` and `*.close` nodes.
- *
- * @param {Object} `node` Instance of [snapdragon-node][]
- * @param {Function} `Node` (required) Node constructor function from [snapdragon-node][].
- * @param {Function} `filter` Optionaly specify a filter function to exclude the node.
- * @return {Object} Returns the node
- * @api public
- */
-
- utils.wrapNodes = function(node, Node, filter) {
- assert(utils.isNode(node), 'expected node to be an instance of Node');
- assert(isFunction(Node), 'expected Node to be a constructor function');
-
- utils.addOpen(node, Node, filter);
- utils.addClose(node, Node, filter);
- return node;
- };
-
- /**
- * Push the given `node` onto `parent.nodes`, and set `parent` as `node.parent.
- *
- * ```js
- * var parent = new Node({type: 'foo'});
- * var node = new Node({type: 'bar'});
- * utils.pushNode(parent, node);
- * console.log(parent.nodes[0].type) // 'bar'
- * console.log(node.parent.type) // 'foo'
- * ```
- * @param {Object} `parent`
- * @param {Object} `node` Instance of [snapdragon-node][]
- * @return {Object} Returns the child node
- * @api public
- */
-
- utils.pushNode = function(parent, node) {
- assert(utils.isNode(parent), 'expected parent node to be an instance of Node');
- assert(utils.isNode(node), 'expected node to be an instance of Node');
-
- node.define('parent', parent);
- parent.nodes = parent.nodes || [];
- parent.nodes.push(node);
- return node;
- };
-
- /**
- * Unshift `node` onto `parent.nodes`, and set `parent` as `node.parent.
- *
- * ```js
- * var parent = new Node({type: 'foo'});
- * var node = new Node({type: 'bar'});
- * utils.unshiftNode(parent, node);
- * console.log(parent.nodes[0].type) // 'bar'
- * console.log(node.parent.type) // 'foo'
- * ```
- * @param {Object} `parent`
- * @param {Object} `node` Instance of [snapdragon-node][]
- * @return {undefined}
- * @api public
- */
-
- utils.unshiftNode = function(parent, node) {
- assert(utils.isNode(parent), 'expected parent node to be an instance of Node');
- assert(utils.isNode(node), 'expected node to be an instance of Node');
-
- node.define('parent', parent);
- parent.nodes = parent.nodes || [];
- parent.nodes.unshift(node);
- };
-
- /**
- * Pop the last `node` off of `parent.nodes`. The advantage of
- * using this method is that it checks for `node.nodes` and works
- * with any version of `snapdragon-node`.
- *
- * ```js
- * var parent = new Node({type: 'foo'});
- * utils.pushNode(parent, new Node({type: 'foo'}));
- * utils.pushNode(parent, new Node({type: 'bar'}));
- * utils.pushNode(parent, new Node({type: 'baz'}));
- * console.log(parent.nodes.length); //=> 3
- * utils.popNode(parent);
- * console.log(parent.nodes.length); //=> 2
- * ```
- * @param {Object} `parent`
- * @param {Object} `node` Instance of [snapdragon-node][]
- * @return {Number|Undefined} Returns the length of `node.nodes` or undefined.
- * @api public
- */
-
- utils.popNode = function(node) {
- assert(utils.isNode(node), 'expected node to be an instance of Node');
- if (typeof node.pop === 'function') {
- return node.pop();
- }
- return node.nodes && node.nodes.pop();
- };
-
- /**
- * Shift the first `node` off of `parent.nodes`. The advantage of
- * using this method is that it checks for `node.nodes` and works
- * with any version of `snapdragon-node`.
- *
- * ```js
- * var parent = new Node({type: 'foo'});
- * utils.pushNode(parent, new Node({type: 'foo'}));
- * utils.pushNode(parent, new Node({type: 'bar'}));
- * utils.pushNode(parent, new Node({type: 'baz'}));
- * console.log(parent.nodes.length); //=> 3
- * utils.shiftNode(parent);
- * console.log(parent.nodes.length); //=> 2
- * ```
- * @param {Object} `parent`
- * @param {Object} `node` Instance of [snapdragon-node][]
- * @return {Number|Undefined} Returns the length of `node.nodes` or undefined.
- * @api public
- */
-
- utils.shiftNode = function(node) {
- assert(utils.isNode(node), 'expected node to be an instance of Node');
- if (typeof node.shift === 'function') {
- return node.shift();
- }
- return node.nodes && node.nodes.shift();
- };
-
- /**
- * Remove the specified `node` from `parent.nodes`.
- *
- * ```js
- * var parent = new Node({type: 'abc'});
- * var foo = new Node({type: 'foo'});
- * utils.pushNode(parent, foo);
- * utils.pushNode(parent, new Node({type: 'bar'}));
- * utils.pushNode(parent, new Node({type: 'baz'}));
- * console.log(parent.nodes.length); //=> 3
- * utils.removeNode(parent, foo);
- * console.log(parent.nodes.length); //=> 2
- * ```
- * @param {Object} `parent`
- * @param {Object} `node` Instance of [snapdragon-node][]
- * @return {Object|undefined} Returns the removed node, if successful, or undefined if it does not exist on `parent.nodes`.
- * @api public
- */
-
- utils.removeNode = function(parent, node) {
- assert(utils.isNode(parent), 'expected parent.node to be an instance of Node');
- assert(utils.isNode(node), 'expected node to be an instance of Node');
-
- if (!parent.nodes) {
- return null;
- }
-
- if (typeof parent.remove === 'function') {
- return parent.remove(node);
- }
-
- var idx = parent.nodes.indexOf(node);
- if (idx !== -1) {
- return parent.nodes.splice(idx, 1);
- }
- };
-
- /**
- * Returns true if `node.type` matches the given `type`. Throws a
- * `TypeError` if `node` is not an instance of `Node`.
- *
- * ```js
- * var Node = require('snapdragon-node');
- * var node = new Node({type: 'foo'});
- * console.log(utils.isType(node, 'foo')); // false
- * console.log(utils.isType(node, 'bar')); // true
- * ```
- * @param {Object} `node` Instance of [snapdragon-node][]
- * @param {String} `type`
- * @return {Boolean}
- * @api public
- */
-
- utils.isType = function(node, type) {
- assert(utils.isNode(node), 'expected node to be an instance of Node');
- switch (typeOf(type)) {
- case 'array':
- var types = type.slice();
- for (var i = 0; i < types.length; i++) {
- if (utils.isType(node, types[i])) {
- return true;
- }
- }
- return false;
- case 'string':
- return node.type === type;
- case 'regexp':
- return type.test(node.type);
- default: {
- throw new TypeError('expected "type" to be an array, string or regexp');
- }
- }
- };
-
- /**
- * Returns true if the given `node` has the given `type` in `node.nodes`.
- * Throws a `TypeError` if `node` is not an instance of `Node`.
- *
- * ```js
- * var Node = require('snapdragon-node');
- * var node = new Node({
- * type: 'foo',
- * nodes: [
- * new Node({type: 'bar'}),
- * new Node({type: 'baz'})
- * ]
- * });
- * console.log(utils.hasType(node, 'xyz')); // false
- * console.log(utils.hasType(node, 'baz')); // true
- * ```
- * @param {Object} `node` Instance of [snapdragon-node][]
- * @param {String} `type`
- * @return {Boolean}
- * @api public
- */
-
- utils.hasType = function(node, type) {
- assert(utils.isNode(node), 'expected node to be an instance of Node');
- if (!Array.isArray(node.nodes)) return false;
- for (var i = 0; i < node.nodes.length; i++) {
- if (utils.isType(node.nodes[i], type)) {
- return true;
- }
- }
- return false;
- };
-
- /**
- * Returns the first node from `node.nodes` of the given `type`
- *
- * ```js
- * var node = new Node({
- * type: 'foo',
- * nodes: [
- * new Node({type: 'text', val: 'abc'}),
- * new Node({type: 'text', val: 'xyz'})
- * ]
- * });
- *
- * var textNode = utils.firstOfType(node.nodes, 'text');
- * console.log(textNode.val);
- * //=> 'abc'
- * ```
- * @param {Array} `nodes`
- * @param {String} `type`
- * @return {Object|undefined} Returns the first matching node or undefined.
- * @api public
- */
-
- utils.firstOfType = function(nodes, type) {
- for (var i = 0; i < nodes.length; i++) {
- var node = nodes[i];
- if (utils.isType(node, type)) {
- return node;
- }
- }
- };
-
- /**
- * Returns the node at the specified index, or the first node of the
- * given `type` from `node.nodes`.
- *
- * ```js
- * var node = new Node({
- * type: 'foo',
- * nodes: [
- * new Node({type: 'text', val: 'abc'}),
- * new Node({type: 'text', val: 'xyz'})
- * ]
- * });
- *
- * var nodeOne = utils.findNode(node.nodes, 'text');
- * console.log(nodeOne.val);
- * //=> 'abc'
- *
- * var nodeTwo = utils.findNode(node.nodes, 1);
- * console.log(nodeTwo.val);
- * //=> 'xyz'
- * ```
- *
- * @param {Array} `nodes`
- * @param {String|Number} `type` Node type or index.
- * @return {Object} Returns a node or undefined.
- * @api public
- */
-
- utils.findNode = function(nodes, type) {
- if (!Array.isArray(nodes)) {
- return null;
- }
- if (typeof type === 'number') {
- return nodes[type];
- }
- return utils.firstOfType(nodes, type);
- };
-
- /**
- * Returns true if the given node is an "*.open" node.
- *
- * ```js
- * var Node = require('snapdragon-node');
- * var brace = new Node({type: 'brace'});
- * var open = new Node({type: 'brace.open'});
- * var close = new Node({type: 'brace.close'});
- *
- * console.log(utils.isOpen(brace)); // false
- * console.log(utils.isOpen(open)); // true
- * console.log(utils.isOpen(close)); // false
- * ```
- * @param {Object} `node` Instance of [snapdragon-node][]
- * @return {Boolean}
- * @api public
- */
-
- utils.isOpen = function(node) {
- assert(utils.isNode(node), 'expected node to be an instance of Node');
- return node.type.slice(-5) === '.open';
- };
-
- /**
- * Returns true if the given node is a "*.close" node.
- *
- * ```js
- * var Node = require('snapdragon-node');
- * var brace = new Node({type: 'brace'});
- * var open = new Node({type: 'brace.open'});
- * var close = new Node({type: 'brace.close'});
- *
- * console.log(utils.isClose(brace)); // false
- * console.log(utils.isClose(open)); // false
- * console.log(utils.isClose(close)); // true
- * ```
- * @param {Object} `node` Instance of [snapdragon-node][]
- * @return {Boolean}
- * @api public
- */
-
- utils.isClose = function(node) {
- assert(utils.isNode(node), 'expected node to be an instance of Node');
- return node.type.slice(-6) === '.close';
- };
-
- /**
- * Returns true if `node.nodes` **has** an `.open` node
- *
- * ```js
- * var Node = require('snapdragon-node');
- * var brace = new Node({
- * type: 'brace',
- * nodes: []
- * });
- *
- * var open = new Node({type: 'brace.open'});
- * console.log(utils.hasOpen(brace)); // false
- *
- * brace.pushNode(open);
- * console.log(utils.hasOpen(brace)); // true
- * ```
- * @param {Object} `node` Instance of [snapdragon-node][]
- * @return {Boolean}
- * @api public
- */
-
- utils.hasOpen = function(node) {
- assert(utils.isNode(node), 'expected node to be an instance of Node');
- var first = node.first || node.nodes ? node.nodes[0] : null;
- if (utils.isNode(first)) {
- return first.type === node.type + '.open';
- }
- return false;
- };
-
- /**
- * Returns true if `node.nodes` **has** a `.close` node
- *
- * ```js
- * var Node = require('snapdragon-node');
- * var brace = new Node({
- * type: 'brace',
- * nodes: []
- * });
- *
- * var close = new Node({type: 'brace.close'});
- * console.log(utils.hasClose(brace)); // false
- *
- * brace.pushNode(close);
- * console.log(utils.hasClose(brace)); // true
- * ```
- * @param {Object} `node` Instance of [snapdragon-node][]
- * @return {Boolean}
- * @api public
- */
-
- utils.hasClose = function(node) {
- assert(utils.isNode(node), 'expected node to be an instance of Node');
- var last = node.last || node.nodes ? node.nodes[node.nodes.length - 1] : null;
- if (utils.isNode(last)) {
- return last.type === node.type + '.close';
- }
- return false;
- };
-
- /**
- * Returns true if `node.nodes` has both `.open` and `.close` nodes
- *
- * ```js
- * var Node = require('snapdragon-node');
- * var brace = new Node({
- * type: 'brace',
- * nodes: []
- * });
- *
- * var open = new Node({type: 'brace.open'});
- * var close = new Node({type: 'brace.close'});
- * console.log(utils.hasOpen(brace)); // false
- * console.log(utils.hasClose(brace)); // false
- *
- * brace.pushNode(open);
- * brace.pushNode(close);
- * console.log(utils.hasOpen(brace)); // true
- * console.log(utils.hasClose(brace)); // true
- * ```
- * @param {Object} `node` Instance of [snapdragon-node][]
- * @return {Boolean}
- * @api public
- */
-
- utils.hasOpenAndClose = function(node) {
- return utils.hasOpen(node) && utils.hasClose(node);
- };
-
- /**
- * Push the given `node` onto the `state.inside` array for the
- * given type. This array is used as a specialized "stack" for
- * only the given `node.type`.
- *
- * ```js
- * var state = { inside: {}};
- * var node = new Node({type: 'brace'});
- * utils.addType(state, node);
- * console.log(state.inside);
- * //=> { brace: [{type: 'brace'}] }
- * ```
- * @param {Object} `state` The `compiler.state` object or custom state object.
- * @param {Object} `node` Instance of [snapdragon-node][]
- * @return {Array} Returns the `state.inside` stack for the given type.
- * @api public
- */
-
- utils.addType = function(state, node) {
- assert(utils.isNode(node), 'expected node to be an instance of Node');
- assert(isObject(state), 'expected state to be an object');
-
- var type = node.parent
- ? node.parent.type
- : node.type.replace(/\.open$/, '');
-
- if (!state.hasOwnProperty('inside')) {
- state.inside = {};
- }
- if (!state.inside.hasOwnProperty(type)) {
- state.inside[type] = [];
- }
-
- var arr = state.inside[type];
- arr.push(node);
- return arr;
- };
-
- /**
- * Remove the given `node` from the `state.inside` array for the
- * given type. This array is used as a specialized "stack" for
- * only the given `node.type`.
- *
- * ```js
- * var state = { inside: {}};
- * var node = new Node({type: 'brace'});
- * utils.addType(state, node);
- * console.log(state.inside);
- * //=> { brace: [{type: 'brace'}] }
- * utils.removeType(state, node);
- * //=> { brace: [] }
- * ```
- * @param {Object} `state` The `compiler.state` object or custom state object.
- * @param {Object} `node` Instance of [snapdragon-node][]
- * @return {Array} Returns the `state.inside` stack for the given type.
- * @api public
- */
-
- utils.removeType = function(state, node) {
- assert(utils.isNode(node), 'expected node to be an instance of Node');
- assert(isObject(state), 'expected state to be an object');
-
- var type = node.parent
- ? node.parent.type
- : node.type.replace(/\.close$/, '');
-
- if (state.inside.hasOwnProperty(type)) {
- return state.inside[type].pop();
- }
- };
-
- /**
- * Returns true if `node.val` is an empty string, or `node.nodes` does
- * not contain any non-empty text nodes.
- *
- * ```js
- * var node = new Node({type: 'text'});
- * utils.isEmpty(node); //=> true
- * node.val = 'foo';
- * utils.isEmpty(node); //=> false
- * ```
- * @param {Object} `node` Instance of [snapdragon-node][]
- * @param {Function} `fn`
- * @return {Boolean}
- * @api public
- */
-
- utils.isEmpty = function(node, fn) {
- assert(utils.isNode(node), 'expected node to be an instance of Node');
-
- if (!Array.isArray(node.nodes)) {
- if (node.type !== 'text') {
- return true;
- }
- if (typeof fn === 'function') {
- return fn(node, node.parent);
- }
- return !utils.trim(node.val);
- }
-
- for (var i = 0; i < node.nodes.length; i++) {
- var child = node.nodes[i];
- if (utils.isOpen(child) || utils.isClose(child)) {
- continue;
- }
- if (!utils.isEmpty(child, fn)) {
- return false;
- }
- }
-
- return true;
- };
-
- /**
- * Returns true if the `state.inside` stack for the given type exists
- * and has one or more nodes on it.
- *
- * ```js
- * var state = { inside: {}};
- * var node = new Node({type: 'brace'});
- * console.log(utils.isInsideType(state, 'brace')); //=> false
- * utils.addType(state, node);
- * console.log(utils.isInsideType(state, 'brace')); //=> true
- * utils.removeType(state, node);
- * console.log(utils.isInsideType(state, 'brace')); //=> false
- * ```
- * @param {Object} `state`
- * @param {String} `type`
- * @return {Boolean}
- * @api public
- */
-
- utils.isInsideType = function(state, type) {
- assert(isObject(state), 'expected state to be an object');
- assert(isString(type), 'expected type to be a string');
-
- if (!state.hasOwnProperty('inside')) {
- return false;
- }
-
- if (!state.inside.hasOwnProperty(type)) {
- return false;
- }
-
- return state.inside[type].length > 0;
- };
-
- /**
- * Returns true if `node` is either a child or grand-child of the given `type`,
- * or `state.inside[type]` is a non-empty array.
- *
- * ```js
- * var state = { inside: {}};
- * var node = new Node({type: 'brace'});
- * var open = new Node({type: 'brace.open'});
- * console.log(utils.isInside(state, open, 'brace')); //=> false
- * utils.pushNode(node, open);
- * console.log(utils.isInside(state, open, 'brace')); //=> true
- * ```
- * @param {Object} `state` Either the `compiler.state` object, if it exists, or a user-supplied state object.
- * @param {Object} `node` Instance of [snapdragon-node][]
- * @param {String} `type` The `node.type` to check for.
- * @return {Boolean}
- * @api public
- */
-
- utils.isInside = function(state, node, type) {
- assert(utils.isNode(node), 'expected node to be an instance of Node');
- assert(isObject(state), 'expected state to be an object');
-
- if (Array.isArray(type)) {
- for (var i = 0; i < type.length; i++) {
- if (utils.isInside(state, node, type[i])) {
- return true;
- }
- }
- return false;
- }
-
- var parent = node.parent;
- if (typeof type === 'string') {
- return (parent && parent.type === type) || utils.isInsideType(state, type);
- }
-
- if (typeOf(type) === 'regexp') {
- if (parent && parent.type && type.test(parent.type)) {
- return true;
- }
-
- var keys = Object.keys(state.inside);
- var len = keys.length;
- var idx = -1;
- while (++idx < len) {
- var key = keys[idx];
- var val = state.inside[key];
-
- if (Array.isArray(val) && val.length !== 0 && type.test(key)) {
- return true;
- }
- }
- }
- return false;
- };
-
- /**
- * Get the last `n` element from the given `array`. Used for getting
- * a node from `node.nodes.`
- *
- * @param {Array} `array`
- * @param {Number} `n`
- * @return {undefined}
- * @api public
- */
-
- utils.last = function(arr, n) {
- return arr[arr.length - (n || 1)];
- };
-
- /**
- * Cast the given `val` to an array.
- *
- * ```js
- * console.log(utils.arrayify(''));
- * //=> []
- * console.log(utils.arrayify('foo'));
- * //=> ['foo']
- * console.log(utils.arrayify(['foo']));
- * //=> ['foo']
- * ```
- * @param {any} `val`
- * @return {Array}
- * @api public
- */
-
- utils.arrayify = function(val) {
- if (typeof val === 'string' && val !== '') {
- return [val];
- }
- if (!Array.isArray(val)) {
- return [];
- }
- return val;
- };
-
- /**
- * Convert the given `val` to a string by joining with `,`. Useful
- * for creating a cheerio/CSS/DOM-style selector from a list of strings.
- *
- * @param {any} `val`
- * @return {Array}
- * @api public
- */
-
- utils.stringify = function(val) {
- return utils.arrayify(val).join(',');
- };
-
- /**
- * Ensure that the given value is a string and call `.trim()` on it,
- * or return an empty string.
- *
- * @param {String} `str`
- * @return {String}
- * @api public
- */
-
- utils.trim = function(str) {
- return typeof str === 'string' ? str.trim() : '';
- };
-
- /**
- * Return true if val is an object
- */
-
- function isObject(val) {
- return typeOf(val) === 'object';
- }
-
- /**
- * Return true if val is a string
- */
-
- function isString(val) {
- return typeof val === 'string';
- }
-
- /**
- * Return true if val is a function
- */
-
- function isFunction(val) {
- return typeof val === 'function';
- }
-
- /**
- * Return true if val is an array
- */
-
- function isArray(val) {
- return Array.isArray(val);
- }
-
- /**
- * Shim to ensure the `.append` methods work with any version of snapdragon
- */
-
- function append(compiler, val, node) {
- if (typeof compiler.append !== 'function') {
- return compiler.emit(val, node);
- }
- return compiler.append(val, node);
- }
-
- /**
- * Simplified assertion. Throws an error is `val` is falsey.
- */
-
- function assert(val, message) {
- if (!val) throw new Error(message);
- }
|