-
Notifications
You must be signed in to change notification settings - Fork 3
/
index-web.js
1049 lines (973 loc) · 30.6 KB
/
index-web.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* global define module require */
(function (factory) {
if (typeof define === "function" && define.amd) {
// AMD. Register as an anonymous module.
define(['prefix-umd'], factory);
} else if (typeof module === "object" && module.exports) {
// Node/CommonJS
module.exports = factory(require('prefix-umd'));
} else {
// Browser globals
window.SimpleScreenshot = factory(window.Prefix);
}
})(function (Prefix) {
//includes方法兼容
if (!Array.prototype.includes) {
Object.defineProperty(Array.prototype, 'includes', {
value: function (valueToFind, fromIndex) {
let o = Object(this);
let len = o.length >>> 0;
if (len === 0) {
return false;
}
let n = fromIndex | 0;
let k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
function sameValueZero(x, y) {
return x === y || (typeof x === 'number' && typeof y === 'number' && isNaN(x) && isNaN(y));
}
while (k < len) {
if (sameValueZero(o[k], valueToFind)) {
return true;
}
k++;
}
return false;
}
});
}
let dashTransform = Prefix.dash("transform");
let dashOrigin = Prefix.dash("transformOrigin");
let URL_REGEX = /url\(['"]?([^'"]+?)['"]?\)/g;
// 根据 computedStyle 判断伪类是否存在,此外还可以通过遍历 styleSheets 来判断。
let contentExit = function (computedStyle) {
let content = computedStyle['content'];
let display = computedStyle['display'];
let exist = true;
if (content != null) {
content = content.trim();
if (content == 'none' || (content == '' && display == 'inline')) {
exist = false;
}
} else {
exist = false;
}
if (!display || display == 'none') {
exist = false;
}
return exist;
}
let RESOURCE_NAME = [];
let RESOURCE_DATA = [];
function isFunction(obj) { // 判断对象是否是function
return obj && {}.toString.call(obj) === "[object Function]";
}
const COMPUTED_STYLE = [
"width",
"height",
"background-attachment",
"background-blend-mode",
"background-clip",
"-webkit-background-clip",
"background-color",
"background-image",
"background-origin",
"background-position",
"background-repeat",
"background-size",
"border-bottom-color",
"border-bottom-left-radius",
"border-bottom-right-radius",
"border-bottom-style",
"border-bottom-width",
"border-collapse",
"border-image-outset",
"border-image-repeat",
"border-image-slice",
"border-image-source",
"border-image-width",
"border-left-color",
"border-left-style",
"border-left-width",
"border-right-color",
"border-right-style",
"border-right-width",
"border-top-color",
"border-top-left-radius",
"border-top-right-radius",
"border-top-style",
"border-top-width",
"bottom",
"box-shadow",
"box-sizing",
"break-after",
"break-before",
"break-inside",
"caption-side",
"clear",
"clip",
"color",
"content",
"cursor",
"direction",
"display",
"empty-cells",
"float",
"font-family",
"font-kerning",
"font-optical-sizing",
"font-size",
"font-stretch",
"font-style",
"font-variant",
"font-variant-ligatures",
"font-variant-caps",
"font-variant-numeric",
"font-variant-east-asian",
"font-weight",
"image-rendering",
"isolation",
"justify-items",
"justify-self",
"left",
"letter-spacing",
"line-height",
"list-style-image",
"list-style-position",
"list-style-type",
"margin-bottom",
"margin-left",
"margin-right",
"margin-top",
"max-height",
"max-width",
"min-height",
"min-width",
"mix-blend-mode",
"object-fit",
"object-position",
"offset-distance",
"offset-path",
"offset-rotate",
"opacity",
"orphans",
"outline-color",
"outline-offset",
"outline-style",
"outline-width",
"overflow-anchor",
"overflow-wrap",
"overflow-x",
"overflow-y",
"padding-bottom",
"padding-left",
"padding-right",
"padding-top",
"pointer-events",
"position",
"resize",
"right",
"scroll-behavior",
"speak",
"table-layout",
"tab-size",
"text-align",
"text-align-last",
"text-decoration",
"text-decoration-line",
"text-decoration-style",
"text-decoration-color",
"text-decoration-skip-ink",
"text-underline-position",
"text-indent",
"text-rendering",
"text-shadow",
"text-size-adjust",
"text-overflow",
"text-transform",
"top",
"touch-action",
"unicode-bidi",
"vertical-align",
"visibility",
"white-space",
"widows",
"will-change",
"word-break",
"word-spacing",
"z-index",
"zoom",
"-webkit-appearance",
"backface-visibility",
"-webkit-border-horizontal-spacing",
"-webkit-border-image",
"-webkit-border-vertical-spacing",
"-webkit-box-align",
"-webkit-box-decoration-break",
"-webkit-box-direction",
"-webkit-box-flex",
"-webkit-box-ordinal-group",
"-webkit-box-orient",
"-webkit-box-pack",
"-webkit-box-reflect",
"column-count",
"column-gap",
"column-rule-color",
"column-rule-style",
"column-rule-width",
"column-span",
"column-width",
"backdrop-filter",
"align-content",
"align-items",
"align-self",
"flex-basis",
"flex-grow",
"flex-shrink",
"flex-direction",
"flex-wrap",
"justify-content",
"-webkit-font-smoothing",
"grid-auto-columns",
"grid-auto-flow",
"grid-auto-rows",
"grid-column-end",
"grid-column-start",
"grid-template-areas",
"grid-template-columns",
"grid-template-rows",
"grid-row-end",
"grid-row-start",
"row-gap",
"-webkit-highlight",
"hyphens",
"-webkit-hyphenate-character",
"-webkit-line-break",
"-webkit-line-clamp",
"-webkit-locale",
"-webkit-margin-before-collapse",
"-webkit-margin-after-collapse",
"-webkit-mask-box-image",
"-webkit-mask-box-image-outset",
"-webkit-mask-box-image-repeat",
"-webkit-mask-box-image-slice",
"-webkit-mask-box-image-source",
"-webkit-mask-box-image-width",
"-webkit-mask-clip",
"-webkit-mask-composite",
"-webkit-mask-image",
"-webkit-mask-origin",
"-webkit-mask-position",
"-webkit-mask-repeat",
"-webkit-mask-size",
"order",
"perspective",
"perspective-origin",
"-webkit-print-color-adjust",
"-webkit-rtl-ordering",
"shape-outside",
"shape-image-threshold",
"shape-margin",
"-webkit-tap-highlight-color",
"-webkit-text-combine",
"-webkit-text-decorations-in-effect",
"-webkit-text-emphasis-color",
"-webkit-text-emphasis-position",
"-webkit-text-emphasis-style",
"-webkit-text-fill-color",
"-webkit-text-orientation",
"-webkit-text-security",
"-webkit-text-stroke-color",
"-webkit-text-stroke-width",
"transform",
"transform-origin",
"transform-style",
"-webkit-user-drag",
"-webkit-user-modify",
"user-select",
"-webkit-writing-mode",
"-webkit-app-region",
"buffered-rendering",
"clip-path",
"clip-rule",
"mask",
"filter",
"flood-color",
"flood-opacity",
"lighting-color",
"stop-color",
"stop-opacity",
"color-interpolation",
"color-interpolation-filters",
"color-rendering",
"fill",
"fill-opacity",
"fill-rule",
"marker-end",
"marker-mid",
"marker-start",
"mask-type",
"shape-rendering",
"stroke",
"stroke-dasharray",
"stroke-dashoffset",
"stroke-linecap",
"stroke-linejoin",
"stroke-miterlimit",
"stroke-opacity",
"stroke-width",
"alignment-baseline",
"baseline-shift",
"dominant-baseline",
"text-anchor",
"writing-mode",
"vector-effect",
"paint-order",
"d",
"cx",
"cy",
"x",
"y",
"r",
"rx",
"ry",
"caret-color",
"line-break"
];
function SimpleScreenshot(params) {
this.devicePixelRatio = params.devicePixelRatio || 1;
this.error = params.error || function () {};
this.log = params.log || console.log;
this.puppeteerServer = params.puppeteerServer; // puppeteer 截屏服务
this.puppeteerGlobalFont = params.puppeteerGlobalFont; // puppeteer 截屏服务全局字体
this.pupServerCanModifyFont = params.pupServerCanModifyFont ? params.pupServerCanModifyFont : true; // puppeteer 截屏服务是否支持配置字体
this.imgType = params.imgType || 'jpeg'; //图片类型
this.imgQuality = parseInt(params.imgQuality) || 80; //图片质量
this.distWidth = params.distWidth; //目标尺寸
this.distHeight = params.distHeight;
this.debug = !params.debug ? false : true; // 调试模式
this.supportForeignObject = false; //是否支持 svg foreignobject 渲染
this.forceScreenshotType = params.forceScreenshotType; //强制截屏方式 server、client
if (this.forceScreenshotType == 'client') { //强制 client 截屏
this.supportForeignObject = true;
} else {
this.checkForeignObject();
}
this._isLoading = false; // 是否正在加载资源
this._wholeTexts = ""; // 全部文字
this._globalInlineFont = {
list: [],
value: ""
}; //全局字体
this.globalInlineFont = params.globalInlineFont || "";
//字体列表
this.fontList = [];
let fontList = params.fontList || [];
for (let i = 0; i < fontList.length; i++) {
this.fontList.push({
name: fontList[i],
texts: "",
});
}
//预加载资源
this.preLoadResource = params.preLoadResource || [];
for (let i = 0; i < this.preLoadResource.length; i++) {
let name = this.preLoadResource[i].name;
let data = this.preLoadResource[i].data;
if (data && data != "") {
RESOURCE_NAME.push(name);
RESOURCE_DATA.push(data);
}
}
/**
* 父元素的部分属性对子元素有影响,但是并不会被子元素继承,比如:opacity、transform、filter;
* 但是小程序中没办法获取父节点元素,目前的解决办法是手动计算,然后通过参数传递。
*/
this.parentCSS = params.parentCSS || [{
name: "opacity",
value: 1,
},
{
name: dashTransform, // 因为是给子元素截图,父元素的 transform-origin 等属性可以不考虑
value: "",
},
{
name: "filter",
value: "",
},
];
//自定义 css 解析过滤器
this.parseCSSFilter = params.parseCSSFilter || [];
/**
* 特殊规则
* autoRenderWidth 自适应渲染宽度,在小程序中因为字体原因,某些文字元素 fields size 并不精确,导致文字超出容器,从而溢出换行。
*/
this.rules = {
autoRenderWidth: false,
};
this.analysisStyleSheets();
}
//解析 stylesheets
SimpleScreenshot.prototype.analysisStyleSheets = function () {
this._hasWidthNodes = [];
this._hasHeightNodes = [];
try {
let styleSheets = document.styleSheets;
for (let i = 0; i < styleSheets.length; i++) {
let sheet = styleSheets[i]
for (let j = 0; j < sheet.cssRules.length; j++) {
let rule = sheet.cssRules[j];
if (rule.type == 1) { // CSSStyleRule
if (rule.style.width) { // 找出样式中设置了固定宽度的元素
let nodes = document.querySelectorAll(rule.selectorText);
for (let z = 0; z < nodes.length; z++) {
if (rule.style.width) {
if (!this._hasWidthNodes.includes[nodes[z]]) {
this._hasWidthNodes.push(nodes[z]);
}
}
}
}
} else if (rule.type == 3) { // CSSImportRule
//
} else if (rule.type == 4) { // CSSMediaRule
//
} else if (rule.type == 5) { // CSSFontFaceRule
this.loadResource({
name: rule.style.fontFamily,
value: rule.style.src
}, function () {}, function () {})
}
}
}
} catch (err) {
//
}
}
//判断浏览器是否支持 foreignobject 渲染
SimpleScreenshot.prototype.checkForeignObject = function () {
let svg = "";
svg += "<svg xmlns='http://www.w3.org/2000/svg' width='1' height='1'>";
svg += "<foreignObject width='100%' height='100%'>";
svg += "<div xmlns='http://www.w3.org/1999/xhtml' style='width:100%;height:100%;background-color:red;'></div>";
svg += "</foreignObject>";
svg += "</svg>"
let self = this;
let img = new Image();
img.src = "data:image/svg+xml;charset=utf-8," + encodeURIComponent(svg);
img.onload = function () {
let canvas = document.createElement('canvas');
canvas.width = 1;
canvas.height = 1;
let ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0, 1, 1);
let imgData = ctx.getImageData(0, 0, 1, 1).data;
if (imgData[0] == 255 && imgData[1] == 0 && imgData[2] == 0 && imgData[3] == 255) { // rgba(255, 0, 0, 255) 红色
self.supportForeignObject = true;
}
}
}
SimpleScreenshot.prototype.nodeItem = function (node, level) {
if (node.nodeType != Node.ELEMENT_NODE) {
return;
}
//获取元素信息
let tagName = node.tagName.toLowerCase();
let $node = {
node: node,
dataset: {
level: level,
name: tagName
},
style: {}, //自定义样式
after: null, //伪类样式
before: null
}
if (tagName == "img") {
$node.dataset.src = node.getAttribute("src");
}
$node.computedStyle = window.getComputedStyle(node);
//伪类
let after = window.getComputedStyle(node, ":after");
if (contentExit(after)) {
$node.after = after;
}
let before = window.getComputedStyle(node, ":before");
if (contentExit(before)) {
$node.before = before;
}
this.nodes.push($node);
//子元素
let start = 0;
let childs = node.childNodes;
for (let i = 0; i < childs.length; i++) {
let child = childs[i];
if (child.nodeType == Node.ELEMENT_NODE) { //元素
this.nodeItem(child, level + '-' + start);
start++;
}
}
}
SimpleScreenshot.prototype.toSVG = function (selector, callback) {
this._wholeTexts = "";
this.nodes = [];
let $first = document.querySelector(selector);
this.nodeItem($first, 0);
let self = this;
this.parseCSSRules(function () {
self.root = self.getNodeByLevel("0");
let rect = self.root.node.getBoundingClientRect();
let width = parseFloat(rect.width) * self.devicePixelRatio;
let height = parseFloat(rect.height) * self.devicePixelRatio;
let html = self.toHTML(self.root, true);
let svg = "";
svg += "<svg xmlns='http://www.w3.org/2000/svg'";
svg += " width='" + width + "'";
svg += " height='" + height + "'";
svg += ">";
svg += "<foreignObject width='100%' height='100%'>";
svg += "<div xmlns='http://www.w3.org/1999/xhtml' style='";
if (self.root.display == "inline-block") { // 防止行内元素的布局被空白字符挤乱
svg += "font-size : 0 ;"
}
self.parentCSS = self.calcParentProperty(self.root)
for (let i = 0; i < self.parentCSS.length; i++) {
let pCSS = self.parentCSS[i];
pCSS.value = pCSS.value == null ? "" : (pCSS.value + "").trim();
if (pCSS.name == dashTransform) {
pCSS.value += " scale(" + self.devicePixelRatio + ")" //考虑设备像素比
}
if (pCSS.value != "") {
svg += pCSS.name + " : " + pCSS.value + " ;";
}
}
svg += dashOrigin + ":0 0;'>";
svg += html;
svg += "</div>";
svg += "</foreignObject>";
svg += "</svg>"
callback({
svg: svg,
width: rect.width,
height: rect.height,
devicePixelRatio: self.devicePixelRatio,
distWidth: self.distWidth ? self.distWidth : rect.width * self.devicePixelRatio, // 默认显示尺寸乘以设备像素比
distHeight: self.distHeight ? self.distHeight : rect.height * self.devicePixelRatio,
fonts: self.fontList,
imgType: self.imgType,
imgQuality: self.imgQuality
});
})
};
//父元素的部分属性对子元素有影响,但是并不会被子元素继承,比如:opacity、transform、filter
SimpleScreenshot.prototype.calcParentProperty = function ($node) {
let list = [{
name: "opacity",
value: 1,
}, {
name: dashTransform, //因为是给子元素截图,父元素的 transform-origin 等属性可以不考虑
value: "",
}, {
name: "filter",
value: "",
}];
let $target = $node.node.parentElement;
while ($target != null) {
let css = window.getComputedStyle($target);
for (let i = 0; i < list.length; i++) {
let property = list[i];
let value = css.getPropertyValue(property.name);
switch (property.name) {
case 'opacity':
value = value == null ? 1 : parseFloat(value);
property.value *= value;
break;
case 'filter':
case dashTransform:
value = (value == null || value == 'none') ? '' : (' ' + value);
property.value += value;
break;
default:
break;
}
}
$target = $target.parentElement;
}
return list;
}
SimpleScreenshot.prototype.toIMG = function (selector, callback) {
let self = this;
self.toSVG(selector, function (svg) {
if (self.supportForeignObject && self.forceScreenshotType != 'server') {
// 插入本地全局字体
let index = svg.svg.indexOf("</style>");
svg.svg = svg.svg.substring(0, index) + self._globalInlineFont.value + svg.svg.substring(index, svg.svg.length);
self.debug && self.log(svg) // log
let base64 = "data:image/svg+xml;charset=utf-8," + encodeURIComponent(svg.svg);
self.toCanvas(base64, svg, function (canvas) {
callback({
canvas: canvas,
base64: canvas.toDataURL(`image/${svg.imgType}`, svg.imgQuality / 100)
});
})
} else {
if (self.puppeteerGlobalFont) {
if (!self.pupServerCanModifyFont) {
self.fontList.push({
name: self.puppeteerGlobalFont,
texts: self._wholeTexts
})
}
svg.globalFont = self.puppeteerGlobalFont; // 全局字体
}
self.debug && self.log(svg) // log
let xhr = new XMLHttpRequest();
xhr.open("POST", self.puppeteerServer);
// xhr.responseType = "blob"; // blob api、URL.createObjectURL 存在兼容性问题,返回数据类型使用 base64
xhr.onerror = function (err) {
self.error(err)
};
xhr.ontimeout = function (err) {
self.error(err)
};
xhr.onload = function () {
self.toCanvas(xhr.response, svg, function (canvas) {
callback({
canvas: canvas,
base64: xhr.response
});
})
};
xhr.send(JSON.stringify(svg));
}
})
}
SimpleScreenshot.prototype.toCanvas = function (base64, svg, callback) {
let img = new Image();
img.width = parseInt(svg.distWidth);
img.height = parseInt(svg.distHeight);
img.onload = function () {
let canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
let ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0, img.width, img.height);
if (isFunction(callback)) {
callback(canvas)
}
}
img.src = base64;
}
//元素转换为 html 代码
SimpleScreenshot.prototype.toHTML = function (node, isRoot) {
let html = "";
let inner = "";
//解析自身
let cssTexts = this.parseCSS(node, isRoot);
//伪类
if (node.after) {
inner += "<span style='" + cssTexts.after + "'></span>";
}
if (node.before) {
inner += "<span style='" + cssTexts.before + "'></span>"
}
//解析子元素
let childs = node.node.childNodes;
let start = 0;
for (let i = 0; i < childs.length; i++) {
let child = childs[i];
if (child.nodeType == Node.ELEMENT_NODE) { //元素
let level = node.dataset.level + "-" + start;
let curNode = this.getNodeByLevel(level);
if (curNode) {
inner += this.toHTML(curNode, false);
start++;
}
} else {
let fontFamily = this.getNodeStyle(node, "font-family");
for (let z = 0; z < this.fontList.length; z++) { // 根据字体拆分文字
let fontItem = this.fontList[z];
if (fontFamily.includes(fontItem.name)) {
for (let k = 0; k < child.wholeText.length; k++) {
if (!fontItem.texts.includes(child.wholeText[k])) { //去重
fontItem.texts += child.wholeText[k];
}
}
}
}
for (let k = 0; k < child.wholeText.length; k++) { //全部文字
if (!this._wholeTexts.includes(child.wholeText[k])) {
this._wholeTexts += child.wholeText[k];
}
}
/**
* 拼接 xml 时如果文本节点中存在条件分割符 & 应该写成 & 否则会报错 EntityRef: expecting ';'
*/
inner += child.wholeText.replace(/[\r\n]/g, "").replace(/&/g, "&"); //去掉换行符
}
}
//拼装
let tagName = node.dataset.name;
html += "<" + tagName + " xmlns='http://www.w3.org/1999/xhtml'";
if (node.dataset.src) {
let index = RESOURCE_NAME.indexOf(node.dataset.src);
if (index != -1) {
html += " src='" + RESOURCE_DATA[index] + "'";
} else {
html += " src='" + node.dataset.src + "'";
}
}
if (!isRoot) {
html += " style='" + cssTexts.style + "'>";
} else {
html += " id='simple-screenshot'>";
html += "<style>";
html += "#simple-screenshot,#simple-screenshot *:not(style){" + cssTexts.style + "} " + this.globalInlineFont;
html += "</style>";
}
html += inner;
html += "</" + tagName + ">";
return html;
}
//解析 cssRules
SimpleScreenshot.prototype.parseCSS = function (node, isRoot) {
let style = "";
let after = "";
let before = "";
node._rules = {
autoRenderWidth: this.rules.autoRenderWidth
}
for (let i = 0; i < this.parseCSSFilter.length; i++) { //自定义 css 解析过滤器
let filter = this.parseCSSFilter[i];
if (isFunction(filter)) {
filter(node)
}
}
for (let i = 0; i < COMPUTED_STYLE.length; i++) {
let name = COMPUTED_STYLE[i];
if (isRoot) { //根节点
if (["margin-top", "margin-left", "margin-bottom", "margin-right"].includes(name)) {
this.setNodeStyle(node, name, 0); //最外层元素的 margin 必须为 0,否则会因为偏移导致错位
} else if (["absolute", "fixed"].includes(this.getNodeStyle(node, "position")) && ["top", "bottom", "left", "right"].includes(name)) {
this.setNodeStyle(node, name, 0); //最外层元素为绝对定位或者固定定位时,top、bottom、left、right 必须为 0,否则会因为定位偏移导致错位
}
} else { //非根节点
if (this.getNodeStyle(node, name) == this.getNodeStyle(this.root, name)) {
continue;
}
}
if (name == "background-image") { //背景图片
style += this.parseBackgroundImage(this.getNodeStyle(node, name));
if (node.after) {
after += this.parseBackgroundImage(node.after[name]);
}
if (node.before) {
before += this.parseBackgroundImage(node.before[name]);
}
} else {
let nodeV = this.getNodeStyle(node, name);
style += name + ":" + nodeV + ";";
if (name == "font-family") { //字体
let n1 = RESOURCE_NAME.indexOf(nodeV)
if (n1 != -1 && !this._globalInlineFont.list.includes(nodeV)) {
this._globalInlineFont.list.push(nodeV);
this._globalInlineFont.value += "@font-face {font-family: '" + nodeV + "';src: url(" + RESOURCE_DATA[n1] + ");}"
}
}
if (node.after) {
let afterV = node.after[name];
after += name + ":" + afterV + ";";
if (name == "font-family") { //字体
let n2 = RESOURCE_NAME.indexOf(afterV)
if (n2 != -1 && !this._globalInlineFont.list.includes(afterV)) {
this._globalInlineFont.list.push(afterV);
this._globalInlineFont.value += "@font-face {font-family: '" + afterV + "';src: url(" + RESOURCE_DATA[n2] + ");}"
}
}
}
if (node.before) {
let beforeV = node.before[name];
before += name + ":" + beforeV + ";";
if (name == "font-family") { //字体
let n3 = RESOURCE_NAME.indexOf(beforeV)
if (n3 != -1 && !this._globalInlineFont.list.includes(beforeV)) {
this._globalInlineFont.list.push(beforeV);
this._globalInlineFont.value += "@font-face {font-family: '" + beforeV + "';src: url(" + RESOURCE_DATA[n3] + ");}"
}
}
}
}
}
if ((node._rules.autoRenderWidth || !this._hasWidthNodes.includes(node.node)) && !isRoot) {
style += "width: auto;";
}
return {
style: style,
after: after,
before: before
}
}
//解析
SimpleScreenshot.prototype.parseBackgroundImage = function (bgValue) {
let matches = null;
let style = "";
let isReplaceResource = false;
URL_REGEX.lastIndex = 0 //重置正则对象
matches = URL_REGEX.exec(bgValue) //匹配url值
if (matches != null) {
let url = matches[1];
if (bgValue && url && url.search(/^(data:)/) == -1) { //非dataurl
let index = RESOURCE_NAME.indexOf(url);
isReplaceResource = true;
if (index != -1) {
style += "background-image : url(" + RESOURCE_DATA[index] + ");";
} else {
/**
* 拼接 xml 时如果 url 中存在条件分割符 &,应该写成 & 否则会报错 EntityRef: expecting ';'
* 可以用 encodeURIComponent 编码来避免这个问题。
*/
style += "background-image : url(" + encodeURIComponent(url) + ");";
}
}
}
if (!isReplaceResource) {
style += "background-image :" + bgValue + ";";
}
return style;
}
//设置节点样式
SimpleScreenshot.prototype.setNodeStyle = function (node, name, value) {
node.style[name] = value;
}
//获取节点样式
SimpleScreenshot.prototype.getNodeStyle = function (node, name) {
return (node.style[name] != null) ? node.style[name] : node.computedStyle[name];
}
//根据级别获取节点
SimpleScreenshot.prototype.getNodeByLevel = function (level) {
for (let i = 0; i < this.nodes.length; i++) {
if (this.nodes[i].dataset.level == level) {
return this.nodes[i];
}
}
return null;
}
//解析 cssRules
SimpleScreenshot.prototype.parseCSSRules = function (finished) {
let count = 0;
let self = this;
self._isLoading = false;
let add = function () {
self._isLoading = true;
count++;
};
let del = function () {
count--;
if (count == 0) {
self._isLoading = true;
finished();
}
};
for (let i = 0; i < this.nodes.length; i++) {
let node = this.nodes[i];
// img src
if (node.dataset.src && node.dataset.src.length > 0) {
this.loadResource({
value: "url(" + node.dataset.src + ")"
}, add, del)
}
// background-image
let bgImg = node.computedStyle['background-image'];
if (bgImg && bgImg.length > 0 && bgImg != 'none') {
this.loadResource({
value: bgImg
}, add, del);
}
if (node.after) {
let afterImg = node.after['background-image']
if (afterImg && afterImg.length > 0 && afterImg != 'none') {
this.loadResource({
value: afterImg
}, add, del);
}
}
if (node.before) {
let beforeImg = node.before['background-image']
if (beforeImg && beforeImg.length > 0 && beforeImg != 'none') {
this.loadResource({
value: beforeImg
}, add, del);
}
}
}
if (count == 0) {
finished();
}
};
//根据后缀获取 dataurl 类型
SimpleScreenshot.prototype.getType = function (url) {
let types = [".png", ".jpg", ".jpeg", ".ttf"];
let names = ["image/png", "image/jpg", "image/jpg", "application/x-font-ttf"];
for (let i = 0; i < types.length; i++) {
if (url.lastIndexOf(types[i]) + types[i].length == url.length) {
return names[i]
}
}
return names[0];
}
//加载外部资源
SimpleScreenshot.prototype.loadResource = function (resource, add, del) {
URL_REGEX.lastIndex = 0; //重置正则对象
let matches = URL_REGEX.exec(resource.value); //匹配url值
let self = this;
if (matches != null) {
let url = matches[1];
let name = resource.name ? resource.name : url;
if (
resource.value &&
url &&