|
| 1 | +const helpers = require('./helpers.js'); |
| 2 | +const removeTestTokens = require('./removeTestTokens.js'); |
| 3 | + |
| 4 | +/** |
| 5 | + * This removes the data-server-rendered="true" from your snapshots. |
| 6 | + * |
| 7 | + * @param {object} $ The markup as a cheerio object |
| 8 | + * @param {object} options Options object for this serializer |
| 9 | + */ |
| 10 | +function removeServerRenderedText ($, options) { |
| 11 | + if (!options || options.removeServerRendered) { |
| 12 | + $('[data-server-rendered]').removeAttr('data-server-rendered'); |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +/** |
| 17 | + * This removes data-v-1234abcd="" from your snapshots. |
| 18 | + * |
| 19 | + * @param {object} $ The markup as a cheerio object |
| 20 | + * @param {object} options Options object for this serializer |
| 21 | + */ |
| 22 | +function removeScopedStylesDataVIDAttributes ($, options) { |
| 23 | + if (!options || options.removeDataVId) { |
| 24 | + // [-\w]+ will catch 1 or more instaces of a-z, A-Z, 0-9, hyphen (-), or underscore (_) |
| 25 | + const regex = / data-v-[-\w]+/g; |
| 26 | + |
| 27 | + // [' data-v-asdf=""', ' data-v-qwer', ' data-v-asdf'] |
| 28 | + let dataVIds = $.html().match(regex) || []; |
| 29 | + // ['data-v-asdf', 'data-v-qwer', 'data-v-asdf'] |
| 30 | + dataVIds = dataVIds.map(function (match) { |
| 31 | + return match.trim().replace('=""', ''); |
| 32 | + }); |
| 33 | + // ['data-v-asdf', 'data-v-qwer'] |
| 34 | + dataVIds = Array.from(new Set(dataVIds)); |
| 35 | + |
| 36 | + dataVIds.forEach(function (attribute) { |
| 37 | + $('[' + attribute + ']').removeAttr(attribute); |
| 38 | + }); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Loops over the attributesToClear array to set the attribute |
| 44 | + * value to empty string on all matching elements. |
| 45 | + * |
| 46 | + * @param {object} $ The markup as a cheerio object |
| 47 | + * @param {object} options Options object for this serializer |
| 48 | + */ |
| 49 | +function clearAttributes ($, options) { |
| 50 | + if ( |
| 51 | + options && |
| 52 | + options.attributesToClear && |
| 53 | + options.attributesToClear.length |
| 54 | + ) { |
| 55 | + options.attributesToClear.forEach(function (attribute) { |
| 56 | + $('[' + attribute + ']').attr(attribute, ''); |
| 57 | + }); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Replaces inline functions with the '[function]' placeholder. |
| 63 | + * |
| 64 | + * @param {object} $ The markup as a cheerio object |
| 65 | + * @param {object} options Options object for this serializer |
| 66 | + */ |
| 67 | +function clearInlineFunctions ($, options) { |
| 68 | + if (options && options.clearInlineFunctions) { |
| 69 | + $('*').each(function (index, element) { |
| 70 | + Object.keys(element.attribs).forEach(function (attributeName) { |
| 71 | + let value = element.attribs[attributeName]; |
| 72 | + if ( |
| 73 | + ( |
| 74 | + value.startsWith('function ') || |
| 75 | + value.startsWith('function(') |
| 76 | + ) && |
| 77 | + value.endsWith('}') && |
| 78 | + helpers.functionRegex.test(value) |
| 79 | + ) { |
| 80 | + element.attribs[attributeName] = '[function]'; |
| 81 | + } |
| 82 | + }); |
| 83 | + }); |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * Inline functions get weird istanbul coverage comments. |
| 89 | + * This removes them. |
| 90 | + * <div title="function () { |
| 91 | + * /* istanbul ignore next *\/ |
| 92 | + * cov_1lmjj6lxv1.f[3]++; |
| 93 | + * cov_1lmjj6lxv1.s[15]++; |
| 94 | + * return true; |
| 95 | + * }"> |
| 96 | + * |
| 97 | + * @param {object} $ The markup as a cheerio object |
| 98 | + * @param {object} options Options object for this serializer |
| 99 | + */ |
| 100 | +function removeIstanbulComments ($, options) { |
| 101 | + if ( |
| 102 | + options && |
| 103 | + options.removeIstanbulComments && |
| 104 | + $.html().includes('/* istanbul') |
| 105 | + ) { |
| 106 | + $('*').each(function (index, element) { |
| 107 | + Object.keys(element.attribs).forEach(function (attributeName) { |
| 108 | + let value = element.attribs[attributeName]; |
| 109 | + if (value.includes('/* istanbul')) { |
| 110 | + value = value.split('\n').filter(function (line) { |
| 111 | + return ( |
| 112 | + !line.trim().startsWith('/* istanbul') && |
| 113 | + !line.trim().startsWith('cov_') |
| 114 | + ); |
| 115 | + }); |
| 116 | + element.attribs[attributeName] = value.join('\n'); |
| 117 | + } |
| 118 | + }); |
| 119 | + }); |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +/** |
| 124 | + * Performs all string manipulations on the rendered DOM |
| 125 | + * prior to formatting. Using Cheerio for DOM manipulation. |
| 126 | + * |
| 127 | + * @param {string} html The markup being serialized |
| 128 | + * @param {object} options Options object for this serializer |
| 129 | + * @return {string} The markup being serialized |
| 130 | + */ |
| 131 | +function cheerioManipulation (html, options) { |
| 132 | + let $ = helpers.$(html); |
| 133 | + |
| 134 | + removeServerRenderedText($, options); |
| 135 | + removeTestTokens($, options); |
| 136 | + removeScopedStylesDataVIDAttributes($, options); |
| 137 | + clearAttributes($, options); |
| 138 | + |
| 139 | + // clearInlineFunctions should always be ran before removeIstanbulComments for speed |
| 140 | + clearInlineFunctions($, options); |
| 141 | + removeIstanbulComments($, options); |
| 142 | + |
| 143 | + return $.html(); |
| 144 | +} |
| 145 | + |
| 146 | +module.exports = cheerioManipulation; |
0 commit comments