Cheerio: Feature request: Implement $.wrap()

Created on 20 Jul 2014  路  6Comments  路  Source: cheeriojs/cheerio

This is actually a rather useful method, and would probably be even better server-side. It would simplify a lot of things server-side. It has been part of jQuery since 1.0, with the callback equivalent since 1.4.

馃挕 Feature

Most helpful comment

A couple of my utility functions for cheerio - feel free to make them into proper methods ;-)

function wrap(selector, wrapper) {
    return $(selector).each(function() {
        $(this).before(wrapper).prev().append(this);
    });
}

function unWrap(selector) {
    $(selector).each(function() {
        var $this = $(this);
        $(this).after($this.contents()).remove();
    });
}

function wrapAll(selector, wrapper) {
    $(selector).first().before(wrapper).prev().append(selector);
}

All 6 comments

$.wrap is definitely missing

PRs welcome :)

Is anybody working on this? I would like to take a crack at it.

@dandlezzz Go for it!

A couple of my utility functions for cheerio - feel free to make them into proper methods ;-)

function wrap(selector, wrapper) {
    return $(selector).each(function() {
        $(this).before(wrapper).prev().append(this);
    });
}

function unWrap(selector) {
    $(selector).each(function() {
        var $this = $(this);
        $(this).after($this.contents()).remove();
    });
}

function wrapAll(selector, wrapper) {
    $(selector).first().before(wrapper).prev().append(selector);
}

A couple of my utility functions for cheerio - feel free to make them into proper methods ;-)

function wrap(selector, wrapper) {
    return $(selector).each(function() {
        $(this).before(wrapper).prev().append(this);
    });
}

function unWrap(selector) {
    $(selector).each(function() {
        var $this = $(this);
        $(this).after($this.contents()).remove();
    });
}

function wrapAll(selector, wrapper) {
    $(selector).first().before(wrapper).prev().append(selector);
}

Very helpful, thank you

Was this page helpful?
0 / 5 - 0 ratings