|
| 1 | +import getCodeElements from './get-code-elements.mjs'; |
| 2 | +import trimSpaces from './trim-spaces.mjs'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Extract the list of CDDL definitions in the current spec. |
| 6 | + * |
| 7 | + * A spec may define more that one CDDL module. For example, the WebDriver BiDi |
| 8 | + * spec has CDDL definitions that apply to either of both the local end and the |
| 9 | + * remote end. The functions returns an array that lists all CDDL modules. |
| 10 | + * |
| 11 | + * Each CDDL module is represented as an object with the following keys whose |
| 12 | + * values are strings: |
| 13 | + * - shortname: the CDDL module shortname. Shortname is "" if there are no |
| 14 | + * - label: A full name for the CDDL module. |
| 15 | + * - cddl: A dump of the CDDL definitions. |
| 16 | + * |
| 17 | + * If the spec defines more than one module, the first item in the array is the |
| 18 | + * "all" module that contains a dump of all CDDL definitions, regardless of the |
| 19 | + * module they are actually defined for (the assumption is that looking at the |
| 20 | + * union of all CDDL modules defined in a spec will always make sense, and that |
| 21 | + * a spec will never reuse the same rule name with a different definition for |
| 22 | + * different CDDL modules). |
| 23 | + * |
| 24 | + * @function |
| 25 | + * @public |
| 26 | + * @return {Promise} The promise to get a dump of the CDDL definitions per |
| 27 | + * CDDL module, or an empty array if the spec does not contain any CDDL. |
| 28 | + */ |
| 29 | +export default function () { |
| 30 | + // Specs with CDDL are either recent enough that they all use the same |
| 31 | + // `<pre class="cddl">` convention, or they don't flag CDDL blocks in any |
| 32 | + // way, making it impossible to extract them. |
| 33 | + const cddlSelector = 'pre.cddl:not(.exclude):not(.extract)'; |
| 34 | + const indexSelector = '#cddl-index'; |
| 35 | + |
| 36 | + // Retrieve all elements that contains CDDL content |
| 37 | + const cddlEls = getCodeElements([cddlSelector], [indexSelector]); |
| 38 | + |
| 39 | + // By convention, CDDL defined without specifying a module is defined |
| 40 | + // for all modules (that CDDL would essentially be lost otherwise, there's |
| 41 | + // no reason for a spec to define CDDL for no module if it uses modules). |
| 42 | + // Start by assembled the list of modules |
| 43 | + const modules = {}; |
| 44 | + for (const el of cddlEls) { |
| 45 | + const elModules = getModules(el); |
| 46 | + for (const name of elModules) { |
| 47 | + modules[name] = []; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + // Assemble the CDDL per module |
| 52 | + const mergedCddl = []; |
| 53 | + for (const el of cddlEls) { |
| 54 | + const cddl = trimSpaces(el.textContent); |
| 55 | + if (!cddl) { |
| 56 | + continue; |
| 57 | + } |
| 58 | + mergedCddl.push(cddl); |
| 59 | + let elModules = getModules(el); |
| 60 | + if (elModules.length === 0) { |
| 61 | + // No module means the CDDL is defined for all modules |
| 62 | + elModules = Object.keys(modules); |
| 63 | + } |
| 64 | + for (const name of elModules) { |
| 65 | + if (!modules[name]) { |
| 66 | + modules[name] = []; |
| 67 | + } |
| 68 | + modules[name].push(cddl); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + if (mergedCddl.length === 0) { |
| 73 | + return []; |
| 74 | + } |
| 75 | + const res = [ { name: "", cddl: mergedCddl.join('\n\n') } ]; |
| 76 | + for (const [name, cddl] of Object.entries(modules)) { |
| 77 | + res.push({ name, cddl: cddl.join('\n\n') }); |
| 78 | + } |
| 79 | + // Remove trailing spaces and use spaces throughout |
| 80 | + for (const cddlModule of res) { |
| 81 | + cddlModule.cddl = cddlModule.cddl |
| 82 | + .replace(/\s+$/gm, '\n') |
| 83 | + .replace(/\t/g, ' ') |
| 84 | + .trim(); |
| 85 | + } |
| 86 | + return res; |
| 87 | +} |
| 88 | + |
| 89 | + |
| 90 | +/** |
| 91 | + * Retrieve the list of CDDL module shortnames that the element references. |
| 92 | + * |
| 93 | + * This list of modules is either specified in a `data-cddl-module` attribute |
| 94 | + * or directly within the class attribute prefixed by `cddl-` or suffixed by |
| 95 | + * `-cddl`. |
| 96 | + */ |
| 97 | +function getModules(el) { |
| 98 | + const moduleAttr = el.getAttribute('data-cddl-module'); |
| 99 | + if (moduleAttr) { |
| 100 | + return moduleAttr.split(',').map(str => str.trim()); |
| 101 | + } |
| 102 | + |
| 103 | + const list = []; |
| 104 | + const classes = el.classList.values() |
| 105 | + for (const name of classes) { |
| 106 | + const match = name.match(/^(.*)-cddl$|^cddl-(.*)$/); |
| 107 | + if (match) { |
| 108 | + const shortname = match[1] ?? match[2]; |
| 109 | + if (!list.includes(shortname)) { |
| 110 | + list.push(shortname); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + return list; |
| 115 | +} |
0 commit comments