htmlprops-to-react 0.1.1

convert html properties to react
Documentation
#[macro_use]
extern crate lazy_static;

use std::collections::HashMap;

/// convert props to react
pub fn convert_props_react(ctx: String) -> String {    
    let mut context = ctx.clone();

    lazy_static! {
        /// html static list of properties that convert to camel case
        static ref HTML_PROPS: HashMap<&'static str, &'static str> = HashMap::from([
            // special attributes
            ("for", "htmlFor"),
            ("class", "className"),
            // end of special
            ("acceptcharset", "acceptCharset"),
            ("accesskey", "accessKey"),
            ("allowfullscreen", "allowFullScreen"),
            ("allowtransparency", "allowTransparency"),
            ("autocomplete", "autoComplete"),
            ("autofocus", "autoFocus"),
            ("autoplay", "autoPlay"),
            ("cellpadding", "cellPadding"),
            ("cellspacing", "cellSpacing"),
            ("charset", "charSet"),
            ("classid", "classID"),
            ("classname", "className"),
            ("colspan", "colSpan"),
            ("contenteditable", "contentEditable"),
            ("contextmenu", "contextMenu"),
            ("crossorigin", "crossOrigin"),
            ("datetime", "dateTime"),
            ("enctype", "encType"),
            ("formaction", "formAction"),
            ("formenctype", "formEncType"),
            ("formmethod", "formMethod"),
            ("formnovalidate", "formNoValidate"),
            ("formtarget", "formTarget"),
            ("frameborder", "frameBorder"),
            ("hreflang", "hrefLang"),
            ("htmlfor", "htmlFor"),
            ("httpequiv", "httpEquiv"),
            ("inputmode", "inputMode"),
            ("keyparams", "keyParams"),
            ("keytype", "keyType"),
            ("marginheight", "marginHeight"),
            ("marginwidth", "marginWidth"),
            ("maxlength", "maxLength"),
            ("mediagroup", "mediaGroup"),
            ("minlength", "minLength"),
            ("novalidate", "noValidate"),
            ("radiogroup", "radioGroup"),
            ("readonly", "readOnly"),
            ("rowspan", "rowSpan"),
            ("spellcheck", "spellCheck"),
            ("srcdoc", "srcDoc"),
            ("srclang", "srcLang"),
            ("srcset", "srcSet"),
            ("tabindex", "tabIndex"),
            ("usemap", "useMap"),
            // "accentheight",
            // "alignmentbaseline",
            // "allowreorder",
            // "arabicform",
            // "attributename",
            // "attributetype",
            // "autoreverse",
            // "azimuth",
            // "basefrequency",
            // "baseprofile",
            // "baselineshift",
            // "calcmode",
            // "capheight",
            // "clippath",
            // "clippathunits",
            // "cliprule",
            // "colorinterpolation",
            // "colorinterpolationfilters",
            // "colorprofile",
            // "colorrendering",
            // "contentscripttype",
            // "contentstyletype",
            // "diffuseconstant",
            // "dominantbaseline",
            // "edgemode",
            // "enablebackground",
            // "externalresourcesrequired",
            // "fillopacity",
            // "fillrule",
            // "filterres",
            // "filterunits",
            // "floodcolor",
            // "floodopacity",
            // "focusable",
            // "fontfamily",
            // "fontsize",
            // "fontsizeadjust",
            // "fontstretch",
            // "fontstyle",
            // "fontvariant",
            // "fontweight",
            // "glyphname",
            // "glyphorientationhorizontal",
            // "glyphorientationvertical",
            // "glyphref",
            // "gradienttransform",
            // "gradientunits",
            // "horizadvx",
            // "horizoriginx",
            // "ideographic",
            // "imagerendering",
            // "kernelmatrix",
            // "kernelunitlength",
            // "keypoints",
            // "keysplines",
            // "keytimes",
            // "lengthadjust",
            // "letterspacing",
            // "lightingcolor",
            // "limitingconeangle",
            // "markerend",
            // "markerheight",
            // "markermid",
            // "markerstart",
            // "markerunits",
            // "markerwidth",
            // "maskcontentunits",
            // "maskunits",
            // "mathematical",
            // "numoctaves",
            // "overlineposition",
            // "overlinethickness",
            // "paintorder",
            // "panose1",
            // "pathlength",
            // "patterncontentunits",
            // "patterntransform",
            // "patternunits",
            // "pointerevents",
            // "pointsatx",
            // "pointsaty",
            // "pointsatz",
            // "preservealpha",
            // "preserveaspectratio",
            // "primitiveunits",
            // "refx",
            // "refy",
            // "renderingintent",
            // "repeatcount",
            // "repeatdur",
            // "requiredextensions",
            // "requiredfeatures",
            // "shaperendering",
            // "specularconstant",
            // "specularexponent",
            // "spreadmethod",
            // "startoffset",
            // "stddeviation",
            // "stitchtiles",
            // "stopcolor",
            // "stopopacity",
            // "strikethroughposition",
            // "strikethroughthickness",
            // "strokedasharray",
            // "strokedashoffset",
            // "strokelinecap",
            // "strokelinejoin",
            // "strokemiterlimit",
            // "strokeopacity",
            // "strokewidth",
            // "surfacescale",
            // "systemlanguage",
            // "tablevalues",
            // "targetx",
            // "targety",
            // "textanchor",
            // "textdecoration",
            // "textlength",
            // "textrendering",
            // "underlineposition",
            // "underlinethickness",
            // "unicodebidi",
            // "unicoderange",
            // "unitsperem",
            // "valphabetic",
            // "vhanging",
            // "videographic",
            // "vmathematical",
            // "vectoreffect",
            // "vertadvy",
            // "vertoriginx",
            // "vertoriginy",
            ("viewbox", "viewBox"),
            ("viewtarget", "viewTarget"),
            ("wordspacing", "wordSpacing"),
            ("writingmode", "writingMode"),
            // "xchannelselector",
            ("xheight", "xHeight"),
            // "xlinkactuate",
            // "xlinkarcrole",
            // "xlinkhref",
            // "xlinkrole",
            // "xlinkshow",
            // "xlinktitle",
            // "xlinktype",
            // "xmlnsxlink",
            ("xmlbase", "xmlBase"),
            ("xmllang", "xmlLang"),
            ("xmlspace", "xmlSpace"),
            ("ychannelselector", "yChannelSelector"),
            ("zoomandpan", "zoomAndPan"),
            ("onabort", "onAbort"),
            ("onanimationend", "onAnimationEnd"),
            ("onanimationiteration", "onAnimationIteration"),
            ("onanimationstart", "onAnimationStart"),
            ("onblur", "onBlur"),
            ("oncanplay", "onCanPlay"),
            ("oncanplaythrough", "onCanPlayThrough"),
            ("onchange", "onChange"),
            ("onclick", "onClick"),
            ("oncompositionend", "onCompositionEnd"),
            ("oncompositionstart", "onCompositionStart"),
            ("oncompositionupdate", "onCompositionUpdate"),
            ("oncontextmenu", "onContextMenu"),
            ("oncopy", "onCopy"),
            ("oncut", "onCut"),
            ("ondoubleclick", "onDoubleClick"),
            ("ondrag", "onDrag"),
            ("ondragend", "onDragEnd"),
            ("ondragenter", "onDragEnter"),
            ("ondragexit", "onDragExit"),
            ("ondragleave", "onDragLeave"),
            ("ondragover", "onDragOver"),
            ("ondragstart", "onDragStart"),
            ("ondrop", "onDrop"),
            ("ondurationchange", "onDurationChange"),
            ("onemptied", "onEmptied"),
            ("onencrypted", "onEncrypted"),
            ("onended", "onEnded"),
            ("onerror", "onError"),
            ("onfocus", "onFocus"),
            ("oninput", "onInput"),
            ("onkeydown", "onKeyDown"),
            ("onkeypress", "onKeyPress"),
            ("onkeyup", "onKeyUp"),
            ("onload", "onLoad"),
            ("onloadeddata", "onLoadedData"),
            ("onloadedmetadata", "onLoadedMetadata"),
            ("onloadstart", "onLoadStart"),
            ("onmousedown", "onMouseDown"),
            ("onmouseenter", "onMouseEnter"),
            ("onmouseleave", "onMouseLeave"),
            ("onmousemove", "onMouseMove"),
            ("onmouseout", "onMouseOut"),
            ("onmouseover", "onMouseOver"),
            ("onmouseup", "onMouseUp"),
            ("onpaste", "onPaste"),
            ("onpause", "onPause"),
            ("onplay", "onPlay"),
            ("onplaying", "onPlaying"),
            ("onprogress", "onProgress"),
            ("onratechange", "onRateChange"),
            ("onscroll", "onScroll"),
            ("onseeked", "onSeeked"),
            ("onseeking", "onSeeking"),
            ("onselect", "onSelect"),
            ("onstalled", "onStalled"),
            ("onsubmit", "onSubmit"),
            ("onsuspend", "onSuspend"),
            ("ontimeupdate", "onTimeUpdate"),
            ("ontouchcancel", "onTouchCancel"),
            ("ontouchend", "onTouchEnd"),
            ("ontouchmove", "onTouchMove"),
            ("ontouchstart", "onTouchStart"),
            ("ontransitionend", "onTransitionEnd"),
            ("onvolumechange", "onVolumeChange"),
            ("onwaiting", "onWaiting"),
            ("onwheel", "onWheel")
        ]);
    };

    // TODO: parse context to get all keys split to check without full iteration
    for (item, value) in HTML_PROPS.iter() {
        if context.contains(&*item) {
            let v = format!("{}=", item);
            let rp = format!("{}=", value);
            context = context.replace(&v, &rp);
        };
    };

    context
}

#[test]
fn convert_props_react_test() {
    // convert special props class
    let html = r#"<img class="something">"#;
    let react_html = convert_props_react(html.to_string());

    assert_eq!("<img className=\"something\">", react_html);

    // convert special props class and for
    let html = r#"<img class="something" for="mystuff">"#;
    let react_html = convert_props_react(html.to_string());

    assert_eq!("<img className=\"something\" htmlFor=\"mystuff\">", react_html);

    // convert special props class, for, and other props
    let html = r#"<img class="something" for="mystuff" tabindex="2">"#;
    let react_html = convert_props_react(html.to_string());

    assert_eq!("<img className=\"something\" htmlFor=\"mystuff\" tabIndex=\"2\">", react_html);
}