less.js icon indicating copy to clipboard operation
less.js copied to clipboard

fix ajax request error for urls created by createObjectURL

Open Sanshain opened this issue 1 year ago • 0 comments

What: enable possibility using urls created by URL.createObjectURL to less compilation

        let c = `h2 { color : green; cursor: pointer; }`
        let b = new Blob([c], {
            type: 'text/css'
        })
        let url =  URL.createObjectURL(b);
        
        const link = document.createElement('link')
        
        link.href = url;
        link.type = "text/x-less"
        link.rel = "stylesheet/less";

        document.body.appendChild(link)

Why: without changes we have following error:

image

Sample code here https://codepen.io/Alsoo/pen/GRxjGZa (not reproduced on codepen)

How: links created using createObjectURL always start with blob: and are automatically added to the base part of the host name. Therefore, the simplest and most effective way to identify such links is to check whether they start with the blob part, bypassing the extractUrlParts function:

const href      = filename.startsWith('blob:') ? filename : hrefParts.url;

instead of

const href      = hrefParts.url;

This solution does not break any test and total solves all problems with blob links (checked)

  • [ ] Documentation
  • [ ] Added/updated unit tests
  • [x] Code complete

Sanshain avatar Jul 13 '22 08:07 Sanshain