thorish
Version:
This is a library of useful JS concepts and data structures for Node and the browser. It it, unashamedly, a dumping ground for code needed by [@samthor](https://twitter.com/samthor)'s projects.
52 lines (51 loc) • 1.67 kB
TypeScript
export declare enum HtmlState {
/**
* Normal HTML state. Regular content allowed here.
*/
Normal = 0,
/**
* Within a tag declaration, e.g. `<foo ...`, but not yet within an attribute.
*/
WithinTag = 2,
/**
* Sitting directly after a `=` within a tag not followed by a quote.
*/
TagAttr = 3,
/**
* Within a comment started with `<!--`.
*/
WithinComment = 9,
/**
* Within a `<script>` tag. Important as it must be closed by `</script>`.
*/
WithinScriptTag = 10,
/**
* Within a `<style>` tag. Important as it must be closed by `</style>`.
*/
WithinStyleTag = 11,
/**
* Within a `<textarea>` tag. Important as it must be closed by `</textarea>`.
*/
WithinTextAreaTag = 12,
/**
* Within a tag started by `"`.
*/
WithinTagAttrDoubleQuote = 17,
/**
* Within a tag started by `'`.
*/
WithinTagAttrSingleQuote = 18
}
export declare function indexOfCloserWithinTagLike(state: HtmlState, check: string): number;
/**
* Escapes the given raw string for its placement within HTML based on the given state.
*
* This may throw in two cases:
* - state is `HtmlState.Within...` and the text contains the closer (dangerous)
* - state is {@link HtmlState.WithinTag}, no valid/obvious interpolation (TODO: could allow valid attr names)
*/
export declare function escapeStringFor(state: HtmlState, s: string | number): string;
export declare function htmlStateMachine(): {
consume(next: string): HtmlState;
};
export declare function preprocessHtmlTemplateTag(arr: TemplateStringsArray): readonly HtmlState[];