Skip to content

Commit

Permalink
rehype-minify-enumerated-attributes: improve schema
Browse files Browse the repository at this point in the history
Closes GH-38.
  • Loading branch information
wooorm committed Sep 11, 2021
1 parent 190ced9 commit b34a034
Show file tree
Hide file tree
Showing 4 changed files with 382 additions and 20 deletions.
15 changes: 11 additions & 4 deletions packages/rehype-minify-enumerated-attribute/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import {visit} from 'unist-util-visit'
import {hasProperty} from 'hast-util-has-property'
import {isElement} from 'hast-util-is-element'
import {stringify} from 'space-separated-tokens'
import {schema} from './schema.js'

const own = {}.hasOwnProperty
Expand All @@ -39,10 +40,16 @@ export default function rehypeMinifyEnumeratedAttribute() {
let prop

for (prop in props) {
if (own.call(schema, prop)) {
const value = props[prop]
if (own.call(schema, prop) && hasProperty(node, prop)) {
let value = props[prop]

if (hasProperty(node, prop) && typeof value === 'string') {
// Note: we don’t really handle enumerated as lists, so instead
// we cast them to a string (assuming they are space-separated).
if (Array.isArray(value)) {
value = stringify(value)
}

if (typeof value === 'string') {
const info = schema[prop]
const definitions = Array.isArray(info) ? info : [info]
let index = -1
Expand All @@ -66,7 +73,7 @@ export default function rehypeMinifyEnumeratedAttribute() {
* @returns {string|null}
*/
function minify(value, info) {
const insensitive = value.toLowerCase()
const insensitive = info.caseSensitive ? value : value.toLowerCase()
const states = info.states
let index = -1
let known = false
Expand Down
1 change: 1 addition & 0 deletions packages/rehype-minify-enumerated-attribute/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@types/hast": "^2.0.0",
"hast-util-has-property": "^2.0.0",
"hast-util-is-element": "^2.0.0",
"space-separated-tokens": "^2.0.0",
"unified": "^10.0.0",
"unist-util-visit": "^4.0.0"
},
Expand Down
47 changes: 31 additions & 16 deletions packages/rehype-minify-enumerated-attribute/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@
* @property {string|null} [invalid]
* @property {Array.<null|string|string[]>} states
* @property {true} [allowUnknown]
* @property {true} [caseSensitive]
*/

/**
* This schema exposes a map of property names to (one or more) definitions.
* Each definition defined how that attribute is enumerated.
* Each definition defines how that attribute is enumerated.
*
* @type {Record<string, Info|Info[]>}
*/
export const schema = {
autoComplete: {
tagNames: null,
tagNames: 'form',
missing: '',
invalid: '',
states: [['', 'on'], 'off']
},
behavior: {
Expand Down Expand Up @@ -251,6 +253,12 @@ export const schema = {
invalid: '',
states: [['', 'anonymous'], 'use-credentials']
},
decoding: {
tagNames: 'img',
missing: '',
invalid: '',
states: ['sync', 'async', ['', 'auto']]
},
dir: {
tagNames: null,
missing: '',
Expand Down Expand Up @@ -282,7 +290,8 @@ export const schema = {
formEncType: {
tagNames: ['button', 'input'],
invalid: 'application/x-www-form-urlencoded',
missing: 'application/x-www-form-urlencoded',
// Note that `missing: null` here is intentionally different from `encType`.
missing: null,
states: [
'application/x-www-form-urlencoded',
'multipart/form-data',
Expand All @@ -293,15 +302,19 @@ export const schema = {
formMethod: {
tagNames: ['button', 'input'],
invalid: 'get',
missing: 'get',
// Note that `missing: null` here is intentionally different from `formMethod`.
missing: null,
states: ['dialog', 'get', 'post']
},
// When changing `formTarget`, please also change `target`.
formTarget: {
tagNames: ['button', 'input'],
missing: '',
// Note that `missing: null` here is intentionally different from `target`.
missing: null,
allowUnknown: true,
states: ['_blank', '_parent', ['', '_self'], '_top']
// Note that `formTarget` uses `_self` and `target` uses `['', '_self']`,
// which is intentional.
states: ['_blank', '_parent', '_self', '_top']
},
inputMode: {
// In fact only applies to `text`, `search`, and `password`.
Expand All @@ -324,12 +337,6 @@ export const schema = {
'verbatim'
]
},
loading: {
tagNames: ['iframe', 'img'],
invalid: 'eager',
missing: 'eager',
states: ['eager', 'lazy']
},
keytype: {
tagNames: 'keygen',
missing: 'rsa',
Expand All @@ -341,6 +348,12 @@ export const schema = {
invalid: 'metadata',
states: ['captions', 'chapters', 'descriptions', 'metadata', 'subtitles']
},
loading: {
tagNames: ['iframe', 'img'],
invalid: 'eager',
missing: 'eager',
states: ['eager', 'lazy']
},
// When changing `method`, please also change `formMethod`.
method: {
tagNames: 'form',
Expand Down Expand Up @@ -437,10 +450,11 @@ export const schema = {
]
},
{
caseSensitive: true,
tagNames: 'li',
missing: '',
invalid: '',
states: ['1', 'a', 'A', 'i', 'I', 'circle', 'disc', 'none', 'square']
states: ['1', 'a', 'A', 'i', 'I', 'circle', 'disc', 'square']
},
{
tagNames: 'menu',
Expand All @@ -453,16 +467,17 @@ export const schema = {
states: ['checkbox', 'command', 'radio']
},
{
caseSensitive: true,
tagNames: 'ol',
missing: '',
invalid: '',
missing: '1',
invalid: '1',
states: ['1', 'a', 'A', 'i', 'I']
},
{
tagNames: 'ul',
missing: '',
invalid: '',
states: ['circle', 'disc', 'none', 'square']
states: ['circle', 'disc', 'square']
}
],
wrap: {
Expand Down
Loading

0 comments on commit b34a034

Please sign in to comment.