35_custprops.js 2.1 KB
Newer Older
1 2 3 4
/* 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';

S
SheetJS 已提交
5
var custregex = /<[^>]+>[^<]*/g;
S
SheetJS 已提交
6 7
function parse_cust_props(data/*:string*/, opts) {
	var p = {}, name = "";
S
SheetJS 已提交
8 9 10
	var m = data.match(custregex);
	if(m) for(var i = 0; i != m.length; ++i) {
		var x = m[i], y = parsexmltag(x);
11 12
		switch(y[0]) {
			case '<?xml': break;
S
SheetJS 已提交
13
			case '<Properties': break;
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
			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:
S
SheetJS 已提交
40
						if(opts.WTF && typeof console !== 'undefined') console.warn('Unexpected', x, type, toks);
41 42 43 44
				}
			} else if(x.substr(0,2) === "</") {
			} else if(opts.WTF) throw new Error(x);
		}
S
SheetJS 已提交
45
	}
46 47 48 49 50 51 52 53
	return p;
}

var CUST_PROPS_XML_ROOT = writextag('Properties', null, {
	'xmlns': XMLNS.CUST_PROPS,
	'xmlns:vt': XMLNS.vt
});

S
SheetJS 已提交
54
function write_cust_props(cp, opts)/*:string*/ {
S
SheetJS 已提交
55
	var o = [XML_HEADER, CUST_PROPS_XML_ROOT];
56 57
	if(!cp) return o.join("");
	var pid = 1;
S
SheetJS 已提交
58 59
	keys(cp).forEach(function custprop(k) { ++pid;
		o[o.length] = (writextag('property', write_vt(cp[k]), {
60 61 62 63 64
			'fmtid': '{D5CDD505-2E9C-101B-9397-08002B2CF9AE}',
			'pid': pid,
			'name': k
		}));
	});
S
SheetJS 已提交
65
	if(o.length>2){ o[o.length] = '</Properties>'; o[1]=o[1].replace("/>",">"); }
66 67
	return o.join("");
}