<commit_before>#!/usr/bin/env node

const fs = require('fs')
const path = require('path')
const https = require('https')
const {JSDOM} = require('jsdom')

const textToCamel = str => str.replace(/((\-| )\w)/g, match => match[1].toUpperCase())
// const ucfirst = str => str.charAt(0).toUpperCase() + str.slice(1)

function update(filename, url, cb) {
    try {
        https.get(url, res => {
            res.setEncoding('utf8')
            let data = ''
            res.on('data', chunk => data += chunk)
            res.on('end', () => fs.writeFileSync(filename, cb(new JSDOM(data).window.document)))
            res.on('error', err => console.error(`Error downloading ${url}`, err))
        })
    } catch (err) {
        console.error(`Error downloading ${url}`, err)
    }
}


update(path.resolve(__dirname, '../src/AriaAttributes.ts'), 'https://www.w3.org/TR/wai-aria-1.1/', doc => {
    const roles = Array
        .from(doc.getElementById('role_definitions').querySelectorAll('dt a'))
        .map(el => el.textContent.trim())
        .filter(r => r.indexOf('abstract role') === -1)

    const attrs = Array
        .from(doc.getElementById('state_prop_def').querySelectorAll('dt a'))
        .map(el => el.textContent.trim())

    return `/// Script-generated.

export type AriaRole
    = ${roles.map(r => `'${r}'`).join('\n    | ')}

export interface AriaAttributes {
    role?: AriaRole
${attrs.map(a => `    '${a}'?: string`).join('\n')}
}
`
})


update(path.resolve(__dirname, '../src/html/HTMLElementContent.ts'), 'https://www.w3.org/TR/html52/fullindex.html', doc => {
    const elems = Array
        .from(
            doc
                .querySelector('#element-content-categories')
                .nextElementSibling
                .nextElementSibling
                .nextElementSibling
                .querySelectorAll('tbody tr')
        )
        .map(row => [
            textToCamel(
                row.cells[0].textContent
                    .trim()
                    .replace('*', '')
            ),
            Array
                .from(row.cells[1].querySelectorAll('a'))
                .map(el => el.textContent.trim())
                .concat(
                    // Elements with exceptions.
                    row.cells[2].textContent
                        .trim()
                        .slice(0, -1)
                        .split(';')
                        .map(tag => tag.trim().split(' ')[0])
                        .filter(tag => tag !== '')
                )
        ])

    return `/// Script-generated.

/**
 * Content categories for HTML elements. For reference, see:
 * https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Content_categories.
 */
export namespace HTMLElementContent {
    export type TransparentContent = string | HTMLElement | SVGSVGElement
${
    elems
        .map(([category, tags]) =>
            `    export type ${category} = ${tags
                .filter(tag => tag !== 'math') // MathML has very poor browser support yet.
                .map(tag => tag === 'Text'
                    ? 'string'
                    : tag === 'svg'
                    ? 'SVGSVGElement'
                    : `HTMLElementTagNameMap['${tag}']`
                )
                .join(' | ')}`)
        .join('\n')
}
}
`
})


update(path.resolve(__dirname, '../src/html/HTMLElementChildrenMap.ts'), 'https://www.w3.org/TR/html52/fullindex.html', doc => {
    const elems = []
    const initialElems = Array
        .from(
            doc
                .querySelector('#index-elements')
                .nextElementSibling
                .nextElementSibling
                .querySelectorAll('tbody tr')
        )
        .map(row => [<commit_msg>Fix paths in types script<commit_after>#!/usr/bin/env node

const fs = require('fs')
const path = require('path')
const https = require('https')
const {JSDOM} = require('jsdom')

const textToCamel = str => str.replace(/((\-| )\w)/g, match => match[1].toUpperCase())
// const ucfirst = str => str.charAt(0).toUpperCase() + str.slice(1)

function update(filename, url, cb) {
    try {
        https.get(url, res => {
            res.setEncoding('utf8')
            let data = ''
            res.on('data', chunk => data += chunk)
            res.on('end', () => fs.writeFileSync(filename, cb(new JSDOM(data).window.document)))
            res.on('error', err => console.error(`Error downloading ${url}`, err))
        })
    } catch (err) {
        console.error(`Error downloading ${url}`, err)
    }
}


update(path.resolve(__dirname, '../src/AriaAttributes.ts'), 'https://www.w3.org/TR/wai-aria-1.1/', doc => {
    const roles = Array
        .from(doc.getElementById('role_definitions').querySelectorAll('dt a'))
        .map(el => el.textContent.trim())
        .filter(r => r.indexOf('abstract role') === -1)

    const attrs = Array
        .from(doc.getElementById('state_prop_def').querySelectorAll('dt a'))
        .map(el => el.textContent.trim())

    return `/// Script-generated.

export type AriaRole
    = ${roles.map(r => `'${r}'`).join('\n    | ')}

export interface AriaAttributes {
    role?: AriaRole
${attrs.map(a => `    '${a}'?: string`).join('\n')}
}
`
})


update(path.resolve(__dirname, '../src/HTMLElementContent.ts'), 'https://www.w3.org/TR/html52/fullindex.html', doc => {
    const elems = Array
        .from(
            doc
                .querySelector('#element-content-categories')
                .nextElementSibling
                .nextElementSibling
                .nextElementSibling
                .querySelectorAll('tbody tr')
        )
        .map(row => [
            textToCamel(
                row.cells[0].textContent
                    .trim()
                    .replace('*', '')
            ),
            Array
                .from(row.cells[1].querySelectorAll('a'))
                .map(el => el.textContent.trim())
                .concat(
                    // Elements with exceptions.
                    row.cells[2].textContent
                        .trim()
                        .slice(0, -1)
                        .split(';')
                        .map(tag => tag.trim().split(' ')[0])
                        .filter(tag => tag !== '')
                )
        ])

    return `/// Script-generated.

/**
 * Content categories for HTML elements. For reference, see:
 * https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Content_categories.
 */
export namespace HTMLElementContent {
    export type TransparentContent = string | HTMLElement | SVGSVGElement
${
    elems
        .map(([category, tags]) =>
            `    export type ${category} = ${tags
                .filter(tag => tag !== 'math') // MathML has very poor browser support yet.
                .map(tag => tag === 'Text'
                    ? 'string'
                    : tag === 'svg'
                    ? 'SVGSVGElement'
                    : `HTMLElementTagNameMap['${tag}']`
                )
                .join(' | ')}`)
        .join('\n')
}
}
`
})


update(path.resolve(__dirname, '../src/HTMLElementChildrenMap.ts'), 'https://www.w3.org/TR/html52/fullindex.html', doc => {
    const elems = []
    const initialElems = Array
        .from(
            doc
                .querySelector('#index-elements')
                .nextElementSibling
                .nextElementSibling
                .querySelectorAll('tbody tr')
        )
        .map(row => [
{'fraction_non_alphanumeric': 0.16559139784946236, 'fraction_numerical': 0.005376344086021506, 'mean_word_length': 1.6950380296993843, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 49, 'https://': 8, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '626396', 'n_tokens_mistral': 2221, 'n_tokens_neox': 2157, 'n_words': 394}