@markuplint/html-parser
Advanced tools
Comparing version 1.0.0-alpha.19 to 1.0.0-alpha.20
@@ -13,2 +13,3 @@ "use strict"; | ||
const walk_1 = require("./walk"); | ||
const remove_deprecated_node_1 = require("./remove-deprecated-node"); | ||
function flattenNodes(nodeTree, rawHtml) { | ||
@@ -108,36 +109,5 @@ const nodeOrders = []; | ||
} | ||
/** | ||
* sorting | ||
*/ | ||
nodeOrders.sort((a, b) => { | ||
if (a.isGhost || b.isGhost) { | ||
return 0; | ||
} | ||
return a.startOffset - b.startOffset; | ||
}); | ||
remove_deprecated_node_1.removeDeprecatedNode(nodeOrders); | ||
{ | ||
/** | ||
* remove duplicated node | ||
*/ | ||
const stack = {}; | ||
const removeIndexes = []; | ||
nodeOrders.forEach((node, i) => { | ||
if (node.isGhost) { | ||
return; | ||
} | ||
const id = `${node.startLine}:${node.startCol}:${node.endLine}:${node.endCol}`; | ||
if (stack[id] != null) { | ||
removeIndexes.push(i); | ||
} | ||
stack[id] = i; | ||
}); | ||
let r = nodeOrders.length; | ||
while (r--) { | ||
if (removeIndexes.includes(r)) { | ||
nodeOrders.splice(r, 1); | ||
} | ||
} | ||
} | ||
{ | ||
/** | ||
* getting last node | ||
@@ -144,0 +114,0 @@ */ |
@@ -5,4 +5,5 @@ export { default as isDocumentFragment } from './is-document-fragment'; | ||
export { flattenNodes } from './flatten-nodes'; | ||
export { removeDeprecatedNode } from './remove-deprecated-node'; | ||
export { default as getEndCol } from './get-end-col'; | ||
export { default as getEndLine } from './get-end-line'; | ||
export { walk, Walker } from './walk'; |
@@ -11,2 +11,4 @@ "use strict"; | ||
Object.defineProperty(exports, "flattenNodes", { enumerable: true, get: function () { return flatten_nodes_1.flattenNodes; } }); | ||
var remove_deprecated_node_1 = require("./remove-deprecated-node"); | ||
Object.defineProperty(exports, "removeDeprecatedNode", { enumerable: true, get: function () { return remove_deprecated_node_1.removeDeprecatedNode; } }); | ||
var get_end_col_1 = require("./get-end-col"); | ||
@@ -13,0 +15,0 @@ Object.defineProperty(exports, "getEndCol", { enumerable: true, get: function () { return get_end_col_1.default; } }); |
@@ -8,3 +8,3 @@ import { MLASTAttr, MLToken } from '@markuplint/ml-ast'; | ||
}; | ||
export default function parseRawTag(raw: string, nodeLine: number, nodeCol: number, startOffset: number): TagTokens; | ||
export default function parseRawTag(raw: string, nodeLine: number, nodeCol: number, startOffset: number, offsetOffset?: number, offsetLine?: number, offsetColumn?: number): TagTokens; | ||
export {}; |
@@ -12,6 +12,6 @@ "use strict"; | ||
const reEndTokens = /(\s*\/)?(\s*)>$/; | ||
function parseRawTag(raw, nodeLine, nodeCol, startOffset) { | ||
let line = nodeLine; | ||
let col = nodeCol; | ||
let offset = startOffset; | ||
function parseRawTag(raw, nodeLine, nodeCol, startOffset, offsetOffset = 0, offsetLine = 0, offsetColumn = 0) { | ||
let line = nodeLine + offsetLine; | ||
let col = nodeCol + offsetColumn; | ||
let offset = startOffset + offsetOffset; | ||
const matches = raw.match(const_1.reTag); | ||
@@ -18,0 +18,0 @@ if (!matches) { |
import { MLASTDocument } from '@markuplint/ml-ast'; | ||
export default function parse(html: string): MLASTDocument; | ||
export default function parse(html: string, offsetOffset?: number, offsetLine?: number, offsetColumn?: number): MLASTDocument; |
102
lib/parse.js
@@ -15,3 +15,3 @@ "use strict"; | ||
const P5_OPTIONS = { sourceCodeLocationInfo: true }; | ||
function parse(html) { | ||
function parse(html, offsetOffset = 0, offsetLine = 0, offsetColumn = 0) { | ||
const isFragment = is_document_fragment_1.default(html); | ||
@@ -21,3 +21,3 @@ const doc = isFragment | ||
: parse5_1.default.parse(html, P5_OPTIONS); | ||
const nodeList = flatten_nodes_1.flattenNodes(traverse(doc, null, html), html); | ||
const nodeList = flatten_nodes_1.flattenNodes(traverse(doc, null, html, offsetOffset, offsetLine, offsetColumn), html); | ||
return { | ||
@@ -29,3 +29,3 @@ nodeList, | ||
exports.default = parse; | ||
function traverse(rootNode, parentNode = null, rawHtml) { | ||
function traverse(rootNode, parentNode, rawHtml, offsetOffset, offsetLine, offsetColumn) { | ||
const nodeList = []; | ||
@@ -35,3 +35,3 @@ const childNodes = getChildNodes(rootNode); | ||
for (const p5node of childNodes) { | ||
const node = nodeize(p5node, prevNode, parentNode, rawHtml); | ||
const node = nodeize(p5node, prevNode, parentNode, rawHtml, offsetOffset, offsetLine, offsetColumn); | ||
if (!node) { | ||
@@ -51,15 +51,21 @@ continue; | ||
} | ||
function nodeize(originNode, prevNode, parentNode, rawHtml) { | ||
function nodeize(originNode, prevNode, parentNode, rawHtml, offsetOffset, offsetLine, offsetColumn) { | ||
const nextNode = null; | ||
if (!originNode.sourceCodeLocation) { | ||
const prevToken = prevNode || parentNode; | ||
const startOffset = prevToken ? prevToken.endOffset : 0; | ||
const endOffset = prevToken ? prevToken.endOffset : 0; | ||
const startLine = prevToken ? prevToken.endLine : 0; | ||
const endLine = prevToken ? prevToken.endLine : 0; | ||
const startCol = prevToken ? prevToken.endCol : 0; | ||
const endCol = prevToken ? prevToken.endCol : 0; | ||
const node = { | ||
uuid: uuid_1.v4(), | ||
raw: '', | ||
startOffset: prevToken ? prevToken.endOffset : 0, | ||
endOffset: prevToken ? prevToken.endOffset : 0, | ||
startLine: prevToken ? prevToken.endLine : 0, | ||
endLine: prevToken ? prevToken.endLine : 0, | ||
startCol: prevToken ? prevToken.endCol : 0, | ||
endCol: prevToken ? prevToken.endCol : 0, | ||
startOffset: startOffset + offsetOffset, | ||
endOffset: endOffset + offsetOffset, | ||
startLine: startLine + offsetLine, | ||
endLine: endLine + offsetLine, | ||
startCol: startCol + offsetColumn, | ||
endCol: endCol + (startLine === endLine ? offsetColumn : 0), | ||
nodeName: originNode.nodeName, | ||
@@ -74,3 +80,3 @@ type: ml_ast_1.MLASTNodeType.OmittedTag, | ||
}; | ||
node.childNodes = traverse(originNode, node, rawHtml); | ||
node.childNodes = traverse(originNode, node, rawHtml, offsetOffset, offsetLine, offsetColumn); | ||
return node; | ||
@@ -91,8 +97,8 @@ } | ||
systemId: originNode.systemId || '', | ||
startOffset, | ||
endOffset, | ||
startLine, | ||
endLine, | ||
startCol, | ||
endCol, | ||
startOffset: startOffset + offsetOffset, | ||
endOffset: endOffset + offsetOffset, | ||
startLine: startLine + offsetLine, | ||
endLine: endLine + offsetLine, | ||
startCol: startCol + offsetColumn, | ||
endCol: endCol + (startLine === endLine ? offsetColumn : 0), | ||
nodeName: '#doctype', | ||
@@ -112,8 +118,8 @@ type: ml_ast_1.MLASTNodeType.Doctype, | ||
raw, | ||
startOffset, | ||
endOffset, | ||
startLine, | ||
endLine, | ||
startCol, | ||
endCol, | ||
startOffset: startOffset + offsetOffset, | ||
endOffset: endOffset + offsetOffset, | ||
startLine: startLine + offsetLine, | ||
endLine: endLine + offsetLine, | ||
startCol: startCol + offsetColumn, | ||
endCol: endCol + (startLine === endLine ? offsetColumn : 0), | ||
nodeName: '#text', | ||
@@ -133,8 +139,8 @@ type: ml_ast_1.MLASTNodeType.Text, | ||
raw, | ||
startOffset, | ||
endOffset, | ||
startLine, | ||
endLine, | ||
startCol, | ||
endCol, | ||
startOffset: startOffset + offsetOffset, | ||
endOffset: endOffset + offsetOffset, | ||
startLine: startLine + offsetLine, | ||
endLine: endLine + offsetLine, | ||
startCol: startCol + offsetColumn, | ||
endCol: endCol + (startLine === endLine ? offsetColumn : 0), | ||
nodeName: '#comment', | ||
@@ -154,3 +160,3 @@ type: ml_ast_1.MLASTNodeType.Comment, | ||
: rawHtml.slice(startOffset, endOffset || startOffset); | ||
const tagTokens = parse_raw_tag_1.default(startTagRaw, startLine, startCol, startOffset); | ||
const tagTokens = parse_raw_tag_1.default(startTagRaw, startLine, startCol, startOffset, offsetOffset, offsetLine, offsetColumn); | ||
const tagName = tagTokens.tagName; | ||
@@ -160,4 +166,5 @@ let endTag = null; | ||
if (endTagLoc) { | ||
const endTagRaw = rawHtml.slice(endTagLoc.startOffset, endTagLoc.endOffset); | ||
const endTagTokens = parse_raw_tag_1.default(endTagRaw, endTagLoc.startLine, endTagLoc.startCol, endTagLoc.startOffset); | ||
const { startOffset, endOffset, startLine, endLine, startCol, endCol } = endTagLoc; | ||
const endTagRaw = rawHtml.slice(startOffset, endOffset); | ||
const endTagTokens = parse_raw_tag_1.default(endTagRaw, startLine, startCol, startOffset, offsetOffset, offsetLine, offsetColumn); | ||
const endTagName = endTagTokens.tagName; | ||
@@ -167,8 +174,8 @@ endTag = { | ||
raw: endTagRaw, | ||
startOffset: endTagLoc.startOffset, | ||
endOffset: endTagLoc.endOffset, | ||
startLine: endTagLoc.startLine, | ||
endLine: endTagLoc.endLine, | ||
startCol: endTagLoc.startCol, | ||
endCol: endTagLoc.endCol, | ||
startOffset: startOffset + offsetOffset, | ||
endOffset: endOffset + offsetOffset, | ||
startLine: startLine + offsetLine, | ||
endLine: endLine + offsetLine, | ||
startCol: startCol + offsetColumn, | ||
endCol: endCol + (startLine === endLine ? offsetColumn : 0), | ||
nodeName: endTagName, | ||
@@ -188,11 +195,14 @@ type: ml_ast_1.MLASTNodeType.EndTag, | ||
} | ||
const _endOffset = startOffset + startTagRaw.length; | ||
const _endLine = get_end_line_1.default(startTagRaw, startLine); | ||
const _endCol = get_end_col_1.default(startTagRaw, startCol); | ||
const startTag = { | ||
uuid: uuid_1.v4(), | ||
raw: startTagRaw, | ||
startOffset, | ||
endOffset: startOffset + startTagRaw.length, | ||
startLine, | ||
endLine: get_end_line_1.default(startTagRaw, startLine), | ||
startCol, | ||
endCol: get_end_col_1.default(startTagRaw, startCol), | ||
startOffset: startOffset + offsetOffset, | ||
endOffset: _endOffset + offsetOffset, | ||
startLine: startLine + offsetLine, | ||
endLine: _endLine + offsetLine, | ||
startCol: startCol + offsetColumn, | ||
endCol: _endCol + (startLine === _endLine ? offsetColumn : 0), | ||
nodeName: tagName, | ||
@@ -216,3 +226,3 @@ type: ml_ast_1.MLASTNodeType.StartTag, | ||
} | ||
startTag.childNodes = traverse(originNode, startTag, rawHtml); | ||
startTag.childNodes = traverse(originNode, startTag, rawHtml, offsetOffset, offsetLine, offsetColumn); | ||
return startTag; | ||
@@ -219,0 +229,0 @@ } |
import { MLASTNode } from '@markuplint/ml-ast'; | ||
export declare type Walker = (node: MLASTNode) => void; | ||
export declare function walk(nodeList: MLASTNode[], walker: Walker): void; | ||
export declare type Walker = (node: MLASTNode, depth: number) => void; | ||
export declare function walk(nodeList: MLASTNode[], walker: Walker, depth?: number): void; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.walk = void 0; | ||
function walk(nodeList, walker) { | ||
const ml_ast_1 = require("@markuplint/ml-ast"); | ||
function walk(nodeList, walker, depth = 0) { | ||
for (const node of nodeList) { | ||
walker(node); | ||
const tag = node; | ||
if (tag.childNodes && tag.childNodes.length) { | ||
walk(tag.childNodes, walker); | ||
walker(node, depth); | ||
if ('childNodes' in node) { | ||
if (node.type === ml_ast_1.MLASTNodeType.EndTag) { | ||
continue; | ||
} | ||
if (node.childNodes && node.childNodes.length) { | ||
walk(node.childNodes, walker, depth + 1); | ||
} | ||
if ('pearNode' in node && node.pearNode) { | ||
walker(node.pearNode, depth); | ||
} | ||
} | ||
if (tag.pearNode) { | ||
walker(tag.pearNode); | ||
} | ||
} | ||
} | ||
exports.walk = walk; |
{ | ||
"name": "@markuplint/html-parser", | ||
"version": "1.0.0-alpha.19", | ||
"version": "1.0.0-alpha.20", | ||
"description": "HTML parser for markuplint", | ||
@@ -28,3 +28,3 @@ "repository": "git@github.com:markuplint/markuplint.git", | ||
}, | ||
"gitHead": "b3e2bf3e0f6410ba1107d8273b510a7c2cc4273a" | ||
"gitHead": "7f08524fa2ddecede72c1cda64c4a463a653b19f" | ||
} |
Sorry, the diff of this file is not supported yet
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
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
232630
34
1015
1
40