Add needed inner shortcode if missing

若如初见. 提交于 2019-12-25 05:28:20

问题


I have the following code:

<textarea class="content">
    [row]
        [col-md-12]
            text
        [/col-md-12]
    [/row]
    [row]
        text
    [/row]
</textarea>

Now I want to add the [col-md-12] inside (directly after) the [row] and close it before the [/row] with jquery. This should be the result:

<textarea class="content">
    [row]
        [col-md-12]
            text
        [/col-md-12]
    [/row]
    [row]
        [col-md-12]
            text
        [/col-md-12]
    [/row]
</textarea>

(Note: The code above is not bootstrap! I just took bootstrap, because its more clear, that the "col-md-12" is needed!)

Here is a jsfiddle example of the current code: http://jsfiddle.net/Zoker/6mzrj1e7/

Now after "converting" there is a '[vc_column width="1/1"]' missing inside the first '[vc_row]' and a '[/vc_column]' before the closing '[/vc_row]'. And thats the shortcode I need to add. I dont know how to proof, if its missing and add it then...

Also I have to click 3 times on the button to get the content completely updated. How can I make it work after 1 click? (edit: that one fixed with https://stackoverflow.com/a/1145525/2977288)


回答1:


LIVE:

It was very hard to achieve: (Hope it helps)


// auto_descritive
function wrap_string( str, open_param, close_param ){
    var text = $(".content").text();

    // all indexes of str
    var re = new RegExp(str,'gi'), results = [];
    while (re.exec(text)){
        results.push(re.lastIndex);
    }

    // check each parent param of each str
    for (var i=0; results[i]; i++) {
        var y_param = text.substring(0, results[i]).lastIndexOf(']')+1;
        var x_param = y_param - open_param.length;
        var lastParam = text.substring(x_param, y_param);
        var half1 = text.substring(0, y_param);
        var half2 = text.substring(results[i]+str.length, text.length);
        if (lastParam != open_param) {
            text = half1 + open_param + str + close_param + half2;
            $(".content").text( text );
            // return true if one was wrapped
            return true;
        }
    }

    // return false in case nothing was wrapped
    return false;

}


// wrap all "text" with "[col-md-12] /"
while( wrap_string( "text", "[col-md-12]", "[/col-md-12]" ) );



来源:https://stackoverflow.com/questions/28832889/add-needed-inner-shortcode-if-missing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!