mbest / knockout.punches

A collection of enhanced binding syntaxes using Knockout 3+
http://mbest.github.io/knockout.punches/
238 stars 19 forks source link

Rewrote the enable all to allow overriding of pre-processors #45

Closed DrSammyD closed 9 years ago

DrSammyD commented 9 years ago

Instead of This, Perhaps just expose the enable function to modification so that I don't have to register another pre-processor that will replicate the existing functionality

mbest commented 9 years ago

Can you explain more how this helps you?

DrSammyD commented 9 years ago

It allows me to write this file in my project instead of my previous pull request, with the enableAll function so that it doesn't duplicate the interpolationMarkupPreprocessor when mine is just an expanded function, and in the event that any other preprocessors are added, I won't have to update a different version of the enableAll function that I created to include my interpolationMarkupPreprocessor without yours. It also exposes the other enable functions for overriding in case anybody wants to do the same.

define(['ko-punches'], function(ko_punches) {
    var ko_punches_interpolationMarkup = ko_punches.interpolationMarkup;
    function parseInterpolationMarkup(textToParse, outerTextCallback, expressionCallback, node) {
        function innerParse(text) {
            var innerMatch = text.match(/^([\s\S]*)}}([\s\S]*?)\{\{([\s\S]*)$/);
            if (innerMatch) {
                innerParse(innerMatch[1]);
                outerTextCallback(innerMatch[2]);
                expressionCallback(innerMatch[3]);
            } else {
                expressionCallback(text);
            }
        }
        var outerMatch = textToParse.match(/^([\s\S]*?)\{\{([\s\S]*)}}([\s\S]*)$/);
        if (outerMatch) {
            outerTextCallback(outerMatch[1]);
            innerParse(outerMatch[2]);
            outerTextCallback(outerMatch[3]);
        } else {
            var current = node,
                siblings = [node],
                search = true;
            while ((siblings.push(current = current.nextSibling), current) && search) {
                if (current.nodeType === 3 && current.nodeValue && ~current.nodeValue.indexOf('}}') && (current.parentNode || {}).nodeName != "TEXTAREA") {
                    search = false;
                    middle = siblings.slice(1, -1);
                    siblings = [];
                    siblings.push(node.nodeValue);
                    for (var i = 0; i < middle.length; i++) {
                        siblings.push(middle[i].outerHTML);
                    }
                    siblings.push(current.nodeValue);
                    textToParse = siblings.join('');
                    outerMatch = textToParse.match(/^([\s\S]*?)\{\{([\s\S]*)}}([\s\S]*)$/);
                    if (outerMatch) {
                        for (i = middle.length - 1; i >= 0; i--) {
                            node.parentElement.removeChild(middle[i]);
                        }
                        node.parentElement.removeChild(current);
                        outerTextCallback(outerMatch[1]);
                        innerParse(outerMatch[2]);
                        outerTextCallback(outerMatch[3]);
                    }
                }
            }
        }
    }

    function interpolationMarkupPreprocessor(node) {
        // only needs to work with text nodes
        if (node.nodeType === 3 && node.nodeValue && node.nodeValue.indexOf('{{') !== -1 && (node.parentNode || {}).nodeName != "TEXTAREA") {
            var nodes = [];

            function addTextNode(text) {
                if (text)
                    nodes.push(document.createTextNode(text));
            }

            function wrapExpr(expressionText) {
                if (expressionText)
                    nodes.push.apply(nodes, ko_punches_interpolationMarkup.wrapExpression(expressionText, node));
            }
            parseInterpolationMarkup(node.nodeValue, addTextNode, wrapExpr, node);

            if (nodes.length) {
                if (node.parentNode) {
                    for (var i = 0, n = nodes.length, parent = node.parentNode; i < n; ++i) {
                        parent.insertBefore(nodes[i], node);
                    }
                    parent.removeChild(node);
                }
                return nodes;
            }
        }
    }
    ko_punches.interpolationMarkup.preprocessor = interpolationMarkupPreprocessor;
    ko_punches.interpolationMarkup.enable = function enableAttributeInterpolationMarkup() {
        ko_punches.utils.addNodePreprocessor(interpolationMarkupPreprocessor);
    };
});
mbest commented 9 years ago

Sorry, but this isn't a direction I want to go in.