Skip to content
This repository has been archived by the owner on Sep 25, 2021. It is now read-only.

problems with jquery 1.9 #17

Closed
christian-andersson opened this issue Jan 30, 2013 · 3 comments
Closed

problems with jquery 1.9 #17

christian-andersson opened this issue Jan 30, 2013 · 3 comments

Comments

@christian-andersson
Copy link

There is a smaller problem with jquery 1.9 in that the new jquery does not allow whitespace to be "first" in the html string, ie $("

test

") is allowed but $("

test

") is not allowed.

this makes it harder to have "pretty" templates in your code when using it with jquery.
for example

<script id="test">

test

</script>

will fail when used with
$(tmpl("test"))

since the template result will start with "whitespace" characrters (return, space, etc)

at the moment the workaround for me is to use the $.parseHTML function so that I now get

$($.parseHTML(tmpl("test")))

Is there any way to make it "trim" the whitespace at the beginning (and at the end) automaticly?

@blueimp
Copy link
Owner

blueimp commented Jan 30, 2013

Please have a look here:
https://github.com/blueimp/JavaScript-Templates#template-parsing

There you'll find the following section:

By default, the plugin preserves whitespace (newlines, carriage returns, tabs and spaces). To strip unnecessary whitespace, you can override the tmpl.func function, e.g. with the following code:

var originalFunc = tmpl.func;
tmpl.func = function (s, p1, p2, p3, p4, p5, offset, str) {
    if (p1 && /\s/.test(p1)) {
        if (!offset || /\s/.test(str.charAt(offset - 1)) ||
                /^\s+$/g.test(str.slice(offset))) {
            return '';
        }
        return ' ';
    }
    return originalFunc.apply(tmpl, arguments);
};

@blueimp blueimp closed this as completed Jan 30, 2013
@christian-andersson
Copy link
Author

This will remove all "extra" whitespace, including whitespace that should be there inside

 tags.
what is needed is a trim() so that whitespace before first tag and after last is removed.
but there is as I said a workaround, use $.parseHTML and it will work, this is currently what I have added in our code, but you might make a notice, of it, in case others get the same problem also..

@blueimp
Copy link
Owner

blueimp commented Jan 30, 2013

You can adjust the function to only strip whitespace at the beginning:

var originalFunc = tmpl.func;
tmpl.func = function (s, p1, p2, p3, p4, p5, offset, str) {
    if (!offset && p1 && /\s/.test(p1)) {
        return '';
    }
    return originalFunc.apply(tmpl, arguments);
};

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

No branches or pull requests

2 participants