Types for fast-html-parser (#38171)

This commit is contained in:
Ryan Howard 2019-09-06 19:20:20 -05:00 committed by Andrew Casey
parent a3e848394f
commit 9b39d947b0
4 changed files with 101 additions and 0 deletions

View File

@ -0,0 +1,30 @@
import { NodeType, parse } from 'fast-html-parser';
const root = parse(
'<!doctype html><html lang="en-us"><html><body><div id="firstdiv"> first-div </div><div> second-div </div></body></html>',
{
lowerCaseTagName: true,
pre: true,
script: true,
style: true,
},
);
const firstDiv = root.querySelector('div');
const paragraph = parse('<p>This is a paragraph</p>');
const firstDivWithParagraph = firstDiv.appendChild(paragraph);
console.log(root.nodeType);
console.log(firstDiv);
console.log(firstDivWithParagraph);
console.log(root.rawAttributes);
console.log(root.rawAttrs);
console.log(root.rawText);
console.log(root.removeWhitespace());
console.log(root.structure);
console.log(root.structuredText);
console.log(root.tagName);
console.log(root.text);
console.log(root.trimRight());
console.log(root.querySelectorAll('div'));
console.log(NodeType.ELEMENT_NODE);

46
types/fast-html-parser/index.d.ts vendored Normal file
View File

@ -0,0 +1,46 @@
// Type definitions for fast-html-parser 1.0
// Project: https://github.com/ashi009/node-fast-html-parser
// Definitions by: Ryan Howard <https://github.com/rollercodester>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.4
export function parse(data: string, options?: ParseOptions): HTMLElement;
export enum NodeType {
ELEMENT_NODE = 1,
TEXT_NODE = 3,
}
export interface Attributes {
[key: string]: string;
}
export interface HTMLElement {
readonly attributes: Attributes;
readonly childNodes: HTMLElement[];
readonly classNames: string[];
readonly firstChild: HTMLElement;
readonly id: string;
readonly isWhitespace: boolean;
readonly lastChild: HTMLElement;
readonly nodeType: NodeType;
readonly rawAttrs: string;
readonly rawAttributes: Attributes;
readonly rawText: string;
readonly structure: string;
readonly structuredText: string;
readonly text: string;
readonly tagName: string;
appendChild(node: HTMLElement): HTMLElement;
querySelector(selector: string): HTMLElement;
querySelectorAll(selector: string): HTMLElement[];
removeWhitespace(): HTMLElement;
trimRight(pattern?: RegExp): HTMLElement;
}
export interface ParseOptions {
lowerCaseTagName?: boolean;
pre?: boolean;
script?: boolean;
style?: boolean;
}

View File

@ -0,0 +1,24 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6",
"dom"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"fast-html-parser-tests.ts"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }