Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Here is a user-side implementation of .wrapAll() #748

Closed
sfinktah opened this issue Sep 7, 2015 · 6 comments
Closed

Here is a user-side implementation of .wrapAll() #748

sfinktah opened this issue Sep 7, 2015 · 6 comments

Comments

@sfinktah
Copy link

sfinktah commented Sep 7, 2015

I would have written this properly and sent a pull request (using the internal functions for dom manipulation, etc), but the current git version is failing testing.

var cheerio = require('cheerio');
var _ = require('lodash');

module.exports = (function() {
    var extendCheerio = function extendCheerio($) {
        _.extend($.prototype, {
            wrapAll: function(wrapper) {
                if (this.length < 1) {
                    return this;
                }

                if (this.length < 2 && this.wrap) { // wrap not defined in npm version,
                    return this.wrap(wrapper);      // and git version fails testing.
                }

                var elems = this;
                var section = $(wrapper);
                var marker = $('<div>');
                marker = marker.insertBefore(elems.first()); // in jQuery marker would remain current
                elems.each(function(k, v) {                  // in Cheerio, we update with the output.
                    section.append($(v));
                });
                section.insertBefore(marker); 
                marker.remove();
                return section;                 // This is what jQuery would return, IIRC.
            },
        });
    };

    if ("test") {
        $ = cheerio.load("<html><body><div><p><span>This <em>is <i>test</p><span>More <em>test");
        extendCheerio($);
        $('span').wrapAll('<section>');
        var passed = ($.html() === '<html><body><div><p><section><span>This <em>is <i>test</i></em>'+
                          '</span><span>More <em>test</em></span></section></p></div></body></html>');
        console.log("passed: " + (passed ? "yes" : "no"));
        // console.log($.html() + "\n\n");
    }
    return extendCheerio;
})();
@ajsnyde
Copy link

ajsnyde commented Oct 31, 2018

Doesn't wrap the children, I don't think. I try to $('a').wrapAll(''):

<a><b></b><a><a><b></b><a><a><b></b><a>

and I get <outer><a></a><a></a></outer><b></b><b></b>

@mhjb
Copy link

mhjb commented Nov 29, 2019

@sfinktah works for me — thank you very much

@fb55
Copy link
Member

fb55 commented Dec 12, 2020

Happy to accept a PR for this, should someone want to polish things up a bit!

@fb55 fb55 closed this as completed Dec 12, 2020
@antecuic
Copy link

Hi guys!
How did you implement this in code?

@mhjb
Copy link

mhjb commented Dec 22, 2020

@antecuic
Copy link

antecuic commented Dec 23, 2020

Screenshot 2020-12-23 at 09 35 21
I need to use .wrapAll() like this.
Screenshot 2020-12-23 at 09 37 10
And this is my way of implementing.
had to change to arrow functions because of tslint and different import because of my project setup.
I'm struggling with this problem for days now since .wrapAll() isn't supported on cheerio.

I added this code in separate file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

5 participants