sheetjs_sheetjs/bits/35_custprops.js

64 lines
1.9 KiB
JavaScript
Raw Normal View History

/* 15.2.12.2 Custom File Properties Part */
var custregex = /<[^<>]+>[^<]*/g;
function parse_cust_props(data/*:string*/, opts) {
var p = {}, name = "";
var m = data.match(custregex);
if(m) for(var i = 0; i != m.length; ++i) {
var x = m[i], y = parsexmltag(x);
2022-08-15 05:20:14 +00:00
switch(strip_ns(y[0])) {
case '<?xml': break;
case '<Properties': break;
case '<property': name = unescapexml(y.name); break;
case '</property>': name = null; break;
default: if (x.indexOf('<vt:') === 0) {
var toks = x.split('>');
var type = toks[0].slice(4), text = toks[1];
/* 22.4.2.32 (CT_Variant). Omit the binary types from 22.4 (Variant Types) */
switch(type) {
case 'lpstr': case 'bstr': case 'lpwstr':
p[name] = unescapexml(text);
break;
case 'bool':
p[name] = parsexmlbool(text);
break;
case 'i1': case 'i2': case 'i4': case 'i8': case 'int': case 'uint':
p[name] = parseInt(text, 10);
break;
case 'r4': case 'r8': case 'decimal':
p[name] = parseFloat(text);
break;
case 'filetime': case 'date':
p[name] = parseDate(text);
break;
case 'cy': case 'error':
p[name] = unescapexml(text);
break;
default:
if(type.slice(-1) == '/') break;
if(opts.WTF && typeof console !== 'undefined') console.warn('Unexpected', x, type, toks);
}
} else if(x.slice(0,2) === "</") {/* empty */
} else if(opts.WTF) throw new Error(x);
}
}
return p;
}
function write_cust_props(cp/*::, opts*/)/*:string*/ {
var o = [XML_HEADER, writextag('Properties', null, {
'xmlns': XMLNS.CUST_PROPS,
'xmlns:vt': XMLNS.vt
})];
if(!cp) return o.join("");
var pid = 1;
keys(cp).forEach(function custprop(k) { ++pid;
o[o.length] = (writextag('property', write_vt(cp[k], true), {
'fmtid': '{D5CDD505-2E9C-101B-9397-08002B2CF9AE}',
'pid': pid,
'name': escapexml(k)
}));
});
if(o.length>2){ o[o.length] = '</Properties>'; o[1]=o[1].replace("/>",">"); }
return o.join("");
}