advanced-cropper / vue-advanced-cropper

The advanced vue cropper library that gives you opportunity to create your own croppers suited for any website design
https://advanced-cropper.github.io/vue-advanced-cropper/
Other
1.01k stars 136 forks source link

Cannot build unit tests of project that imports vue-advanced-cropper #12

Closed struchon closed 5 years ago

struchon commented 5 years ago

Since I added vue-advanced-cropper to a project built with webpack, unit tests (ran with vue-cli-service test:unit) fail to compile.

TypeError: Cannot read property 'startsWith' of undefined
    at styleInject ([...]\dist\webpack:\node_modules\vue-advanced-cropper\dist\index.es.js:68:1)
    at Module../node_modules/vue-advanced-cropper/dist/index.es.js ([...]\dist\webpack:\node_modules\vue-advanced-cropper\dist\index.es.js:87:1)
    at __webpack_require__ ([...]\dist\webpack:\webpack\bootstrap:25:1)

The problem stems from the file index.es.js:

function styleInject(id, css) {
    if (!css || typeof document === 'undefined') { return; }

    var element;
    var head = document.head || document.getElementsByTagName('head')[0];
    var styleElements  = head.getElementsByTagName('style');

    // To prevent dublicate of the style code during the script reload
    for (var i = 0; i < styleElements.length; i++) {
        if (styleElements[i].innerText.startsWith('/*for=' + id)) {
            element = styleElements[i];
        }
    }

Changing

if (styleElements[i].innerText.startsWith('/*for=' + id)) {

to

if (styleElements[i].innerText && styleElements[i].innerText.startsWith('/*for=' + id)) {

fixes it.

Is there something else I should be doing to make it work on my end, or is it a bug?

Norserium commented 5 years ago

Hello!

It's a pretty strange bug, and it concerns my other library that I use as a dependency here.

I've added this fix to that library and updated this library to 0.12.5

struchon commented 5 years ago

Thanks for the quick followup! That fixed it.