From 9c1d7e66ac319062fea631b659e348330c894350 Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 24 May 2017 16:26:29 -0400 Subject: [PATCH 1/3] option to set height attribute, similar to width attribute --- index.js | 1 + lib/inline-css.js | 4 ++++ lib/setHeightAttrs.js | 19 +++++++++++++++++++ 3 files changed, 24 insertions(+) create mode 100644 lib/setHeightAttrs.js diff --git a/index.js b/index.js index 77ea6b8..6af8de1 100644 --- a/index.js +++ b/index.js @@ -15,6 +15,7 @@ module.exports = function (html, options) { preserveMediaQueries: false, removeHtmlSelectors: false, applyWidthAttributes: false, + applyHeightAttributes: false, applyTableAttributes: false, xmlMode: false, decodeEntities: false, diff --git a/lib/inline-css.js b/lib/inline-css.js index 259bbd7..979b004 100644 --- a/lib/inline-css.js +++ b/lib/inline-css.js @@ -60,6 +60,10 @@ module.exports = function (html, css, options) { setWidthAttrs(el, $); } + if (opts.applyHeightAttributes) { + setHeightAttrs(el, $); + } + if (opts.removeHtmlSelectors) { removeClassId(el, $); } diff --git a/lib/setHeightAttrs.js b/lib/setHeightAttrs.js new file mode 100644 index 0000000..c0fbbe1 --- /dev/null +++ b/lib/setHeightAttrs.js @@ -0,0 +1,19 @@ +'use strict'; + +var heightElements = [ 'table', 'td', 'img' ]; + +module.exports = function (el, $) { + var i, + pxHeight; + + if (heightElements.indexOf(el.name) > -1) { + for (i in el.styleProps) { + if (el.styleProps[i].prop === 'height' && el.styleProps[i].value.match(/px/)) { + pxHeight = el.styleProps[i].value.replace('px', ''); + + $(el).attr('height', pxHeight); + return; + } + } + } +}; From a5310b47fcc86d4c79f95189b1270c2ebbca5144 Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 24 May 2017 16:28:59 -0400 Subject: [PATCH 2/3] option to set align=center based on margin auto --- index.js | 1 + lib/inline-css.js | 6 ++++++ lib/setCenterAttrs.js | 16 ++++++++++++++++ 3 files changed, 23 insertions(+) create mode 100644 lib/setCenterAttrs.js diff --git a/index.js b/index.js index 6af8de1..23592e0 100644 --- a/index.js +++ b/index.js @@ -16,6 +16,7 @@ module.exports = function (html, options) { removeHtmlSelectors: false, applyWidthAttributes: false, applyHeightAttributes: false, + applyCenterAttributes: false, applyTableAttributes: false, xmlMode: false, decodeEntities: false, diff --git a/lib/inline-css.js b/lib/inline-css.js index 979b004..d478929 100644 --- a/lib/inline-css.js +++ b/lib/inline-css.js @@ -6,7 +6,9 @@ var parseCSS = require('css-rules'), handleRule = require('./handleRule'), flatten = require('flatten'), setStyleAttrs = require('./setStyleAttrs'), + setHeightAttrs = require('./setHeightAttrs'), setWidthAttrs = require('./setWidthAttrs'), + setCenterAttrs = require('./setCenterAttrs'), removeClassId = require('./removeClassId'), setTableAttrs = require('./setTableAttrs'), pick = require('object.pick'); @@ -64,6 +66,10 @@ module.exports = function (html, css, options) { setHeightAttrs(el, $); } + if (opts.applyCenterAttributes) { + setCenterAttrs(el, $); + } + if (opts.removeHtmlSelectors) { removeClassId(el, $); } diff --git a/lib/setCenterAttrs.js b/lib/setCenterAttrs.js new file mode 100644 index 0000000..e83e89a --- /dev/null +++ b/lib/setCenterAttrs.js @@ -0,0 +1,16 @@ +'use strict'; + +var centerElements = [ 'table', 'td', 'img' ]; + +module.exports = function (el, $) { + var i; + + if (centerElements.indexOf(el.name) > -1) { + for (i in el.styleProps) { + if (el.styleProps[i].prop === 'margin' && el.styleProps[i].value.match(/auto/)) { + $(el).attr('align', 'center'); + return; + } + } + } +}; From 168a22b215734b2d4f16e22457c9df791a9d39e5 Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 24 May 2017 16:53:33 -0400 Subject: [PATCH 3/3] update centering to apply wrapper
element to images, align attr to tables --- lib/setCenterAttrs.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/setCenterAttrs.js b/lib/setCenterAttrs.js index e83e89a..b55defd 100644 --- a/lib/setCenterAttrs.js +++ b/lib/setCenterAttrs.js @@ -1,14 +1,30 @@ 'use strict'; -var centerElements = [ 'table', 'td', 'img' ]; +var centerElements = [ 'img' ]; +var centerContainers = [ 'table' ]; module.exports = function (el, $) { - var i; + var i, + wrapper; if (centerElements.indexOf(el.name) > -1) { + for (i in el.styleProps) { + if (el.styleProps[i].prop === 'margin' && el.styleProps[i].value.match(/auto/)) { + // create wrapper container + wrapper = $('
' + $(el).toString() + '
'); + + $(el).replaceWith(wrapper); + + return; + } + } + } + + if (centerContainers.indexOf(el.name) > -1) { for (i in el.styleProps) { if (el.styleProps[i].prop === 'margin' && el.styleProps[i].value.match(/auto/)) { $(el).attr('align', 'center'); + return; } }