clean-html icon indicating copy to clipboard operation
clean-html copied to clipboard

Return value

Open mithray opened this issue 6 years ago • 1 comments

I noticed the main clean function in index.js has no return value, this makes it very difficult to compose in a chain of operations, i tried to get my project going for hours until i realized this was happening and i solved it by simply adding on return html onto the end of the function clean in index.js. Is there any reason that this would break the program? I would like to submit this as a pull request but not sure if there is any compatibility issues. I think not.

mithray avatar Jan 31 '19 14:01 mithray

Off the top of my head, I can't think of a reason why not.

dave-kennedy avatar Feb 05 '19 14:02 dave-kennedy

I thought about it for four years and finally realized why this won't work. This is what the clean function looked like when you asked:

function clean(html, opt, callback) {
    if (typeof opt == 'function') {
        callback = opt;
        opt = null;
    }

    setup(opt);

    var handler = new htmlparser.DomHandler(function (err, dom) {
        if (err) {
            throw err;
        }

        var html = render(dom);
        html = indent(html).trim();

        callback(html);
    });

    var parser = new htmlparser.Parser(handler);
    parser.write(html);
    parser.done();
}

Notice the html argument is shadowed by the inner html variable assigned inside the DomHandler callback. That's misleading. Thankfully, @joeyparrish took care of it in a3747f2.

Anyways, if you add return html after parser.done(), you'll return the original value - the one passed as the html argument to the clean function, as opposed to the one that was passed to the callback argument.

I assume it's been long enough that you no longer care so I'll close this issue. Let me know otherwise and we can figure something out.

dave-kennedy avatar Apr 07 '23 03:04 dave-kennedy