sheetjs/bits/35_custprops.js
SheetJS 6bc24374b9 version bump 0.7.7: needs more cowbell
- parsexmltag and other hot functions now better optimized for v8
- monomorphic functions (different types -> different funcs)
- more efficient decode_range implementation when source is trusted
- regular expressions cached and simplified without breaking correctness
- more efficient utf8 techniques when available
- XLSX: large functions broken down into sub-functions (e.g. `parse_ws_xml`)
- XLSB: avoid unnecessary binds
- XLSB: assume no exotic codepage exists (no one else tries to write XLSB)
- demo exposes rABS / worker / transferable options
- more tests
- jszip updated to 2.3.0
- SSF updated to 0.8.1
- codepage updated to 1.3.1
2014-06-29 14:29:45 -04:00

71 lines
2.2 KiB
JavaScript

/* 15.2.12.2 Custom File Properties Part */
XMLNS.CUST_PROPS = "http://schemas.openxmlformats.org/officeDocument/2006/custom-properties";
RELS.CUST_PROPS = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties';
var custregex = /<[^>]+>[^<]*/g;
function parse_cust_props(data, 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);
switch(y[0]) {
case '<?xml': break;
case '<Properties':
if(y.xmlns !== XMLNS.CUST_PROPS) throw "unrecognized xmlns " + y.xmlns;
if(y.xmlnsvt && y.xmlnsvt !== XMLNS.vt) throw "unrecognized vt " + y.xmlnsvt;
break;
case '<property': name = y.name; break;
case '</property>': name = null; break;
default: if (x.indexOf('<vt:') === 0) {
var toks = x.split('>');
var type = toks[0].substring(4), text = toks[1];
/* 22.4.2.32 (CT_Variant). Omit the binary types from 22.4 (Variant Types) */
switch(type) {
case 'lpstr': case 'lpwstr': case 'bstr': case 'lpwstr':
p[name] = unescapexml(text);
break;
case 'bool':
p[name] = parsexmlbool(text, '<vt:bool>');
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] = new Date(text);
break;
case 'cy': case 'error':
p[name] = unescapexml(text);
break;
default:
if(typeof console !== 'undefined') console.warn('Unexpected', x, type, toks);
}
} else if(x.substr(0,2) === "</") {
} else if(opts.WTF) throw new Error(x);
}
}
return p;
}
var CUST_PROPS_XML_ROOT = writextag('Properties', null, {
'xmlns': XMLNS.CUST_PROPS,
'xmlns:vt': XMLNS.vt
});
function write_cust_props(cp, opts) {
var o = [XML_HEADER, CUST_PROPS_XML_ROOT];
if(!cp) return o.join("");
var pid = 1;
keys(cp).forEach(function custprop(k) { ++pid;
o[o.length] = (writextag('property', write_vt(cp[k]), {
'fmtid': '{D5CDD505-2E9C-101B-9397-08002B2CF9AE}',
'pid': pid,
'name': k
}));
});
if(o.length>2){ o[o.length] = '</Properties>'; o[1]=o[1].replace("/>",">"); }
return o.join("");
}