OOXML.js 101 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304
  1. o2.xApplication.process.Xform.widget = o2.xApplication.process.Xform.widget || {};
  2. o2.xApplication.process.Xform.widget.OOXML = o2.xApplication.process.Xform.widget.OOXML || {};
  3. o2.OOXML = o2.OOXML || {};
  4. o2.xApplication.process.Xform.widget.OOXML.WordprocessingML = o2.OOXML.WML = new Class({
  5. Implements: [Options, Events],
  6. options: {
  7. "AppVersion": "16.0000",
  8. "Application": "o2oa",
  9. "Company": "",
  10. "ScaleCrop": "false",
  11. "LinksUpToDate": "false",
  12. "SharedDoc": "false",
  13. "HyperlinksChanged": "false",
  14. "w_document": "<w:document xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"\n" +
  15. " xmlns:wpc=\"http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas\"\n" +
  16. " xmlns:cx=\"http://schemas.microsoft.com/office/drawing/2014/chartex\"\n" +
  17. " xmlns:cx1=\"http://schemas.microsoft.com/office/drawing/2015/9/8/chartex\"\n" +
  18. " xmlns:cx2=\"http://schemas.microsoft.com/office/drawing/2015/10/21/chartex\"\n" +
  19. " xmlns:cx3=\"http://schemas.microsoft.com/office/drawing/2016/5/9/chartex\"\n" +
  20. " xmlns:cx4=\"http://schemas.microsoft.com/office/drawing/2016/5/10/chartex\"\n" +
  21. " xmlns:cx5=\"http://schemas.microsoft.com/office/drawing/2016/5/11/chartex\"\n" +
  22. " xmlns:cx6=\"http://schemas.microsoft.com/office/drawing/2016/5/12/chartex\"\n" +
  23. " xmlns:cx7=\"http://schemas.microsoft.com/office/drawing/2016/5/13/chartex\"\n" +
  24. " xmlns:cx8=\"http://schemas.microsoft.com/office/drawing/2016/5/14/chartex\"\n" +
  25. " xmlns:mc=\"http://schemas.openxmlformats.org/markup-compatibility/2006\"\n" +
  26. " xmlns:aink=\"http://schemas.microsoft.com/office/drawing/2016/ink\"\n" +
  27. " xmlns:am3d=\"http://schemas.microsoft.com/office/drawing/2017/model3d\"\n" +
  28. " xmlns:o=\"urn:schemas-microsoft-com:office:office\"\n" +
  29. " xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"\n" +
  30. " xmlns:m=\"http://schemas.openxmlformats.org/officeDocument/2006/math\" xmlns:v=\"urn:schemas-microsoft-com:vml\"\n" +
  31. " xmlns:wp14=\"http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing\"\n" +
  32. " xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\"\n" +
  33. " xmlns:w10=\"urn:schemas-microsoft-com:office:word\"\n" +
  34. " xmlns:w14=\"http://schemas.microsoft.com/office/word/2010/wordml\"\n" +
  35. " xmlns:w15=\"http://schemas.microsoft.com/office/word/2012/wordml\"\n" +
  36. " xmlns:w16cex=\"http://schemas.microsoft.com/office/word/2018/wordml/cex\"\n" +
  37. " xmlns:w16cid=\"http://schemas.microsoft.com/office/word/2016/wordml/cid\"\n" +
  38. " xmlns:w16=\"http://schemas.microsoft.com/office/word/2018/wordml\"\n" +
  39. " xmlns:w16sdtdh=\"http://schemas.microsoft.com/office/word/2020/wordml/sdtdatahash\"\n" +
  40. " xmlns:w16se=\"http://schemas.microsoft.com/office/word/2015/wordml/symex\"\n" +
  41. " xmlns:wpg=\"http://schemas.microsoft.com/office/word/2010/wordprocessingGroup\"\n" +
  42. " xmlns:wpi=\"http://schemas.microsoft.com/office/word/2010/wordprocessingInk\"\n" +
  43. " xmlns:wne=\"http://schemas.microsoft.com/office/word/2006/wordml\"\n" +
  44. " xmlns:wps=\"http://schemas.microsoft.com/office/word/2010/wordprocessingShape\"\n" +
  45. " xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\"\n" +
  46. " mc:Ignorable=\"w14 w15 w16se w16cid w16 w16cex w16sdtdh wp14\">",
  47. "w_setting": "<w:settings xmlns:mc=\"http://schemas.openxmlformats.org/markup-compatibility/2006\"\n" +
  48. " xmlns:o=\"urn:schemas-microsoft-com:office:office\"\n" +
  49. " xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"\n" +
  50. " xmlns:m=\"http://schemas.openxmlformats.org/officeDocument/2006/math\" xmlns:v=\"urn:schemas-microsoft-com:vml\"\n" +
  51. " xmlns:w10=\"urn:schemas-microsoft-com:office:word\"\n" +
  52. " xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"\n" +
  53. " xmlns:w14=\"http://schemas.microsoft.com/office/word/2010/wordml\"\n" +
  54. " xmlns:w15=\"http://schemas.microsoft.com/office/word/2012/wordml\"\n" +
  55. " xmlns:w16cex=\"http://schemas.microsoft.com/office/word/2018/wordml/cex\"\n" +
  56. " xmlns:w16cid=\"http://schemas.microsoft.com/office/word/2016/wordml/cid\"\n" +
  57. " xmlns:w16=\"http://schemas.microsoft.com/office/word/2018/wordml\"\n" +
  58. " xmlns:w16sdtdh=\"http://schemas.microsoft.com/office/word/2020/wordml/sdtdatahash\"\n" +
  59. " xmlns:w16se=\"http://schemas.microsoft.com/office/word/2015/wordml/symex\"\n" +
  60. " xmlns:sl=\"http://schemas.openxmlformats.org/schemaLibrary/2006/main\"\n" +
  61. " mc:Ignorable=\"w14 w15 w16se w16cid w16 w16cex w16sdtdh\">",
  62. "xmlHead": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>",
  63. "divAsP": false,
  64. "protection": false,
  65. "firstPageNumber": true,
  66. "pageHeight": "841.9",
  67. "pageWidth": "595.3",
  68. "page-margin-left": "79.4",
  69. "page-margin-right": "73.7",
  70. "page-margin-top": "104.9",
  71. "page-margin-bottom": "99.25"
  72. },
  73. initialize: function(options){
  74. this.setOptions(options);
  75. this.path = "../x_component_process_Xform/widget/$OOXML/WordprocessingML/";
  76. this.dpi = this.getDPI();
  77. this.rid = 11;
  78. },
  79. getDPI: function(){
  80. var div = new Element("div", {"styles": {"width": "1in", "height": "1in"}}).inject(document.body);
  81. var dpi = div.offsetWidth.toInt();
  82. div.destroy();
  83. return dpi;
  84. },
  85. getZipTemplate: function(){
  86. //var zipFileName = (this.options.firstPageNumber) ? "template.zip" : "template.zip";
  87. var zipFileName = "template.zip";
  88. return fetch(this.path+zipFileName).then(function(res){
  89. return res.blob().then(JSZip.loadAsync);
  90. });
  91. },
  92. load: function(data){
  93. return new Promise(function(resolve){
  94. o2.load(["../o2_lib/jszip/jszip.min.js", "../o2_lib/jszip/FileSaver.js"], function(){
  95. //this.getZipTemplate();
  96. this.getZipTemplate().then(function(zip){
  97. //console.log(zip.files);
  98. this.zip = zip;
  99. return this.processDocument(data);
  100. }.bind(this)).then(function(oo_content){
  101. var word = new Blob( [oo_content], {type : "application/vnd.openxmlformats-officedocument.wordprocessingml.document"} );
  102. //oo_content.type = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
  103. resolve(word);
  104. });
  105. }.bind(this));
  106. }.bind(this));
  107. },
  108. processDocument: function(data){
  109. var promise = [];
  110. var p1 = this.zip.file("word/document.xml").async("text").then(function(oo_string){
  111. return this.processWordDocument(oo_string, data);
  112. }.bind(this)).then(function(oo_str){
  113. if (oo_str.substring(0, 5)!=="<?xml"){
  114. oo_str = oo_str.replace(/<w:document.*\>/, this.options.w_document);
  115. oo_str = this.options.xmlHead + oo_str;
  116. }
  117. this.zip.file("word/document.xml", oo_str);
  118. if (this.pics && this.pics.length){
  119. return this.zip.file("word/_rels/document.xml.rels").async("text").then(function(oo_relString){
  120. return this.processWordRel(oo_relString);
  121. }.bind(this)).then(function(oo_relStr){
  122. //return oo_relStrPromise.then(function(oo_relStr){
  123. if (oo_relStr.substring(0, 5)!=="<?xml"){
  124. oo_relStr = this.options.xmlHead + oo_relStr;
  125. }
  126. //return this.zip.file("word/_rels/document.xml.rels", oo_relStr).generateAsync({type:"blob"});
  127. return this.zip.file("word/_rels/document.xml.rels", oo_relStr);
  128. //}.bind(this));
  129. }.bind(this));
  130. }
  131. return this.zip;
  132. // this.zip.file("word/document.xml", oo_str).generateAsync({type:"blob"}).then(function(oo_content) {
  133. // this.saveAs(oo_content, "example.docx");
  134. // }.bind(this));
  135. }.bind(this));
  136. promise.push(p1);
  137. if (this.options.protection){
  138. var p2 = this.zip.file("word/settings.xml").async("text").then(function(oo_string){
  139. return this.processWordSetting(oo_string);
  140. }.bind(this)).then(function(oo_settingStr){
  141. if (oo_settingStr.substring(0, 5)!=="<?xml"){
  142. oo_settingStr = oo_settingStr.replace(/<w:settings.*\>/, this.options.w_setting);
  143. oo_settingStr = this.options.xmlHead + oo_settingStr;
  144. }
  145. return this.zip.file("word/settings.xml", oo_settingStr);
  146. }.bind(this));
  147. promise.push(p2);
  148. }
  149. return Promise.all(promise).then(function(){
  150. return this.zip.generateAsync({type:"blob"});
  151. }.bind(this));
  152. },
  153. processWordSetting: function(oo_string){
  154. var domparser = new DOMParser();
  155. var oo_doc = domparser.parseFromString(oo_string, "text/xml");
  156. var oo_settings = oo_doc.documentElement;
  157. return new Promise(function(resolve){
  158. o2.require("o2.widget.Base64", function(){
  159. o2.load(["../o2_lib/CryptoJS/tripledes.js", "../o2_lib/CryptoJS/components/aes.js"], function () {
  160. var keyStr = (new o2.widget.UUID()).toString();
  161. var key = CryptoJS.enc.Utf8.parse(keyStr.substr(0, 16));
  162. var iv = CryptoJS.enc.Utf8.parse(keyStr.substr(0, 16));
  163. var ps = new o2.widget.UUID().toString();
  164. //var srcs = CryptoJS.enc.Utf8.parse(ps);
  165. encrypted = CryptoJS.AES.encrypt(ps, key, {
  166. iv: iv,
  167. mode: CryptoJS.mode.CBC,
  168. padding: CryptoJS.pad.Pkcs7
  169. });
  170. var hash = o2.widget.Base64.encode(encrypted.ciphertext.toString());
  171. var salt = o2.widget.Base64.encode(keyStr);
  172. var oo_protection = this.createEl(oo_doc, "documentProtection");
  173. this.setAttrs(oo_protection, {
  174. "salt": salt,
  175. "hash": hash,
  176. "cryptSpinCount": "100000",
  177. "cryptAlgorithmSid": "4",
  178. "cryptAlgorithmType": "typeAny",
  179. "cryptAlgorithmClass": "hash",
  180. "cryptProviderType": "rsaAES",
  181. "enforcement": "1",
  182. "formatting": "1",
  183. "edit": "readOnly"
  184. });
  185. oo_settings.appendChild(oo_protection);
  186. var s = new XMLSerializer();
  187. resolve(s.serializeToString(oo_doc));
  188. }.bind(this));
  189. }.bind(this));
  190. }.bind(this));
  191. },
  192. saveAs: function(content, name){
  193. o2.saveAs(content, name);
  194. },
  195. getPageRule: function(cssRules){
  196. var pageRule = null;
  197. if (cssRules){
  198. for (var i=0; i<cssRules.length; i++){
  199. if (cssRules[i].type===CSSRule.PAGE_RULE){
  200. pageRule = cssRules[i];
  201. break;
  202. }
  203. }
  204. }
  205. return pageRule;
  206. },
  207. getPicExt: function(contentType){
  208. switch (contentType.toLowerCase()){
  209. case "image/tiff": return "tif";
  210. case "image/gif": return "gif";
  211. case "image/jpeg": return "jpg";
  212. case "image/png": return "png";
  213. }
  214. return "";
  215. },
  216. processWordRel_createRel: function(name, idx, oo_doc, oo_relationships){
  217. var oo_relationship = this.createEl(oo_doc, "Relationship", "rel");
  218. this.setAttrs(oo_relationship, {"Target": "media/"+name, "Type": "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", "Id": "rId"+idx}, false);
  219. oo_relationships.appendChild(oo_relationship);
  220. },
  221. processWordRel_getPicByUrl: function(pic, idx, oo_doc, oo_relationships){
  222. var name = "image"+idx;
  223. var headers = {
  224. "Authorization": layout.session.user.token
  225. };
  226. headers[o2.tokenName] = layout.session.user.token;
  227. return fetch(pic,{
  228. credentials: 'include', // include, same-origin, *omit
  229. headers: headers,
  230. mode: 'cors', // no-cors, cors, *same-origin
  231. }).then(function(res){
  232. var ext = this.getPicExt(res.headers.get("content-type"));
  233. var fullName = name+"."+ext;
  234. return res.blob().then(function(d){
  235. this.zip.file("word/media/"+fullName, d);
  236. this.processWordRel_createRel(fullName, idx, oo_doc, oo_relationships);
  237. }.bind(this));
  238. }.bind(this))
  239. },
  240. processWordRel_getPicByBase64: function(pic, idx, oo_doc, oo_relationships){
  241. var name = "image"+idx;
  242. var arr = pic.split(','), contentType = arr[0].match(/:(.*?);/)[1];
  243. var data = arr[1];
  244. var ext = this.getPicExt(contentType);
  245. var fullName = name+"."+ext;
  246. this.zip.file("word/media/"+fullName, data, {"base64": true});
  247. this.processWordRel_createRel(fullName, idx, oo_doc, oo_relationships);
  248. return Promise.resolve();
  249. },
  250. processWordRel: function(oo_string, data){
  251. var domparser = new DOMParser();
  252. var oo_doc = domparser.parseFromString(oo_string, "text/xml");
  253. var oo_relationships = oo_doc.documentElement;
  254. // var oo_relationshipList = oo_relationships.querySelectorAll("relationship");
  255. // var idx = oo_relationshipList.length+1;
  256. var promises = [];
  257. var idx = 11;
  258. this.pics.each(function(pic){
  259. if (pic.substring(0, 4).toLowerCase()==="data"){
  260. promises.push( this.processWordRel_getPicByBase64(pic, idx, oo_doc, oo_relationships));
  261. }else{
  262. promises.push( this.processWordRel_getPicByUrl(pic, idx, oo_doc, oo_relationships));
  263. }
  264. idx++;
  265. // this.zip.file("word/media/"+pic.name, new Blob(pic.data.data));
  266. //
  267. // var oo_relationship = this.createEl(oo_doc, "Relationship", false);
  268. // this.setAttrs(oo_relationship, {"Target": "media/"+pic.name, "Type": "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", "Id": "rId"+idx}, false);
  269. // oo_relationships.appendChild(oo_relationship);
  270. //
  271. // idx++;
  272. // promises.push(
  273. // res.blob().then(function(d){
  274. //
  275. // }.bind(this), function(){idx++;})
  276. // );
  277. }.bind(this));
  278. return Promise.all(promises).then(function(){
  279. var s = new XMLSerializer();
  280. return s.serializeToString(oo_doc);
  281. });
  282. },
  283. processWordDocument: function(oo_string, data){
  284. //wgxpath.install();
  285. var domparser = new DOMParser();
  286. var oo_doc = domparser.parseFromString(oo_string, "text/xml");
  287. //var oo_body = oo_doc.evaluate("//w:document/w:body", oo_doc, this.nsResolver, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  288. // var oo_document = oo_doc.documentElement;
  289. // var keys = Object.keys(this.options.xmlns);
  290. // keys.forEach(function(k){
  291. // oo_document.setAttribute(k, this.options.xmlns[k]);
  292. // }.bind(this));
  293. var oo_body = oo_doc.documentElement.querySelector("body");
  294. // var dom_div;
  295. // if (o2.typeOf(data) === "string"){
  296. var dom_div = new Element("div", {"styles": {
  297. "display": "block",
  298. "width": "442.2pt",
  299. "padding": "104.9pt 73.7pt 99.25pt 79.4pt",
  300. }}).set("html", data).inject(document.body);
  301. // }else{
  302. // dom_div = data;
  303. // }
  304. return new Promise(function(resolve){
  305. var imgs = dom_div.getElements("img");
  306. var promises = [];
  307. imgs.each(function(img){
  308. if (!img.complete){
  309. var p = new Promise(function(r){
  310. img.addEvent("load", function(){
  311. r();
  312. }.bind(this))
  313. });
  314. promises.push(p);
  315. }
  316. }.bind(this));
  317. Promise.all(promises).then(function(){
  318. var style= dom_div.getElement("style");
  319. if (style){
  320. var dom_pageRule = this.getPageRule(dom_div.getElement("style").sheet.cssRules);
  321. if (dom_pageRule) this.processPageSection(dom_pageRule, oo_body);
  322. }
  323. var dom_wordSection = dom_div.getElement(".WordSection1");
  324. if (dom_wordSection){
  325. this.processPageSection(dom_wordSection, oo_body);
  326. this.processDom(dom_wordSection, oo_body);
  327. }
  328. dom_div.destroy();
  329. var s = new XMLSerializer();
  330. resolve(s.serializeToString(oo_doc));
  331. }.bind(this));
  332. }.bind(this));
  333. // var style= dom_div.getElement("style");
  334. // if (style){
  335. // var dom_pageRule = this.getPageRule(dom_div.getElement("style").sheet.cssRules);
  336. // if (dom_pageRule) this.processPageSection(dom_pageRule, oo_body);
  337. // }
  338. // var dom_wordSection = dom_div.getElement(".WordSection1");
  339. // if (dom_wordSection){
  340. // this.processPageSection(dom_wordSection, oo_body);
  341. // this.processDom(dom_wordSection, oo_body);
  342. // }
  343. //
  344. //
  345. // dom_div.destroy();
  346. // var s = new XMLSerializer();
  347. // return s.serializeToString(oo_doc);
  348. },
  349. processDom: function(dom, oo_body, append, divAsP){
  350. dom = dom.getFirst();
  351. while (dom){
  352. if (dom.getStyle("display")!=="none"){
  353. if (dom.hasClass("doc_layout_redHeader")){
  354. var node = dom.firstChild;
  355. while (node){
  356. if (node.nodeType===Node.TEXT_NODE){
  357. var text = node.nodeValue.replace(/(?=\s|\n|\r|\t)[^\u3000]/g, '');
  358. if (text){
  359. var oo_p = this.createParagraphFromDom(dom, oo_body, append);
  360. this.processRun(dom, oo_p, dom, text);
  361. }
  362. }else{
  363. this.processParagraph(node, oo_body, append);
  364. //this.processDom(node, oo_body, append, true);
  365. }
  366. node = node.nextSibling;
  367. }
  368. // if (node && node.nodeType===Node.TEXT_NODE && node.nodeValue.trim()){
  369. // var oo_p = this.createParagraphFromDom(dom, oo_body, append);
  370. // this.processRun(dom, oo_p, dom, node.nodeValue);
  371. // }
  372. //
  373. // this.processParagraph(dom, oo_body, append);
  374. }else if (dom.hasClass("doc_layout_filetext")){
  375. this.processFiletext(dom, oo_body, append);
  376. }else if (dom.hasClass("doc_layout_attachment_text")){
  377. this.processFiletext(dom, oo_body, append);
  378. }else if (dom.hasClass("doc_layout_editionArea")){
  379. var wordSection = dom.getParent(".WordSection1");
  380. var h = this.pxToPt(dom.getSize().y);
  381. var top = this.pxToPt(dom.getPosition(wordSection).y);
  382. var pageH = this.pageHeight/20-this["page-margin-top"]/20-this["page-margin-bottom"]/20;
  383. var offsetY = 0;
  384. if (wordSection) {
  385. var pageBreakNodes = wordSection.querySelectorAll(".cke_pagebreak");
  386. pageBreakNodes.forEach(function(node){
  387. var t = this.pxToPt(node.getPosition(wordSection).y);
  388. offsetY += pageH-(t % pageH);
  389. }.bind(this));
  390. }
  391. top += offsetY;
  392. var tmpp = new Element("p").set('class', 'MsoNormal').set('html', '&nbsp;').inject(dom, "top");
  393. var lineHeight = tmpp.getSize().y;
  394. tmpp.destroy();
  395. var tmp = (top+h)/pageH;
  396. var tmp2 = (top+lineHeight)/pageH;
  397. var ps = tmp.toInt();
  398. var ps2 = tmp2.toInt();
  399. if (tmp>ps) ps = ps+1;
  400. if (tmp2>ps2) ps2 = ps2+1;
  401. if ((ps % 2)!=0){
  402. // var p = new Element("p", {"styles": {"page-break-after":"always"}}).inject(dom, "top");
  403. // if ((ps2 % 2)==0){new Element("p", {"styles": {"page-break-after":"always"}}).inject(dom, "top");}
  404. var p = new Element("p").set('class', 'MsoNormal').set('html', '&nbsp;').setStyles({"page-break-after":"always"}).inject(dom, "top");
  405. var lineHeight = p.getSize().y;
  406. new Element("p").set("html", "<br>").inject(dom, "top");
  407. if (ps2<ps) {
  408. if ((ps2 - tmp2) * pageH > this.pxToPt(lineHeight)) {
  409. new Element("p").set('class', 'MsoNormal').setStyles({"page-break-after": "always"}).inject(dom, "top");
  410. }
  411. }
  412. }
  413. this.processDom(dom, oo_body, append, divAsP);
  414. }else if (dom.tagName.toLowerCase() === "p" || ((!!divAsP || !!this.options.divAsP) && dom.tagName.toLowerCase() === "div")){
  415. this.processParagraph(dom, oo_body, append);
  416. // }else if (dom.tagName.toLowerCase() === "span") {
  417. // this.processRun(dom, oo_body, append);
  418. }else if (dom.tagName.toLowerCase() === "hr") {
  419. this.processHr(dom, oo_body, append);
  420. }else if (dom.tagName.toLowerCase() === "img") {
  421. var oo_p = this.createParagraphFromDom(dom, oo_body, append);
  422. this.processPic(dom, oo_p, append);
  423. }else if (dom.tagName.toLowerCase() === "table") {
  424. this.processTable(dom, oo_body, append);
  425. }else if (dom.tagName.toLowerCase() === "span") {
  426. if (!oo_body || oo_body.tagName.toString().toLowerCase()!=="w:p") var oo_body = this.createParagraphFromDom(dom, oo_body, append);
  427. this.processRun(dom, oo_body);
  428. }else{
  429. this.processDom(dom, oo_body, append, divAsP);
  430. }
  431. }
  432. dom = dom.getNext();
  433. }
  434. },
  435. // processFiletextParagraphRun: function(node, oo_p, p){
  436. // node = node.firstChild;
  437. // while (node){
  438. // if (node.nodeType===Node.TEXT_NODE){
  439. // if (node.nodeValue.trim()) this.processRun(node.parentElement, oo_p, p);
  440. // }else if (node.nodeType===Node.ELEMENT_NODE){
  441. // if (node.tagName.toLowerCase() === "span") {
  442. // this.processRun(node, oo_p, p);
  443. // }else if (node.tagName.toLowerCase() === "table") {
  444. // this.processTable(node, oo_p, p);
  445. // }else{
  446. // this.processFiletextParagraphRun(node, oo_p, p);
  447. // }
  448. // }else{
  449. // this.processFiletextParagraphRun(node, oo_p, p);
  450. // }
  451. // node = node.nextSibling;
  452. // }
  453. // },
  454. processFiletext: function(dom, oo_body, append){
  455. var node = dom.firstChild;
  456. if (node && node.nodeType===Node.TEXT_NODE){
  457. var text = node.nodeValue.replace(/(?=\s|\n|\r|\t)[^\u3000]/g, '');
  458. if (text){
  459. var oo_p = this.createParagraphFromDom(dom, oo_body, append);
  460. this.processRun(dom, oo_p, dom, text);
  461. }
  462. }
  463. node = dom.getFirst();
  464. while (node){
  465. if (node.tagName.toLowerCase() === "div" || node.tagName.toLowerCase() === "p") {
  466. this.processParagraph(node, oo_body, append);
  467. }else if (node.tagName.toLowerCase() === "img") {
  468. var oo_p = this.createParagraphFromDom(node, oo_body, append);
  469. this.processPic(node, oo_p, append);
  470. }else if (node.tagName.toLowerCase() === "table") {
  471. this.processTable(node, oo_body, append);
  472. }
  473. node = node.getNext();
  474. }
  475. },
  476. // processFiletextParagraph: function(){
  477. //
  478. // },
  479. isEmptyP: function(p){
  480. var oo_t = p.querySelector("t");
  481. var oo_drawing = p.querySelector("drawing");
  482. var oo_br = p.querySelector("br");
  483. return !oo_t && !oo_drawing && !oo_br;
  484. },
  485. processParagraphRun: function(node, oo_p, p, oo_body, append, ilvl){
  486. node = node.firstChild;
  487. while (node){
  488. if (node.nodeType===Node.TEXT_NODE){
  489. var text = node.nodeValue.replace(/(?=\s|\n|\r|\t)[^\u3000]/g, '');
  490. if (text) this.processRun(node.parentElement || node.parentNode, oo_p, p, text);
  491. }else if (node.nodeType===Node.ELEMENT_NODE){
  492. if (node.getStyle("display") === "none") {
  493. continue;
  494. }else if (node.tagName.toLowerCase() === "span") {
  495. this.processRun(node, oo_p, p);
  496. }else if (node.tagName.toLowerCase() === "br") {
  497. // if (node.nextSibling)
  498. if (node.nextSibling || (!node.nextSibling && !node.previousSibling)){
  499. this.processRun(node, oo_p, p, "", "br");
  500. }
  501. }else if (node.tagName.toLowerCase() === "div" || node.tagName.toLowerCase() === "p") {
  502. if (!this.isEmptyP(oo_p)){
  503. oo_p = this.createParagraphFromDom(node, oo_body, append);
  504. }else{
  505. this.setParagraphAttrFromDom(node, oo_p);
  506. }
  507. this.processParagraphRun(node, oo_p, p, oo_body, append, ilvl);
  508. }else if (node.tagName.toLowerCase() === "ul" || node.tagName.toLowerCase() === "ol") {
  509. this.processNumbering(node, oo_p, p, oo_body, append, ilvl);
  510. }else if (node.tagName.toLowerCase() === "table") {
  511. this.processTable(node, oo_body);
  512. if (this.isEmptyP(oo_p)) oo_p.destroy();
  513. oo_p = this.createParagraphFromDom(p, oo_body, append);
  514. }else if (node.tagName.toLowerCase() === "img") {
  515. this.processPic(node, oo_p, append);
  516. if (this.isEmptyP(oo_p)) oo_p.destroy();
  517. oo_p = this.createParagraphFromDom(p, oo_body, append);
  518. }else{
  519. this.processParagraphRun(node, oo_p, p, oo_body, append, ilvl);
  520. }
  521. }else{
  522. this.processParagraphRun(node, oo_p, p, oo_body, append, ilvl);
  523. }
  524. node = node.nextSibling;
  525. }
  526. },
  527. processNumbering: function(node, oo_p, p, oo_body, append, ilvl){
  528. ilvl = ilvl || 0;
  529. var nextIlvl = ilvl + 1;
  530. var numId = (node.tagName.toLowerCase() === "ul") ? "1" : "2";
  531. var lis = node.getChildren("li");
  532. for (var i=0; i<lis.length; i++){
  533. var li = lis[i];
  534. if (!this.isEmptyP(oo_p)) oo_p = this.createParagraphFromDom(li, oo_body, append);
  535. var oo_pPr = oo_p.querySelector("pPr");
  536. if (!oo_pPr){
  537. oo_pPr = this.createEl(oo_body.ownerDocument, "pPr");
  538. oo_p.appendChild(oo_pPr);
  539. }
  540. oo_numPr = this.createEl(oo_body.ownerDocument, "numPr");
  541. oo_ilvl = this.createEl(oo_body.ownerDocument, "ilvl");
  542. this.setAttrs(oo_ilvl, {"val": ilvl});
  543. oo_numId = this.createEl(oo_body.ownerDocument, "numId");
  544. this.setAttrs(oo_numId, {"val": numId});
  545. oo_numPr.appendChild(oo_ilvl);
  546. oo_numPr.appendChild(oo_numId);
  547. oo_pPr.appendChild(oo_numPr);
  548. this.processParagraphRun(li, oo_p, li, oo_body, append, nextIlvl);
  549. }
  550. },
  551. getPPrs: function(dom){
  552. var pPrs = {};
  553. var align = dom.getStyle("text-align");
  554. if (align){
  555. var jc = "start"
  556. switch (align){
  557. case "center": jc = "center"; break;
  558. case "right":
  559. case "end": jc = "end"; break;
  560. case "justify": jc = "both"; break;
  561. }
  562. pPrs.jc = {"val": jc};
  563. }
  564. var left = dom.getStyle("margin-left");
  565. if (left && left.toFloat()){
  566. var left = this.pxToPt(left)*20;
  567. if (left) {
  568. if (!pPrs.ind) pPrs.ind = {};
  569. pPrs.ind.left = left;
  570. }
  571. }
  572. var right = dom.getStyle("margin-left");
  573. if (right && right.toFloat()){
  574. var right = this.pxToPt(right)*20;
  575. if (right) {
  576. if (!pPrs.ind) pPrs.ind = {};
  577. pPrs.ind.right = right;
  578. }
  579. }
  580. var indent = dom.getStyle("text-indent");
  581. if (indent && indent.toFloat()){
  582. var indent = this.pxToPt(indent)*20;
  583. if (indent) {
  584. if (!pPrs.ind) pPrs.ind = {};
  585. if (indent>0){
  586. pPrs.ind.firstLine = indent;
  587. }else{
  588. pPrs.ind.hanging = Math.abs(indent);
  589. }
  590. }
  591. }
  592. var line = (dom.currentStyle) ? dom.currentStyle["line-height"] : dom.getStyle("line-height");
  593. //var line = dom.getStyle("line-height");
  594. var msoStyle = this.getMsoStyle(dom);
  595. var lineRule = msoStyle["mso-line-rule"] || "exact";
  596. if (line && parseFloat(line)){
  597. var line = this.pxToPt(line)*20;
  598. if (line) {
  599. pPrs.spacing = {
  600. lineRule: lineRule,
  601. line: line
  602. };
  603. }
  604. }
  605. // if (line && line.toFloat()){
  606. // var line = this.pxToPt(line)*20;
  607. // if (line) {
  608. // pPrs.spacing = {
  609. // lineRule: "exact",
  610. // line: line
  611. // };
  612. // }
  613. // }
  614. var pageBreak = dom.getStyle("page-break-after");
  615. if (pageBreak && pageBreak.toString().toLowerCase()=="avoid"){
  616. pPrs.keepNext = {};
  617. }
  618. if (pageBreak && pageBreak.toString().toLowerCase()=="always"){
  619. pPrs.pageBreak = {};
  620. }
  621. return pPrs;
  622. },
  623. setParagraphAttrFromDom: function(dom, oo_p){
  624. var pPrs = this.getPPrs(dom);
  625. var oo_pPr = oo_p.querySelector("pPr");
  626. if (!oo_pPr){
  627. oo_pPr = this.createEl(oo_p.ownerDocument, "pPr");
  628. oo_p.appendChild(oo_pPr);
  629. }
  630. Object.keys(pPrs).each(function(k){
  631. if (k=="pageBreak") {
  632. var oo_r = this.createEl(oo_p.ownerDocument, "r");
  633. var oo_br = this.createEl(oo_p.ownerDocument, "br");
  634. this.setAttrs(oo_br, {"type": "page"});
  635. oo_r.appendChild(oo_br);
  636. oo_p.appendChild(oo_r);
  637. }else{
  638. var node = oo_pPr.querySelector(k);
  639. if (!node) node = this.createEl(oo_p.ownerDocument, k);
  640. this.setAttrs(node, pPrs[k]);
  641. oo_pPr.appendChild(node);
  642. }
  643. }.bind(this));
  644. },
  645. createParagraphFromDom: function(dom, oo_body, append){
  646. var oo_p = this.createParagraph(oo_body.ownerDocument, {"pPrs": this.getPPrs(dom)});
  647. if (append){
  648. oo_body.appendChild(oo_p);
  649. }else{
  650. var oo_sectPr = this.getEl(oo_body, "sectPr");
  651. if (oo_sectPr){
  652. this.insertSiblings(oo_sectPr, [oo_p], "beforebegin");
  653. }else{
  654. this.insertChildren(oo_body, [oo_p]);
  655. }
  656. }
  657. return oo_p;
  658. },
  659. processParagraph: function(dom, oo_body, append){
  660. var oo_p = this.createParagraphFromDom(dom, oo_body, append);
  661. this.processParagraphRun(dom, oo_p, dom, oo_body, append);
  662. if (this.isEmptyP(oo_p)) oo_p.destroy();
  663. return oo_p;
  664. },
  665. getTableTblW: function(table){
  666. var type = "dxa";
  667. //var w = table.clientWidth;
  668. // var w;
  669. // var tag = table.tagName.toString().toLowerCase();
  670. // if (tag==="td" || tag==="th"){
  671. // w = table.clientWidth;
  672. // w = this.pxToPt(w);
  673. // }else{
  674. // w = table.style.width;
  675. // if (!w) w = table.style.width;
  676. // if (!w) w = table.get("width");
  677. // }
  678. var w = table.style.width;
  679. //if (!w) w = table.style.width;
  680. if (!w){
  681. w = table.get("width");
  682. //if (w) w = this.pxToPt(w);
  683. }
  684. //if (w) w = this.pxToPt(w);
  685. if (w && o2.typeOf(w)==="string"){
  686. var u = w.substring(w.length-1, w.length);
  687. if (u==="%"){
  688. w = w.toFloat()*50;
  689. type = "pct";
  690. }else{
  691. u = w.substring(w.length-2, w.length);
  692. if (u.toLowerCase()!=="pt"){
  693. w = this.pxToPt(w);
  694. }
  695. }
  696. }
  697. if (!w || !w.toFloat()){
  698. w = 0;
  699. type = "auto";
  700. }else{
  701. //w = w.toFloat();
  702. if (type === "dxa") w = w.toFloat()*20;
  703. }
  704. return {"w": w, "type": type};
  705. },
  706. getTableBorder: function(table, where){
  707. var attr = {
  708. "space": "0",
  709. "val": "single",
  710. "color": "auto",
  711. "sz": "0"
  712. }
  713. // var border = (table.currentStyle) ? table.currentStyle[("border-"+where+"-style").camelCase()] : table.getStyle("border-"+where);
  714. // if (border==="none") border = table.get("border");
  715. // if (!border || border==="none"){
  716. // attr.val = "none";
  717. // }else{
  718. var sz;
  719. var border = (table.currentStyle) ? table.currentStyle[("border-"+where+"-style").camelCase()] : table.getStyle("border-"+where);
  720. if (!border || border==="none"){
  721. sz = table.get("border");
  722. if (!sz || sz==="none") sz = table.getStyle("border-"+where+"-width");
  723. }else{
  724. sz = (table.currentStyle) ? table.currentStyle[("border-"+where+"-width").camelCase()] : table.getStyle("border-"+where+"-width");
  725. if (!sz || !sz.toFloat()) sz = table.get("border");
  726. }
  727. if (sz && o2.typeOf(sz)==="string"){
  728. u = sz.substring(sz.length-2, sz.length);
  729. if (u.toLowerCase()!=="pt"){
  730. sz = this.pxToPt(sz);
  731. }
  732. }
  733. if (!sz || !sz.toFloat()) sz = 0;
  734. attr.sz = sz.toFloat()*8;
  735. if (Browser.name=="firefox") attr.sz = attr.sz*1.25; //firefox边框计算问题
  736. var color = this.getColorHex(((table.currentStyle) ? table.currentStyle[("border-"+where+"-color").camelCase()] : table.getStyle("border-"+where+"-color")));
  737. if (!color) color = "auto";
  738. attr.color = color;
  739. var style = (table.currentStyle) ? table.currentStyle[("border-"+where+"-style").camelCase()] : table.getStyle("border-"+where+"-style");
  740. switch (style){
  741. case "dashed": case "dotted": case "double": attr.val = "double"; break;
  742. default: attr.val = "single";
  743. }
  744. if (attr.sz===0) attr.val="none";
  745. // }
  746. // var sz = table.get("border");
  747. // if (sz) sz = this.pxToPt(sz);
  748. // if (!sz) sz = table.getStyle("border-"+where+"-width");
  749. // if (!sz || !sz.toFloat()) sz = 0;
  750. // attr.sz = sz.toFloat()*20;
  751. var space = table.get("cellspacing");
  752. if (space) attr.space = this.pxToPt(space);
  753. return attr;
  754. },
  755. getTableTblGrid: function(table){
  756. var grids = [];
  757. var trs = table.rows;
  758. for (var i = 0; i < trs.length; i++){
  759. var idx = 0;
  760. tds = trs[i].cells;
  761. for (var j=0; j<tds.length; j++){
  762. var td = tds[j];
  763. var colspan = td.get("colspan");
  764. if (!colspan || colspan.toInt()===1) {
  765. while (grids.length<=idx) grids.push(0);
  766. var pt = this.pxToPt(td.clientWidth);
  767. if (pt>grids[idx]) grids[idx] = pt;
  768. }else{
  769. var addTd = colspan.toInt()-1;
  770. var tempTds = [];
  771. for (var n=0; n<addTd; n++) tempTds.push(new Element("td").inject(td, "after"));
  772. while (grids.length<=idx) grids.push(0);
  773. var pt = this.pxToPt(td.clientWidth);
  774. if (pt>grids[idx]) grids[idx] = pt;
  775. tempTds.each(function(tmpTd){
  776. idx++;
  777. while (grids.length<=idx) grids.push(0);
  778. var pt = this.pxToPt(tmpTd.clientWidth);
  779. if (pt>grids[idx]) grids[idx] = pt;
  780. }.bind(this));
  781. tempTds.each(function(tmpTd){
  782. tmpTd.destroy();
  783. });
  784. }
  785. idx++;
  786. }
  787. }
  788. return grids;
  789. },
  790. getTdValign: function(td){
  791. var v = "";
  792. var valign = td.getStyle("vertical-align") || td.get("valign");
  793. if (valign) {
  794. switch (valign){
  795. case "bottom": v = "bottom"; break;
  796. case "top": v = "top"; break;
  797. default: v = "center";
  798. }
  799. }
  800. return v;
  801. },
  802. getMsoStyle: function(dom){
  803. var o = {};
  804. var s = dom.getAttribute("style");
  805. if (s){
  806. var sList = s.split(/\s*;\s*/g);
  807. sList.map(function(style){
  808. var styles = style.split(/\s*:\s*/g);
  809. if (styles.length===2){
  810. if (styles[0].substr(0,3).toLowerCase()==="mso"){
  811. o[styles[0]] = styles[1];
  812. }
  813. }
  814. return false;
  815. });
  816. }
  817. var d = dom.dataset;
  818. if (d){
  819. Object.keys(d).forEach(function(k){
  820. if (k.substr(0,3).toLowerCase()==="mso"){
  821. o[k.hyphenate()] = d[k];
  822. }
  823. });
  824. }
  825. return o;
  826. },
  827. processTableDom: function(dom, oo_body, append, divAsP, oo_tc){
  828. dom = dom.firstChild;
  829. while (dom){
  830. if (dom.nodeType===Node.ELEMENT_NODE){
  831. if (dom.getStyle("display") === "none") {
  832. continue;
  833. }else if (dom.hasClass("doc_layout_filetext")){
  834. this.processFiletext(dom, oo_body, append);
  835. }else if (dom.tagName.toLowerCase() === "p" || ((!!divAsP || !!this.options.divAsP) && dom.tagName.toLowerCase() === "div")){
  836. this.processParagraph(dom, oo_body, append);
  837. // }else if (dom.tagName.toLowerCase() === "span") {
  838. // this.processRun(dom, oo_body, append);
  839. }else if (dom.tagName.toLowerCase() === "br") {
  840. if (!oo_body || oo_body.tagName.toString().toLowerCase()!=="w:p") var oo_body = this.createParagraphFromDom(dom, oo_body, dom.parentElement);
  841. this.processRun(dom, oo_body, append, "", "br");
  842. }else if (dom.tagName.toLowerCase() === "hr") {
  843. this.processHr(dom, oo_body, append);
  844. }else if (dom.tagName.toLowerCase() === "img") {
  845. if (!oo_body || oo_body.tagName.toString().toLowerCase()!=="w:p") var oo_body = this.createParagraphFromDom(dom, oo_body, dom.parentElement);
  846. this.processPic(dom, oo_body, append);
  847. }else if (dom.tagName.toLowerCase() === "table") {
  848. this.processTable(dom, oo_body, append);
  849. }else if (dom.tagName.toLowerCase() === "span") {
  850. if (!oo_body || oo_body.tagName.toString().toLowerCase()!=="w:p") var oo_body = this.createParagraphFromDom(dom, oo_body, dom.parentElement);
  851. this.processRun(dom, oo_body, append);
  852. }else{
  853. this.processTableDom(dom, oo_body, append, divAsP);
  854. }
  855. }else if (dom.nodeType===Node.TEXT_NODE){
  856. var text = dom.nodeValue.replace(/(?=\s|\n|\r|\t)[^\u3000]/g, '');
  857. if (text){
  858. if (!oo_body || oo_body.tagName.toString().toLowerCase()!=="w:p") var oo_body = this.createParagraphFromDom(dom.parentElement || dom.parentNode, oo_body, dom.parentElement);
  859. this.processRun(dom.parentElement || dom.parentNode, oo_body, append, text);
  860. }
  861. }else{
  862. this.processTableDom(dom, oo_body, append);
  863. }
  864. dom = dom.nextSibling;
  865. }
  866. },
  867. arrangeTable: function(table){
  868. //检查table,不合理的colspan
  869. var tableMatrix = [];
  870. var trs = table.rows;
  871. var rowspan = {};
  872. for (var i=0; i<trs.length; i++){
  873. tableMatrix[i] = [];
  874. //垂直合并单元格处理
  875. var tdIdx = 0;
  876. var rowspanObj = rowspan[tdIdx];
  877. while (rowspanObj && rowspanObj.count){
  878. rowspanObj.count--;
  879. tableMatrix[i].push({"td": rowspanObj.td, type: 0});
  880. if (rowspanObj.count<1) delete rowspan[tdIdx];
  881. tdIdx++;
  882. rowspanObj = rowspan[tdIdx];
  883. }
  884. var tds = trs[i].cells;
  885. for (var j=0; j<tds.length; j++){
  886. var td = tds[j];
  887. //记录实体单元格
  888. tableMatrix[i].push({"td": td, type: 1});
  889. //记录此单元格是否有垂直合并
  890. var cspan = td.get("colspan");
  891. var rspan = td.get("rowspan");
  892. if (rspan && parseInt(rspan)>1){
  893. rowspan[tdIdx] = {
  894. "td": td,
  895. "count": parseInt(rspan)-1
  896. };
  897. if (cspan && parseInt(cspan)>1){
  898. for (var n = 1; n<parseInt(cspan); n++){
  899. rowspan[tdIdx+n] = {
  900. "td": td,
  901. "count": parseInt(rspan)-1
  902. };
  903. }
  904. }
  905. }
  906. //补齐水平合并的虚拟单元格
  907. if (cspan && parseInt(cspan)>1){
  908. for (var n=1; n<parseInt(cspan); n++){
  909. tableMatrix[i].push({"td": td, type: 0});
  910. tdIdx++;
  911. }
  912. }
  913. //检查有没有之前的垂直合并内容
  914. var nextIdx = tdIdx+1;
  915. var rowspanObj = rowspan[nextIdx];
  916. while (rowspanObj && rowspanObj.count){
  917. rowspanObj.count--;
  918. tableMatrix[i].push({"td": rowspanObj.td, type: 0});
  919. if (rowspanObj.count<1) delete rowspan[nextIdx];
  920. nextIdx++;
  921. rowspanObj = rowspan[nextIdx];
  922. }
  923. // if (cspan && parseInt(cspan)>1){
  924. // if (rowspan[tdIdx]){
  925. // rowspan[tdIdx].count = (rowspan[tdIdx].count)*parseInt(cspan);
  926. // for (var n=1; n<parseInt(cspan); n++){
  927. // rowspan[tdIdx+n] = rowspan[tdIdx];
  928. // }
  929. // }
  930. // }
  931. tdIdx = nextIdx-1;
  932. tdIdx++;
  933. }
  934. }
  935. var y = tableMatrix.length;
  936. if (y>0){
  937. var x = tableMatrix[0].length;
  938. for (var x1=0; x1<x; x1++){
  939. var flag = 0;
  940. for (var y1=0; y1<y; y1++){
  941. if (tableMatrix[y1][x1].type!==0){
  942. flag=1;
  943. break;
  944. }
  945. }
  946. if (flag===0){
  947. for (var y1=0; y1<y; y1++){
  948. if (y1==tableMatrix[y1][x1].td.getParent("tr").rowIndex){
  949. if (tableMatrix[y1][x1].td){
  950. var colspan = tableMatrix[y1][x1].td.get("colspan");
  951. colspan = parseInt(colspan)-1;
  952. tableMatrix[y1][x1].td.set("colspan", colspan);
  953. }
  954. }
  955. }
  956. }
  957. }
  958. }
  959. },
  960. processTable(table, oo_body, append) {
  961. this.arrangeTable(table);
  962. var oo_doc = oo_body.ownerDocument;
  963. var oo_tbl = this.createEl(oo_doc, 'tbl');
  964. var oo_tblPr = this.createEl(oo_doc, 'tblPr');
  965. //表格宽度属性
  966. var oo_tblW = this.createEl(oo_doc, 'tblW');
  967. var tblW = this.getTableTblW(table);
  968. this.setAttrs(oo_tblW, tblW);
  969. oo_tblPr.appendChild(oo_tblW);
  970. //表格边框属性
  971. var oo_tblBorders = this.createEl(oo_doc, 'tblBorders');
  972. var oo_top = this.createEl(oo_doc, 'top');
  973. this.setAttrs(oo_top, this.getTableBorder(table, 'top'));
  974. var oo_start = this.createEl(oo_doc, 'start');
  975. this.setAttrs(oo_start, this.getTableBorder(table, 'left'));
  976. var oo_bottom = this.createEl(oo_doc, 'bottom');
  977. this.setAttrs(oo_bottom, this.getTableBorder(table, 'bottom'));
  978. var oo_end = this.createEl(oo_doc, 'end');
  979. this.setAttrs(oo_end, this.getTableBorder(table, 'right'));
  980. this.insertSiblings(oo_tblBorders, [oo_top, oo_start, oo_bottom, oo_end], 'beforeend');
  981. oo_tblPr.appendChild(oo_tblBorders);
  982. //表格边距
  983. var mar = parseFloat(table.get('cellpadding'));
  984. if (!mar) mar = 0;
  985. //if (mar){
  986. mar = this.pxToPt(mar) * 20;
  987. var left = table.getStyle('padding-left');
  988. var right = table.getStyle('padding-right');
  989. var top = table.getStyle('padding-top');
  990. var bottom = table.getStyle('padding-bottom');
  991. left = (left) ? this.pxToPt(left) * 20 : 0;
  992. right = (right) ? this.pxToPt(right) * 20 : 0;
  993. top = (top) ? this.pxToPt(top) * 20 : 0;
  994. bottom = (bottom) ? this.pxToPt(bottom) * 20 : 0;
  995. var oo_tblCellMar = this.createEl(oo_doc, 'tblCellMar');
  996. var oo_mar = this.createEl(oo_doc, 'start');
  997. this.setAttrs(oo_mar, {'type': 'dxa', 'w': left || mar});
  998. oo_tblCellMar.appendChild(oo_mar);
  999. oo_mar = this.createEl(oo_doc, 'end');
  1000. this.setAttrs(oo_mar, {'type': 'dxa', 'w': right || mar});
  1001. oo_tblCellMar.appendChild(oo_mar);
  1002. oo_mar = this.createEl(oo_doc, 'top');
  1003. this.setAttrs(oo_mar, {'type': 'dxa', 'w': top || mar});
  1004. oo_tblCellMar.appendChild(oo_mar);
  1005. oo_mar = this.createEl(oo_doc, 'bottom');
  1006. this.setAttrs(oo_mar, {'type': 'dxa', 'w': bottom || mar});
  1007. oo_tblCellMar.appendChild(oo_mar);
  1008. oo_tblPr.appendChild(oo_tblCellMar);
  1009. //}
  1010. //左右对齐
  1011. var align = table.get('align');
  1012. if (align) {
  1013. var jc = 'start';
  1014. switch (align) {
  1015. case 'center':
  1016. jc = 'center';
  1017. break;
  1018. case 'right':
  1019. case 'end':
  1020. jc = 'end';
  1021. break;
  1022. case 'justify':
  1023. jc = 'both';
  1024. break;
  1025. }
  1026. var oo_jc = this.createEl(oo_doc, 'jc');
  1027. this.setAttrs(oo_jc, {'val': jc});
  1028. oo_tblPr.appendChild(oo_jc);
  1029. }
  1030. //表格浮动
  1031. var floatTable = false;
  1032. var msoStyle = this.getMsoStyle(table);
  1033. var horzAnchor = msoStyle['mso-table-anchor-horizontal'];
  1034. var vertAnchor = msoStyle['mso-table-anchor-vertical'];
  1035. var tblpXSpec = msoStyle['mso-table-left'];
  1036. var tblpYSpec = msoStyle['mso-table-top'];
  1037. if (horzAnchor || vertAnchor || tblpXSpec || tblpYSpec) {
  1038. if (horzAnchor && horzAnchor !== 'page' && horzAnchor !== 'margin' && horzAnchor !== 'text') horzAnchor = 'margin';
  1039. if (vertAnchor && vertAnchor !== 'page' && vertAnchor !== 'margin' && vertAnchor !== 'text') vertAnchor = 'margin';
  1040. var o = {
  1041. 'horzAnchor': horzAnchor || null,
  1042. 'vertAnchor': vertAnchor || null,
  1043. 'tblpXSpec': tblpXSpec || null,
  1044. 'tblpYSpec': tblpYSpec || null
  1045. };
  1046. var oo_tblpPr = this.createEl(oo_doc, 'tblpPr');
  1047. this.setAttrs(oo_tblpPr, o);
  1048. oo_tblPr.appendChild(oo_tblpPr);
  1049. floatTable = true;
  1050. }
  1051. //表格背景
  1052. var bg = table.getStyle('background-color');
  1053. if (bg && bg !== 'transparent') {
  1054. bg = this.getColorHex(bg);
  1055. if (bg !== 'transparent') {
  1056. var oo_shd = this.createEl(oo_doc, 'shd');
  1057. this.setAttrs(oo_shd, {'val': 'clear', 'color': 'auto', 'fill': bg});
  1058. oo_tblPr.appendChild(oo_shd);
  1059. }
  1060. }
  1061. //表格必须使用固定宽度表格布局算法
  1062. //<w:tblLayout w:type="fixed"/>;
  1063. var oo_tblLayout = this.createEl(oo_doc, 'tblLayout');
  1064. this.setAttrs(oo_tblLayout, {'type': 'fixed'});
  1065. oo_tblPr.appendChild(oo_tblLayout);
  1066. oo_tbl.appendChild(oo_tblPr);
  1067. //表格网格
  1068. var grids = this.getTableTblGrid(table);
  1069. var oo_tblGrid = this.createEl(oo_doc, 'tblGrid');
  1070. grids.forEach(function (grid) {
  1071. var oo_gridCol = this.createEl(oo_doc, 'gridCol');
  1072. if (grid) this.setAttrs(oo_gridCol, {'w': grid * 20});
  1073. oo_tblGrid.appendChild(oo_gridCol);
  1074. }.bind(this));
  1075. oo_tbl.appendChild(oo_tblGrid);
  1076. var vmge = {};
  1077. var trs = table.rows;
  1078. for (var i = 0; i < trs.length; i++) {
  1079. var tr = trs[i];
  1080. if (!tr.hasClass('doc_layout_seal_position') || tr.querySelector('img')) {
  1081. var oo_tr = this.createEl(oo_doc, 'tr');
  1082. var oo_trPr = this.createEl(oo_doc, 'trPr');
  1083. oo_tr.appendChild(oo_trPr);
  1084. //行高度 设置模板中table的 data-compute-tr-height 为 y 时计算
  1085. if (table.dataset.computeTrHeight === 'y') {
  1086. var oo_trH = this.createEl(oo_doc, 'trHeight');
  1087. var trH = this.getTableTrH(tr);
  1088. this.setAttrs(oo_trH, trH);
  1089. oo_trPr.appendChild(oo_trH);
  1090. }
  1091. if (floatTable) {
  1092. // var oo_trPr = this.createEl(oo_doc, "trPr");
  1093. var oo_cantSplit = this.createEl(oo_doc, 'cantSplit');
  1094. var oo_tblHeader = this.createEl(oo_doc, 'tblHeader');
  1095. oo_trPr.appendChild(oo_cantSplit);
  1096. oo_trPr.appendChild(oo_tblHeader);
  1097. }
  1098. debugger;
  1099. var tdIdx = 0;
  1100. //垂直合并单元格
  1101. var nextIdx = tdIdx;
  1102. var mge = vmge['td' + nextIdx];
  1103. while (mge) {
  1104. // if (mge.idx>0){
  1105. vmge['td' + nextIdx].idx--;
  1106. var tcPr = vmge['td' + nextIdx].tcPr;
  1107. var oo_mtc = this.createEl(oo_doc, 'tc');
  1108. if (tcPr) {
  1109. var oo_mtcPr = tcPr.cloneNode(true);
  1110. var oo_mvMerge = oo_mtcPr.querySelector('vMerge');
  1111. if (oo_mvMerge) oo_mvMerge.destroy();
  1112. oo_mvMerge = this.createEl(oo_doc, 'vMerge');
  1113. oo_mtcPr.appendChild(oo_mvMerge);
  1114. if (mge.colspan && mge.colspan!='null'){
  1115. nextIdx = nextIdx + (parseInt(mge.colspan) - 1);
  1116. // tdIdx = tdIdx + (parseInt(mge.colspan) - 1);
  1117. var oo_gridSpan = this.createEl(oo_doc, 'gridSpan');
  1118. this.setAttrs(oo_gridSpan, {'val': mge.colspan});
  1119. oo_mtcPr.appendChild(oo_gridSpan);
  1120. }
  1121. oo_mtc.appendChild(oo_mtcPr);
  1122. } else {
  1123. var oo_mtcPr = this.createEl(oo_doc, 'tcPr');
  1124. var oo_mvMerge = this.createEl(oo_doc, 'vMerge');
  1125. oo_mtcPr.appendChild(oo_mvMerge);
  1126. if (mge.colspan && mge.colspan!='null'){
  1127. nextIdx = nextIdx + (parseInt( mge.colspan) - 1);
  1128. // tdIdx = tdIdx + (parseInt( mge.colspan) - 1);
  1129. var oo_gridSpan = this.createEl(oo_doc, 'gridSpan');
  1130. this.setAttrs(oo_gridSpan, {'val': mge.colspan});
  1131. oo_mtcPr.appendChild(oo_gridSpan);
  1132. }
  1133. oo_mtc.appendChild(oo_mtcPr);
  1134. }
  1135. // var oo_mtc = this.createEl(oo_doc, "tc");
  1136. // var oo_mtcPr = this.createEl(oo_doc, "tcPr");
  1137. // var oo_mvMerge = this.createEl(oo_doc, "vMerge");
  1138. // oo_mtcPr.appendChild(oo_mvMerge);
  1139. // oo_mtc.appendChild(oo_mtcPr);
  1140. var oo_mp = this.createEl(oo_doc, 'p');
  1141. oo_mtc.appendChild(oo_mp);
  1142. oo_tr.appendChild(oo_mtc);
  1143. if (vmge['td' + tdIdx].idx < 1) delete vmge['td' + tdIdx];
  1144. nextIdx++;
  1145. tdIdx = nextIdx;
  1146. mge = vmge['td' + nextIdx];
  1147. // }
  1148. // tdIdx++;
  1149. }
  1150. // tdIdx--;
  1151. var tds = tr.cells;
  1152. for (var j = 0; j < tds.length; j++) {
  1153. var td = tds[j];
  1154. var oo_tc = this.createEl(oo_doc, 'tc');
  1155. var oo_tcPr = this.createEl(oo_doc, 'tcPr');
  1156. //单元格宽度
  1157. var oo_tcW = this.createEl(oo_doc, 'tcW');
  1158. var tcW = this.getTableTblW(td);
  1159. this.setAttrs(oo_tcW, tcW);
  1160. //this.setAttrs(oo_tcW, {"w": this.pxToPt(td.clientWidth)*20, "type": "dxa"});
  1161. oo_tcPr.appendChild(oo_tcW);
  1162. // //单元格垂直对齐
  1163. // var valign = td.get('valign');
  1164. // v = (valign!=='top' && valign!=='bottom') ? 'center' : valign;
  1165. // var oo_vAlign = this.createEl(oo_doc, "vAlign");
  1166. // this.setAttrs(oo_vAlign, {'val': v})
  1167. // // <w:vAlign w:val="center"/>
  1168. // oo_tcPr.appendChild(oo_vAlign);
  1169. //单元格边框
  1170. var oo_tcBorders = this.createEl(oo_doc, 'tcBorders');
  1171. var oo_top = this.createEl(oo_doc, 'top');
  1172. this.setAttrs(oo_top, this.getTableBorder(td, 'top'));
  1173. var oo_start = this.createEl(oo_doc, 'start');
  1174. this.setAttrs(oo_start, this.getTableBorder(td, 'left'));
  1175. var oo_bottom = this.createEl(oo_doc, 'bottom');
  1176. this.setAttrs(oo_bottom, this.getTableBorder(td, 'bottom'));
  1177. var oo_end = this.createEl(oo_doc, 'end');
  1178. this.setAttrs(oo_end, this.getTableBorder(td, 'right'));
  1179. this.insertSiblings(oo_tcBorders, [oo_top, oo_start, oo_bottom, oo_end], 'beforeend');
  1180. oo_tcPr.appendChild(oo_tcBorders);
  1181. //单元格背景
  1182. var bg = td.getStyle('background-color');
  1183. if (bg && bg !== 'transparent') {
  1184. bg = this.getColorHex(bg);
  1185. if (bg !== 'transparent') {
  1186. var oo_shd = this.createEl(oo_doc, 'shd');
  1187. this.setAttrs(oo_shd, {'val': 'clear', 'color': 'auto', 'fill': bg});
  1188. oo_tcPr.appendChild(oo_shd);
  1189. }
  1190. }
  1191. //单元格边距
  1192. var left = td.getStyle('padding-left');
  1193. var right = td.getStyle('padding-right');
  1194. var top = td.getStyle('padding-top');
  1195. var bottom = td.getStyle('padding-bottom');
  1196. left = (left) ? this.pxToPt(left) * 20 : 0;
  1197. right = (right) ? this.pxToPt(right) * 20 : 0;
  1198. top = (top) ? this.pxToPt(top) * 20 : 0;
  1199. bottom = (bottom) ? this.pxToPt(bottom) * 20 : 0;
  1200. var oo_tcMar = this.createEl(oo_doc, 'tcMar');
  1201. var oo_mar = this.createEl(oo_doc, 'start');
  1202. this.setAttrs(oo_mar, {'type': 'dxa', 'w': left});
  1203. oo_tcMar.appendChild(oo_mar);
  1204. oo_mar = this.createEl(oo_doc, 'end');
  1205. this.setAttrs(oo_mar, {'type': 'dxa', 'w': right});
  1206. oo_tcMar.appendChild(oo_mar);
  1207. oo_mar = this.createEl(oo_doc, 'top');
  1208. this.setAttrs(oo_mar, {'type': 'dxa', 'w': top});
  1209. oo_tcMar.appendChild(oo_mar);
  1210. oo_mar = this.createEl(oo_doc, 'bottom');
  1211. this.setAttrs(oo_mar, {'type': 'dxa', 'w': bottom});
  1212. oo_tcMar.appendChild(oo_mar);
  1213. oo_tcPr.appendChild(oo_tcMar);
  1214. var v = this.getTdValign(td);
  1215. if (v) {
  1216. var oo_vAlign = this.createEl(oo_doc, 'vAlign');
  1217. this.setAttrs(oo_vAlign, {'val': v});
  1218. oo_tcPr.appendChild(oo_vAlign);
  1219. }
  1220. var oo_hideMark = this.createEl(oo_doc, 'hideMark');
  1221. oo_tcPr.appendChild(oo_hideMark);
  1222. //垂直合并单元格
  1223. var colspan = td.get('colspan');
  1224. var rowspan = td.get('rowspan');
  1225. if (rowspan && parseInt(rowspan) > 1) {
  1226. vmge['td' + tdIdx] = {
  1227. 'tcPr': oo_tcPr,
  1228. 'idx': parseInt(rowspan) - 1,
  1229. 'colspan': (colspan && colspan!=='null') ? colspan : null
  1230. };
  1231. var oo_vMerge = this.createEl(oo_doc, 'vMerge');
  1232. this.setAttrs(oo_vMerge, {'val': 'restart'});
  1233. oo_tcPr.appendChild(oo_vMerge);
  1234. }
  1235. //水平合并单元格
  1236. if (colspan && parseInt(colspan) > 1) {
  1237. tdIdx = tdIdx + (parseInt(colspan) - 1);
  1238. var oo_gridSpan = this.createEl(oo_doc, 'gridSpan');
  1239. this.setAttrs(oo_gridSpan, {'val': colspan});
  1240. oo_tcPr.appendChild(oo_gridSpan);
  1241. }
  1242. oo_tc.appendChild(oo_tcPr);
  1243. //表格内容;
  1244. this.processTableDom(td, oo_tc, td, true, oo_tc);
  1245. var pflag = false;
  1246. var node = oo_tc.firstChild;
  1247. while (node) {
  1248. if (node.tagName === 'w:p') {
  1249. pflag = true;
  1250. break;
  1251. }
  1252. node = node.nextSibling;
  1253. }
  1254. if (!pflag) {
  1255. var oo_p = this.createEl(oo_doc, 'p');
  1256. oo_tc.appendChild(oo_p);
  1257. }
  1258. oo_tr.appendChild(oo_tc);
  1259. //垂直合并单元格
  1260. tdIdx++;
  1261. var nextIdx = tdIdx;
  1262. var mge = vmge['td' + nextIdx];
  1263. while (mge) {
  1264. vmge['td' + nextIdx].idx--;
  1265. var tcPr = vmge['td' + nextIdx].tcPr;
  1266. var oo_mtc = this.createEl(oo_doc, 'tc');
  1267. if (tcPr) {
  1268. var oo_mtcPr = tcPr.cloneNode(true);
  1269. var oo_mvMerge = oo_mtcPr.querySelector('vMerge');
  1270. if (oo_mvMerge) oo_mvMerge.destroy();
  1271. oo_mvMerge = this.createEl(oo_doc, 'vMerge');
  1272. oo_mtcPr.appendChild(oo_mvMerge);
  1273. if (mge.colspan && mge.colspan!='null'){
  1274. nextIdx = nextIdx + (parseInt( mge.colspan) - 1);
  1275. // tdIdx = tdIdx + (parseInt( mge.colspan) - 1);
  1276. var oo_gridSpan = this.createEl(oo_doc, 'gridSpan');
  1277. this.setAttrs(oo_gridSpan, {'val': mge.colspan});
  1278. oo_mtcPr.appendChild(oo_gridSpan);
  1279. }
  1280. oo_mtc.appendChild(oo_mtcPr);
  1281. } else {
  1282. var oo_mtcPr = this.createEl(oo_doc, 'tcPr');
  1283. var oo_mvMerge = this.createEl(oo_doc, 'vMerge');
  1284. oo_mtcPr.appendChild(oo_mvMerge);
  1285. if (mge.colspan && mge.colspan!='null'){
  1286. nextIdx = nextIdx + (parseInt( mge.colspan) - 1);
  1287. // tdIdx = tdIdx + (parseInt( mge.colspan) - 1);
  1288. var oo_gridSpan = this.createEl(oo_doc, 'gridSpan');
  1289. this.setAttrs(oo_gridSpan, {'val': mge.colspan});
  1290. oo_mtcPr.appendChild(oo_gridSpan);
  1291. }
  1292. oo_mtc.appendChild(oo_mtcPr);
  1293. }
  1294. var oo_mp = this.createEl(oo_doc, 'p');
  1295. oo_mtc.appendChild(oo_mp);
  1296. oo_tr.appendChild(oo_mtc);
  1297. if (vmge['td' + tdIdx].idx < 1) delete vmge['td' + tdIdx];
  1298. nextIdx++;
  1299. mge = vmge['td' + nextIdx];
  1300. }
  1301. // if (colspan && parseInt(colspan) > 1) {
  1302. // if (vmge['td' + tdIdx]) {
  1303. // vmge['td' + tdIdx].idx = (vmge['td' + tdIdx].idx) * parseInt(colspan);
  1304. // for (var n = 1; n < parseInt(colspan); n++) {
  1305. // var m = tdIdx + n;
  1306. // rowspan[m] = vmge['td' + tdIdx];
  1307. // }
  1308. // }
  1309. // }
  1310. tdIdx = nextIdx - 1;
  1311. tdIdx++;
  1312. }
  1313. oo_tbl.appendChild(oo_tr);
  1314. }
  1315. }
  1316. if (append) {
  1317. oo_body.appendChild(oo_tbl);
  1318. } else {
  1319. var oo_sectPr = this.getEl(oo_body, 'sectPr');
  1320. if (oo_sectPr) {
  1321. this.insertSiblings(oo_sectPr, [oo_tbl], 'beforebegin');
  1322. } else {
  1323. this.insertChildren(oo_body, [oo_tbl]);
  1324. }
  1325. }
  1326. },
  1327. pxToPt: function(px){
  1328. if (!px) return 0;
  1329. var v = px;
  1330. if (px && o2.typeOf(px)==="string"){
  1331. u = px.substring(px.length-2, px.length);
  1332. if (u.toLowerCase()!=="pt"){
  1333. v = (px.toFloat()/this.dpi)*72;
  1334. }else{
  1335. v = px.toFloat();
  1336. }
  1337. }else{
  1338. v = (px.toFloat()/this.dpi)*72;
  1339. }
  1340. return v;
  1341. },
  1342. setPics: function(img){
  1343. if (!this.pics || !this.pics.length) this.pics = [];
  1344. this.pics.push(img.src);
  1345. },
  1346. processPic: function(img, oo_p, append){
  1347. if (!img.src) return "";
  1348. this.setPics(img);
  1349. var idx = this.rid;
  1350. this.rid++;
  1351. var oo_doc = oo_p.ownerDocument;
  1352. //
  1353. // var oo_p = this.createParagraph(oo_doc, {});
  1354. // if (append){
  1355. // oo_body.appendChild(oo_p);
  1356. // }else{
  1357. // var oo_sectPr = this.getEl(oo_body, "sectPr");
  1358. // if (oo_sectPr){
  1359. // this.insertSiblings(oo_sectPr, [oo_p], "beforebegin");
  1360. // }else{
  1361. // this.insertChildren(oo_body, [oo_p]);
  1362. // }
  1363. // }
  1364. var oo_run = this.createRun(oo_doc, {"rPrs": {"noProof":{}}});
  1365. var oo_drawing = this.createEl(oo_doc, "drawing");
  1366. var msoStyle = this.getMsoStyle(img);
  1367. var position = img.getStyle("position");
  1368. var p = (msoStyle["mso-position-vertical"]==="absolute") ? "anchor" : "inline";
  1369. var oo_position;
  1370. if (p==="anchor"){
  1371. //var pos = img.getPosition(img.getParent(".WordSection1"));
  1372. //var pos = img.getPosition();
  1373. var positionV = this.pxToPt(msoStyle["mso-top"]);
  1374. var positionH = this.pxToPt(msoStyle["mso-left"]);
  1375. if (!positionV || !positionH){
  1376. var pos = img.getPosition(img.getOffsetParent());
  1377. if (!positionH) positionH = this.pxToPt(pos.x);
  1378. if (!positionV) positionV = this.pxToPt(pos.y);
  1379. }
  1380. positionH = (positionH*12700).toInt();
  1381. positionV = (positionV*12700).toInt();
  1382. var oo_anchor = this.createEl(oo_doc, "anchor", "wp");
  1383. var behindDoc = (msoStyle["mso-behinddoc"]=="true") ? "1" : "0";
  1384. this.setAttrs(oo_anchor, {
  1385. "distT": "0", "distB": "0", "distL": "0", "distR": "0", "simplePos": "0","behindDoc": behindDoc, "relativeHeight": "500", "locked": "0", "layoutInCell":"1", "allowOverlap": "0"
  1386. }, false);
  1387. oo_run.appendChild(oo_drawing);
  1388. oo_drawing.appendChild(oo_anchor);
  1389. var oo_simplePos = this.createEl(oo_doc, "simplePos", "wp");
  1390. this.setAttrs(oo_simplePos, {"x": "0", "y": "0"}, false);
  1391. var relativeFrom = msoStyle["mso-position-horizontal-relative"] || "margin";
  1392. var oo_positionH = this.createEl(oo_doc, "positionH", "wp");
  1393. this.setAttrs(oo_positionH, {"relativeFrom": relativeFrom}, false);
  1394. var oo_posOffset = this.createEl(oo_doc, "posOffset", "wp");
  1395. oo_posOffset.appendChild(oo_doc.createTextNode(positionH));
  1396. oo_positionH.appendChild(oo_posOffset);
  1397. relativeFrom = msoStyle["mso-position-vertical-relative"] || "margin";
  1398. var oo_positionV = this.createEl(oo_doc, "positionV", "wp");
  1399. this.setAttrs(oo_positionV, {"relativeFrom": relativeFrom}, false);
  1400. var oo_posOffset = this.createEl(oo_doc, "posOffset", "wp");
  1401. oo_posOffset.appendChild(oo_doc.createTextNode(positionV)); //此处需要根据行高来设置数值,暂时固定数值
  1402. oo_positionV.appendChild(oo_posOffset);
  1403. oo_position = oo_anchor;
  1404. this.insertSiblings(oo_position, [oo_simplePos, oo_positionH, oo_positionV], "beforeend");
  1405. }else{
  1406. var oo_inline = this.createEl(oo_doc, "inline", "wp");
  1407. this.setAttrs(oo_inline, {
  1408. "distT": "0", "distB": "0", "distL": "0", "distR": "0"
  1409. }, false);
  1410. oo_run.appendChild(oo_drawing);
  1411. oo_drawing.appendChild(oo_inline);
  1412. oo_position = oo_inline;
  1413. }
  1414. var oo_extent = this.createEl(oo_doc, "extent", "wp");
  1415. var cx = this.pxToPt(img.clientWidth)*12700;
  1416. var cy = this.pxToPt(img.clientHeight)*12700;
  1417. this.setAttrs(oo_extent, {"cx": cx, "cy": cy}, false); //(pt*12700)
  1418. var oo_effectExtent = this.createEl(oo_doc, "effectExtent", "wp");
  1419. this.setAttrs(oo_effectExtent, {"l": "0", "t": "0", "r": "0", "b": "0"}, false);
  1420. var oo_wrapNone = this.createEl(oo_doc, "wrapNone", "wp");
  1421. var oo_docPr = this.createEl(oo_doc, "docPr", "wp");
  1422. id = (Math.random()*100).toInt();
  1423. this.setAttrs(oo_docPr, {"id": id, "name": "PIC"+id}, false); //id设置随机整数
  1424. var oo_cNvGraphicFramePr = this.createEl(oo_doc, "cNvGraphicFramePr", "wp");
  1425. var oo_graphicFrameLocks = this.createEl(oo_doc, "graphicFrameLocks", "a");
  1426. this.setAttrs(oo_graphicFrameLocks, {"noChangeAspect": "1"}, false);
  1427. oo_cNvGraphicFramePr.appendChild(oo_graphicFrameLocks);
  1428. this.insertSiblings(oo_position, [oo_extent, oo_effectExtent, oo_wrapNone, oo_docPr, oo_cNvGraphicFramePr], "beforeend");
  1429. var oo_graphic = this.createEl(oo_doc, "graphic", "a");
  1430. var oo_graphicData = this.createEl(oo_doc, "graphicData", "a");
  1431. this.setAttrs(oo_graphicData, {"uri": "http://schemas.openxmlformats.org/drawingml/2006/picture"}, false);
  1432. var oo_pic = this.createEl(oo_doc, "pic", "pic");
  1433. this.insertChildren(oo_position, [oo_graphic, oo_graphicData, oo_pic], "beforeend");
  1434. var oo_nvPicPr = this.createEl(oo_doc, "nvPicPr", "pic");
  1435. var oo_cNvPr = this.createEl(oo_doc, "cNvPr", "pic");
  1436. this.setAttrs(oo_cNvPr, {"id": id, "name": "PIC"+id}, false);
  1437. var oo_cNvPicPr = this.createEl(oo_doc, "cNvPicPr", "pic");
  1438. oo_nvPicPr.appendChild(oo_cNvPr);
  1439. oo_nvPicPr.appendChild(oo_cNvPicPr);
  1440. oo_pic.appendChild(oo_nvPicPr);
  1441. var oo_blipFill = this.createEl(oo_doc, "blipFill", "pic");
  1442. var oo_blip = this.createEl(oo_doc, "blip", "a");
  1443. this.setAttrs(oo_blip, {"embed": "rId"+idx}, "r");
  1444. this.setAttrs(oo_blip, {"cstate": "print"}, false);
  1445. var oo_extLst = this.createEl(oo_doc, "extLst", "a");
  1446. var oo_ext = this.createEl(oo_doc, "ext", "a");
  1447. this.setAttrs(oo_ext, {"uri": "{28A0092B-C50C-407E-A947-70E740481C1C}"}, false);
  1448. var oo_useLocalDpi = this.createEl(oo_doc, "useLocalDpi", "a14");
  1449. this.setAttrs(oo_useLocalDpi, {"val": "0"}, false);
  1450. oo_ext.appendChild(oo_useLocalDpi);
  1451. oo_extLst.appendChild(oo_ext);
  1452. oo_blip.appendChild(oo_extLst);
  1453. oo_blipFill.appendChild(oo_blip);
  1454. var oo_stretch = this.createEl(oo_doc, "stretch", "a");
  1455. var oo_fillRect = this.createEl(oo_doc, "fillRect", "a");
  1456. oo_stretch.appendChild(oo_fillRect);
  1457. oo_blipFill.appendChild(oo_stretch);
  1458. oo_pic.appendChild(oo_blipFill);
  1459. var oo_spPr = this.createEl(oo_doc, "spPr", "pic");
  1460. var oo_xfrm = this.createEl(oo_doc, "xfrm", "a");
  1461. var oo_off = this.createEl(oo_doc, "off", "a");
  1462. this.setAttrs(oo_off, {"x": "0", "y": "0"}, false);
  1463. var oo_ext = this.createEl(oo_doc, "ext", "a");
  1464. this.setAttrs(oo_ext, {"cx": cx, "cy": cy}, false);
  1465. oo_xfrm.appendChild(oo_off);
  1466. oo_xfrm.appendChild(oo_ext);
  1467. oo_spPr.appendChild(oo_xfrm);
  1468. var oo_prstGeom = this.createEl(oo_doc, "prstGeom", "a");
  1469. this.setAttrs(oo_prstGeom, {"prst": "rect"}, false);
  1470. var oo_avLst = this.createEl(oo_doc, "avLst", "a");
  1471. oo_prstGeom.appendChild(oo_avLst);
  1472. oo_spPr.appendChild(oo_prstGeom);
  1473. oo_pic.appendChild(oo_spPr);
  1474. // " <pic:blipFill>\n" +
  1475. // " <a:blip r:embed=\"rId4\" cstate=\"print\">\n" +
  1476. // " <a:extLst>\n" +
  1477. // " <a:ext uri=\"{28A0092B-C50C-407E-A947-70E740481C1C}\">\n" +
  1478. // " <a14:useLocalDpi xmlns:a14=\"http://schemas.microsoft.com/office/drawing/2010/main\" val=\"0\"/>\n" +
  1479. // " </a:ext>\n" +
  1480. // " </a:extLst>\n" +
  1481. // " </a:blip>\n" +
  1482. // " <a:stretch>\n" +
  1483. // " <a:fillRect/>\n" +
  1484. // " </a:stretch>\n" +
  1485. // " </pic:blipFill>\n" +
  1486. // " <pic:spPr>\n" +
  1487. // " <a:xfrm>\n" +
  1488. // " <a:off x=\"0\" y=\"0\"/>\n" +
  1489. // " <a:ext cx=\""+cx+"\" cy=\""+cy+"\"/>\n" +
  1490. // " </a:xfrm>\n" +
  1491. // " <a:prstGeom prst=\"rect\">\n" +
  1492. // " <a:avLst/>\n" +
  1493. // " </a:prstGeom>\n" +
  1494. // " </pic:spPr>";
  1495. //
  1496. // //oo_pic.appendHTML(inner, "top");
  1497. // oo_pic.innerHTML = inner;
  1498. oo_p.appendChild(oo_run);
  1499. },
  1500. processHr: function(hr, oo_body, append){
  1501. var oo_doc = oo_body.ownerDocument;
  1502. var oo_p = this.createParagraph(oo_doc, {
  1503. "pPrs": {
  1504. "spacing": {
  1505. "lineRule": "exact",
  1506. "line": "40" //段落行高设置为固定值2pt
  1507. }
  1508. }
  1509. });
  1510. if (append){
  1511. oo_body.appendChild(oo_p);
  1512. }else{
  1513. var oo_sectPr = this.getEl(oo_body, "sectPr");
  1514. if (oo_sectPr){
  1515. this.insertSiblings(oo_sectPr, [oo_p], "beforebegin");
  1516. }else{
  1517. this.insertChildren(oo_body, [oo_p]);
  1518. }
  1519. }
  1520. var oo_run = this.createRun(oo_doc, {"rPrs": {"noProof":{}}});
  1521. var oo_drawing = this.createEl(oo_doc, "drawing");
  1522. var oo_anchor = this.createEl(oo_doc, "anchor", "wp");
  1523. this.setAttrs(oo_anchor, {
  1524. "distT": "0", "distB": "0", "distL": "0", "distR": "0", "simplePos": "false","behindDoc": "false","relativeHeight": "500", "locked": "false", "layoutInCell":"true", "allowOverlap": "false"
  1525. }, false);
  1526. oo_run.appendChild(oo_drawing);
  1527. oo_drawing.appendChild(oo_anchor);
  1528. var oo_simplePos = this.createEl(oo_doc, "simplePos", "wp");
  1529. this.setAttrs(oo_simplePos, {"x": "0", "y": "0"}, false);
  1530. var oo_positionH = this.createEl(oo_doc, "positionH", "wp");
  1531. this.setAttrs(oo_positionH, {"relativeFrom": "column"}, false);
  1532. var oo_posOffset = this.createEl(oo_doc, "posOffset", "wp");
  1533. oo_posOffset.appendChild(oo_doc.createTextNode("0"));
  1534. oo_positionH.appendChild(oo_posOffset);
  1535. var oo_positionV = this.createEl(oo_doc, "positionV", "wp");
  1536. this.setAttrs(oo_positionV, {"relativeFrom": "paragraph"}, false);
  1537. var oo_posOffset = this.createEl(oo_doc, "posOffset", "wp");
  1538. oo_posOffset.appendChild(oo_doc.createTextNode("3810")); //此处需要根据行高来设置数值,暂时固定数值
  1539. oo_positionV.appendChild(oo_posOffset);
  1540. var oo_extent = this.createEl(oo_doc, "extent", "wp");
  1541. var cx = this.pxToPt(hr.clientWidth)*12700;
  1542. this.setAttrs(oo_extent, {"cx": cx, "cy": "0"}, false); //cx为线长度(pt*12700)
  1543. var oo_effectExtent = this.createEl(oo_doc, "effectExtent", "wp");
  1544. this.setAttrs(oo_effectExtent, {"l": "0", "t": "0", "r": "0", "b": "0"}, false);
  1545. var oo_wrapNone = this.createEl(oo_doc, "wrapNone", "wp");
  1546. var oo_docPr = this.createEl(oo_doc, "docPr", "wp");
  1547. id = (Math.random()*100).toInt();
  1548. this.setAttrs(oo_docPr, {"id": id, "name": "Red Line"}, false); //id设置随机整数
  1549. var oo_cNvGraphicFramePr = this.createEl(oo_doc, "cNvGraphicFramePr", "wp");
  1550. this.insertSiblings(oo_anchor, [oo_simplePos, oo_positionH, oo_positionV, oo_extent, oo_effectExtent, oo_wrapNone, oo_docPr, oo_cNvGraphicFramePr], "beforeend");
  1551. var oo_graphic = this.createEl(oo_doc, "graphic", "a");
  1552. var oo_graphicData = this.createEl(oo_doc, "graphicData", "a");
  1553. this.setAttrs(oo_graphicData, {"uri": "http://schemas.microsoft.com/office/word/2010/wordprocessingShape"}, false);
  1554. var oo_wsp = this.createEl(oo_doc, "wsp", "wps");
  1555. this.insertChildren(oo_anchor, [oo_graphic, oo_graphicData, oo_wsp], "beforeend");
  1556. var oo_cNvCnPr = this.createEl(oo_doc, "cNvCnPr", "wps");
  1557. var oo_spPr = this.createEl(oo_doc, "spPr", "wps")
  1558. var oo_xfrm = this.createEl(oo_doc, "xfrm", "a");
  1559. var oo_off = this.createEl(oo_doc, "off", "a");
  1560. this.setAttrs(oo_off, {"x": "0", "y": "0"}, false);
  1561. var oo_ext = this.createEl(oo_doc, "ext", "a");
  1562. this.setAttrs(oo_ext, {"cx": cx, "cy": "0"}, false);
  1563. this.insertSiblings(oo_xfrm, [oo_off, oo_ext], "beforeend");
  1564. var oo_prstGeom = this.createEl(oo_doc, "prstGeom", "a");
  1565. this.setAttrs(oo_prstGeom, {"prst": "line"}, false);
  1566. var oo_avLst = this.createEl(oo_doc, "avLst", "a");
  1567. oo_prstGeom.appendChild(oo_avLst);
  1568. var oo_ln = this.createEl(oo_doc, "ln", "a");
  1569. var w = this.pxToPt(hr.clientHeight)*12700;
  1570. this.setAttrs(oo_ln, {"w": w}, false); //线的粗细 pt*12700
  1571. var oo_solidFill = this.createEl(oo_doc, "solidFill", "a");
  1572. var oo_srgbClr = this.createEl(oo_doc, "srgbClr", "a");
  1573. var color = this.getColorHex(hr.get("color"));
  1574. if (!color) color = this.getColorHex(hr.getStyle("background-color"));
  1575. if (!color) color = "FF0000";
  1576. this.setAttrs(oo_srgbClr, {"val": color}, false); //line color
  1577. oo_solidFill.appendChild(oo_srgbClr);
  1578. oo_ln.appendChild(oo_solidFill);
  1579. this.insertSiblings(oo_spPr, [oo_xfrm, oo_prstGeom, oo_ln], "beforeend");
  1580. var oo_style = this.createEl(oo_doc, "style", "wps");
  1581. //oo_style.innerHTML = '<a:lnRef idx="1"><a:schemeClr val="accent1"/></a:lnRef><a:fillRef idx="0"><a:schemeClr val="accent1"/></a:fillRef><a:effectRef idx="0"><a:schemeClr val="accent1"/></a:effectRef><a:fontRef idx="minor"><a:schemeClr val="tx1"/></a:fontRef>';
  1582. var oo_lnRef = this.createEl(oo_doc, "lnRef", "a");
  1583. this.setAttrs(oo_lnRef, {"idx": "1"}, false);
  1584. var oo_schemeClr = this.createEl(oo_doc, "schemeClr", "a");
  1585. this.setAttrs(oo_schemeClr, {"val": "accent1"}, false);
  1586. oo_lnRef.appendChild(oo_schemeClr);
  1587. var oo_fillRef = this.createEl(oo_doc, "fillRef", "a");
  1588. this.setAttrs(oo_fillRef, {"idx": "0"}, false);
  1589. var oo_schemeClr = this.createEl(oo_doc, "schemeClr", "a");
  1590. this.setAttrs(oo_schemeClr, {"val": "accent1"}, false);
  1591. oo_fillRef.appendChild(oo_schemeClr);
  1592. var oo_effectRef = this.createEl(oo_doc, "effectRef", "a");
  1593. this.setAttrs(oo_effectRef, {"idx": "0"}, false);
  1594. var oo_schemeClr = this.createEl(oo_doc, "schemeClr", "a");
  1595. this.setAttrs(oo_schemeClr, {"val": "accent1"}, false);
  1596. oo_effectRef.appendChild(oo_schemeClr);
  1597. var oo_fontRef = this.createEl(oo_doc, "fontRef", "a");
  1598. this.setAttrs(oo_fontRef, {"idx": "minor"}, false);
  1599. var oo_schemeClr = this.createEl(oo_doc, "schemeClr", "a");
  1600. this.setAttrs(oo_schemeClr, {"val": "tx1"}, false);
  1601. oo_fontRef.appendChild(oo_schemeClr);
  1602. this.insertSiblings(oo_style, [oo_lnRef, oo_fillRef, oo_effectRef, oo_fontRef], "beforeend");
  1603. var oo_bodyPr = this.createEl(oo_doc, "bodyPr", "wps");
  1604. this.insertSiblings(oo_wsp, [oo_cNvCnPr, oo_spPr, oo_style, oo_bodyPr], "beforeend");
  1605. var w = hr.style.width;
  1606. if (w && o2.typeOf(w)==="string"){
  1607. var u = w.substring(w.length-1, w.length);
  1608. if (u==="%"){
  1609. w = w.toFloat()*1000;
  1610. var oo_sizeRelH = this.createEl(oo_doc, "sizeRelH", "wp14");
  1611. this.setAttrs(oo_sizeRelH, {"relativeFrom": "margin"}, false);
  1612. var oo_pctWidth = this.createEl(oo_doc, "pctWidth", "wp14");
  1613. oo_pctWidth.appendChild(oo_doc.createTextNode(w));
  1614. oo_sizeRelH.appendChild(oo_pctWidth);
  1615. oo_anchor.appendChild(oo_sizeRelH);
  1616. }
  1617. }
  1618. oo_p.appendChild(oo_run);
  1619. },
  1620. processRunFont: function(node, rPrs, font){
  1621. //字体处理缩放
  1622. var msoStyle = this.getMsoStyle(node);
  1623. if (msoStyle["mso-font-width"]) rPrs.w = {"val": msoStyle["mso-font-width"].toFloat()};
  1624. //处理字号
  1625. if (msoStyle["mso-ansi-font-size"]) rPrs.sz = {"val": this.parseFontSize(msoStyle["mso-ansi-font-size"])*2};
  1626. if (msoStyle["mso-hansi-font-size"]) rPrs.sz = {"val": this.parseFontSize(msoStyle["mso-hansi-font-size"])*2};
  1627. if (msoStyle["mso-font-size"]) rPrs.sz = {"val": this.parseFontSize(msoStyle["mso-font-size"])*2};
  1628. if (msoStyle["mso-fareast-font-size"]) rPrs.sz = {"val": this.parseFontSize(msoStyle["mso-fareast-font-size"])*2};
  1629. //处理字体
  1630. if (msoStyle["mso-ansi-font-family"]){
  1631. if (!font) font = { "hint": "eastAsia" };
  1632. font.ascii = this.parseFont(msoStyle["mso-ansi-font-family"]);
  1633. }
  1634. if (msoStyle["mso-hansi-font-family"]){
  1635. if (!font) font = { "hint": "eastAsia" };
  1636. font.hAnsi = this.parseFont(msoStyle["mso-hansi-font-family"]);
  1637. }
  1638. if (msoStyle["mso-font-family"]){
  1639. if (!font) font = { "hint": "eastAsia" };
  1640. font.eastAsia = this.parseFont(msoStyle["mso-font-family"]);
  1641. }
  1642. if (msoStyle["mso-fareast-font-family"]){
  1643. if (!font) font = { "hint": "eastAsia" };
  1644. font.eastAsia = this.parseFont(msoStyle["mso-fareast-font-family"]);
  1645. }
  1646. },
  1647. parseFont: function(name){
  1648. if (name.substr(0, 1)==="\""){
  1649. return name.substr(1, name.length-2);
  1650. }else{
  1651. return name;
  1652. }
  1653. },
  1654. parseFontSize: function(sz){
  1655. var size = sz;
  1656. if (size && o2.typeOf(size)==="string"){
  1657. u = size.substring(size.length-2, size.length);
  1658. if (u.toLowerCase()!=="pt"){
  1659. size = Math.round(this.pxToPt(size));
  1660. }
  1661. }
  1662. return size.toFloat();
  1663. },
  1664. processRun: function(span, oo_p, p, text, br){
  1665. var rPrs = {"noProof": {}};
  1666. var font = null;
  1667. var styles = span.getStyles("font-size", "color", "letter-spacing", "font-weight", "font-family", "line-height");
  1668. var keys = Object.keys(styles);
  1669. var msoStyle = this.getMsoStyle(span);
  1670. styles["font-size"] = window.getComputedStyle(span).fontSize;
  1671. for (var i = 0; i<keys.length; i++){
  1672. switch (keys[i]){
  1673. case "font-size":
  1674. rPrs.sz = {"val": this.parseFontSize(styles["font-size"])*2};
  1675. break;
  1676. case "color":
  1677. rPrs.color = {"val": this.getColorHex(styles["color"])};
  1678. break;
  1679. case "letter-spacing":
  1680. //实际测试发现letter-spacing * 0.55 转换word比较合适
  1681. rPrs.spacing = {"val": (styles["letter-spacing"].toFloat()*20 || 0)};
  1682. break;
  1683. case "font-weight":
  1684. var b = styles["font-weight"];
  1685. if (b.toLowerCase()=="normal"){
  1686. //nothing
  1687. }else if (b.toLowerCase()=="bold") {
  1688. rPrs.b = {"val": "true"};
  1689. }else{
  1690. var n = b.toFloat();
  1691. if (n>=600) rPrs.b = {"val": "true"};
  1692. }
  1693. break;
  1694. case "font-family":
  1695. var fonts = styles["font-family"].split(/,\s*/);
  1696. font = {
  1697. "hint": "eastAsia",
  1698. "eastAsia": this.parseFont(fonts[fonts.length-1])
  1699. }
  1700. if (fonts.length>1) font.other = this.parseFont(fonts[0]);
  1701. break;
  1702. case "line-height":
  1703. var h = this.pxToPt(styles["line-height"])*20;
  1704. if (oo_p){
  1705. var oo_pPrs = oo_p.getElementsByTagNameNS(this.nsResolver("w"), "pPr");
  1706. var oo_pPr = (oo_pPrs.length) ? oo_pPrs.item(0) : null;
  1707. if (!oo_pPr){
  1708. oo_pPr = this.createEl(oo_p.ownerDocument,"pPr");
  1709. oo_p.appendChild(oo_pPr);
  1710. }
  1711. var oo_spacings = oo_pPr.getElementsByTagNameNS(this.nsResolver("w"), "spacing");
  1712. var oo_spacing = (oo_spacings.length) ? oo_spacings.item(0) : null;
  1713. if (!oo_spacing){
  1714. oo_spacing = this.createEl(oo_p.ownerDocument,"spacing");
  1715. oo_pPr.appendChild(oo_spacing);
  1716. }
  1717. var line = oo_spacing.getAttributeNS(this.nsResolver("w"), "line");
  1718. if (line<h){
  1719. this.setAttrs(oo_spacing, {"lineRule": "exact", "line": h});
  1720. }
  1721. }
  1722. default:
  1723. //nothing
  1724. }
  1725. }
  1726. if (msoStyle["mso-letter-spacing"]){
  1727. rPrs.spacing = {"val": (msoStyle["mso-letter-spacing"].toFloat()*20 || 0)};
  1728. }
  1729. if (p) this.processRunFont(p, rPrs, font);
  1730. this.processRunFont(span, rPrs, font);
  1731. var runPrs = {"rPrs": rPrs, "font": font};
  1732. if (!text && !br){
  1733. //if (span.tagName.toString().toLowerCase()==="span"){
  1734. this.processRunTextDom(span, oo_p, runPrs);
  1735. //}
  1736. }else{
  1737. runPrs.br = br;
  1738. var oo_run = this.createRun(oo_p.ownerDocument, runPrs);
  1739. if (text){
  1740. text = text.replace(/(?=\s|\n|\r|\t)[^\u3000]/g, '');
  1741. var oo_t = this.createEl(oo_run.ownerDocument,"t");
  1742. oo_t.appendChild(oo_run.ownerDocument.createTextNode(text));
  1743. oo_run.appendChild(oo_t);
  1744. }
  1745. oo_p.appendChild(oo_run);
  1746. }
  1747. },
  1748. processRunSpan: function(span, oo_p, runPrs) {
  1749. //var runPrs = {"rPrs": rPrs, "font": font, "text": text, "br": br};
  1750. var rPrs = Object.clone(runPrs.rPrs);
  1751. var font = Object.clone(runPrs.font);
  1752. var styles = span.getStyles("font-size", "color", "letter-spacing", "font-weight", "font-family")
  1753. var keys = Object.keys(styles);
  1754. styles["font-size"] = window.getComputedStyle(span).fontSize;
  1755. for (var i = 0; i<keys.length; i++){
  1756. switch (keys[i]){
  1757. case "font-size":
  1758. rPrs.sz = {"val": this.parseFontSize(styles["font-size"])*2};
  1759. break;
  1760. case "color":
  1761. rPrs.color = {"val": this.getColorHex(styles["color"])};
  1762. break;
  1763. case "letter-spacing":
  1764. //实际测试发现letter-spacing * 0.55 转换word比较合适
  1765. rPrs.spacing = {"val": (styles["letter-spacing"].toFloat()*20 || 0)};
  1766. break;
  1767. case "font-weight":
  1768. var b = styles["font-weight"];
  1769. if (b.toLowerCase()=="normal"){
  1770. //nothing
  1771. }else if (b.toLowerCase()=="bold") {
  1772. rPrs.b = {"val": "true"};
  1773. }else{
  1774. var n = b.toFloat();
  1775. if (n>=600) rPrs.b = {"val": "true"};
  1776. }
  1777. break;
  1778. case "font-family":
  1779. var fonts = styles["font-family"].split(/,\s*/);
  1780. if (!font) font = {};
  1781. font.hint = "eastAsia";
  1782. font.eastAsia = this.parseFont(fonts[fonts.length-1]);
  1783. // font = {
  1784. // "hint": "eastAsia",
  1785. // "eastAsia": this.parseFont(fonts[fonts.length-1])
  1786. // }
  1787. if (fonts.length>1) font.other = this.parseFont(fonts[0]);
  1788. break;
  1789. default:
  1790. //nothing
  1791. }
  1792. }
  1793. this.processRunFont(span, rPrs, font);
  1794. var runPrs = {"rPrs": rPrs, "font": font};
  1795. this.processRunTextDom(span, oo_p, runPrs);
  1796. },
  1797. processRunTextDom: function(span, oo_p, runPrs){
  1798. var node = span.firstChild;
  1799. while (node){
  1800. if (node.nodeType===Node.ELEMENT_NODE){
  1801. if (node.getStyle("display") === "none") {
  1802. continue;
  1803. }else if (node.tagName.toLowerCase() === "span"){
  1804. this.processRunSpan(node, oo_p, runPrs);
  1805. }else if (node.tagName.toLowerCase() === "br"){
  1806. runPrs.br = "br";
  1807. var oo_run = this.createRun(oo_p.ownerDocument, runPrs);
  1808. oo_p.appendChild(oo_run);
  1809. runPrs.br = "";
  1810. }else{
  1811. this.processRunTextDom(node, oo_p, runPrs);
  1812. }
  1813. }else if (node.nodeType===Node.TEXT_NODE){
  1814. //if (node.nodeValue.trim()){
  1815. var oo_run = this.createRun(oo_p.ownerDocument, runPrs);
  1816. var oo_t = this.createEl(oo_p.ownerDocument,"t");
  1817. oo_t.appendChild(oo_p.ownerDocument.createTextNode(node.nodeValue));
  1818. oo_run.appendChild(oo_t);
  1819. oo_p.appendChild(oo_run);
  1820. //}
  1821. }else{
  1822. this.processRunTextDom(node, oo_p, runPrs);
  1823. }
  1824. node = node.nextSibling;
  1825. }
  1826. },
  1827. processPageSection: function(dom_pageRule, oo_body){
  1828. var oo_sectPr = this.getOrCreateEl(oo_body, "sectPr");
  1829. if (oo_sectPr){
  1830. for (var i = 0; i<dom_pageRule.style.length; i++){
  1831. switch (dom_pageRule.style[i]){
  1832. case "size":
  1833. if( dom_pageRule.style["size"] ){
  1834. var v = dom_pageRule.style["size"].split(/\s/);
  1835. var w = v[0].toFloat()*20, h=v[1].toFloat()*20;
  1836. var oo_pgSz = this.getOrCreateEl(oo_sectPr, "pgSz");
  1837. this.setAttrs(oo_pgSz, {"w": w, "h": h});
  1838. this.pageHeight = h;
  1839. this.pageWidth = w;
  1840. }
  1841. break;
  1842. case "margin-top":
  1843. case "margin-right":
  1844. case "margin-bottom":
  1845. case "margin-left":
  1846. var p = dom_pageRule.style[i].split("-")[1];
  1847. var v = dom_pageRule.style["margin"+p.capitalize()].toFloat()*20;
  1848. var oo_pgMar = this.getOrCreateEl(oo_sectPr, "pgMar");
  1849. if (p==="bottom"){
  1850. var footer = oo_pgMar.getAttributeNS(this.nsResolver("w"), "footer");
  1851. if ((parseInt(footer)+454)>v){
  1852. footer = v-454;
  1853. this.setAttrs(oo_pgMar, {"footer": footer});
  1854. }
  1855. }
  1856. var attrs = {};
  1857. attrs[p] = v
  1858. this.setAttrs(oo_pgMar, attrs);
  1859. this["page-"+dom_pageRule.style[i]] = v;
  1860. break;
  1861. case "line-height":
  1862. case "letter-spacing":
  1863. var oo_docGrid = this.getOrCreateEl(oo_sectPr, "docGrid");
  1864. var lh = dom_pageRule.style["lineHeight"].toFloat()*20;
  1865. var cs = dom_pageRule.style["letterSpacing"].toFloat()*4096;
  1866. var attrs = {"type": "linesAndChars"};
  1867. if (lh) attrs["linePitch"] = lh;
  1868. //if (cs) attrs["charSpace"] = cs;
  1869. this.setAttrs(oo_docGrid, attrs);
  1870. break;
  1871. default:
  1872. //nothing
  1873. }
  1874. }
  1875. if (!this.pageHeight) this.pageHeight = this.options.pageHeight.toFloat()*20;
  1876. if (!this.pageWidth) this.pageWidth = this.options.pageWidth.toFloat()*20;
  1877. if (!this["page-margin-left"]) this["page-margin-left"] = this.options["page-margin-left"].toFloat()*20;
  1878. if (!this["page-margin-right"]) this["page-margin-right"] = this.options["page-margin-right"].toFloat()*20;
  1879. if (!this["page-margin-top"]) this["page-margin-top"] = this.options["page-margin-top"].toFloat()*20;
  1880. if (!this["page-margin-bottom"]) this["page-margin-bottom"] = this.options["page-margin-bottom"].toFloat()*20;
  1881. if (!this.options.firstPageNumber){
  1882. var oo_titlePg = this.createEl(oo_body.ownerDocument, "titlePg");
  1883. oo_sectPr.appendChild(oo_titlePg);
  1884. }
  1885. }
  1886. },
  1887. getOrCreateEl: function(el, tag){
  1888. var node = this.getEl(el, tag);
  1889. if (!node){
  1890. node = this.createEl(el.ownerDocument, tag);
  1891. el.appendChild(node);
  1892. }
  1893. return node;
  1894. },
  1895. createParagraph: function(xmlDoc, options){
  1896. var p = this.createEl(xmlDoc,"p");
  1897. var pPr = this.createEl(xmlDoc,"pPr");
  1898. p.appendChild(pPr);
  1899. /*
  1900. * //如:对齐方式描述如下
  1901. * {
  1902. * "jc": { val: "both" },
  1903. * }
  1904. * */
  1905. if (options && options.pPrs){
  1906. Object.keys(options.pPrs).each(function(k){
  1907. if (k=="pageBreak"){
  1908. var oo_r = this.createEl(xmlDoc, "r");
  1909. var oo_br = this.createEl(xmlDoc, "br");
  1910. this.setAttrs(oo_br, {"type": "page"});
  1911. oo_r.appendChild(oo_br);
  1912. p.appendChild(oo_r);
  1913. }else{
  1914. var node = this.createEl(xmlDoc, k);
  1915. this.setAttrs(node, options.pPrs[k]);
  1916. pPr.appendChild(node);
  1917. }
  1918. }.bind(this));
  1919. }
  1920. return p;
  1921. },
  1922. createRun: function(xmlDoc, options){
  1923. var r = this.createEl(xmlDoc, "r");
  1924. var rPr = this.createEl(xmlDoc,"rPr");
  1925. r.appendChild(rPr);
  1926. if (options && options.text){
  1927. var t = this.createEl(xmlDoc,"t");
  1928. options.text = options.text.replace(/[\u200B-\u200D\uFEFF]/g, '');
  1929. t.appendChild(xmlDoc.createTextNode(options.text));
  1930. r.appendChild(t);
  1931. }
  1932. if (options && options.br){
  1933. var oo_br = this.createEl(xmlDoc,"br");
  1934. r.appendChild(oo_br);
  1935. }
  1936. if (options && options.font){
  1937. var rFonts = this.createEl(xmlDoc,"rFonts");
  1938. var font = {
  1939. "eastAsia": options.font.eastAsia || options.font.font,
  1940. "ascii": options.font.ascii || options.font.other || options.font.eastAsia || options.font.font,
  1941. "hAnsi": options.font.hAnsi || options.font.other || options.font.eastAsia || options.font.font,
  1942. }
  1943. if (options.font.hint) font.hint = options.font.hint;
  1944. this.setAttrs(rFonts, font);
  1945. rPr.appendChild(rFonts);
  1946. }
  1947. /*
  1948. * //如:粗体和字体颜色描述如下
  1949. * {
  1950. * "b": { val: "true" },
  1951. * "color": { val: "FF0000" }
  1952. * }
  1953. * */
  1954. if (options && options.rPrs){
  1955. Object.keys(options.rPrs).each(function(k){
  1956. var node = this.createEl(xmlDoc, k);
  1957. this.setAttrs(node, options.rPrs[k]);
  1958. rPr.appendChild(node);
  1959. }.bind(this));
  1960. }
  1961. return r;
  1962. },
  1963. insertChildren: function(p, els, position){
  1964. this.insertAdjacent(p, els, position, "beforeend");
  1965. },
  1966. insertSiblings: function(p, els, position){
  1967. this.insertAdjacent(p, els, position, "afterend");
  1968. },
  1969. insertAdjacent: function(p, els, posFirst, posNext){
  1970. var pos = posFirst || "beforeend";
  1971. var posMapping = {
  1972. 'beforebegin': "before",
  1973. 'afterbegin': "top",
  1974. 'beforeend': "bottom",
  1975. 'afterend': "after"
  1976. };
  1977. els.each(function(e){
  1978. (p.insertAdjacentElement) ? p.insertAdjacentElement(pos, e) : e.inject(p, posMapping[pos]);
  1979. p = e;
  1980. pos = posNext || "afterend";
  1981. });
  1982. },
  1983. getEl: function(el, tag, ns){
  1984. return this.getEls(el, tag, ns)[0];
  1985. },
  1986. getEls: function(el, tag, ns){
  1987. var n = ns || "w";
  1988. return el.getElementsByTagNameNS(this.nsResolver(n), tag);
  1989. },
  1990. createEl: function(xmlDoc, tag, ns){
  1991. if (ns===false){
  1992. return xmlDoc.createElement(tag);
  1993. }else{
  1994. var n = ns || "w";
  1995. if (false && Browser.name==="ie"){
  1996. return xmlDoc.createElement(n+":"+tag);
  1997. }
  1998. return xmlDoc.createElementNS(this.nsResolver(n), n+":"+tag);
  1999. }
  2000. },
  2001. setAttr: function(node, name, value, ns){
  2002. if (ns===false){
  2003. node.setAttribute(name, value);
  2004. }else{
  2005. var n = ns || "w";
  2006. if (false && Browser.name==="ie") {
  2007. node.setAttribute(n+":"+name, value);
  2008. }else{
  2009. node.setAttributeNS(this.nsResolver(n), n+":"+name, value);
  2010. }
  2011. }
  2012. },
  2013. setAttrs: function(node, attrs, ns){
  2014. if (ns===false){
  2015. Object.keys(attrs).forEach(function(key){
  2016. node.setAttribute(key, attrs[key]);
  2017. });
  2018. }else{
  2019. var n = this.nsResolver(ns || "w");
  2020. Object.keys(attrs).forEach(function(key){
  2021. if (false && Browser.name==="ie") {
  2022. node.setAttribute((ns || "w")+":"+key, attrs[key]);
  2023. }else{
  2024. node.setAttributeNS(n, (ns || "w")+":"+key, attrs[key]);
  2025. }
  2026. });
  2027. }
  2028. },
  2029. nsResolver: function(prefix){
  2030. var ns = {
  2031. "w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main",
  2032. "wpc": "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas",
  2033. "cx": "http://schemas.microsoft.com/office/drawing/2014/chartex",
  2034. "cx1": "http://schemas.microsoft.com/office/drawing/2015/9/8/chartex",
  2035. "cx2": "http://schemas.microsoft.com/office/drawing/2015/10/21/chartex",
  2036. "cx3": "http://schemas.microsoft.com/office/drawing/2016/5/9/chartex",
  2037. "cx4": "http://schemas.microsoft.com/office/drawing/2016/5/10/chartex",
  2038. "cx5": "http://schemas.microsoft.com/office/drawing/2016/5/11/chartex",
  2039. "cx6": "http://schemas.microsoft.com/office/drawing/2016/5/12/chartex",
  2040. "cx7": "http://schemas.microsoft.com/office/drawing/2016/5/13/chartex",
  2041. "cx8": "http://schemas.microsoft.com/office/drawing/2016/5/14/chartex",
  2042. "mc": "http://schemas.openxmlformats.org/markup-compatibility/2006",
  2043. "aink": "http://schemas.microsoft.com/office/drawing/2016/ink",
  2044. "am3d": "http://schemas.microsoft.com/office/drawing/2017/model3d",
  2045. "o": "urn:schemas-microsoft-com:office:office",
  2046. "r": "http://schemas.openxmlformats.org/officeDocument/2006/relationships",
  2047. "m": "http://schemas.openxmlformats.org/officeDocument/2006/math",
  2048. "v": "urn:schemas-microsoft-com:vml",
  2049. "wp14": "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing",
  2050. "wp": "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing",
  2051. "w10": "urn:schemas-microsoft-com:office:word",
  2052. "w14": "http://schemas.microsoft.com/office/word/2010/wordml",
  2053. "w15": "http://schemas.microsoft.com/office/word/2012/wordml",
  2054. "w16cex": "http://schemas.microsoft.com/office/word/2018/wordml/cex",
  2055. "w16cid": "http://schemas.microsoft.com/office/word/2016/wordml/cid",
  2056. "w16": "http://schemas.microsoft.com/office/word/2018/wordml",
  2057. "w16sdtdh": "http://schemas.microsoft.com/office/word/2020/wordml/sdtdatahash",
  2058. "w16se": "http://schemas.microsoft.com/office/word/2015/wordml/symex",
  2059. "wpg": "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup",
  2060. "wpi": "http://schemas.microsoft.com/office/word/2010/wordprocessingInk",
  2061. "wne": "http://schemas.microsoft.com/office/word/2006/wordml",
  2062. "wps": "http://schemas.microsoft.com/office/word/2010/wordprocessingShape",
  2063. "a": "http://schemas.openxmlformats.org/drawingml/2006/main",
  2064. "pic": "http://schemas.openxmlformats.org/drawingml/2006/picture",
  2065. "a14": "http://schemas.microsoft.com/office/drawing/2010/main",
  2066. "rel": "http://schemas.openxmlformats.org/package/2006/relationships"
  2067. };
  2068. return ns[prefix] || null;
  2069. },
  2070. getColorHex: function(clr){
  2071. if (!clr) return "";
  2072. var colorKeys = {
  2073. "black": "000000",
  2074. "silver": "c0c0c0",
  2075. "gray": "808080",
  2076. "white": "ffffff",
  2077. "maroon": "800000",
  2078. "red": "ff0000",
  2079. "purple": "800080",
  2080. "fuchsia": "ff00ff",
  2081. "green": "008000",
  2082. "lime": "00ff00",
  2083. "olive": "808000",
  2084. "yellow": "ffff00",
  2085. "navy": "000080",
  2086. "blue": "0000ff",
  2087. "teal": "008080",
  2088. "aqua": "00ffff",
  2089. "initial": "000000"
  2090. }
  2091. if (colorKeys[clr]) return colorKeys[clr];
  2092. var f = clr.substr(0,1);
  2093. if (f==="#") return clr.replace("#", "");
  2094. return clr.rgbToHex() || clr;
  2095. }
  2096. });