Commit cb9eb363 authored by rockyl's avatar rockyl

修改脚本

parent 5b16ae67
{ {
"name": "html-shot", "name": "html-shot",
"version": "1.0.6", "version": "1.0.10",
"main": "dist/index.js", "main": "dist/index.js",
"types": "types/index.d.ts", "types": "types/index.d.ts",
"license": "MIT", "license": "MIT",
......
...@@ -19,6 +19,10 @@ const styleKeys = [ ...@@ -19,6 +19,10 @@ const styleKeys = [
'fontStyle', 'fontStyle',
] ]
const includeStyleKeys = [
'fontSize',
]
export function parseDom(el: HTMLElement = document.body) { export function parseDom(el: HTMLElement = document.body) {
const {left: pX, top: pY, width, height} = el.getBoundingClientRect(); const {left: pX, top: pY, width, height} = el.getBoundingClientRect();
...@@ -28,16 +32,18 @@ export function parseDom(el: HTMLElement = document.body) { ...@@ -28,16 +32,18 @@ export function parseDom(el: HTMLElement = document.body) {
switch (childNode.nodeName) { switch (childNode.nodeName) {
case 'IMG': case 'IMG':
node = childNode; node = childNode;
if (node.src) { //过滤src为空的图片
vNode = { vNode = {
type: 2, type: 2,
src: childNode.src, src: node.src,
}
} }
break; break;
case 'CANVAS': case 'CANVAS':
node = childNode; node = childNode;
vNode = { vNode = {
type: 3, type: 3,
img: childNode, img: node,
} }
break; break;
case '#text': case '#text':
...@@ -64,7 +70,7 @@ export function parseDom(el: HTMLElement = document.body) { ...@@ -64,7 +70,7 @@ export function parseDom(el: HTMLElement = document.body) {
bound = childNode.getBoundingClientRect(); bound = childNode.getBoundingClientRect();
} }
let styles = getStylesWithoutDefaults(node || childNode); let styles = getStylesWithoutDefaults(node || childNode, includeStyleKeys);
if (!isText) { if (!isText) {
for (let skey of commonStyleKeys) { for (let skey of commonStyleKeys) {
...@@ -115,7 +121,7 @@ function walkNode(root, callback) { ...@@ -115,7 +121,7 @@ function walkNode(root, callback) {
} }
} }
function getStylesWithoutDefaults(element): any { function getStylesWithoutDefaults(element, includes: string[] = []): any {
let dummy = document.createElement('element-' + (new Date().getTime())); let dummy = document.createElement('element-' + (new Date().getTime()));
document.body.appendChild(dummy); document.body.appendChild(dummy);
...@@ -124,8 +130,7 @@ function getStylesWithoutDefaults(element): any { ...@@ -124,8 +130,7 @@ function getStylesWithoutDefaults(element): any {
let diff = {}; let diff = {};
for (let key in elementStyles) { for (let key in elementStyles) {
if (elementStyles.hasOwnProperty(key) if (includes.indexOf(key) >= 0 || (elementStyles.hasOwnProperty(key) && defaultStyles[key] !== elementStyles[key])) {
&& defaultStyles[key] !== elementStyles[key]) {
diff[key] = elementStyles[key]; diff[key] = elementStyles[key];
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment