2021-08-06 21:07:58 +10:00
import htmlData from "@wzlin/html-data" ;
import { writeFileSync } from "fs" ;
import { join } from "path" ;
import { RUST_OUT_DIR } from "./_common" ;
2020-06-19 17:16:23 +10:00
const rsTagAttr = ( {
2021-08-10 16:07:38 +10:00
boolean = false ,
caseInsensitive = false ,
collapse = false ,
2020-06-19 17:16:23 +10:00
defaultValue ,
2021-08-10 16:07:38 +10:00
redundantIfEmpty = false ,
trim = false ,
2020-11-13 22:15:51 +11:00
} : {
2021-08-10 16:07:38 +10:00
boolean ? : boolean ;
caseInsensitive? : boolean ;
collapse? : boolean ;
2020-11-13 22:15:51 +11:00
defaultValue? : string ;
2021-08-10 16:07:38 +10:00
redundantIfEmpty? : boolean ;
trim? : boolean ;
2021-08-06 21:07:58 +10:00
} ) = >
2021-08-10 16:07:38 +10:00
`
AttributeMinification {
boolean : $ { boolean } ,
case_insensitive : $ { caseInsensitive } ,
collapse : $ { collapse } ,
2021-08-10 16:33:20 +10:00
default_value : $ {
defaultValue == undefined ? "None" : ` Some(b" ${ defaultValue } ") `
} ,
2021-08-10 16:07:38 +10:00
redundant_if_empty : $ { redundantIfEmpty } ,
trim : $ { trim } ,
}
` ;
2020-06-19 17:16:23 +10:00
2020-11-13 22:15:51 +11:00
let code = `
2020-07-03 17:32:09 +10:00
use lazy_static : : lazy_static ;
use std : : collections : : HashMap ;
2021-08-09 17:45:42 +10:00
use crate : : common : : spec : : tag : : ns : : Namespace ;
2020-06-19 17:16:23 +10:00
pub struct AttributeMinification {
pub boolean : bool ,
2021-08-10 16:07:38 +10:00
pub case_insensitive : bool ,
pub collapse : bool ,
2020-06-19 17:16:23 +10:00
pub default_value : Option < & ' static [ u8 ] > ,
2021-08-10 16:07:38 +10:00
pub redundant_if_empty : bool ,
pub trim : bool ,
2020-06-19 17:16:23 +10:00
}
pub enum AttrMapEntry {
AllNamespaceElements ( AttributeMinification ) ,
2020-07-03 17:32:09 +10:00
SpecificNamespaceElements ( HashMap < & ' static [ u8 ] , AttributeMinification > ) ,
2020-06-19 17:16:23 +10:00
}
pub struct ByNamespace {
// Make pub so this struct can be statically created in gen/attrs.rs.
2020-07-03 17:32:09 +10:00
pub html : Option < AttrMapEntry > ,
pub svg : Option < AttrMapEntry > ,
2020-06-19 17:16:23 +10:00
}
impl ByNamespace {
2020-07-03 17:32:09 +10:00
fn get ( & self , ns : Namespace ) - > Option < & AttrMapEntry > {
2020-06-19 17:16:23 +10:00
match ns {
2020-07-03 17:32:09 +10:00
Namespace : : Html = > self . html . as_ref ( ) ,
Namespace : : Svg = > self . svg . as_ref ( ) ,
2020-06-19 17:16:23 +10:00
}
}
}
2020-07-03 17:32:09 +10:00
pub struct AttrMap ( HashMap < & ' static [ u8 ] , ByNamespace > ) ;
2020-06-19 17:16:23 +10:00
impl AttrMap {
2020-07-03 17:32:09 +10:00
pub const fn new ( map : HashMap < & ' static [ u8 ] , ByNamespace > ) - > AttrMap {
2020-06-19 17:16:23 +10:00
AttrMap ( map )
}
pub fn get ( & self , ns : Namespace , tag : & [ u8 ] , attr : & [ u8 ] ) - > Option < & AttributeMinification > {
self . 0 . get ( attr ) . and_then ( | namespaces | namespaces . get ( ns ) ) . and_then ( | entry | match entry {
AttrMapEntry : : AllNamespaceElements ( min ) = > Some ( min ) ,
AttrMapEntry : : SpecificNamespaceElements ( map ) = > map . get ( tag ) ,
} )
}
}
` ;
2020-11-13 22:15:51 +11:00
code += `
2020-07-03 17:32:09 +10:00
lazy_static ! {
pub static ref ATTRS : AttrMap = {
let mut m = HashMap : : < & ' static [ u8 ] , ByNamespace > : : new ( ) ;
2021-08-06 21:07:58 +10:00
$ { [ . . . Object . entries ( htmlData . attributes ) ]
. map (
( [ attr_name , namespaces ] ) = > ` m.insert(b \ " ${ attr_name } \ ", ByNamespace {
$ { ( [ "html" , "svg" ] as const )
. map (
( ns ) = >
` ${ ns } : ` +
( ( ) = > {
const tagsMap = namespaces [ ns ] ;
if ( ! tagsMap ) {
return "None" ;
}
const globalAttr = tagsMap [ "*" ] ;
if ( globalAttr ) {
return ` Some(AttrMapEntry::AllNamespaceElements( ${ rsTagAttr (
globalAttr
) } ) ) ` ;
}
const entries = Object . entries ( tagsMap ) ;
return ` Some({
let $ {
entries . length ? "mut" : ""
} m = HashMap : : < & ' static [ u8 ] , AttributeMinification > : : new ( ) ;
$ { entries
. map (
( [ tagName , tagAttr ] ) = >
` m.insert(b \ " ${ tagName } \ ", ${ rsTagAttr ( tagAttr ) } ); `
)
. join ( "\n" ) }
2020-07-03 17:32:09 +10:00
AttrMapEntry : : SpecificNamespaceElements ( m )
} ) ` ;
2021-08-06 21:07:58 +10:00
} ) ( ) +
","
)
. join ( "\n" ) }
2020-07-03 17:32:09 +10:00
} ) ;
2021-08-06 21:07:58 +10:00
`
)
. join ( "" ) }
2020-07-03 17:32:09 +10:00
AttrMap : : new ( m )
} ;
} ` ;
2020-06-19 17:16:23 +10:00
2021-08-06 21:07:58 +10:00
writeFileSync ( join ( RUST_OUT_DIR , "attrs.rs" ) , code ) ;