1- import fs from 'fs'
2- import https from 'https'
1+ import fs from 'node: fs'
2+ import https from 'node: https'
33import { bail } from 'bail'
44import concat from 'concat-stream'
55import alphaSort from 'alpha-sort'
6- import unified from 'unified'
6+ import { unified } from 'unified'
77import parse from 'rehype-parse'
8- // @ts -ignore Remove when types are added
9- import q from 'hast-util-select'
10- // @ts -ignore Remove when types are added
11- import toString from 'hast-util-to-string'
12- // @ts -ignore Remove when types are added
13- import ev from 'hast-util-is-event-handler'
8+ import { select , selectAll } from 'hast-util-select'
9+ import { toString } from 'hast-util-to-string'
10+ import { isEventHandler } from 'hast-util-is-event-handler'
1411
1512/**
1613 * @typedef {import('http').IncomingMessage } IncomingMessage
@@ -20,13 +17,13 @@ import ev from 'hast-util-is-event-handler'
2017 * @typedef {Object.<string, Array.<string>> } Map
2118 */
2219
23- var proc = unified ( ) . use ( parse )
20+ const proc = unified ( ) . use ( parse )
2421
25- var actual = 0
26- var expected = 3
22+ let actual = 0
23+ const expected = 3
2724
2825/** @type {Map } */
29- var all = { }
26+ const all = { }
3027
3128https . get ( 'https://www.w3.org/TR/SVG11/attindex.html' , onsvg1 )
3229https . get ( 'https://www.w3.org/TR/SVGTiny12/attributeTable.html' , ontiny )
@@ -42,11 +39,11 @@ function onsvg1(response) {
4239 * @param {Buffer } buf
4340 */
4441 function onconcat ( buf ) {
45- var tree = proc . parse ( buf )
42+ const tree = proc . parse ( buf )
4643 /** @type {Map } */
47- var map = { }
44+ const map = { }
4845 /** @type {Element[] } */
49- var nodes = q . selectAll ( '.property-table tr' , tree )
46+ const nodes = selectAll ( '.property-table tr' , tree )
5047
5148 if ( nodes . length === 0 ) {
5249 throw new Error ( 'Couldn’t find rows in SVG 1' )
@@ -61,19 +58,14 @@ function onsvg1(response) {
6158 */
6259 function each ( node ) {
6360 /** @type {Element[] } */
64- var elements = q . selectAll ( '.element-name' , node )
61+ const elements = selectAll ( '.element-name' , node )
6562
66- q . selectAll ( '.attr-name' , node ) . forEach ( every )
67-
68- /**
69- * @param {string } name
70- */
71- function every ( name ) {
63+ selectAll ( '.attr-name' , node ) . forEach ( ( name ) => {
7264 elements
7365 . map ( toString )
7466 . map ( clean )
7567 . forEach ( add ( map , clean ( toString ( name ) ) ) )
76- }
68+ } )
7769 }
7870
7971 /**
@@ -96,11 +88,11 @@ function ontiny(response) {
9688 * @param {Buffer } buf
9789 */
9890 function onconcat ( buf ) {
99- var tree = proc . parse ( buf )
91+ const tree = proc . parse ( buf )
10092 /** @type {Map } */
101- var map = { }
93+ const map = { }
10294 /** @type {Element[] } */
103- var nodes = q . selectAll ( '#attributes .attribute' , tree )
95+ const nodes = selectAll ( '#attributes .attribute' , tree )
10496
10597 if ( nodes . length === 0 ) {
10698 throw new Error ( 'Couldn’t find nodes in SVG Tiny' )
@@ -115,11 +107,11 @@ function ontiny(response) {
115107 */
116108 function each ( node ) {
117109 /** @type {Element[] } */
118- var all = q . selectAll ( '.element' , node )
110+ const all = selectAll ( '.element' , node )
119111
120112 all
121113 . map ( toString )
122- . forEach ( add ( map , toString ( q . select ( '.attribute-name' , node ) ) ) )
114+ . forEach ( add ( map , toString ( select ( '.attribute-name' , node ) ) ) )
123115 }
124116 }
125117}
@@ -134,11 +126,11 @@ function onsvg2(response) {
134126 * @param {Buffer } buf
135127 */
136128 function onconcat ( buf ) {
137- var tree = proc . parse ( buf )
129+ const tree = proc . parse ( buf )
138130 /** @type {Map } */
139- var map = { }
131+ const map = { }
140132 /** @type {Element[] } */
141- var nodes = q . selectAll ( 'tbody tr' , tree )
133+ const nodes = selectAll ( 'tbody tr' , tree )
142134
143135 if ( nodes . length === 0 ) {
144136 throw new Error ( 'Couldn’t find nodes in SVG 2' )
@@ -153,11 +145,11 @@ function onsvg2(response) {
153145 */
154146 function each ( node ) {
155147 /** @type {Element[] } */
156- var all = q . selectAll ( '.element-name span' , node )
148+ const all = selectAll ( '.element-name span' , node )
157149
158150 all
159151 . map ( toString )
160- . forEach ( add ( map , toString ( q . select ( '.attr-name span' , node ) ) ) )
152+ . forEach ( add ( map , toString ( select ( '.attr-name span' , node ) ) ) )
161153 }
162154 }
163155}
@@ -176,7 +168,7 @@ function done(map) {
176168 if ( actual === expected ) {
177169 fs . writeFile (
178170 'index.js' ,
179- 'export var svgElementAttributes = ' +
171+ 'export const svgElementAttributes = ' +
180172 JSON . stringify ( sort ( all ) , null , 2 ) +
181173 '\n' ,
182174 bail
@@ -192,7 +184,7 @@ function done(map) {
192184 */
193185function add ( map , name ) {
194186 if (
195- ev ( name ) ||
187+ isEventHandler ( name ) ||
196188 name === 'role' ||
197189 name . slice ( 0 , 5 ) === 'aria-' ||
198190 name . slice ( 0 , 3 ) === 'ev:' ||
@@ -208,7 +200,7 @@ function add(map, name) {
208200 * @param {string } tagName Element name
209201 */
210202 function fn ( tagName ) {
211- var attributes = map [ tagName ] || ( map [ tagName ] = [ ] )
203+ const attributes = map [ tagName ] || ( map [ tagName ] = [ ] )
212204
213205 if ( ! attributes . includes ( name ) ) {
214206 attributes . push ( name )
@@ -224,11 +216,11 @@ function add(map, name) {
224216 */
225217function clean ( map ) {
226218 /** @type {Map } */
227- var result = { }
219+ let result = { }
228220 /** @type {Array.<string> } */
229- var list = [ ]
221+ const list = [ ]
230222 /** @type {Array.<string> } */
231- var globals = [ ]
223+ const globals = [ ]
232224
233225 // Find all used attributes.
234226 Object . keys ( map ) . forEach ( function ( tagName ) {
@@ -241,9 +233,9 @@ function clean(map) {
241233
242234 // Find global attributes.
243235 list . forEach ( function ( attribute ) {
244- var global = true
236+ let global = true
245237 /** @type {string } */
246- var key
238+ let key
247239
248240 for ( key in map ) {
249241 if ( ! map [ key ] . includes ( attribute ) ) {
@@ -265,7 +257,7 @@ function clean(map) {
265257 Object . keys ( map )
266258 . sort ( )
267259 . forEach ( function ( tagName ) {
268- var attributes = map [ tagName ]
260+ const attributes = map [ tagName ]
269261 . filter ( function ( attribute ) {
270262 return ! globals . includes ( attribute )
271263 } )
@@ -303,7 +295,7 @@ function merge(left, right) {
303295 * @param {Map } map
304296 */
305297function cleanAll ( map ) {
306- var globals = map [ '*' ]
298+ const globals = map [ '*' ]
307299
308300 Object . keys ( map ) . forEach ( function ( tagName ) {
309301 if ( tagName !== '*' ) {
@@ -320,7 +312,7 @@ function cleanAll(map) {
320312 */
321313function sort ( map ) {
322314 /** @type {Map } */
323- var result = { }
315+ const result = { }
324316
325317 Object . keys ( map )
326318 . sort ( alphaSort ( ) )
0 commit comments