2017-03-27 21:35:15 +00:00
|
|
|
/* 9.3 Relationships */
|
2017-02-10 19:23:01 +00:00
|
|
|
var RELS = ({
|
2014-05-16 00:33:34 +00:00
|
|
|
WB: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument",
|
2017-03-28 04:41:01 +00:00
|
|
|
SHEET: "http://sheetjs.openxmlformats.org/officeDocument/2006/relationships/officeDocument",
|
2017-03-31 00:47:35 +00:00
|
|
|
HLINK: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink",
|
2017-04-02 06:47:25 +00:00
|
|
|
VML: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing",
|
2019-11-01 03:09:14 +00:00
|
|
|
XPATH: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/externalLinkPath",
|
|
|
|
XMISS: "http://schemas.microsoft.com/office/2006/relationships/xlExternalLinkPath/xlPathMissing",
|
|
|
|
XLINK: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/externalLink",
|
2020-09-11 08:38:33 +00:00
|
|
|
CXML: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml",
|
|
|
|
CXMLP: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXmlProps",
|
2017-03-28 04:41:01 +00:00
|
|
|
VBA: "http://schemas.microsoft.com/office/2006/relationships/vbaProject"
|
2017-02-10 19:23:01 +00:00
|
|
|
}/*:any*/);
|
2014-05-16 00:33:34 +00:00
|
|
|
|
2017-03-27 21:35:15 +00:00
|
|
|
/* 9.3.3 Representing Relationships */
|
|
|
|
function get_rels_path(file/*:string*/)/*:string*/ {
|
|
|
|
var n = file.lastIndexOf("/");
|
2018-01-11 08:01:25 +00:00
|
|
|
return file.slice(0,n+1) + '_rels/' + file.slice(n+1) + ".rels";
|
2017-03-27 21:35:15 +00:00
|
|
|
}
|
|
|
|
|
2017-02-10 19:23:01 +00:00
|
|
|
function parse_rels(data/*:?string*/, currentFilePath/*:string*/) {
|
2019-11-15 01:46:49 +00:00
|
|
|
var rels = {"!id":{}};
|
|
|
|
if (!data) return rels;
|
2014-05-16 00:33:34 +00:00
|
|
|
if (currentFilePath.charAt(0) !== '/') {
|
|
|
|
currentFilePath = '/'+currentFilePath;
|
|
|
|
}
|
|
|
|
var hash = {};
|
|
|
|
|
2017-02-10 19:23:01 +00:00
|
|
|
(data.match(tagregex)||[]).forEach(function(x) {
|
2014-05-16 00:33:34 +00:00
|
|
|
var y = parsexmltag(x);
|
|
|
|
/* 9.3.2.2 OPC_Relationships */
|
|
|
|
if (y[0] === '<Relationship') {
|
|
|
|
var rel = {}; rel.Type = y.Type; rel.Target = y.Target; rel.Id = y.Id; rel.TargetMode = y.TargetMode;
|
2017-03-27 21:35:15 +00:00
|
|
|
var canonictarget = y.TargetMode === 'External' ? y.Target : resolve_path(y.Target, currentFilePath);
|
2014-05-16 00:33:34 +00:00
|
|
|
rels[canonictarget] = rel;
|
|
|
|
hash[y.Id] = rel;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
rels["!id"] = hash;
|
|
|
|
return rels;
|
|
|
|
}
|
|
|
|
|
|
|
|
XMLNS.RELS = 'http://schemas.openxmlformats.org/package/2006/relationships';
|
|
|
|
|
|
|
|
var RELS_ROOT = writextag('Relationships', null, {
|
|
|
|
//'xmlns:ns0': XMLNS.RELS,
|
|
|
|
'xmlns': XMLNS.RELS
|
|
|
|
});
|
|
|
|
|
|
|
|
/* TODO */
|
2017-02-10 19:23:01 +00:00
|
|
|
function write_rels(rels)/*:string*/ {
|
2017-04-08 06:55:35 +00:00
|
|
|
var o = [XML_HEADER, RELS_ROOT];
|
|
|
|
keys(rels['!id']).forEach(function(rid) {
|
|
|
|
o[o.length] = (writextag('Relationship', null, rels['!id'][rid]));
|
2014-05-16 00:33:34 +00:00
|
|
|
});
|
2014-06-29 18:29:45 +00:00
|
|
|
if(o.length>2){ o[o.length] = ('</Relationships>'); o[1]=o[1].replace("/>",">"); }
|
2014-05-16 00:33:34 +00:00
|
|
|
return o.join("");
|
|
|
|
}
|
2017-03-31 00:47:35 +00:00
|
|
|
|
2019-11-01 03:09:14 +00:00
|
|
|
var RELS_EXTERN = [RELS.HLINK, RELS.XPATH, RELS.XMISS];
|
|
|
|
function add_rels(rels, rId/*:number*/, f, type, relobj, targetmode/*:?string*/)/*:number*/ {
|
2017-03-31 00:47:35 +00:00
|
|
|
if(!relobj) relobj = {};
|
|
|
|
if(!rels['!id']) rels['!id'] = {};
|
2017-05-09 18:07:57 +00:00
|
|
|
if(rId < 0) for(rId = 1; rels['!id']['rId' + rId]; ++rId){/* empty */}
|
2017-03-31 00:47:35 +00:00
|
|
|
relobj.Id = 'rId' + rId;
|
|
|
|
relobj.Type = type;
|
|
|
|
relobj.Target = f;
|
2019-11-01 03:09:14 +00:00
|
|
|
if(targetmode) relobj.TargetMode = targetmode;
|
|
|
|
else if(RELS_EXTERN.indexOf(relobj.Type) > -1) relobj.TargetMode = "External";
|
2017-03-31 00:47:35 +00:00
|
|
|
if(rels['!id'][relobj.Id]) throw new Error("Cannot rewrite rId " + rId);
|
|
|
|
rels['!id'][relobj.Id] = relobj;
|
|
|
|
rels[('/' + relobj.Target).replace("//","/")] = relobj;
|
|
|
|
return rId;
|
|
|
|
}
|