From 54430730ee9dbe7187f2c9cc630b716a56c94c5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BB=98=E7=99=BB=E8=8D=A3?= Date: Fri, 31 Aug 2018 10:25:59 +0800 Subject: [PATCH] parse style attr to json --- src/html2json.js | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/html2json.js b/src/html2json.js index 65a3595..b2897fb 100644 --- a/src/html2json.js +++ b/src/html2json.js @@ -10,6 +10,12 @@ return '"' + v + '"'; } + function camelCase( str ) { + return str.replace( /-([a-z])/g, function( all, i ){ + return i.toUpperCase(); + } ) + } + function removeDOCTYPE(html) { return html .replace(/<\?xml.*\?>\n/, '') @@ -39,9 +45,20 @@ // has multi attibutes // make it array of attribute - if (value.match(/ /)) { + if (name === 'style') { + var arr = value.split(';'); + if (!arr.length) return; + value = {}; + arr.map(item => { + var attr = item.split(':'); + if (attr && attr.length > 1) { + var camelCaseAttr = camelCase(attr[0]).trim(); + value[camelCaseAttr] = attr[1].trim(); + } + }) + } else if (value.match(/ /)) { value = value.split(' '); - } + } // if attr already exists // merge it