Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 1 | 'use strict'; |
| 2 | |
| 3 | const { isRoot, isAtRule, isRule } = require('./typeGuards'); |
| 4 | |
| 5 | /** @typedef {import('postcss').Root} Root */ |
| 6 | /** @typedef {import('postcss').Root} Document */ |
| 7 | /** @typedef {import('postcss').Node} PostcssNode */ |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 8 | /** @typedef {import('postcss').Container} PostcssContainerNode */ |
| 9 | /** @typedef {import('postcss').Declaration} Declaration */ |
| 10 | /** @typedef {(callbackFn: (decl: Declaration, index: number, decls: Declaration[]) => void) => void} EachDeclaration */ |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 11 | |
| 12 | /** |
| 13 | * @param {PostcssNode} node |
| 14 | * @returns {node is PostcssContainerNode} |
| 15 | */ |
| 16 | function isContainerNode(node) { |
| 17 | return isRule(node) || isAtRule(node) || isRoot(node); |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * In order to accommodate nested blocks (postcss-nested), |
| 22 | * we need to run a shallow loop (instead of eachDecl() or eachRule(), |
| 23 | * which loop recursively) and allow each nested block to accumulate |
| 24 | * its own list of properties -- so that a property in a nested rule |
| 25 | * does not conflict with the same property in the parent rule |
| 26 | * executes a provided function once for each declaration block. |
| 27 | * |
| 28 | * @param {Root | Document} root - root element of file. |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 29 | * @param {(eachDecl: EachDeclaration) => void} callback - Function to execute for each declaration block |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 30 | * |
| 31 | * @returns {void} |
| 32 | */ |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 33 | module.exports = function eachDeclarationBlock(root, callback) { |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 34 | /** |
| 35 | * @param {PostcssNode} statement |
| 36 | * |
| 37 | * @returns {void} |
| 38 | */ |
| 39 | function each(statement) { |
| 40 | if (!isContainerNode(statement)) return; |
| 41 | |
| 42 | if (statement.nodes && statement.nodes.length) { |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 43 | /** @type {Declaration[]} */ |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 44 | const decls = []; |
| 45 | |
Tim van der Lippe | 4cb0974 | 2022-01-07 14:25:03 +0100 | [diff] [blame^] | 46 | for (const node of statement.nodes) { |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 47 | if (node.type === 'decl') { |
| 48 | decls.push(node); |
| 49 | } |
| 50 | |
| 51 | each(node); |
Tim van der Lippe | 4cb0974 | 2022-01-07 14:25:03 +0100 | [diff] [blame^] | 52 | } |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 53 | |
| 54 | if (decls.length) { |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 55 | callback(decls.forEach.bind(decls)); |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | each(root); |
| 61 | }; |