No more jQuery

in the website


Here’s a change: your Sheepie experience is now free of jQuery unless you are a dirty IE9 peasant.

This morning I removed the last piece of jQuery-dependent code, a preprocessor for highlight.js. highlight.js operates on <pre> tags with a <code> child element. Since I have a few hundred older posts without code that I didn’t want to hand-edit, I used jQuery’s .wrapInner() function to prepare them:

if ($('pre').length) {
    $('pre:not(:has(> code))').wrapInner('<code></code>');
}

I replaced it with my own version that uses vanilla JS:

function wrapInsideElement(selector, wrapper) {
    selector = document.querySelectorAll(selector);
    wrapper = wrapper.replace(/(^<.*\/|>$)/g, '');

    [].forEach.call(selector, function(element) {
        if (!element.querySelectorAll(wrapper).length) {
            var newChild = document.createElement(wrapper);
            
            while (element.childNodes.length) {
                newChild.appendChild(element.childNodes[0]);
            }

            element.appendChild(newChild);
        }
    });
}

wrapInsideElement('pre', '<code></code>');

Update: No jQuery at all. You can still load it as a script dependency if necessary.



March 20

in me


Your email address will not be published. Required fields are marked *