* Wrapper class for data urls and operations
| 35 | */ |
| 36 | |
| 37 | class DataURL { |
| 38 | /** |
| 39 | * @constructor |
| 40 | * @param {URL} url - data url |
| 41 | */ |
| 42 | constructor(url) { |
| 43 | this.initString = url; |
| 44 | |
| 45 | var colon = url.substring(0,15).indexOf(":"); |
| 46 | if ( colon != -1) { |
| 47 | this.scheme = url.substring(0,colon); |
| 48 | if (schemeMappings[this.scheme]) { |
| 49 | url = `data:${schemeMappings[this.scheme]},${url}` |
| 50 | } |
| 51 | } else { |
| 52 | if (url.charAt(0) == "?") { |
| 53 | this.editable = true; |
| 54 | url = url.substring(1); |
| 55 | } |
| 56 | |
| 57 | this.dataPrefix = HEAD_TAGS_EXTENDED(); |
| 58 | // todo: gzip starts with 0x1f8b |
| 59 | let encoding = url.startsWith("XQA") ? bitty.LZMA_MARKER : bitty.GZIP_MARKER; |
| 60 | url = `data:text/html;charset=utf-8;format=${encoding};base64,${url}`; |
| 61 | } |
| 62 | |
| 63 | let match = url.match(dataUrlRE); |
| 64 | |
| 65 | this.params = {}; |
| 66 | |
| 67 | if (match) { |
| 68 | let info = match.groups; |
| 69 | Object.assign(this, info); |
| 70 | this.params = info.params ? JSON.parse('{"' + decodeURI(info.params?.substring(1)).replace(/"/g, '\\"').replace(/;/g, '","').replace(/=/g,'":"') + '"}') : {}; |
| 71 | } |
| 72 | if (this.encoding) { |
| 73 | this.data = this.data.replace(/=/g,""); |
| 74 | } else { |
| 75 | this.data = decodeURIComponent(this.data); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | get href() { |
| 80 | let urlString = "data:"; |
| 81 | |
| 82 | if (this.mediatype) urlString += this.mediatype |
| 83 | if (this.params) Object.entries(this.params).forEach( e => { if (!e[0].startsWith("_")) urlString += `;${e[0]}=${e[1]}`}) |
| 84 | if (this.encoding) urlString += ";" + this.encoding |
| 85 | |
| 86 | if (!this.encoding && this.dataPrefix) { |
| 87 | this.dataPrefix = atob(this.dataPrefix); |
| 88 | } |
| 89 | urlString += "," + (this.dataPrefix || '') + this.data; |
| 90 | return urlString; |
| 91 | } |
| 92 | |
| 93 | get format() { |
| 94 | return this.params.format || this.encoding; |
nothing calls this directly
no test coverage detected