es-html-parser
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -20,13 +20,14 @@ export declare enum NodeTypes { | ||
DoctypeStart = "DoctypeStart", | ||
DoctypeAttribute = "DoctypeAttribute", | ||
DoctypeEnd = "DoctypeEnd", | ||
ScriptTag = "ScriptTag", | ||
OpenTagStartScript = "OpenTagStartScript", | ||
OpenTagEndScript = "OpenTagEndScript", | ||
OpenScriptTagStart = "OpenScriptTagStart", | ||
OpenScriptTagEnd = "OpenScriptTagEnd", | ||
ScriptTagContent = "ScriptTagContent", | ||
StyleTag = "StyleTag", | ||
OpenTagStartStyle = "OpenTagStartStyle", | ||
OpenTagEndStyle = "OpenTagEndStyle", | ||
OpenStyleTagStart = "OpenStyleTagStart", | ||
OpenStyleTagEnd = "OpenStyleTagEnd", | ||
StyleTagContent = "StyleTagContent", | ||
CloseTagStyle = "CloseTagStyle", | ||
CloseTagScript = "CloseTagScript", | ||
CloseStyleTag = "CloseStyleTag", | ||
CloseScriptTag = "CloseScriptTag", | ||
DoctypeAttributeValue = "DoctypeAttributeValue", | ||
@@ -33,0 +34,0 @@ DoctypeAttributeWrapperStart = "DoctypeAttributeWrapperStart", |
@@ -24,13 +24,14 @@ "use strict"; | ||
NodeTypes["DoctypeStart"] = "DoctypeStart"; | ||
NodeTypes["DoctypeAttribute"] = "DoctypeAttribute"; | ||
NodeTypes["DoctypeEnd"] = "DoctypeEnd"; | ||
NodeTypes["ScriptTag"] = "ScriptTag"; | ||
NodeTypes["OpenTagStartScript"] = "OpenTagStartScript"; | ||
NodeTypes["OpenTagEndScript"] = "OpenTagEndScript"; | ||
NodeTypes["OpenScriptTagStart"] = "OpenScriptTagStart"; | ||
NodeTypes["OpenScriptTagEnd"] = "OpenScriptTagEnd"; | ||
NodeTypes["ScriptTagContent"] = "ScriptTagContent"; | ||
NodeTypes["StyleTag"] = "StyleTag"; | ||
NodeTypes["OpenTagStartStyle"] = "OpenTagStartStyle"; | ||
NodeTypes["OpenTagEndStyle"] = "OpenTagEndStyle"; | ||
NodeTypes["OpenStyleTagStart"] = "OpenStyleTagStart"; | ||
NodeTypes["OpenStyleTagEnd"] = "OpenStyleTagEnd"; | ||
NodeTypes["StyleTagContent"] = "StyleTagContent"; | ||
NodeTypes["CloseTagStyle"] = "CloseTagStyle"; | ||
NodeTypes["CloseTagScript"] = "CloseTagScript"; | ||
NodeTypes["CloseStyleTag"] = "CloseStyleTag"; | ||
NodeTypes["CloseScriptTag"] = "CloseScriptTag"; | ||
NodeTypes["DoctypeAttributeValue"] = "DoctypeAttributeValue"; | ||
@@ -37,0 +38,0 @@ NodeTypes["DoctypeAttributeWrapperStart"] = "DoctypeAttributeWrapperStart"; |
@@ -19,10 +19,10 @@ export declare enum TokenTypes { | ||
CommentEnd = "CommentEnd", | ||
OpenTagStartScript = "OpenTagStartScript", | ||
OpenTagEndScript = "OpenTagEndScript", | ||
OpenScriptTagStart = "OpenScriptTagStart", | ||
OpenScriptTagEnd = "OpenScriptTagEnd", | ||
ScriptTagContent = "ScriptTagContent", | ||
CloseTagScript = "CloseTagScript", | ||
OpenTagStartStyle = "OpenTagStartStyle", | ||
OpenTagEndStyle = "OpenTagEndStyle", | ||
CloseScriptTag = "CloseScriptTag", | ||
OpenStyleTagStart = "OpenStyleTagStart", | ||
OpenStyleTagEnd = "OpenStyleTagEnd", | ||
StyleTagContent = "StyleTagContent", | ||
CloseTagStyle = "CloseTagStyle" | ||
CloseStyleTag = "CloseStyleTag" | ||
} |
@@ -23,10 +23,10 @@ "use strict"; | ||
TokenTypes["CommentEnd"] = "CommentEnd"; | ||
TokenTypes["OpenTagStartScript"] = "OpenTagStartScript"; | ||
TokenTypes["OpenTagEndScript"] = "OpenTagEndScript"; | ||
TokenTypes["OpenScriptTagStart"] = "OpenScriptTagStart"; | ||
TokenTypes["OpenScriptTagEnd"] = "OpenScriptTagEnd"; | ||
TokenTypes["ScriptTagContent"] = "ScriptTagContent"; | ||
TokenTypes["CloseTagScript"] = "CloseTagScript"; | ||
TokenTypes["OpenTagStartStyle"] = "OpenTagStartStyle"; | ||
TokenTypes["OpenTagEndStyle"] = "OpenTagEndStyle"; | ||
TokenTypes["CloseScriptTag"] = "CloseScriptTag"; | ||
TokenTypes["OpenStyleTagStart"] = "OpenStyleTagStart"; | ||
TokenTypes["OpenStyleTagEnd"] = "OpenStyleTagEnd"; | ||
TokenTypes["StyleTagContent"] = "StyleTagContent"; | ||
TokenTypes["CloseTagStyle"] = "CloseTagStyle"; | ||
TokenTypes["CloseStyleTag"] = "CloseStyleTag"; | ||
})(TokenTypes = exports.TokenTypes || (exports.TokenTypes = {})); |
export { parse } from "./parser"; | ||
export { NodeTypes } from "./constants/node-types"; | ||
export { TokenTypes } from "./constants/token-types"; | ||
export * from "./types/node"; | ||
export { NodeTypes, TokenTypes } from "./constants"; | ||
export { AnyToken, AnyNode, ParseResult } from "./types"; |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -20,6 +6,4 @@ exports.TokenTypes = exports.NodeTypes = exports.parse = void 0; | ||
Object.defineProperty(exports, "parse", { enumerable: true, get: function () { return parser_1.parse; } }); | ||
var node_types_1 = require("./constants/node-types"); | ||
Object.defineProperty(exports, "NodeTypes", { enumerable: true, get: function () { return node_types_1.NodeTypes; } }); | ||
var token_types_1 = require("./constants/token-types"); | ||
Object.defineProperty(exports, "TokenTypes", { enumerable: true, get: function () { return token_types_1.TokenTypes; } }); | ||
__exportStar(require("./types/node"), exports); | ||
var constants_1 = require("./constants"); | ||
Object.defineProperty(exports, "NodeTypes", { enumerable: true, get: function () { return constants_1.NodeTypes; } }); | ||
Object.defineProperty(exports, "TokenTypes", { enumerable: true, get: function () { return constants_1.TokenTypes; } }); |
@@ -1,5 +0,2 @@ | ||
import { DocumentNode, Token } from "../types"; | ||
export declare function parse(html: string): { | ||
ast: DocumentNode; | ||
tokens: Token[]; | ||
}; | ||
import { ParseResult } from "../types"; | ||
export declare function parse(html: string): ParseResult; |
@@ -10,5 +10,4 @@ "use strict"; | ||
const { ast } = (0, tree_constructor_1.constructTree)(tokens, undefined); | ||
(0, utils_1.clearParent)(ast); | ||
return { | ||
ast, | ||
ast: (0, utils_1.clearParent)(ast), | ||
tokens, | ||
@@ -15,0 +14,0 @@ }; |
@@ -1,2 +0,2 @@ | ||
import { Token, TokenizerState } from "../../types"; | ||
export declare function parse(chars: string, state: TokenizerState, tokens: Token[]): void; | ||
import { AnyToken, TokenizerState } from "../../types"; | ||
export declare function parse(chars: string, state: TokenizerState, tokens: AnyToken[]): void; |
@@ -9,3 +9,3 @@ "use strict"; | ||
if (isKeyBreak(chars)) { | ||
return parseKeyEnd(state, tokens, chars === "\n"); | ||
return parseKeyEnd(state, tokens); | ||
} | ||
@@ -25,3 +25,3 @@ state.accumulatedContent += state.decisionBuffer; | ||
} | ||
function parseKeyEnd(state, tokens, isNewLine) { | ||
function parseKeyEnd(state, tokens) { | ||
const position = (0, utils_1.calculateTokenPosition)(state, { keepBuffer: false }); | ||
@@ -31,11 +31,4 @@ tokens.push({ | ||
value: state.accumulatedContent, | ||
range: [position.startPosition, position.endPosition], | ||
loc: { | ||
start: { | ||
line: position.loc.start.line, | ||
}, | ||
end: { | ||
line: position.loc.end.line - Number(isNewLine), | ||
}, | ||
}, | ||
range: position.range, | ||
loc: position.loc, | ||
}); | ||
@@ -42,0 +35,0 @@ state.accumulatedContent = ""; |
@@ -1,3 +0,3 @@ | ||
import { Token, TokenizerState } from "../../types"; | ||
export declare function parseValueEnd(state: TokenizerState, tokens: Token[], isNewLine: boolean): void; | ||
export declare function parse(chars: string, state: TokenizerState, tokens: Token[]): void; | ||
import { AnyToken, TokenizerState } from "../../types"; | ||
export declare function parseValueEnd(state: TokenizerState, tokens: AnyToken[]): void; | ||
export declare function parse(chars: string, state: TokenizerState, tokens: AnyToken[]): void; |
@@ -5,18 +5,10 @@ "use strict"; | ||
const constants_1 = require("../../constants"); | ||
const token_types_1 = require("../../constants/token-types"); | ||
const utils_1 = require("../../utils"); | ||
function parseValueEnd(state, tokens, isNewLine) { | ||
function parseValueEnd(state, tokens) { | ||
const position = (0, utils_1.calculateTokenPosition)(state, { keepBuffer: false }); | ||
tokens.push({ | ||
type: token_types_1.TokenTypes.AttributeValue, | ||
type: constants_1.TokenTypes.AttributeValue, | ||
value: state.accumulatedContent, | ||
range: [position.startPosition, position.endPosition], | ||
loc: { | ||
start: { | ||
line: position.loc.start.line, | ||
}, | ||
end: { | ||
line: position.loc.end.line - Number(isNewLine), | ||
}, | ||
}, | ||
range: position.range, | ||
loc: position.loc, | ||
}); | ||
@@ -30,3 +22,3 @@ state.accumulatedContent = ""; | ||
if ((0, utils_1.isWhitespace)(chars) || chars === ">" || chars === "/") { | ||
return parseValueEnd(state, tokens, chars === "\n"); | ||
return parseValueEnd(state, tokens); | ||
} | ||
@@ -33,0 +25,0 @@ state.accumulatedContent += state.decisionBuffer; |
@@ -1,2 +0,2 @@ | ||
import { Token, TokenizerState } from "../../types"; | ||
export declare function parse(chars: string, state: TokenizerState, tokens: Token[]): void; | ||
import { AnyToken, TokenizerState } from "../../types"; | ||
export declare function parse(chars: string, state: TokenizerState, tokens: AnyToken[]): void; |
@@ -5,3 +5,2 @@ "use strict"; | ||
const constants_1 = require("../../constants"); | ||
const constants_2 = require("../../constants"); | ||
const utils_1 = require("../../utils"); | ||
@@ -20,17 +19,16 @@ function parse(chars, state, tokens) { | ||
const position = (0, utils_1.calculateTokenPosition)(state, { keepBuffer: false }); | ||
const endWrapperPosition = position.endPosition; | ||
const endWrapperPosition = position.range[1]; | ||
tokens.push({ | ||
type: constants_2.TokenTypes.AttributeValue, | ||
type: constants_1.TokenTypes.AttributeValue, | ||
value: state.accumulatedContent, | ||
range: [position.startPosition, position.endPosition], | ||
range: position.range, | ||
loc: position.loc, | ||
}); | ||
const range = [endWrapperPosition, endWrapperPosition + 1]; | ||
const loc = (0, utils_1.calculateTokenLocation)(state.source, range); | ||
tokens.push({ | ||
type: constants_2.TokenTypes.AttributeValueWrapperEnd, | ||
type: constants_1.TokenTypes.AttributeValueWrapperEnd, | ||
value: state.decisionBuffer, | ||
range: [endWrapperPosition, endWrapperPosition + 1], | ||
loc: { | ||
start: position.loc.end, | ||
end: position.loc.end, | ||
}, | ||
range, | ||
loc, | ||
}); | ||
@@ -37,0 +35,0 @@ state.accumulatedContent = ""; |
@@ -1,2 +0,2 @@ | ||
import { TokenizerState, Token } from "../../types"; | ||
export declare function parse(chars: string, state: TokenizerState, tokens: Token[]): void; | ||
import { TokenizerState, AnyToken } from "../../types"; | ||
export declare function parse(chars: string, state: TokenizerState, tokens: AnyToken[]): void; |
@@ -22,14 +22,9 @@ "use strict"; | ||
const wrapper = state.decisionBuffer; | ||
const range = [state.caretPosition, state.caretPosition + 1]; | ||
const loc = (0, utils_1.calculateTokenLocation)(state.source, range); | ||
tokens.push({ | ||
type: constants_1.TokenTypes.AttributeValueWrapperStart, | ||
value: wrapper, | ||
range: [state.caretPosition, state.caretPosition + 1], | ||
loc: { | ||
start: { | ||
line: state.linePosition, | ||
}, | ||
end: { | ||
line: state.linePosition, | ||
}, | ||
}, | ||
range, | ||
loc, | ||
}); | ||
@@ -36,0 +31,0 @@ state.accumulatedContent = ""; |
@@ -1,2 +0,2 @@ | ||
import { Token, TokenizerState } from "../../types"; | ||
export declare function parse(chars: string, state: TokenizerState, tokens: Token[]): void; | ||
import { AnyToken, TokenizerState } from "../../types"; | ||
export declare function parse(chars: string, state: TokenizerState, tokens: AnyToken[]): void; |
@@ -39,3 +39,3 @@ "use strict"; | ||
value: state.decisionBuffer, | ||
range: [position.startPosition, position.endPosition], | ||
range: position.range, | ||
loc: position.loc, | ||
@@ -42,0 +42,0 @@ }); |
@@ -1,2 +0,2 @@ | ||
import { Token, TokenizerState } from "../../types"; | ||
export declare function parse(chars: string, state: TokenizerState, tokens: Token[]): void; | ||
import { AnyToken, TokenizerState } from "../../types"; | ||
export declare function parse(chars: string, state: TokenizerState, tokens: AnyToken[]): void; |
@@ -20,3 +20,3 @@ "use strict"; | ||
value: state.accumulatedContent + state.decisionBuffer, | ||
range: [position.startPosition, position.endPosition], | ||
range: position.range, | ||
loc: position.loc, | ||
@@ -23,0 +23,0 @@ }); |
@@ -1,2 +0,2 @@ | ||
import { Token, TokenizerState } from "../../types"; | ||
export declare function parse(chars: string, state: TokenizerState, tokens: Token[]): void; | ||
import { AnyToken, TokenizerState } from "../../types"; | ||
export declare function parse(chars: string, state: TokenizerState, tokens: AnyToken[]): void; |
@@ -22,10 +22,11 @@ "use strict"; | ||
const position = (0, utils_1.calculateTokenPosition)(state, { keepBuffer: false }); | ||
const endRange = { | ||
startPosition: position.endPosition, | ||
endPosition: position.endPosition + COMMENT_END.length, | ||
}; | ||
const endRange = [ | ||
position.range[1], | ||
position.range[1] + COMMENT_END.length, | ||
]; | ||
const endLoc = (0, utils_1.calculateTokenLocation)(state.source, endRange); | ||
tokens.push({ | ||
type: constants_1.TokenTypes.CommentContent, | ||
value: state.accumulatedContent, | ||
range: [position.startPosition, position.endPosition], | ||
range: position.range, | ||
loc: position.loc, | ||
@@ -35,7 +36,4 @@ }, { | ||
value: state.decisionBuffer, | ||
range: [endRange.startPosition, endRange.endPosition], | ||
loc: { | ||
start: position.loc.end, | ||
end: position.loc.end, | ||
}, | ||
range: endRange, | ||
loc: endLoc, | ||
}); | ||
@@ -42,0 +40,0 @@ state.accumulatedContent = ""; |
@@ -1,3 +0,3 @@ | ||
import { Token, TokenizerState } from "../../types"; | ||
export declare function parse(chars: string, state: TokenizerState, tokens: Token[]): void; | ||
export declare function handleContentEnd(state: TokenizerState, tokens: Token[]): void; | ||
import { AnyToken, TokenizerState } from "../../types"; | ||
export declare function parse(chars: string, state: TokenizerState, tokens: AnyToken[]): void; | ||
export declare function handleContentEnd(state: TokenizerState, tokens: AnyToken[]): void; |
@@ -41,3 +41,3 @@ "use strict"; | ||
value: textContent, | ||
range: [position.startPosition, position.endPosition], | ||
range: position.range, | ||
loc: position.loc, | ||
@@ -53,3 +53,3 @@ }); | ||
value: state.accumulatedContent, | ||
range: [position.startPosition, position.endPosition], | ||
range: position.range, | ||
loc: position.loc, | ||
@@ -90,22 +90,12 @@ }; | ||
} | ||
const commentStartPosition = { | ||
startPosition: state.caretPosition - (COMMENT_START.length - 1), | ||
endPosition: state.caretPosition + 1, | ||
loc: { | ||
start: { | ||
line: state.linePosition, | ||
}, | ||
end: { | ||
line: state.linePosition, | ||
}, | ||
}, | ||
}; | ||
const range = [ | ||
state.caretPosition - (COMMENT_START.length - 1), | ||
state.caretPosition + 1, | ||
]; | ||
const loc = (0, utils_1.calculateTokenLocation)(state.source, range); | ||
tokens.push({ | ||
type: constants_1.TokenTypes.CommentStart, | ||
value: state.decisionBuffer, | ||
range: [ | ||
commentStartPosition.startPosition, | ||
commentStartPosition.endPosition, | ||
], | ||
loc: commentStartPosition.loc, | ||
range: range, | ||
loc, | ||
}); | ||
@@ -112,0 +102,0 @@ state.accumulatedContent = ""; |
@@ -1,2 +0,2 @@ | ||
import { Token, TokenizerState } from "../../types"; | ||
export declare function parse(chars: string, state: TokenizerState, tokens: Token[]): void; | ||
import { AnyToken, TokenizerState } from "../../types"; | ||
export declare function parse(chars: string, state: TokenizerState, tokens: AnyToken[]): void; |
@@ -8,3 +8,3 @@ "use strict"; | ||
if ((0, utils_1.isWhitespace)(chars) || chars === ">") { | ||
return parseAttributeEnd(state, tokens, chars === "\n"); | ||
return parseAttributeEnd(state, tokens); | ||
} | ||
@@ -16,3 +16,3 @@ state.accumulatedContent += state.decisionBuffer; | ||
exports.parse = parse; | ||
function parseAttributeEnd(state, tokens, isNewLine) { | ||
function parseAttributeEnd(state, tokens) { | ||
const position = (0, utils_1.calculateTokenPosition)(state, { keepBuffer: false }); | ||
@@ -22,11 +22,4 @@ tokens.push({ | ||
value: state.accumulatedContent, | ||
range: [position.startPosition, position.endPosition], | ||
loc: { | ||
start: { | ||
line: position.loc.start.line, | ||
}, | ||
end: { | ||
line: position.loc.end.line - Number(isNewLine), | ||
}, | ||
}, | ||
range: position.range, | ||
loc: position.loc, | ||
}); | ||
@@ -33,0 +26,0 @@ state.accumulatedContent = ""; |
@@ -1,2 +0,2 @@ | ||
import { Token, TokenizerState } from "../../types"; | ||
export declare function parse(chars: string, state: TokenizerState, tokens: Token[]): void; | ||
import { AnyToken, TokenizerState } from "../../types"; | ||
export declare function parse(chars: string, state: TokenizerState, tokens: AnyToken[]): void; |
@@ -18,14 +18,16 @@ "use strict"; | ||
const position = (0, utils_1.calculateTokenPosition)(state, { keepBuffer: false }); | ||
const endWrapperPosition = position.endPosition; | ||
const endWrapperPosition = position.range[1]; | ||
tokens.push({ | ||
type: constants_1.TokenTypes.DoctypeAttributeValue, | ||
value: state.accumulatedContent, | ||
range: [position.startPosition, position.endPosition], | ||
range: position.range, | ||
loc: position.loc, | ||
}); | ||
const range = [endWrapperPosition, endWrapperPosition + 1]; | ||
const loc = (0, utils_1.calculateTokenLocation)(state.source, range); | ||
tokens.push({ | ||
type: constants_1.TokenTypes.DoctypeAttributeWrapperEnd, | ||
value: state.decisionBuffer, | ||
range: [endWrapperPosition, endWrapperPosition + 1], | ||
loc: position.loc, | ||
range, | ||
loc, | ||
}); | ||
@@ -32,0 +34,0 @@ state.accumulatedContent = ""; |
@@ -1,2 +0,2 @@ | ||
import { Token, TokenizerState } from "../../types"; | ||
export declare function parse(chars: string, state: TokenizerState, tokens: Token[]): void; | ||
import { AnyToken, TokenizerState } from "../../types"; | ||
export declare function parse(chars: string, state: TokenizerState, tokens: AnyToken[]): void; |
@@ -22,14 +22,12 @@ "use strict"; | ||
const wrapper = state.decisionBuffer; | ||
const range = [ | ||
state.caretPosition, | ||
state.caretPosition + wrapper.length, | ||
]; | ||
const loc = (0, utils_1.calculateTokenLocation)(state.source, range); | ||
tokens.push({ | ||
type: constants_1.TokenTypes.DoctypeAttributeWrapperStart, | ||
value: wrapper, | ||
range: [state.caretPosition, state.caretPosition + wrapper.length], | ||
loc: { | ||
start: { | ||
line: state.linePosition, | ||
}, | ||
end: { | ||
line: state.linePosition, | ||
}, | ||
}, | ||
range, | ||
loc, | ||
}); | ||
@@ -36,0 +34,0 @@ state.accumulatedContent = ""; |
@@ -1,2 +0,2 @@ | ||
import { Token, TokenizerState } from "../../types"; | ||
export declare function parse(chars: string, state: TokenizerState, tokens: Token[]): void; | ||
import { AnyToken, TokenizerState } from "../../types"; | ||
export declare function parse(chars: string, state: TokenizerState, tokens: AnyToken[]): void; |
@@ -11,3 +11,3 @@ "use strict"; | ||
value: state.decisionBuffer, | ||
range: [position.startPosition, position.endPosition], | ||
range: position.range, | ||
loc: position.loc, | ||
@@ -14,0 +14,0 @@ }); |
@@ -1,2 +0,2 @@ | ||
import { Token, TokenizerState } from "../../types"; | ||
export declare function parse(chars: string, state: TokenizerState, tokens: Token[]): void; | ||
import { AnyToken, TokenizerState } from "../../types"; | ||
export declare function parse(chars: string, state: TokenizerState, tokens: AnyToken[]): void; |
@@ -8,3 +8,3 @@ "use strict"; | ||
if ((0, utils_1.isWhitespace)(chars)) { | ||
return parseWhitespace(state, tokens, chars === "\n"); | ||
return parseWhitespace(state, tokens); | ||
} | ||
@@ -18,3 +18,3 @@ if (chars === ">") { | ||
exports.parse = parse; | ||
function generateDoctypeStartToken(state, isNewLine = false) { | ||
function generateDoctypeStartToken(state) { | ||
const position = (0, utils_1.calculateTokenPosition)(state, { keepBuffer: false }); | ||
@@ -24,13 +24,8 @@ return { | ||
value: state.accumulatedContent, | ||
range: [position.startPosition, position.endPosition], | ||
loc: { | ||
start: position.loc.start, | ||
end: { | ||
line: position.loc.end.line - Number(isNewLine), | ||
}, | ||
}, | ||
range: position.range, | ||
loc: position.loc, | ||
}; | ||
} | ||
function parseWhitespace(state, tokens, isNewLine) { | ||
tokens.push(generateDoctypeStartToken(state, isNewLine)); | ||
function parseWhitespace(state, tokens) { | ||
tokens.push(generateDoctypeStartToken(state)); | ||
state.accumulatedContent = ""; | ||
@@ -37,0 +32,0 @@ state.decisionBuffer = ""; |
@@ -1,2 +0,2 @@ | ||
import { Token, TokenizerState } from "../../types"; | ||
export declare function parse(chars: string, state: TokenizerState, tokens: Token[]): void; | ||
import { AnyToken, TokenizerState } from "../../types"; | ||
export declare function parse(chars: string, state: TokenizerState, tokens: AnyToken[]): void; |
@@ -7,4 +7,4 @@ "use strict"; | ||
const tokensMap = { | ||
script: constants_1.TokenTypes.OpenTagEndScript, | ||
style: constants_1.TokenTypes.OpenTagEndStyle, | ||
script: constants_1.TokenTypes.OpenScriptTagEnd, | ||
style: constants_1.TokenTypes.OpenStyleTagEnd, | ||
default: constants_1.TokenTypes.OpenTagEnd, | ||
@@ -32,3 +32,3 @@ }; | ||
value: state.accumulatedContent + state.decisionBuffer, | ||
range: [position.startPosition, position.endPosition], | ||
range: position.range, | ||
loc: position.loc, | ||
@@ -35,0 +35,0 @@ }); |
@@ -1,2 +0,2 @@ | ||
import { Token, TokenizerState } from "../../types"; | ||
export declare function parse(chars: string, state: TokenizerState, tokens: Token[]): void; | ||
import { AnyToken, TokenizerState } from "../../types"; | ||
export declare function parse(chars: string, state: TokenizerState, tokens: AnyToken[]): void; |
@@ -7,4 +7,4 @@ "use strict"; | ||
const tokensMap = { | ||
script: constants_1.TokenTypes.OpenTagStartScript, | ||
style: constants_1.TokenTypes.OpenTagStartStyle, | ||
script: constants_1.TokenTypes.OpenScriptTagStart, | ||
style: constants_1.TokenTypes.OpenStyleTagStart, | ||
default: constants_1.TokenTypes.OpenTagStart, | ||
@@ -17,3 +17,3 @@ }; | ||
if ((0, utils_1.isWhitespace)(chars)) { | ||
return parseWhitespace(state, tokens, chars === "\n"); | ||
return parseWhitespace(state, tokens); | ||
} | ||
@@ -25,3 +25,3 @@ state.accumulatedContent += state.decisionBuffer; | ||
exports.parse = parse; | ||
function parseWhitespace(state, tokens, isNewLine) { | ||
function parseWhitespace(state, tokens) { | ||
const tagName = (0, utils_1.parseOpenTagName)(state.accumulatedContent); | ||
@@ -32,9 +32,4 @@ const position = (0, utils_1.calculateTokenPosition)(state, { keepBuffer: false }); | ||
value: state.accumulatedContent, | ||
range: [position.startPosition, position.endPosition], | ||
loc: { | ||
start: position.loc.start, | ||
end: { | ||
line: position.loc.end.line - Number(isNewLine), | ||
}, | ||
}, | ||
range: position.range, | ||
loc: position.loc, | ||
}); | ||
@@ -53,3 +48,3 @@ state.accumulatedContent = ""; | ||
value: state.accumulatedContent, | ||
range: [position.startPosition, position.endPosition], | ||
range: position.range, | ||
loc: position.loc, | ||
@@ -56,0 +51,0 @@ }); |
@@ -1,2 +0,2 @@ | ||
import { Token, TokenizerState } from "../../types"; | ||
export declare function parse(chars: string, state: TokenizerState, tokens: Token[]): void; | ||
import { AnyToken, TokenizerState } from "../../types"; | ||
export declare function parse(chars: string, state: TokenizerState, tokens: AnyToken[]): void; |
@@ -27,21 +27,16 @@ "use strict"; | ||
value: state.accumulatedContent, | ||
range: [position.startPosition, position.endPosition], | ||
range: position.range, | ||
loc: position.loc, | ||
}); | ||
} | ||
const range = [ | ||
state.caretPosition - (state.decisionBuffer.length - 1), | ||
state.caretPosition + 1, | ||
]; | ||
const loc = (0, utils_1.calculateTokenLocation)(state.source, range); | ||
tokens.push({ | ||
type: constants_1.TokenTypes.CloseTagScript, | ||
type: constants_1.TokenTypes.CloseScriptTag, | ||
value: state.decisionBuffer, | ||
range: [ | ||
state.caretPosition - (state.decisionBuffer.length - 1), | ||
state.caretPosition + 1, | ||
], | ||
loc: { | ||
start: { | ||
line: state.linePosition, | ||
}, | ||
end: { | ||
line: state.linePosition, | ||
}, | ||
}, | ||
range, | ||
loc, | ||
}); | ||
@@ -48,0 +43,0 @@ state.accumulatedContent = ""; |
@@ -1,2 +0,2 @@ | ||
import { Token, TokenizerState } from "../../types"; | ||
export declare function parse(chars: string, state: TokenizerState, tokens: Token[]): void; | ||
import { AnyToken, TokenizerState } from "../../types"; | ||
export declare function parse(chars: string, state: TokenizerState, tokens: AnyToken[]): void; |
@@ -28,21 +28,16 @@ "use strict"; | ||
value: state.accumulatedContent, | ||
range: [position.startPosition, position.endPosition], | ||
range: position.range, | ||
loc: position.loc, | ||
}); | ||
} | ||
const range = [ | ||
state.caretPosition - (state.decisionBuffer.length - 1), | ||
state.caretPosition + 1, | ||
]; | ||
const loc = (0, utils_1.calculateTokenLocation)(state.source, range); | ||
tokens.push({ | ||
type: constants_1.TokenTypes.CloseTagStyle, | ||
type: constants_1.TokenTypes.CloseStyleTag, | ||
value: state.decisionBuffer, | ||
range: [ | ||
state.caretPosition - (state.decisionBuffer.length - 1), | ||
state.caretPosition + 1, | ||
], | ||
loc: { | ||
start: { | ||
line: state.linePosition, | ||
}, | ||
end: { | ||
line: state.linePosition, | ||
}, | ||
}, | ||
range, | ||
loc, | ||
}); | ||
@@ -49,0 +44,0 @@ state.accumulatedContent = ""; |
@@ -1,7 +0,7 @@ | ||
import { Token, TokenizerState } from "../types"; | ||
export declare function tokenize(content: string | undefined, existingState: TokenizerState | undefined, { isFinalChunk, }?: { | ||
import { AnyToken, TokenizerState } from "../types"; | ||
export declare function tokenize(source?: string, { isFinalChunk, }?: { | ||
isFinalChunk?: boolean; | ||
}): { | ||
state: TokenizerState; | ||
tokens: Token[]; | ||
tokens: AnyToken[]; | ||
}; |
@@ -48,19 +48,14 @@ "use strict"; | ||
} | ||
function tokenize(content = "", existingState, { isFinalChunk, } = {}) { | ||
function tokenize(source = "", { isFinalChunk, } = {}) { | ||
isFinalChunk = isFinalChunk === undefined ? true : isFinalChunk; | ||
let state; | ||
if (existingState !== undefined) { | ||
state = Object.assign({}, existingState); | ||
} | ||
else { | ||
state = { | ||
currentContext: constants_1.TokenizerContextTypes.Data, | ||
contextParams: {}, | ||
decisionBuffer: "", | ||
accumulatedContent: "", | ||
caretPosition: 0, | ||
linePosition: 1, | ||
}; | ||
} | ||
const chars = state.decisionBuffer + content; | ||
const state = { | ||
currentContext: constants_1.TokenizerContextTypes.Data, | ||
contextParams: {}, | ||
decisionBuffer: "", | ||
accumulatedContent: "", | ||
caretPosition: 0, | ||
linePosition: 1, | ||
source, | ||
}; | ||
const chars = state.decisionBuffer + source; | ||
const tokens = []; | ||
@@ -67,0 +62,0 @@ const positionOffset = state.caretPosition - state.decisionBuffer.length; |
@@ -1,5 +0,5 @@ | ||
import { ConstructTreeState, Token, DocumentNode } from "../types"; | ||
export declare function constructTree(tokens: Token[], existingState: ConstructTreeState | undefined): { | ||
import { ConstructTreeState, DocumentNode, AnyToken } from "../types"; | ||
export declare function constructTree(tokens: AnyToken[], existingState: ConstructTreeState<any> | undefined): { | ||
state: ConstructTreeState<any> | undefined; | ||
ast: DocumentNode; | ||
}; |
@@ -22,2 +22,13 @@ "use strict"; | ||
}; | ||
const EMPTY_RANGE = [0, 0]; | ||
const EMPTY_LOC = { | ||
start: { | ||
line: 1, | ||
column: 0, | ||
}, | ||
end: { | ||
line: 1, | ||
column: 0, | ||
}, | ||
}; | ||
function constructTree(tokens, existingState) { | ||
@@ -32,13 +43,10 @@ let state = existingState; | ||
const lastToken = (0, utils_1.last)(tokens); | ||
const range = lastToken ? [0, lastToken.range[1]] : [0, 0]; | ||
const firstToken = (0, utils_1.first)(tokens); | ||
const range = lastToken ? [0, lastToken.range[1]] : EMPTY_RANGE; | ||
const loc = lastToken | ||
? (0, clone_location_1.cloneLocation)(lastToken.loc) | ||
: { | ||
start: { | ||
line: 1, | ||
}, | ||
end: { | ||
line: 1, | ||
}, | ||
}; | ||
? { | ||
start: (0, clone_location_1.cloneLocation)(firstToken.loc).start, | ||
end: (0, clone_location_1.cloneLocation)(lastToken.loc).end, | ||
} | ||
: EMPTY_LOC; | ||
loc.start.line = 1; | ||
@@ -67,4 +75,2 @@ const rootNode = { | ||
const token = tokens[tokenIndex]; | ||
// debugger; | ||
// @ts-ignore | ||
const handler = contextHandlers[state.currentContext.type].construct; | ||
@@ -71,0 +77,0 @@ state = handler(token, state); |
@@ -1,2 +0,2 @@ | ||
import { ConstructTreeState, Token } from "../../types"; | ||
export declare function construct(token: Token, state: ConstructTreeState): ConstructTreeState; | ||
import { AnyToken, ConstructTreeState, ContextualTagNode } from "../../types"; | ||
export declare function construct(token: AnyToken, state: ConstructTreeState<ContextualTagNode>): ConstructTreeState<ContextualTagNode>; |
@@ -10,4 +10,4 @@ "use strict"; | ||
constants_1.TokenTypes.AttributeAssignment, | ||
constants_1.TokenTypes.OpenTagEndScript, | ||
constants_1.TokenTypes.OpenTagEndStyle, | ||
constants_1.TokenTypes.OpenScriptTagEnd, | ||
constants_1.TokenTypes.OpenStyleTagEnd, | ||
]; | ||
@@ -32,11 +32,4 @@ function getLastAttribute(state) { | ||
if (!attribute.key) { | ||
attribute.range = [token.range[0], token.range[1]]; | ||
attribute.loc = { | ||
start: { | ||
...token.loc.start, | ||
}, | ||
end: { | ||
...token.loc.end, | ||
}, | ||
}; | ||
attribute.range = (0, utils_1.cloneRange)(token.range); | ||
attribute.loc = (0, utils_1.cloneLocation)(token.loc); | ||
} | ||
@@ -43,0 +36,0 @@ state.caretPosition++; |
@@ -1,2 +0,2 @@ | ||
import { ConstructTreeState, Token } from "../../types"; | ||
export declare function construct(token: Token, state: ConstructTreeState): ConstructTreeState<any>; | ||
import { AnyToken, ConstructTreeState, ContextualTagNode } from "../../types"; | ||
export declare function construct(token: AnyToken, state: ConstructTreeState<ContextualTagNode>): ConstructTreeState<ContextualTagNode>; |
@@ -8,4 +8,4 @@ "use strict"; | ||
constants_1.TokenTypes.OpenTagEnd, | ||
constants_1.TokenTypes.OpenTagEndScript, | ||
constants_1.TokenTypes.OpenTagEndStyle, | ||
constants_1.TokenTypes.OpenScriptTagEnd, | ||
constants_1.TokenTypes.OpenStyleTagEnd, | ||
]; | ||
@@ -12,0 +12,0 @@ function getLastAttribute(state) { |
@@ -1,2 +0,2 @@ | ||
import { ConstructTreeState, Token } from "../../types"; | ||
export declare function construct(token: Token, state: ConstructTreeState): ConstructTreeState<any>; | ||
import { AnyToken, ConstructTreeState, ContextualTagNode } from "../../types"; | ||
export declare function construct(token: AnyToken, state: ConstructTreeState<ContextualTagNode>): ConstructTreeState<ContextualTagNode>; |
@@ -6,3 +6,2 @@ "use strict"; | ||
const utils_1 = require("../../utils"); | ||
const clone_location_1 = require("../../utils/clone-location"); | ||
const ATTRIBUTE_START_TOKENS = [ | ||
@@ -14,9 +13,7 @@ constants_1.TokenTypes.AttributeKey, | ||
constants_1.TokenTypes.OpenTagEnd, | ||
constants_1.TokenTypes.OpenTagEndStyle, | ||
constants_1.TokenTypes.OpenTagEndScript, | ||
constants_1.TokenTypes.OpenStyleTagEnd, | ||
constants_1.TokenTypes.OpenScriptTagEnd, | ||
]; | ||
function handlerAttributeStart(state, token) { | ||
if (state.currentNode.attributes === undefined) { | ||
state.currentNode.attributes = []; | ||
} | ||
(0, utils_1.initAttributesIfNone)(state.currentNode); | ||
// new empty attribute | ||
@@ -26,3 +23,3 @@ state.currentNode.attributes.push({ | ||
range: (0, utils_1.cloneRange)(token.range), | ||
loc: (0, clone_location_1.cloneLocation)(token.loc), | ||
loc: (0, utils_1.cloneLocation)(token.loc), | ||
}); | ||
@@ -29,0 +26,0 @@ state.currentContext = { |
@@ -1,2 +0,2 @@ | ||
import { ConstructTreeState, Token } from "../../types"; | ||
export declare function construct(token: Token, state: ConstructTreeState): ConstructTreeState<any>; | ||
import { AnyToken, ConstructTreeState, ContextualCommentNode } from "../../types"; | ||
export declare function construct(token: AnyToken, state: ConstructTreeState<ContextualCommentNode>): ConstructTreeState<ContextualCommentNode>; |
@@ -1,2 +0,2 @@ | ||
import { ConstructTreeState, Token } from "../../types"; | ||
export declare function construct(token: Token, state: ConstructTreeState): ConstructTreeState<any>; | ||
import { AnyToken, ConstructTreeState, ContextualDoctypeNode } from "../../types"; | ||
export declare function construct(token: AnyToken, state: ConstructTreeState<ContextualDoctypeNode>): ConstructTreeState<ContextualDoctypeNode>; |
@@ -20,4 +20,4 @@ "use strict"; | ||
} | ||
attribute.value = token; | ||
if (!attribute.key && !attribute.startWrapper) { | ||
attribute.value = (0, utils_1.createNodeFrom)(token); | ||
if (!attribute.startWrapper) { | ||
attribute.range = (0, utils_1.cloneRange)(token.range); | ||
@@ -30,3 +30,3 @@ } | ||
const attribute = getLastAttribute(state); | ||
if (attribute.start !== undefined || attribute.value !== undefined) { | ||
if (attribute.value !== undefined) { | ||
state.currentContext = state.currentContext.parentRef; | ||
@@ -36,5 +36,3 @@ return state; | ||
attribute.startWrapper = (0, utils_1.createNodeFrom)(token); | ||
if (!attribute.key) { | ||
attribute.range = [token.range[0], token.range[1]]; | ||
} | ||
attribute.range = (0, utils_1.cloneRange)(token.range); | ||
state.caretPosition++; | ||
@@ -41,0 +39,0 @@ return state; |
@@ -1,2 +0,2 @@ | ||
import { ConstructTreeState, Token } from "../../types"; | ||
export declare function construct(token: Token, state: ConstructTreeState): ConstructTreeState<any>; | ||
import { AnyToken, ConstructTreeState, ContextualDoctypeNode } from "../../types"; | ||
export declare function construct(token: AnyToken, state: ConstructTreeState<ContextualDoctypeNode>): ConstructTreeState<ContextualDoctypeNode>; |
@@ -6,3 +6,2 @@ "use strict"; | ||
const utils_1 = require("../../utils"); | ||
const clone_location_1 = require("../../utils/clone-location"); | ||
const ATTRIBUTE_START_TOKENS = [ | ||
@@ -17,10 +16,8 @@ constants_1.TokenTypes.DoctypeAttributeWrapperStart, | ||
function handleAttribute(state, token) { | ||
if (state.currentNode.attributes === undefined) { | ||
state.currentNode.attributes = []; | ||
} | ||
(0, utils_1.initAttributesIfNone)(state.currentNode); | ||
// new empty attribute | ||
state.currentNode.attributes.push({ | ||
type: constants_1.NodeTypes.Attribute, | ||
type: constants_1.NodeTypes.DoctypeAttribute, | ||
range: (0, utils_1.cloneRange)(token.range), | ||
loc: (0, clone_location_1.cloneLocation)(token.loc), | ||
loc: (0, utils_1.cloneLocation)(token.loc), | ||
}); | ||
@@ -27,0 +24,0 @@ state.currentContext = { |
@@ -1,2 +0,2 @@ | ||
import { ConstructTreeState, Token } from "../../types"; | ||
export declare function construct(token: Token, state: ConstructTreeState): ConstructTreeState<any>; | ||
import { AnyToken, ConstructTreeState, ContextualDoctypeNode } from "../../types"; | ||
export declare function construct(token: AnyToken, state: ConstructTreeState<ContextualDoctypeNode>): ConstructTreeState<ContextualDoctypeNode>; |
@@ -1,2 +0,2 @@ | ||
import { ConstructTreeState, Token } from "../../types"; | ||
export declare function construct(token: Token, state: ConstructTreeState): ConstructTreeState<any>; | ||
import { AnyToken, ConstructTreeState, ContextualScriptTagNode } from "../../types"; | ||
export declare function construct(token: AnyToken, state: ConstructTreeState<ContextualScriptTagNode>): ConstructTreeState<ContextualScriptTagNode>; |
@@ -10,3 +10,3 @@ "use strict"; | ||
]; | ||
function handleOpenTagStartScript(state, token) { | ||
function handleOpenScriptTagStart(state, token) { | ||
state.currentNode.openStart = (0, utils_1.createNodeFrom)(token); | ||
@@ -24,3 +24,3 @@ (0, utils_1.updateNodeEnd)(state.currentNode, token); | ||
} | ||
function handleOpenTagEndScript(state, token) { | ||
function handleOpenScriptTagEnd(state, token) { | ||
state.currentNode.openEnd = (0, utils_1.createNodeFrom)(token); | ||
@@ -37,3 +37,3 @@ (0, utils_1.updateNodeEnd)(state.currentNode, token); | ||
} | ||
function handleCloseTagScript(state, token) { | ||
function handleCloseScriptTag(state, token) { | ||
state.currentNode.close = (0, utils_1.createNodeFrom)(token); | ||
@@ -47,4 +47,4 @@ (0, utils_1.updateNodeEnd)(state.currentNode, token); | ||
function construct(token, state) { | ||
if (token.type === constants_1.TokenTypes.OpenTagStartScript) { | ||
return handleOpenTagStartScript(state, token); | ||
if (token.type === constants_1.TokenTypes.OpenScriptTagStart) { | ||
return handleOpenScriptTagStart(state, token); | ||
} | ||
@@ -54,4 +54,4 @@ if (ATTRIBUTE_START_TOKENS.indexOf(token.type) !== -1) { | ||
} | ||
if (token.type === constants_1.TokenTypes.OpenTagEndScript) { | ||
return handleOpenTagEndScript(state, token); | ||
if (token.type === constants_1.TokenTypes.OpenScriptTagEnd) { | ||
return handleOpenScriptTagEnd(state, token); | ||
} | ||
@@ -61,4 +61,4 @@ if (token.type === constants_1.TokenTypes.ScriptTagContent) { | ||
} | ||
if (token.type === constants_1.TokenTypes.CloseTagScript) { | ||
return handleCloseTagScript(state, token); | ||
if (token.type === constants_1.TokenTypes.CloseScriptTag) { | ||
return handleCloseScriptTag(state, token); | ||
} | ||
@@ -65,0 +65,0 @@ state.caretPosition++; |
@@ -1,2 +0,2 @@ | ||
import { ConstructTreeState, Token } from "../../types"; | ||
export declare function construct(token: Token, state: ConstructTreeState): ConstructTreeState<any>; | ||
import { AnyToken, ConstructTreeState, ContextualStyleTagNode } from "../../types"; | ||
export declare function construct(token: AnyToken, state: ConstructTreeState<ContextualStyleTagNode>): ConstructTreeState<ContextualStyleTagNode>; |
@@ -10,3 +10,3 @@ "use strict"; | ||
]; | ||
function handleOpenTagStartStyle(state, token) { | ||
function handleOpenStyleTagStart(state, token) { | ||
state.currentNode.openStart = (0, utils_1.createNodeFrom)(token); | ||
@@ -24,3 +24,3 @@ (0, utils_1.updateNodeEnd)(state.currentNode, token); | ||
} | ||
function handleOpenTagEndStyle(state, token) { | ||
function handleOpenStyleTagEnd(state, token) { | ||
state.currentNode.openEnd = (0, utils_1.createNodeFrom)(token); | ||
@@ -37,3 +37,3 @@ (0, utils_1.updateNodeEnd)(state.currentNode, token); | ||
} | ||
function handleCloseTagStyle(state, token) { | ||
function handleCloseStyleTag(state, token) { | ||
state.currentNode.close = (0, utils_1.createNodeFrom)(token); | ||
@@ -47,4 +47,4 @@ (0, utils_1.updateNodeEnd)(state.currentNode, token); | ||
function construct(token, state) { | ||
if (token.type === constants_1.TokenTypes.OpenTagStartStyle) { | ||
return handleOpenTagStartStyle(state, token); | ||
if (token.type === constants_1.TokenTypes.OpenStyleTagStart) { | ||
return handleOpenStyleTagStart(state, token); | ||
} | ||
@@ -54,4 +54,4 @@ if (ATTRIBUTE_START_TOKENS.indexOf(token.type) !== -1) { | ||
} | ||
if (token.type === constants_1.TokenTypes.OpenTagEndStyle) { | ||
return handleOpenTagEndStyle(state, token); | ||
if (token.type === constants_1.TokenTypes.OpenStyleTagEnd) { | ||
return handleOpenStyleTagEnd(state, token); | ||
} | ||
@@ -61,4 +61,4 @@ if (token.type === constants_1.TokenTypes.StyleTagContent) { | ||
} | ||
if (token.type === constants_1.TokenTypes.CloseTagStyle) { | ||
return handleCloseTagStyle(state, token); | ||
if (token.type === constants_1.TokenTypes.CloseStyleTag) { | ||
return handleCloseStyleTag(state, token); | ||
} | ||
@@ -65,0 +65,0 @@ state.caretPosition++; |
@@ -1,2 +0,2 @@ | ||
import { ConstructTreeState, Token } from "../../types"; | ||
export declare function construct(token: Token, state: ConstructTreeState): ConstructTreeState<any>; | ||
import { ConstructTreeState, AnyToken, ContextualTagNode, ContextualDocumentNode } from "../../types"; | ||
export declare function construct(token: AnyToken, state: ConstructTreeState<ContextualTagNode>): ConstructTreeState<ContextualTagNode | ContextualDocumentNode>; |
@@ -6,7 +6,4 @@ "use strict"; | ||
const utils_1 = require("../../utils"); | ||
const clone_location_1 = require("../../utils/clone-location"); | ||
function handleOpenTagStart(state, token) { | ||
if (state.currentNode.children === undefined) { | ||
state.currentNode.children = []; | ||
} | ||
(0, utils_1.initChildrenIfNone)(state.currentNode); | ||
const tagNode = { | ||
@@ -16,3 +13,3 @@ type: constants_1.NodeTypes.Tag, | ||
range: (0, utils_1.cloneRange)(token.range), | ||
loc: (0, clone_location_1.cloneLocation)(token.loc), | ||
loc: (0, utils_1.cloneLocation)(token.loc), | ||
attributes: [], | ||
@@ -39,5 +36,3 @@ children: [], | ||
function handleCommentStart(state, token) { | ||
if (state.currentNode.children === undefined) { | ||
state.currentNode.children = []; | ||
} | ||
(0, utils_1.initChildrenIfNone)(state.currentNode); | ||
const commentNode = { | ||
@@ -47,5 +42,6 @@ type: constants_1.NodeTypes.Comment, | ||
range: (0, utils_1.cloneRange)(token.range), | ||
loc: (0, clone_location_1.cloneLocation)(token.loc), | ||
loc: (0, utils_1.cloneLocation)(token.loc), | ||
}; | ||
state.currentNode.children.push(commentNode); | ||
// @ts-ignore | ||
state.currentNode = commentNode; | ||
@@ -59,5 +55,3 @@ state.currentContext = { | ||
function handleDoctypeStart(state, token) { | ||
if (state.currentNode.children === undefined) { | ||
state.currentNode.children = []; | ||
} | ||
(0, utils_1.initChildrenIfNone)(state.currentNode); | ||
const doctypeNode = { | ||
@@ -67,5 +61,7 @@ type: constants_1.NodeTypes.Doctype, | ||
range: (0, utils_1.cloneRange)(token.range), | ||
loc: (0, clone_location_1.cloneLocation)(token.loc), | ||
loc: (0, utils_1.cloneLocation)(token.loc), | ||
attributes: [], | ||
}; | ||
state.currentNode.children.push(doctypeNode); | ||
// @ts-ignore | ||
state.currentNode = doctypeNode; | ||
@@ -79,5 +75,3 @@ state.currentContext = { | ||
function handleText(state, token) { | ||
if (state.currentNode.children === undefined) { | ||
state.currentNode.children = []; | ||
} | ||
(0, utils_1.initChildrenIfNone)(state.currentNode); | ||
const textNode = (0, utils_1.createNodeFrom)(token); | ||
@@ -88,6 +82,4 @@ state.currentNode.children.push(textNode); | ||
} | ||
function handleOpenTagStartScript(state, token) { | ||
if (state.currentNode.children === undefined) { | ||
state.currentNode.children = []; | ||
} | ||
function handleOpenScriptTagStart(state, token) { | ||
(0, utils_1.initChildrenIfNone)(state.currentNode); | ||
const scriptNode = { | ||
@@ -97,6 +89,7 @@ type: constants_1.NodeTypes.ScriptTag, | ||
range: (0, utils_1.cloneRange)(token.range), | ||
loc: (0, clone_location_1.cloneLocation)(token.loc), | ||
loc: (0, utils_1.cloneLocation)(token.loc), | ||
attributes: [], | ||
}; | ||
state.currentNode.children.push(scriptNode); | ||
// @ts-ignore | ||
state.currentNode = scriptNode; | ||
@@ -109,6 +102,4 @@ state.currentContext = { | ||
} | ||
function handleOpenTagStartStyle(state, token) { | ||
if (state.currentNode.children === undefined) { | ||
state.currentNode.children = []; | ||
} | ||
function handleOpenStyleTagStart(state, token) { | ||
(0, utils_1.initChildrenIfNone)(state.currentNode); | ||
const styleNode = { | ||
@@ -118,6 +109,7 @@ type: constants_1.NodeTypes.StyleTag, | ||
range: (0, utils_1.cloneRange)(token.range), | ||
loc: (0, clone_location_1.cloneLocation)(token.loc), | ||
loc: (0, utils_1.cloneLocation)(token.loc), | ||
attributes: [], | ||
}; | ||
state.currentNode.children.push(styleNode); | ||
// @ts-ignore | ||
state.currentNode = styleNode; | ||
@@ -131,7 +123,7 @@ state.currentContext = { | ||
function construct(token, state) { | ||
if (token.type === constants_1.TokenTypes.OpenTagStartScript) { | ||
return handleOpenTagStartScript(state, token); | ||
if (token.type === constants_1.TokenTypes.OpenScriptTagStart) { | ||
return handleOpenScriptTagStart(state, token); | ||
} | ||
if (token.type === constants_1.TokenTypes.OpenTagStartStyle) { | ||
return handleOpenTagStartStyle(state, token); | ||
if (token.type === constants_1.TokenTypes.OpenStyleTagStart) { | ||
return handleOpenStyleTagStart(state, token); | ||
} | ||
@@ -138,0 +130,0 @@ if (token.type === constants_1.TokenTypes.OpenTagStart) { |
@@ -1,2 +0,2 @@ | ||
import { ConstructTreeState, Token } from "../../types"; | ||
export declare function construct(token: Token, state: ConstructTreeState): ConstructTreeState<any>; | ||
import { AnyToken, ConstructTreeState, ContextualTagNode } from "../../types"; | ||
export declare function construct(token: AnyToken, state: ConstructTreeState<ContextualTagNode>): ConstructTreeState<ContextualTagNode>; |
@@ -1,2 +0,2 @@ | ||
import { ConstructTreeState, Token } from "../../types"; | ||
export declare function construct(token: Token, state: ConstructTreeState): ConstructTreeState<any>; | ||
import { AnyToken, ConstructTreeState, ContextualTagNode } from "../../types"; | ||
export declare function construct(token: AnyToken, state: ConstructTreeState<ContextualTagNode>): ConstructTreeState<ContextualTagNode>; |
import { ConstructTreeContextTypes } from "../constants"; | ||
import { DocumentNode } from "./node"; | ||
export declare type ConstructTreeState<CurNode = any> = { | ||
import { AnyContextualNode } from "./contextual-node"; | ||
export declare type ConstructTreeState<N extends AnyContextualNode> = { | ||
caretPosition: number; | ||
@@ -8,6 +9,6 @@ currentContext: { | ||
parentRef?: any; | ||
content?: any; | ||
content?: any[]; | ||
}; | ||
currentNode: CurNode; | ||
currentNode: N; | ||
rootNode: DocumentNode; | ||
}; |
@@ -8,3 +8,4 @@ export * from "./token"; | ||
export * from "./range"; | ||
export * from "./temp-node"; | ||
export * from "./contextual-node"; | ||
export * from "./base-node"; | ||
export * from "./parse-result"; |
@@ -24,3 +24,4 @@ "use strict"; | ||
__exportStar(require("./range"), exports); | ||
__exportStar(require("./temp-node"), exports); | ||
__exportStar(require("./contextual-node"), exports); | ||
__exportStar(require("./base-node"), exports); | ||
__exportStar(require("./parse-result"), exports); |
import { NodeTypes } from "../constants"; | ||
import { SimpleNode } from "./simple-node"; | ||
import { BaseNode } from "./base-node"; | ||
export declare type TextNode = SimpleNode<NodeTypes.Text>; | ||
export declare type CloseTagNode = SimpleNode<NodeTypes.CloseTag>; | ||
export declare type OpenTagStartNode = SimpleNode<NodeTypes.OpenTagStart>; | ||
export declare type OpenTagEndNode = SimpleNode<NodeTypes.OpenTagEnd>; | ||
export declare type AttributeKeyNode = SimpleNode<NodeTypes.AttributeKey>; | ||
export declare type AttributeValueNode = SimpleNode<NodeTypes.AttributeValue>; | ||
export declare type AttributeValueWrapperStartNode = SimpleNode<NodeTypes.AttributeValueWrapperStart>; | ||
export declare type AttributeValueWrapperEndNode = SimpleNode<NodeTypes.AttributeValueWrapperEnd>; | ||
export declare type OpenTagStartStyleNode = SimpleNode<NodeTypes.OpenTagStartStyle>; | ||
export declare type OpenTagEndStyleNode = SimpleNode<NodeTypes.OpenTagEndStyle>; | ||
export declare type OpenTagStartScriptNode = SimpleNode<NodeTypes.OpenTagStartScript>; | ||
export declare type OpenTagEndScriptNode = SimpleNode<NodeTypes.OpenTagEndScript>; | ||
export declare type CommentStartNode = SimpleNode<NodeTypes.CommentStart>; | ||
export declare type CommentEndNode = SimpleNode<NodeTypes.CommentEnd>; | ||
export declare type CommentContentNode = SimpleNode<NodeTypes.CommentContent>; | ||
export declare type ScriptTagContentNode = SimpleNode<NodeTypes.ScriptTagContent>; | ||
export declare type StyleTagContentNode = SimpleNode<NodeTypes.StyleTagContent>; | ||
export declare type DoctypeStartNode = SimpleNode<NodeTypes.DoctypeStart>; | ||
export declare type CloseTagScriptNode = SimpleNode<NodeTypes.CloseTagScript>; | ||
export declare type CloseTagStyleNode = SimpleNode<NodeTypes.CloseTagStyle>; | ||
export declare type DoctypeAttributeValueNode = SimpleNode<NodeTypes.DoctypeAttributeValue>; | ||
export declare type DoctypeAttributeWrapperStart = SimpleNode<NodeTypes.DoctypeAttributeWrapperStart>; | ||
export declare type DoctypeAttributeWrapperEnd = SimpleNode<NodeTypes.DoctypeAttributeWrapperEnd>; | ||
export interface DocumentNode extends BaseNode { | ||
type: NodeTypes.Document; | ||
children: Array<TextNode | TagNode | ScriptNode | StyleNode | CommentNode | TextNode>; | ||
children: Array<TextNode | TagNode | ScriptTagNode | StyleTagNode | CommentNode>; | ||
} | ||
export declare type TextNode = SimpleNode<NodeTypes.Text>; | ||
export interface TagNode extends BaseNode { | ||
@@ -35,43 +13,46 @@ type: NodeTypes.Tag; | ||
name: string; | ||
openStart: OpenTagStartNode; | ||
openEnd: OpenTagEndNode; | ||
close?: CloseTagNode; | ||
openEnd: OpenTagEndNode; | ||
openStart: OpenTagStartNode; | ||
children: Array<TextNode | TagNode | ScriptNode | StyleNode | CommentNode | TextNode>; | ||
children: Array<TextNode | TagNode | ScriptTagNode | StyleTagNode | CommentNode>; | ||
attributes: Array<AttributeNode>; | ||
} | ||
export declare type OpenTagStartNode = SimpleNode<NodeTypes.OpenTagStart>; | ||
export declare type OpenTagEndNode = SimpleNode<NodeTypes.OpenTagEnd>; | ||
export declare type CloseTagNode = SimpleNode<NodeTypes.CloseTag>; | ||
export interface AttributeNode extends BaseNode { | ||
type: NodeTypes.Attribute; | ||
key: AttributeKeyNode; | ||
value?: AttributeValueNode; | ||
startWrapper?: AttributeValueWrapperStartNode; | ||
value?: AttributeValueNode; | ||
endWrapper?: AttributeValueWrapperEndNode; | ||
} | ||
export interface StyleNode extends BaseNode { | ||
type: NodeTypes.StyleTag; | ||
attributes: Array<AttributeNode>; | ||
openStart: OpenTagStartStyleNode; | ||
openEnd: OpenTagEndStyleNode; | ||
close: CloseTagStyleNode; | ||
value?: StyleTagContentNode; | ||
} | ||
export interface ScriptNode extends BaseNode { | ||
export declare type AttributeKeyNode = SimpleNode<NodeTypes.AttributeKey>; | ||
export declare type AttributeValueNode = SimpleNode<NodeTypes.AttributeValue>; | ||
export declare type AttributeValueWrapperStartNode = SimpleNode<NodeTypes.AttributeValueWrapperStart>; | ||
export declare type AttributeValueWrapperEndNode = SimpleNode<NodeTypes.AttributeValueWrapperEnd>; | ||
export interface ScriptTagNode extends BaseNode { | ||
type: NodeTypes.ScriptTag; | ||
attributes: Array<AttributeNode>; | ||
openStart: OpenTagStartScriptNode; | ||
openEnd: OpenTagEndScriptNode; | ||
close: CloseTagScriptNode; | ||
openStart: OpenScriptTagStartNode; | ||
openEnd: OpenScriptTagEndNode; | ||
close: CloseScriptTagNode; | ||
value?: ScriptTagContentNode; | ||
} | ||
export interface DoctypeAttributeNode extends BaseNode { | ||
type: NodeTypes.Attribute; | ||
key: AttributeKeyNode; | ||
startWrapper?: DoctypeAttributeWrapperStart; | ||
value?: DoctypeAttributeValueNode; | ||
endWrapper?: DoctypeAttributeWrapperEnd; | ||
export declare type OpenScriptTagStartNode = SimpleNode<NodeTypes.OpenScriptTagStart>; | ||
export declare type CloseScriptTagNode = SimpleNode<NodeTypes.CloseScriptTag>; | ||
export declare type OpenScriptTagEndNode = SimpleNode<NodeTypes.OpenScriptTagEnd>; | ||
export declare type ScriptTagContentNode = SimpleNode<NodeTypes.ScriptTagContent>; | ||
export interface StyleTagNode extends BaseNode { | ||
type: NodeTypes.StyleTag; | ||
attributes: Array<AttributeNode>; | ||
openStart: OpenStyleTagStartNode; | ||
openEnd: OpenStyleTagEndNode; | ||
close: CloseStyleTagNode; | ||
value?: StyleTagContentNode; | ||
} | ||
export interface DoctypeNode extends BaseNode { | ||
type: NodeTypes.Doctype; | ||
start: DoctypeStartNode; | ||
end: DoctypeNode; | ||
} | ||
export declare type OpenStyleTagStartNode = SimpleNode<NodeTypes.OpenStyleTagStart>; | ||
export declare type OpenStyleTagEndNode = SimpleNode<NodeTypes.OpenStyleTagEnd>; | ||
export declare type StyleTagContentNode = SimpleNode<NodeTypes.StyleTagContent>; | ||
export declare type CloseStyleTagNode = SimpleNode<NodeTypes.CloseStyleTag>; | ||
export interface CommentNode extends BaseNode { | ||
@@ -83,1 +64,22 @@ type: NodeTypes.Comment; | ||
} | ||
export declare type CommentStartNode = SimpleNode<NodeTypes.CommentStart>; | ||
export declare type CommentEndNode = SimpleNode<NodeTypes.CommentEnd>; | ||
export declare type CommentContentNode = SimpleNode<NodeTypes.CommentContent>; | ||
export interface DoctypeNode extends BaseNode { | ||
type: NodeTypes.Doctype; | ||
start: DoctypeStartNode; | ||
end: DoctypeEndNode; | ||
attributes: DoctypeAttributeNode[]; | ||
} | ||
export declare type DoctypeStartNode = SimpleNode<NodeTypes.DoctypeStart>; | ||
export declare type DoctypeEndNode = SimpleNode<NodeTypes.DoctypeEnd>; | ||
export interface DoctypeAttributeNode extends BaseNode { | ||
type: NodeTypes.DoctypeAttribute; | ||
startWrapper?: DoctypeAttributeWrapperStart; | ||
value?: DoctypeAttributeValueNode; | ||
endWrapper?: DoctypeAttributeWrapperEnd; | ||
} | ||
export declare type DoctypeAttributeValueNode = SimpleNode<NodeTypes.DoctypeAttributeValue>; | ||
export declare type DoctypeAttributeWrapperStart = SimpleNode<NodeTypes.DoctypeAttributeWrapperStart>; | ||
export declare type DoctypeAttributeWrapperEnd = SimpleNode<NodeTypes.DoctypeAttributeWrapperEnd>; | ||
export declare type AnyNode = DocumentNode | TextNode | TagNode | OpenTagStartNode | OpenTagEndNode | CloseTagNode | AttributeNode | AttributeKeyNode | AttributeValueNode | AttributeValueWrapperStartNode | AttributeValueWrapperEndNode | ScriptTagNode | OpenScriptTagStartNode | CloseScriptTagNode | OpenScriptTagEndNode | ScriptTagContentNode | StyleTagNode | OpenStyleTagStartNode | OpenStyleTagEndNode | StyleTagContentNode | CloseStyleTagNode | CommentNode | CommentStartNode | CommentEndNode | CommentContentNode | DoctypeNode | DoctypeStartNode | DoctypeEndNode | DoctypeAttributeNode | DoctypeAttributeValueNode | DoctypeAttributeWrapperStart | DoctypeAttributeWrapperEnd; |
export interface Position { | ||
line: number; | ||
column: number; | ||
} |
import { TokenTypes } from "../constants"; | ||
import { SourceLocation } from "./source-location"; | ||
import { Range } from "./range"; | ||
export interface Token { | ||
type: TokenTypes; | ||
export interface Token<T extends TokenTypes> { | ||
type: T; | ||
value: string; | ||
@@ -10,1 +10,2 @@ range: Range; | ||
} | ||
export declare type AnyToken = Token<TokenTypes.Text> | Token<TokenTypes.OpenTagStart> | Token<TokenTypes.OpenTagEnd> | Token<TokenTypes.CloseTag> | Token<TokenTypes.AttributeKey> | Token<TokenTypes.AttributeAssignment> | Token<TokenTypes.AttributeValueWrapperStart> | Token<TokenTypes.AttributeValue> | Token<TokenTypes.AttributeValueWrapperEnd> | Token<TokenTypes.DoctypeStart> | Token<TokenTypes.DoctypeAttributeValue> | Token<TokenTypes.DoctypeAttributeWrapperStart> | Token<TokenTypes.DoctypeAttributeWrapperEnd> | Token<TokenTypes.DoctypeEnd> | Token<TokenTypes.CommentStart> | Token<TokenTypes.CommentContent> | Token<TokenTypes.CommentEnd> | Token<TokenTypes.OpenScriptTagStart> | Token<TokenTypes.OpenScriptTagEnd> | Token<TokenTypes.ScriptTagContent> | Token<TokenTypes.CloseScriptTag> | Token<TokenTypes.OpenStyleTagStart> | Token<TokenTypes.OpenStyleTagEnd> | Token<TokenTypes.StyleTagContent> | Token<TokenTypes.CloseStyleTag>; |
@@ -0,5 +1,6 @@ | ||
import { AnyToken } from "./token"; | ||
import { TokenizerState } from "./tokenizer-state"; | ||
export interface TokenizeHandler { | ||
parse(chars: string, state: TokenizerState, tokens: any[]): void; | ||
handleContentEnd?: (state: TokenizerState, tokens: any[]) => void; | ||
parse(chars: string, state: TokenizerState, tokens: AnyToken[]): void; | ||
handleContentEnd?: (state: TokenizerState, tokens: AnyToken[]) => void; | ||
} |
@@ -23,3 +23,4 @@ import { TokenizerContextTypes } from "../constants"; | ||
linePosition: number; | ||
source: string; | ||
}; | ||
export {}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); |
@@ -1,7 +0,4 @@ | ||
import { TokenizerState } from "../types"; | ||
import { TokenizerState, Range } from "../types"; | ||
export declare function calculateTokenCharactersRange(state: TokenizerState, { keepBuffer }: { | ||
keepBuffer: boolean; | ||
}): { | ||
startPosition: number; | ||
endPosition: number; | ||
}; | ||
}): Range; |
@@ -5,7 +5,2 @@ "use strict"; | ||
function calculateTokenCharactersRange(state, { keepBuffer }) { | ||
if (keepBuffer === undefined) { | ||
throw new Error("Unable to calculate characters range for token.\n" + | ||
'"keepBuffer" parameter is not specified to decide if ' + | ||
"the decision buffer is a part of characters range."); | ||
} | ||
const startPosition = state.caretPosition - | ||
@@ -21,4 +16,4 @@ (state.accumulatedContent.length - 1) - | ||
} | ||
return { startPosition, endPosition: endPosition + 1 }; | ||
return [startPosition, endPosition + 1]; | ||
} | ||
exports.calculateTokenCharactersRange = calculateTokenCharactersRange; |
@@ -1,11 +0,11 @@ | ||
import { TokenizerState } from "../types"; | ||
export declare function calculateTokenLocation(state: TokenizerState, { keepBuffer }: { | ||
keepBuffer: boolean; | ||
}): { | ||
import { Range } from "../types"; | ||
export declare function calculateTokenLocation(source: string, range: Range): { | ||
start: { | ||
line: number; | ||
column: number; | ||
}; | ||
end: { | ||
line: number; | ||
column: number; | ||
}; | ||
}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.calculateTokenLocation = void 0; | ||
function calculateTokenLocation(state, { keepBuffer }) { | ||
if (keepBuffer === undefined) { | ||
throw new Error("Unable to calculate location for token.\n" + | ||
'"keepBuffer" parameter is not specified to decide if ' + | ||
"the decision buffer is a part of characters range."); | ||
} | ||
let startLine = state.linePosition - | ||
[...state.accumulatedContent].filter((ch) => ch === "\n").length; | ||
if (!keepBuffer) { | ||
startLine -= [...state.decisionBuffer].filter((ch) => ch === "\n").length; | ||
} | ||
const get_line_info_1 = require("./get-line-info"); | ||
function calculateTokenLocation(source, range) { | ||
return { | ||
start: { | ||
line: startLine, | ||
}, | ||
end: { | ||
line: state.linePosition, | ||
}, | ||
start: (0, get_line_info_1.getLineInfo)(source, range[0]), | ||
end: (0, get_line_info_1.getLineInfo)(source, range[1]), | ||
}; | ||
} | ||
exports.calculateTokenLocation = calculateTokenLocation; |
@@ -5,12 +5,13 @@ import { TokenizerState } from "../types"; | ||
}): { | ||
range: import("../types").Range; | ||
loc: { | ||
start: { | ||
line: number; | ||
column: number; | ||
}; | ||
end: { | ||
line: number; | ||
column: number; | ||
}; | ||
}; | ||
startPosition: number; | ||
endPosition: number; | ||
}; |
@@ -8,5 +8,5 @@ "use strict"; | ||
const range = (0, calculate_token_characters_range_1.calculateTokenCharactersRange)(state, options); | ||
const loc = (0, calculate_token_location_1.calculateTokenLocation)(state, options); | ||
const loc = (0, calculate_token_location_1.calculateTokenLocation)(state.source, range); | ||
return { | ||
...range, | ||
range, | ||
loc, | ||
@@ -13,0 +13,0 @@ }; |
@@ -1,1 +0,2 @@ | ||
export declare function clearParent(ast: any): any; | ||
import { ContextualDocumentNode, DocumentNode } from "../types"; | ||
export declare function clearParent(ast: ContextualDocumentNode): DocumentNode; |
@@ -8,2 +8,3 @@ "use strict"; | ||
if (Array.isArray(ast.children)) { | ||
// @ts-ignore | ||
cleanAst.children = ast.children.map((node) => { | ||
@@ -10,0 +11,0 @@ return clearParent(node); |
@@ -8,5 +8,7 @@ "use strict"; | ||
line: loc.start.line, | ||
column: loc.start.column, | ||
}, | ||
end: { | ||
line: loc.end.line, | ||
column: loc.end.column, | ||
}, | ||
@@ -13,0 +15,0 @@ }; |
@@ -1,7 +0,2 @@ | ||
import { Token } from "../types"; | ||
export declare function createNodeFrom(token: Token): { | ||
type: import("..").TokenTypes; | ||
value: string; | ||
loc: import("../types/source-location").SourceLocation; | ||
range: import("../types").Range; | ||
}; | ||
import { AnyToken } from "../types"; | ||
export declare function createNodeFrom<T extends AnyToken>(token: T): unknown; |
@@ -12,1 +12,5 @@ export * from "./calculate-token-characters-range"; | ||
export * from "./update-node-end"; | ||
export * from "./get-line-info"; | ||
export * from "./first"; | ||
export * from "./clone-location"; | ||
export * from "./init-if-none"; |
@@ -28,1 +28,5 @@ "use strict"; | ||
__exportStar(require("./update-node-end"), exports); | ||
__exportStar(require("./get-line-info"), exports); | ||
__exportStar(require("./first"), exports); | ||
__exportStar(require("./clone-location"), exports); | ||
__exportStar(require("./init-if-none"), exports); |
@@ -1,2 +0,2 @@ | ||
import { BaseNode, Token } from "../types"; | ||
export declare function updateNodeEnd(node: BaseNode, token: Token): void; | ||
import { AnyContextualNode, AnyNode, AnyToken } from "../types"; | ||
export declare function updateNodeEnd(node: AnyNode | AnyContextualNode, token: AnyToken): void; |
{ | ||
"name": "es-html-parser", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"main": "dist/index.js", | ||
@@ -11,8 +11,12 @@ "license": "MIT", | ||
"build": "rimraf dist && tsc -b tsconfig.build.json", | ||
"check:lint": "eslint src", | ||
"check:ts": "tsc --noEmit", | ||
"check:format": "prettier --list-different .", | ||
"prepublish": "yarn check:ts && yarn check:format && yarn build" | ||
"prepublish": "yarn check:lint && yarn check:ts && yarn check:format && yarn build" | ||
}, | ||
"devDependencies": { | ||
"@types/jest": "^28.1.7", | ||
"@typescript-eslint/eslint-plugin": "^5.36.1", | ||
"@typescript-eslint/parser": "^5.36.1", | ||
"eslint": "^8.23.0", | ||
"jest": "^28.1.3", | ||
@@ -22,4 +26,9 @@ "prettier": "^2.7.1", | ||
"ts-jest": "^28.0.8", | ||
"typescript": "^4.7.4" | ||
} | ||
"typescript": "^4.8.2" | ||
}, | ||
"files": [ | ||
"README.md", | ||
"LICENSE.md", | ||
"dist" | ||
] | ||
} |
691
README.md
# ES HTML Parser | ||
# es-html-parser | ||
ES HTML Parser is an HTML parser that generates an abstract syntax tree similar to the ESTree specification. | ||
This project began as a fork of [hyntax](https://github.com/mykolaharmash/hyntax) and is developed to follow [ESTree](https://github.com/estree/estree)-like ast specification. | ||
## Table of Contents | ||
- [Install](#install) | ||
- [Usage](#usage) | ||
- [API Reference](#api-reference) | ||
- [AST Format](#ast-format) | ||
- [License](#license) | ||
## Install | ||
``` | ||
npm install es-html-parser | ||
``` | ||
## Usage | ||
```js | ||
import { parse } from "es-html-parser"; | ||
const input = ` | ||
<html> | ||
<body> | ||
<button type="button"> press here </button> | ||
</body> | ||
</html> | ||
`; | ||
const { ast, tokens } = parse(input); | ||
``` | ||
## API Reference | ||
- [Functions](#functions) | ||
- [Types](#types) | ||
- [Constants](#constants) | ||
### Functions | ||
#### parse | ||
```ts | ||
parse(html: string): ParseResult; | ||
``` | ||
**Arguments** | ||
- `html`: HTML string to parse. | ||
**Returns** | ||
- `ParseResult`: Result of parsing | ||
### Types | ||
#### ParseResult | ||
```ts | ||
interface ParseResult { | ||
ast: DocumentNode; | ||
tokens: AnyToken[]; | ||
} | ||
``` | ||
- `ast`: The root node of the ast. | ||
- `tokens`: An array of resulting tokens. | ||
#### AnyNode | ||
The `AnyNode` is an union type of all nodes. | ||
```ts | ||
type AnyNode = | ||
| DocumentNode | ||
| TextNode | ||
| TagNode | ||
| OpenTagStartNode | ||
| OpenTagEndNode | ||
| CloseTagNode | ||
| AttributeNode | ||
| AttributeKeyNode | ||
| AttributeValueNode | ||
| AttributeValueWrapperStartNode | ||
| AttributeValueWrapperEndNode | ||
| ScriptTagNode | ||
| OpenScriptTagStartNode | ||
| CloseScriptTagNode | ||
| OpenScriptTagEndNode | ||
| ScriptTagContentNode | ||
| StyleTagNode | ||
| OpenStyleTagStartNode | ||
| OpenStyleTagEndNode | ||
| StyleTagContentNode | ||
| CloseStyleTagNode | ||
| CommentNode | ||
| CommentStartNode | ||
| CommentEndNode | ||
| CommentContentNode | ||
| DoctypeNode | ||
| DoctypeStartNode | ||
| DoctypeEndNode | ||
| DoctypeAttributeNode | ||
| DoctypeAttributeValueNode | ||
| DoctypeAttributeWrapperStart | ||
| DoctypeAttributeWrapperEnd; | ||
``` | ||
#### AnyToken | ||
The `AnyToken` is an union type all tokens. | ||
```ts | ||
type AnyToken = | ||
| Token<TokenTypes.Text> | ||
| Token<TokenTypes.OpenTagStart> | ||
| Token<TokenTypes.OpenTagEnd> | ||
| Token<TokenTypes.CloseTag> | ||
| Token<TokenTypes.AttributeKey> | ||
| Token<TokenTypes.AttributeAssignment> | ||
| Token<TokenTypes.AttributeValueWrapperStart> | ||
| Token<TokenTypes.AttributeValue> | ||
| Token<TokenTypes.AttributeValueWrapperEnd> | ||
| Token<TokenTypes.DoctypeStart> | ||
| Token<TokenTypes.DoctypeAttributeValue> | ||
| Token<TokenTypes.DoctypeAttributeWrapperStart> | ||
| Token<TokenTypes.DoctypeAttributeWrapperEnd> | ||
| Token<TokenTypes.DoctypeEnd> | ||
| Token<TokenTypes.CommentStart> | ||
| Token<TokenTypes.CommentContent> | ||
| Token<TokenTypes.CommentEnd> | ||
| Token<TokenTypes.OpenScriptTagStart> | ||
| Token<TokenTypes.OpenScriptTagEnd> | ||
| Token<TokenTypes.ScriptTagContent> | ||
| Token<TokenTypes.CloseScriptTag> | ||
| Token<TokenTypes.OpenStyleTagStart> | ||
| Token<TokenTypes.OpenStyleTagEnd> | ||
| Token<TokenTypes.StyleTagContent> | ||
| Token<TokenTypes.CloseStyleTag>; | ||
``` | ||
### Constants | ||
#### TokenTypes | ||
```ts | ||
enum TokenTypes { | ||
Text = "Text", | ||
OpenTagStart = "OpenTagStart", | ||
OpenTagEnd = "OpenTagEnd", | ||
CloseTag = "CloseTag", | ||
AttributeKey = "AttributeKey", | ||
AttributeAssignment = "AttributeAssignment", | ||
AttributeValueWrapperStart = "AttributeValueWrapperStart", | ||
AttributeValue = "AttributeValue", | ||
AttributeValueWrapperEnd = "AttributeValueWrapperEnd", | ||
DoctypeStart = "DoctypeStart", | ||
DoctypeAttributeValue = "DoctypeAttributeValue", | ||
DoctypeAttributeWrapperStart = "DoctypeAttributeWrapperStart", | ||
DoctypeAttributeWrapperEnd = "DoctypeAttributeWrapperEnd", | ||
DoctypeEnd = "DoctypeEnd", | ||
CommentStart = "CommentStart", | ||
CommentContent = "CommentContent", | ||
CommentEnd = "CommentEnd", | ||
OpenScriptTagStart = "OpenScriptTagStart", | ||
OpenScriptTagEnd = "OpenScriptTagEnd", | ||
ScriptTagContent = "ScriptTagContent", | ||
CloseScriptTag = "CloseScriptTag", | ||
OpenStyleTagStart = "OpenStyleTagStart", | ||
OpenStyleTagEnd = "OpenStyleTagEnd", | ||
StyleTagContent = "StyleTagContent", | ||
CloseStyleTag = "CloseStyleTag", | ||
} | ||
``` | ||
#### NodeTypes | ||
```ts | ||
enum NodeTypes { | ||
Document = "Document", | ||
Tag = "Tag", | ||
Text = "Text", | ||
Doctype = "Doctype", | ||
Comment = "Comment", | ||
CommentStart = "CommentStart", | ||
CommentEnd = "CommentEnd", | ||
CommentContent = "CommentContent", | ||
Attribute = "Attribute", | ||
AttributeKey = "AttributeKey", | ||
AttributeValue = "AttributeValue", | ||
AttributeAssignment = "AttributeAssignment", | ||
AttributeValueWrapperStart = "AttributeValueWrapperStart", | ||
AttributeValueWrapperEnd = "AttributeValueWrapperEnd", | ||
CloseTag = "CloseTag", | ||
OpenTagEnd = "OpenTagEnd", | ||
OpenTagStart = "OpenTagStart", | ||
DoctypeStart = "DoctypeStart", | ||
DoctypeAttribute = "DoctypeAttribute", | ||
DoctypeEnd = "DoctypeEnd", | ||
ScriptTag = "ScriptTag", | ||
OpenScriptTagStart = "OpenScriptTagStart", | ||
OpenScriptTagEnd = "OpenScriptTagEnd", | ||
ScriptTagContent = "ScriptTagContent", | ||
StyleTag = "StyleTag", | ||
OpenStyleTagStart = "OpenStyleTagStart", | ||
OpenStyleTagEnd = "OpenStyleTagEnd", | ||
StyleTagContent = "StyleTagContent", | ||
CloseStyleTag = "CloseStyleTag", | ||
CloseScriptTag = "CloseScriptTag", | ||
DoctypeAttributeValue = "DoctypeAttributeValue", | ||
DoctypeAttributeWrapperStart = "DoctypeAttributeWrapperStart", | ||
DoctypeAttributeWrapperEnd = "DoctypeAttributeWrapperEnd", | ||
} | ||
``` | ||
## AST Format | ||
- [Common](#common) | ||
- [BaseNode](#basenode) | ||
- [SourceLocation](#sourcelocation) | ||
- [Position](#position) | ||
- [Token](#token) | ||
- [DocumentNode](#documentnode) | ||
- [TextNode](#textnode) | ||
- [TagNode](#tagnode) | ||
- [OpenTagStartNode](#opentagstartnode) | ||
- [OpenTagEndNode](#opentagendnode) | ||
- [CloseTagNode](#closetagnode) | ||
- [AttributeNode](#attributenode) | ||
- [AttributeKeyNode](#attributekeynode) | ||
- [AttributeValueWrapperStartNode](#attributevaluewrapperstartnode) | ||
- [AttributeValueWrapperEndNode](#attributevaluewrapperendnode) | ||
- [AttributeValueNode](#attributevaluenode) | ||
- [ScriptTagNode](#scripttagnode) | ||
- [OpenScriptTagStartNode](#openscripttagstartnode) | ||
- [OpenScriptTagEndNode](#openscripttagendnode) | ||
- [CloseScriptTagNode](#closescripttagnode) | ||
- [ScriptTagContentNode](#scripttagcontentnode) | ||
- [StyleTagNode](#styletagnode) | ||
- [OpenStyleTagStartNode](#openstyletagstartnode) | ||
- [OpenStyleTagEndNode](#openstyletagendnode) | ||
- [CloseStyleTagNode](#closestyletagnode) | ||
- [StyleTagContentNode](#styletagcontentnode) | ||
- [CommentNode](#commentnode) | ||
- [CommentStartNode](#commentstartnode) | ||
- [CommentEndNode](#commentendnode) | ||
- [CommentContentNode](#commentcontentnode) | ||
- [DoctypeNode](#doctypenode) | ||
- [DoctypeStartNode](#doctypestartnode) | ||
- [DoctypeEndNode](#doctypeendnode) | ||
- [DoctypeAttributeNode](#doctypeattributenode) | ||
- [DoctypeAttributeValueNode](#doctypeattributevaluenode) | ||
- [DoctypeAttributeWrapperStartNode](#doctypeattributewrapperstartnode) | ||
- [DoctypeAttributeWrapperEndNode](#doctypeattributewrapperendnode) | ||
### Common | ||
#### BaseNode | ||
Every AST node and token implements the `BaseNode` interface. | ||
```ts | ||
interface BaseNode { | ||
type: string; | ||
loc: SourceLocation; | ||
range: [number, number]; | ||
} | ||
``` | ||
The `type` field is representing the AST type. Its value is one of the `NodeTypes` or `TokenTypes`. | ||
The `loc` and `range` fields represent the source location of the node. | ||
#### SourceLocation | ||
```ts | ||
interface SourceLocation { | ||
start: Position; | ||
end: Position; | ||
} | ||
``` | ||
The `start` field represents the start location of the node. | ||
The `end` field represents the end location of the node. | ||
#### Position | ||
```ts | ||
interface Position { | ||
line: number; // >= 1 | ||
column: number; // >= 0 | ||
} | ||
``` | ||
The `line` field is a number representing the line number where the node positioned. (1-based index). | ||
The `column` field is a number representing the offset in the line. (0-based index). | ||
#### Token | ||
All tokens implement the `Token` interface. | ||
```ts | ||
interface Token<T extends TokenTypes> extends BaseNode { | ||
type: T; | ||
value: string; | ||
} | ||
``` | ||
### DocumentNode | ||
`DocumentNode` represents a whole parsed document. It's a root node of the AST. | ||
```ts | ||
interface DocumentNode extends BaseNode { | ||
type: "Document"; | ||
children: Array<TextNode | TagNode | ScriptNode | StyleNode | CommentNode>; | ||
} | ||
``` | ||
### TextNode | ||
`TextNode` represents any plain text in HTML. | ||
```ts | ||
interface TextNode extends BaseNode { | ||
type: "Text"; | ||
value: string; | ||
} | ||
``` | ||
### TagNode | ||
`TagNode` represents all kinds of tag nodes in HTML except for doctype, script, style, and comment. (e.g. `<div></div>`, `<span></span>` ...) | ||
```ts | ||
interface TagNode extends BaseNode { | ||
type: "Tag"; | ||
selfClosing: boolean; | ||
name: string; | ||
openStart: OpenTagStartNode; | ||
openEnd: OpenTagEndNode; | ||
close?: CloseTagNode; | ||
children: Array<TextNode | TagNode | ScriptNode | StyleNode | CommentNode>; | ||
attributes: Array<AttributeNode>; | ||
} | ||
``` | ||
#### OpenTagStartNode | ||
`OpenTagStartNode` represents the opening part of the [Start tags](https://www.w3.org/TR/2011/WD-html5-20110405/syntax.html#start-tags). (e.g. `<div`) | ||
```ts | ||
interface OpenTagStartNode { | ||
type: "OpenTagStart"; | ||
value: string; | ||
} | ||
``` | ||
#### OpenTagEndNode | ||
`OpenTagEndNode` represents the closing part of the [Start tags](https://www.w3.org/TR/2011/WD-html5-20110405/syntax.html#start-tags). (e.g. `>`, `/>`) | ||
```ts | ||
interface OpenTagEndNode { | ||
type: "OpenTagEnd"; | ||
value: string; | ||
} | ||
``` | ||
#### CloseTagNode | ||
`ClosingTagNode` represents the [End tags](https://www.w3.org/TR/2011/WD-html5-20110405/syntax.html#end-tags). (e.g. `</div>`) | ||
```ts | ||
interface CloseTagNode { | ||
type: "CloseTag"; | ||
value: string; | ||
} | ||
``` | ||
### AttributeNode | ||
`AttributeNode` represents an attribute. (e.g. `id="foo"`) | ||
```ts | ||
interface AttributeNode extends BaseNode { | ||
type: "Attribute"; | ||
key: AttributeKeyNode; | ||
value?: AttributeValueNode; | ||
startWrapper?: AttributeValueWrapperStartNode; | ||
endWrapper?: AttributeValueWrapperEndNode; | ||
} | ||
``` | ||
#### AttributeKeyNode | ||
`AttributeKeyNode` represents a key part of an attribute. (e.g. `id`) | ||
```ts | ||
interface AttributeKeyNode extends BaseNode { | ||
type: "AttributeKey"; | ||
value: string; | ||
} | ||
``` | ||
#### AttributeValueWrapperStartNode | ||
`AttributeValueWrapperStartNode` represents the left side character that wraps the value of the attribute. (e.g. `"`, `'`) | ||
```ts | ||
interface AttributeValueWrapperStartNode extends BaseNode { | ||
type: "AttributeValueWrapperStart"; | ||
value: string; | ||
} | ||
``` | ||
#### AttributeValueWrapperEndNode | ||
`AttributeValueWrapperEndNode` represents the right side character that wraps the value of the attribute. (e.g. `"`, `'`) | ||
```ts | ||
interface AttributeValueWrapperEndNode extends BaseNode { | ||
type: "AttributeValueWrapperEnd"; | ||
value: string; | ||
} | ||
``` | ||
#### AttributeValueNode | ||
`AttributeValueNode` represents the value part of the attribute. It does not include wrapper characters. (e.g. `foo`) | ||
```ts | ||
interface AttributeValueNode extends BaseNode { | ||
type: "AttributeValue"; | ||
value: string; | ||
} | ||
``` | ||
### ScriptTagNode | ||
The `ScriptTagNode` represents a script tags in the HTML. (e.g.` <script> console.log('hello'); </script>`). | ||
```ts | ||
interface ScriptTagNode extends BaseNode { | ||
type: "ScriptTag"; | ||
attributes: Array<AttributeNode>; | ||
openStart: OpenScriptTagStartNode; | ||
openEnd: OpenScriptTagEndNode; | ||
close: CloseScriptTagNode; | ||
value?: ScriptTagContentNode; | ||
} | ||
``` | ||
#### OpenScriptTagStartNode | ||
`OpenScriptTagStartNode` represents an opening part of a start script tag. (e.g. `<script`) | ||
```ts | ||
interface OpenScriptTagStartNode extends BaseNode { | ||
type: "OpenScriptTagStart"; | ||
value: string; | ||
} | ||
``` | ||
#### OpenScriptTagEndNode | ||
`OpenScriptTagEndNode` represents a closing part of a start script tag. (e.g. `>`) | ||
```ts | ||
interface OpenScriptTagEndNode extends BaseNode { | ||
type: "OpenScriptTagEnd"; | ||
value: string; | ||
} | ||
``` | ||
#### CloseScriptTagNode | ||
`CloseScriptTagNode` represents a close script tag. (e.g. `</script>`) | ||
```ts | ||
interface CloseScriptTagNode extends BaseNode { | ||
type: "CloseScriptTag"; | ||
value: string; | ||
} | ||
``` | ||
#### ScriptTagContentNode | ||
`ScriptTagContentNode` represents a script content in script tag. (e.g. `console.log('hello');`) | ||
```ts | ||
interface ScriptTagContentNode extends BaseNode { | ||
type: "ScriptTagContent"; | ||
value: string; | ||
} | ||
``` | ||
### StyleTagNode | ||
`StyleTagNode` represents style tags. (e.g. `<style> .foo {} </style>`) | ||
```ts | ||
interface StyleTagNode extends BaseNode { | ||
type: "StyleTag"; | ||
attributes: Array<AttributeNode>; | ||
openStart: OpenStyleTagStartNode; | ||
openEnd: OpenStyleTagEndNode; | ||
close: CloseStyleTagNode; | ||
value?: StyleTagContentNode; | ||
} | ||
``` | ||
#### OpenStyleTagStartNode | ||
`OpenStyleTagStartNode` represents an opening part of a start style tag. (e.g. `<style`) | ||
```ts | ||
interface OpenStyleTagStartNode extends BaseNode { | ||
type: "OpenStyleTagStart"; | ||
value: string; | ||
} | ||
``` | ||
#### OpenStyleTagEndNode | ||
`OpenStyleTagEndNode` represents a closing part of a start style tag. (e.g. `>`) | ||
```ts | ||
interface OpenStyleTagEndNode extends BaseNode { | ||
type: "OpenStyleTagEnd"; | ||
value: string; | ||
} | ||
``` | ||
#### CloseStyleTagNode | ||
`CloseStyleTagNode` represents a close style tag. (e.g. `</style>`) | ||
```ts | ||
interface CloseStyleTagNode extends BaseNode { | ||
type: "CloseStyleTag"; | ||
value: string; | ||
} | ||
``` | ||
#### StyleTagContentNode | ||
`StyleTagContentNode` represents a style content in style tag. | ||
```ts | ||
interface StyleTagContentNode extends BaseNode { | ||
type: "StyleTagContent"; | ||
value: string; | ||
} | ||
``` | ||
### CommentNode | ||
`CommentNode` represents comment in HTML. (e.g. `<!-- content --> `) | ||
```ts | ||
interface CommentNode extends BaseNode { | ||
type: "Comment"; | ||
start: CommentStartNode; | ||
end: CommentEndNode; | ||
value: CommentContentNode; | ||
} | ||
``` | ||
#### CommentStartNode | ||
`CommentStartNode` represents comment start character sequence. (e.g. `<!--`) | ||
```ts | ||
interface CommentStartNode extends BaseNode { | ||
type: "CommentStart"; | ||
value: string; | ||
} | ||
``` | ||
#### CommentEndNode | ||
`CommentEndNode` represents comment end character sequence. (e.g. `-->`) | ||
```ts | ||
interface CommentEndNode extends BaseNode { | ||
type: "CommentEnd"; | ||
value: string; | ||
} | ||
``` | ||
#### CommentContentNode | ||
The `CommentContentNode` represents text in the comment. | ||
```ts | ||
interface CommentContentNode extends BaseNode { | ||
type: "CommentContent"; | ||
value: string; | ||
} | ||
``` | ||
### DoctypeNode | ||
`DoctypeNode` represents the [DOCTYPE](https://www.w3.org/TR/2011/WD-html5-20110525/syntax.html#the-doctype) in html. | ||
```ts | ||
interface DoctypeNode extends BaseNode { | ||
type: "Doctype"; | ||
attributes: Array<DoctypeAttributeNode>; | ||
start: DoctypeStartNode; | ||
end: DoctypeEndNode; | ||
} | ||
``` | ||
#### DoctypeStartNode | ||
`DoctypeStartNode` represents character sequence of doctype start . (`<!DOCTYPE`) | ||
```ts | ||
interface DoctypeStartNode extends BaseNode { | ||
type: "DoctypeStart"; | ||
value: string; | ||
} | ||
``` | ||
#### DoctypeEndNode | ||
`DoctypeEndNode` represents the doctype end character sequence (e.g. `>`) | ||
```ts | ||
interface DoctypeEndNode extends BaseNode { | ||
type: "DoctypeEnd"; | ||
value: string; | ||
} | ||
``` | ||
### DoctypeAttributeNode | ||
`DoctypeAttributeNode` represents an attribute of doctype node. (e.g. `html`, `"-//W3C//DTD HTML 4.01 Transitional//EN"`) | ||
```ts | ||
interface DoctypeAttributeNode extends BaseNode { | ||
type: "DoctypeAttribute"; | ||
key: DoctypeAttributeKey; | ||
} | ||
``` | ||
#### DoctypeAttributeValueNode | ||
`DoctypeAttributeValueNode` represents a value of doctype node's attribute. (e.g. `html`, `-//W3C//DTD HTML 4.01 Transitional//EN`) | ||
. It does not include wrapper characters (`'`, `"`) | ||
```ts | ||
interface DoctypeAttributeValueNode extends BaseNode { | ||
type: "DoctypeAttributeValue"; | ||
value: string; | ||
} | ||
``` | ||
#### DoctypeAttributeWrapperStartNode | ||
`DoctypeAttributeWrapperStartNode` represents a left side character that wraps the value of the attribute. (e.g. `"`, `'`) | ||
```ts | ||
interface DoctypeAttributeWrapperStartNode extends BaseNode { | ||
type: "DoctypeAttributeWrapperStart"; | ||
value: string; | ||
} | ||
``` | ||
#### DoctypeAttributeWrapperEndNode | ||
`DoctypeAttributeWrapperEndNode` represents a right side character that wraps the value of the attribute. (e.g. `"`, `'`) | ||
```ts | ||
interface DoctypeAttributeWrapperEndNode extends BaseNode { | ||
type: "DoctypeAttributeWrapperEnd"; | ||
value: string; | ||
} | ||
``` | ||
## License | ||
[MIT](./LICENSE.md) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
693
124337
9
30553
153
2647
1