|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// Filesystem paths are repository fixtures or files in a new temporary directory. |
| 4 | +/* eslint-disable security/detect-non-literal-fs-filename */ |
| 5 | + |
| 6 | +const assert = require('assert'); |
| 7 | +const fs = require('fs'); |
| 8 | +const os = require('os'); |
| 9 | +const path = require('path'); |
| 10 | +const { createRequire } = require('module'); |
| 11 | +const semver = require('semver'); |
| 12 | +const postcss = require('postcss'); |
| 13 | +const sanitizeHtml = require('sanitize-html'); |
| 14 | +const lock = require('../package-lock.json'); |
| 15 | + |
| 16 | +describe('PostCSS dependency regressions', function () { |
| 17 | + it('installs the reviewed release for CSS loading and HTML sanitization', function () { |
| 18 | + const copies = Object.entries(lock.packages).filter(([name]) => name.endsWith('/postcss')); |
| 19 | + assert.ok(copies.length > 0); |
| 20 | + copies.forEach(([name, entry]) => { |
| 21 | + // These module paths come only from the committed lockfile. |
| 22 | + // eslint-disable-next-line security/detect-non-literal-require |
| 23 | + const installed = require(path.resolve(__dirname, '..', name, 'package.json')); |
| 24 | + assert.strictEqual(installed.version, entry.version); |
| 25 | + assert.ok(semver.satisfies(installed.version, '>=8.5.28 <9')); |
| 26 | + }); |
| 27 | + for (const consumer of ['css-loader', 'sanitize-html']) { |
| 28 | + const consumerRequire = createRequire(require.resolve(consumer)); |
| 29 | + const installed = consumerRequire('postcss/package.json'); |
| 30 | + // eslint-disable-next-line security/detect-non-literal-require |
| 31 | + const parent = require(consumer + '/package.json'); |
| 32 | + assert.ok(semver.satisfies(installed.version, parent.dependencies.postcss)); |
| 33 | + assert.strictEqual(consumerRequire('postcss'), postcss); |
| 34 | + } |
| 35 | + }); |
| 36 | + |
| 37 | + const stylesheets = [ |
| 38 | + 'static/css/drawer.css', |
| 39 | + 'static/css/dropdown.css', |
| 40 | + 'static/css/sgv.css', |
| 41 | + 'views/clockviews/clock-shared.css', |
| 42 | + 'views/clockviews/clock-config.css' |
| 43 | + ]; |
| 44 | + stylesheets.forEach(file => { |
| 45 | + it('preserves CSS and source-map content for ' + file, function () { |
| 46 | + const from = path.resolve(__dirname, '..', file); |
| 47 | + const css = fs.readFileSync(from, 'utf8'); |
| 48 | + const result = postcss([{ postcssPlugin: 'round-trip', Once() {} }]).process(css, { |
| 49 | + from, |
| 50 | + map: { inline: false, annotation: false } |
| 51 | + }); |
| 52 | + assert.strictEqual(result.css, css); |
| 53 | + assert.deepStrictEqual(result.map.toJSON().sourcesContent, [css]); |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + it('preserves harmless hash-prefixed comments and a leading BOM', function () { |
| 58 | + const css = '\uFEFF/*# Nightscout theme */\n.widget { color: red }'; |
| 59 | + const result = postcss([{ postcssPlugin: 'round-trip', Once() {} }]) |
| 60 | + .process(css, { from: 'theme.css', map: false }); |
| 61 | + assert.strictEqual(result.css, css); |
| 62 | + assert.strictEqual(postcss().process(css, { from: 'theme.css', map: false }).css, css); |
| 63 | + }); |
| 64 | + |
| 65 | + it('keeps a custom property separate from an appended comment', function () { |
| 66 | + const root = postcss.parse('.widget{--accent:red}'); |
| 67 | + root.first.append(postcss.comment({ text: 'theme' })); |
| 68 | + const parsed = postcss.parse(root.toString()); |
| 69 | + assert.strictEqual(parsed.first.first.value, 'red'); |
| 70 | + assert.deepStrictEqual(parsed.first.nodes.map(node => node.type), ['decl', 'comment']); |
| 71 | + }); |
| 72 | + |
| 73 | + it('preserves empty comma-list items without inventing whitespace items', function () { |
| 74 | + assert.deepStrictEqual(postcss.list.comma('red,,blue,'), ['red', '', 'blue', '']); |
| 75 | + assert.deepStrictEqual(postcss.list.space(' \t\r\n '), []); |
| 76 | + assert.deepStrictEqual(postcss.list.comma('rgb(1, 2, 3), "a,b"'), ['rgb(1, 2, 3)', '"a,b"']); |
| 77 | + }); |
| 78 | + |
| 79 | + it('visits inserted CSS rules consistently in sync and async processing', async function () { |
| 80 | + function plugin(order) { |
| 81 | + return { |
| 82 | + postcssPlugin: 'unwrap-nested', |
| 83 | + Rule(rule) { |
| 84 | + order.push(rule.selector); |
| 85 | + rule.each(child => { |
| 86 | + if (child.type === 'rule') { |
| 87 | + child.selector = rule.selector + ' ' + child.selector; |
| 88 | + rule.after(child); |
| 89 | + } |
| 90 | + }); |
| 91 | + }, |
| 92 | + RootExit() { order.push('exit'); } |
| 93 | + }; |
| 94 | + } |
| 95 | + const syncOrder = []; |
| 96 | + const asyncOrder = []; |
| 97 | + const css = '.widget { .value { span {} } }'; |
| 98 | + const options = { from: 'theme.css', map: false }; |
| 99 | + const sync = postcss([plugin(syncOrder)]).process(css, options).css; |
| 100 | + const asyncResult = await postcss([plugin(asyncOrder)]).process(css, options); |
| 101 | + assert.strictEqual(asyncResult.css, sync); |
| 102 | + assert.deepStrictEqual(asyncOrder, syncOrder); |
| 103 | + const nestedVisit = syncOrder.indexOf('.widget .value span'); |
| 104 | + assert.ok(nestedVisit >= 0 && nestedVisit < syncOrder.indexOf('exit')); |
| 105 | + assert.ok(sync.includes('.widget .value span')); |
| 106 | + }); |
| 107 | + |
| 108 | + it('preserves formatting when a CSS AST is serialized and restored', function () { |
| 109 | + const css = '.one {}\n.two {}\n\n.three {}\n'; |
| 110 | + const json = JSON.parse(JSON.stringify(postcss.parse(css).toJSON())); |
| 111 | + assert.strictEqual(postcss.fromJSON(json).toString(), css); |
| 112 | + }); |
| 113 | + |
| 114 | + it('prevents serialized AST properties from replacing the node prototype', function () { |
| 115 | + const json = JSON.parse('{"type":"decl","prop":"color","value":"red","__proto__":{"hijacked":true}}'); |
| 116 | + const node = postcss.fromJSON(json); |
| 117 | + assert.strictEqual(node.hijacked, undefined); |
| 118 | + assert.strictEqual(node.toString(), 'color: red'); |
| 119 | + }); |
| 120 | + |
| 121 | + it('preserves permitted inline styles through sanitize-html', function () { |
| 122 | + const result = sanitizeHtml('<p style="color: red; /* note */ font-weight: 700; position: fixed">note</p>', { |
| 123 | + allowedTags: ['p'], |
| 124 | + allowedAttributes: { p: ['style'] }, |
| 125 | + allowedStyles: { '*': { color: [/^red$/], 'font-weight': [/^700$/] } } |
| 126 | + }); |
| 127 | + assert.strictEqual(result, '<p style="color:red;font-weight:700">note</p>'); |
| 128 | + }); |
| 129 | + |
| 130 | + it('preserves Nightscout note text while stripping styles and active markup', function () { |
| 131 | + const purify = require('../lib/server/purifier')(); |
| 132 | + const note = { notes: '<p style="color:red" onclick="alert(1)">BG < 70</p><script>alert(1)</script>' }; |
| 133 | + purify.purifyObject(note); |
| 134 | + assert.deepStrictEqual(note, { notes: '<p>BG < 70</p>' }); |
| 135 | + assert.strictEqual(purify.sanitizeString('Fish & Chips; BG < 70'), 'Fish & Chips; BG < 70'); |
| 136 | + }); |
| 137 | + |
| 138 | + describe('previous source-map boundaries', function () { |
| 139 | + let directory; |
| 140 | + let from; |
| 141 | + const sourceMap = JSON.stringify({ |
| 142 | + version: 3, sources: ['theme.scss'], names: [], mappings: 'AAAA', |
| 143 | + sourcesContent: ['.widget { color: red }'] |
| 144 | + }); |
| 145 | + function input(annotation, options = { from }) { |
| 146 | + return postcss.parse('.widget { color: red }\n/*# sourceMappingURL=' + annotation + ' */', options).source.input; |
| 147 | + } |
| 148 | + before(function () { |
| 149 | + directory = fs.mkdtempSync(path.join(os.tmpdir(), 'nightscout-postcss-')); |
| 150 | + fs.mkdirSync(path.join(directory, 'css')); |
| 151 | + from = path.join(directory, 'css', 'theme.css'); |
| 152 | + fs.writeFileSync(path.join(directory, 'css', 'theme.css.map'), sourceMap); |
| 153 | + fs.writeFileSync(path.join(directory, 'outside.map'), sourceMap); |
| 154 | + }); |
| 155 | + after(function () { |
| 156 | + fs.rmSync(directory, { recursive: true, force: true }); |
| 157 | + }); |
| 158 | + |
| 159 | + it('loads a source map beside the stylesheet', function () { |
| 160 | + assert.strictEqual(input('theme.css.map').map.text, sourceMap); |
| 161 | + }); |
| 162 | + it('continues to accept inline and explicitly supplied maps', function () { |
| 163 | + const inline = 'data:application/json;base64,' + Buffer.from(sourceMap).toString('base64'); |
| 164 | + assert.strictEqual(input(inline).map.text, sourceMap); |
| 165 | + assert.strictEqual(input('ignored.map', { from, map: { prev: sourceMap } }).map.text, sourceMap); |
| 166 | + }); |
| 167 | + it('ignores annotations outside the stylesheet directory', function () { |
| 168 | + assert.strictEqual(input('../outside.map').map, undefined); |
| 169 | + }); |
| 170 | + it('ignores file annotations when no source filename is provided', function () { |
| 171 | + assert.strictEqual(input(path.join(directory, 'outside.map'), {}).map, undefined); |
| 172 | + }); |
| 173 | + it('does not follow a source-map symlink outside the stylesheet directory', function () { |
| 174 | + fs.symlinkSync(path.join(directory, 'outside.map'), path.join(directory, 'css', 'linked.map'), 'file'); |
| 175 | + assert.strictEqual(input('linked.map').map, undefined); |
| 176 | + }); |
| 177 | + }); |
| 178 | +}); |
0 commit comments