'use strict';

var direction = require('direction');

function onchangedirectioninside(parent) {
    var node, currentDirection, nodeDirection;

    if (!parent) {
        return;
    }

    node = parent.head;

    while (node) {
        nodeDirection = node.data.direction;

        if (nodeDirection !== 'neutral') {
            if (!currentDirection) {
                currentDirection = nodeDirection;
            } else if (nodeDirection !== currentDirection) {
                currentDirection = 'neutral';
                break;
            }
        }

        node = node.next;
    }

    parent.data.direction = currentDirection || 'neutral';

    onchangedirectioninside(parent.parent);
}

function oninsert() {
    onchangedirectioninside(this.parent);
}

function onremove(previousParent) {
    onchangedirectioninside(previousParent);
}

function onchangetext(value) {
    var data = this.data,
        oldDirection, newDirection;

    oldDirection = data.direction;
    newDirection = value ? direction(value) : 'neutral';

    data.direction = newDirection;

    if (newDirection !== oldDirection) {
        onchangedirectioninside(this.parent);
    }
}

function attach(retext) {
    var Node = retext.parser.TextOM.Node;

    Node.on('changetext', onchangetext);
    Node.on('insert', oninsert);
    Node.on('remove', onremove);
}

exports = module.exports = function () {};

exports.attach = attach;

'use strict';

/**
 * Dependencies.
 */

var direction;

direction = require('direction');

/**
 * Define `directionality`.
 */

function directionality() {}

/**
 * Any change handler.
 *
 * @param {Node} parent
 */

function onchangeinside(parent) {
    var node,
        currentDirection,
        nodeDirection;

    if (!parent) {
        return;
    }

    node = parent.head;

    while (node) {
        nodeDirection = node.data.direction;

        if (nodeDirection !== 'neutral') {
            if (!currentDirection) {
                currentDirection = nodeDirection;
            } else if (nodeDirection !== currentDirection) {
                currentDirection = 'neutral';
                break;
            }
        }

        node = node.next;
    }

    parent.data.direction = currentDirection || 'neutral';

    onchangeinside(parent.parent);
}

/**
 * Handler for `insert`.
 *
 * @this {Child}
 */

function oninsert() {
    onchangeinside(this.parent);
}

/**
 * Handler for `remove`.
 *
 * @param {Parent} previousParent
 * @this {Child}
 */

function onremove(previousParent) {
    onchangeinside(previousParent);
}

/**
 * Handler for `changetext`.
 *
 * @param {string} value
 * @this {Child}
 */

function onchangetext(value) {
    var data,
        previousDirection,
        newDirection;

    data = this.data;
    previousDirection = data.direction;

    newDirection = value ? direction(value) : 'neutral';

    if (newDirection !== previousDirection) {
        data.direction = newDirection;

        onchangeinside(this.parent);
    }
}

/**
 * Define `attach`.
 *
 * @param {Retext} retext
 */

function attach(retext) {
    var Node;

    Node = retext.TextOM.Node;

    Node.on('changetext', onchangetext);
    Node.on('insert', oninsert);
    Node.on('remove', onremove);
}

/**
 * Expose `attach`.
 */

directionality.attach = attach;

/**
 * Expose `directionality`.
 */

module.exports = directionality;

Refactor API