mjml-parser-xml
Advanced tools
Comparing version 4.14.1 to 4.15.0
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; | ||
Object.defineProperty(exports, "__esModule", { | ||
@@ -9,8 +8,7 @@ value: true | ||
exports.default = cleanNode; | ||
var _forEach2 = _interopRequireDefault(require("lodash/forEach")); | ||
function cleanNode(node) { | ||
delete node.parent; // Delete children if needed | ||
delete node.parent; | ||
// Delete children if needed | ||
if (node.children && node.children.length) { | ||
@@ -20,5 +18,5 @@ (0, _forEach2.default)(node.children, cleanNode); | ||
delete node.children; | ||
} // Delete attributes if needed | ||
} | ||
// Delete attributes if needed | ||
if (node.attributes && Object.keys(node.attributes).length === 0) { | ||
@@ -28,3 +26,2 @@ delete node.attributes; | ||
} | ||
module.exports = exports.default; |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; | ||
Object.defineProperty(exports, "__esModule", { | ||
@@ -9,5 +8,3 @@ value: true | ||
exports.default = convertBooleansOnAttrs; | ||
var _mapValues2 = _interopRequireDefault(require("lodash/mapValues")); | ||
/** | ||
@@ -17,2 +14,3 @@ * Convert "true" and "false" string attributes values | ||
*/ | ||
function convertBooleansOnAttrs(attrs) { | ||
@@ -23,11 +21,8 @@ return (0, _mapValues2.default)(attrs, val => { | ||
} | ||
if (val === 'false') { | ||
return false; | ||
} | ||
return val; | ||
}); | ||
} | ||
module.exports = exports.default; |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; | ||
Object.defineProperty(exports, "__esModule", { | ||
@@ -9,5 +8,3 @@ value: true | ||
exports.default = setEmptyAttributes; | ||
var _forEach2 = _interopRequireDefault(require("lodash/forEach")); | ||
function setEmptyAttributes(node) { | ||
@@ -17,3 +14,2 @@ if (!node.attributes) { | ||
} | ||
if (node.children) { | ||
@@ -23,3 +19,2 @@ (0, _forEach2.default)(node.children, setEmptyAttributes); | ||
} | ||
module.exports = exports.default; |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; | ||
Object.defineProperty(exports, "__esModule", { | ||
@@ -9,42 +8,24 @@ value: true | ||
exports.default = MJMLParser; | ||
var _flow2 = _interopRequireDefault(require("lodash/fp/flow")); | ||
var _map2 = _interopRequireDefault(require("lodash/fp/map")); | ||
var _filter2 = _interopRequireDefault(require("lodash/fp/filter")); | ||
var _find2 = _interopRequireDefault(require("lodash/find")); | ||
var _findLastIndex2 = _interopRequireDefault(require("lodash/findLastIndex")); | ||
var _isObject2 = _interopRequireDefault(require("lodash/isObject")); | ||
var _htmlparser = require("htmlparser2"); | ||
var _path = _interopRequireDefault(require("path")); | ||
var _fs = _interopRequireDefault(require("fs")); | ||
var _cleanNode = _interopRequireDefault(require("./helpers/cleanNode")); | ||
var _convertBooleansOnAttrs = _interopRequireDefault(require("./helpers/convertBooleansOnAttrs")); | ||
var _setEmptyAttributes = _interopRequireDefault(require("./helpers/setEmptyAttributes")); | ||
const isNode = require('detect-node'); | ||
const indexesForNewLine = xml => { | ||
const regex = /\n/gi; | ||
const indexes = [0]; | ||
while (regex.exec(xml)) { | ||
indexes.push(regex.lastIndex); | ||
} | ||
return indexes; | ||
}; | ||
const isSelfClosing = (indexes, parser) => indexes.startIndex === parser.startIndex && indexes.endIndex === parser.endIndex; | ||
function MJMLParser(xml, options = {}, includedIn = []) { | ||
@@ -61,10 +42,9 @@ const { | ||
} = options; | ||
const endingTags = (0, _flow2.default)((0, _filter2.default)(component => component.endingTag), (0, _map2.default)(component => component.getTagName()))({ ...components | ||
const endingTags = (0, _flow2.default)((0, _filter2.default)(component => component.endingTag), (0, _map2.default)(component => component.getTagName()))({ | ||
...components | ||
}); | ||
let cwd = process.cwd(); | ||
if (isNode && filePath) { | ||
try { | ||
const isDir = _fs.default.lstatSync(filePath).isDirectory(); | ||
cwd = isDir ? filePath : _path.default.dirname(filePath); | ||
@@ -75,3 +55,2 @@ } catch (e) { | ||
} | ||
let mjml = null; | ||
@@ -86,14 +65,9 @@ let cur = null; | ||
}; | ||
const findTag = (tagName, tree) => (0, _find2.default)(tree.children, { | ||
tagName | ||
}); | ||
const lineIndexes = indexesForNewLine(xml); | ||
const handleCssHtmlInclude = (file, attrs, line) => { | ||
const partialPath = _path.default.resolve(cwd, file); | ||
let content; | ||
try { | ||
@@ -121,3 +95,2 @@ content = _fs.default.readFileSync(partialPath, 'utf8'); | ||
} | ||
if (attrs.type === 'html') { | ||
@@ -135,3 +108,2 @@ const newNode = { | ||
} | ||
const attributes = attrs['css-inline'] === 'inline' ? { | ||
@@ -151,6 +123,4 @@ inline: 'inline' | ||
}; | ||
const handleInclude = (file, line) => { | ||
const partialPath = _path.default.resolve(cwd, file); | ||
const curBeforeInclude = cur; | ||
@@ -161,3 +131,2 @@ if ((0, _find2.default)(cur.includedIn, { | ||
let content; | ||
try { | ||
@@ -185,5 +154,5 @@ content = _fs.default.readFileSync(partialPath, 'utf8'); | ||
} | ||
content = content.indexOf('<mjml>') === -1 ? `<mjml><mj-body>${content}</mj-body></mjml>` : content; | ||
const partialMjml = MJMLParser(content, { ...options, | ||
const partialMjml = MJMLParser(content, { | ||
...options, | ||
filePath: partialPath, | ||
@@ -195,14 +164,11 @@ actualPath: partialPath | ||
}]); | ||
const bindToTree = (children, tree = cur) => children.map(c => ({ ...c, | ||
const bindToTree = (children, tree = cur) => children.map(c => ({ | ||
...c, | ||
parent: tree | ||
})); | ||
if (partialMjml.tagName !== 'mjml') { | ||
return; | ||
} | ||
const body = findTag('mj-body', partialMjml); | ||
const head = findTag('mj-head', partialMjml); | ||
if (body) { | ||
@@ -212,6 +178,4 @@ const boundChildren = bindToTree(body.children); | ||
} | ||
if (head) { | ||
let curHead = findTag('mj-head', mjml); | ||
if (!curHead) { | ||
@@ -228,15 +192,12 @@ mjml.children.push({ | ||
} | ||
const boundChildren = bindToTree(head.children, curHead); | ||
curHead.children = [...curHead.children, ...boundChildren]; | ||
} // must restore cur to the cur before include started | ||
} | ||
// must restore cur to the cur before include started | ||
cur = curBeforeInclude; | ||
}; | ||
const parser = new _htmlparser.Parser({ | ||
onopentag: (name, attrs) => { | ||
const isAnEndingTag = endingTags.indexOf(name) !== -1; | ||
if (inEndingTag > 0) { | ||
@@ -246,6 +207,4 @@ if (isAnEndingTag) inEndingTag += 1; | ||
} | ||
if (isAnEndingTag) { | ||
inEndingTag += 1; | ||
if (inEndingTag === 1) { | ||
@@ -257,8 +216,5 @@ // we're entering endingTag | ||
} | ||
const line = (0, _findLastIndex2.default)(lineIndexes, i => i <= parser.startIndex) + 1; | ||
if (name === 'mj-include') { | ||
if (ignoreIncludes || !isNode) return; | ||
if (attrs.type === 'css' || attrs.type === 'html') { | ||
@@ -268,3 +224,2 @@ handleCssHtmlInclude(decodeURIComponent(attrs.path), attrs, line); | ||
} | ||
inInclude = true; | ||
@@ -274,3 +229,2 @@ handleInclude(decodeURIComponent(attrs.path), line); | ||
} | ||
if (convertBooleans) { | ||
@@ -280,3 +234,2 @@ // "true" and "false" will be converted to bools | ||
} | ||
const newNode = { | ||
@@ -292,3 +245,2 @@ file: actualPath, | ||
}; | ||
if (cur) { | ||
@@ -299,3 +251,2 @@ cur.children.push(newNode); | ||
} | ||
cur = newNode; | ||
@@ -306,3 +257,2 @@ }, | ||
inEndingTag -= 1; | ||
if (!inEndingTag) { | ||
@@ -318,11 +268,9 @@ // we're getting out of endingTag | ||
} | ||
if (inEndingTag > 0) return; | ||
if (inInclude) { | ||
inInclude = false; | ||
} // for includes, setting cur is handled in handleInclude because when there is | ||
} | ||
// for includes, setting cur is handled in handleInclude because when there is | ||
// only mj-head in include it doesn't create any elements, so setting back to parent is wrong | ||
if (name !== 'mj-include') cur = cur && cur.parent || null; | ||
@@ -332,3 +280,2 @@ }, | ||
if (inEndingTag > 0) return; | ||
if (text && text.trim() && cur) { | ||
@@ -340,3 +287,2 @@ cur.content = `${cur && cur.content || ''}${text.trim()}`.trim(); | ||
if (inEndingTag > 0) return; | ||
if (cur && keepComments) { | ||
@@ -356,18 +302,17 @@ cur.children.push({ | ||
lowerCaseAttributeNames: false | ||
}); // Apply preprocessors to raw xml | ||
}); | ||
// Apply preprocessors to raw xml | ||
xml = (0, _flow2.default)(preprocessors)(xml); | ||
parser.write(xml); | ||
parser.end(); | ||
if (!(0, _isObject2.default)(mjml)) { | ||
throw new Error('Parsing failed. Check your mjml.'); | ||
} | ||
(0, _cleanNode.default)(mjml); | ||
(0, _cleanNode.default)(mjml); // Assign "attributes" property if not set | ||
// Assign "attributes" property if not set | ||
if (addEmptyAttributes) { | ||
(0, _setEmptyAttributes.default)(mjml); | ||
} | ||
if (cssIncludes.length) { | ||
@@ -377,3 +322,2 @@ const head = (0, _find2.default)(mjml.children, { | ||
}); | ||
if (head) { | ||
@@ -394,6 +338,4 @@ if (head.children) { | ||
} | ||
return mjml; | ||
} | ||
module.exports = exports.default; |
{ | ||
"name": "mjml-parser-xml", | ||
"description": "mjml-parser-xml", | ||
"version": "4.14.1", | ||
"version": "4.15.0", | ||
"main": "lib/index.js", | ||
@@ -26,4 +26,4 @@ "files": [ | ||
"@babel/runtime": "^7.14.6", | ||
"detect-node": "2.0.4", | ||
"htmlparser2": "^8.0.1", | ||
"detect-node": "2.1.0", | ||
"htmlparser2": "^9.1.0", | ||
"lodash": "^4.17.15" | ||
@@ -34,7 +34,7 @@ }, | ||
"chai": "^4.1.1", | ||
"mjml": "4.14.1", | ||
"mjml-core": "4.14.1", | ||
"mjml": "4.15.0", | ||
"mjml-core": "4.15.0", | ||
"rimraf": "^3.0.2" | ||
}, | ||
"gitHead": "988819de3375867c09585d28f555166b97415200" | ||
"gitHead": "af922bd11c16f4182057077a8a060b7a7f7cc4e3" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
418
15694
+ Addeddetect-node@2.1.0(transitive)
+ Addedhtmlparser2@9.1.0(transitive)
- Removeddetect-node@2.0.4(transitive)
- Removedhtmlparser2@8.0.2(transitive)
Updateddetect-node@2.1.0
Updatedhtmlparser2@^9.1.0