basisjs-tools-ast
Advanced tools
Comparing version 1.0.3 to 1.0.4
var htmlparser = require('htmlparser2'); | ||
var N = 10; // \n | ||
var F = 12; // \f | ||
var R = 13; // \r | ||
var parserConfig = { | ||
@@ -53,10 +57,2 @@ lowerCaseTags: true | ||
function posToLineColumn(str, pos, offset){ | ||
var lines = str.substr(0, pos).split('\n'); | ||
return { | ||
line: lines.length - 1 + (offset || 0), | ||
column: lines.pop().length + (offset || 0) | ||
}; | ||
} | ||
function walk(ast, handlers, context){ | ||
@@ -99,8 +95,7 @@ function walkNode(nodes){ | ||
module.exports = { | ||
parse: function(html){ | ||
parse: function(html, options){ | ||
// prepare parser | ||
var handler = new htmlparser.DomHandler(false, { | ||
refParent: true | ||
}); | ||
var handler = new htmlparser.DomHandler(false); | ||
var parser = new htmlparser.Parser(handler, parserConfig); | ||
var posMap = new Map([[0, { line: 1, column: 1 }]]); | ||
var attrInfo = {}; | ||
@@ -110,2 +105,62 @@ var head = null; | ||
var lastPos = 0; | ||
if (!options) | ||
options = { | ||
location: true | ||
}; | ||
function posToLineColumn(str, pos){ | ||
var prevIndex; | ||
if (pos > lastPos) | ||
{ | ||
prevIndex = lastPos; | ||
lastPos = pos; | ||
} | ||
else | ||
{ | ||
if (posMap.has(pos)) | ||
return posMap.get(pos); | ||
for (var i = pos; i >= 0; i--) | ||
if (posMap.has(i)) | ||
{ | ||
prevIndex = i; | ||
break; | ||
} | ||
} | ||
var prevInfo = posMap.get(prevIndex); | ||
var line = prevInfo.line; | ||
var column = prevInfo.column; | ||
var prevCharCode = prevIndex ? str.charCodeAt(prevIndex - 1) : 0; | ||
for (var i = prevIndex; i < pos; i++) | ||
{ | ||
var charCode = str.charCodeAt(i); | ||
if (charCode === R || | ||
(prevCharCode !== R && charCode === N) || | ||
charCode === F) | ||
{ | ||
line++; | ||
column = 1; | ||
} | ||
else | ||
{ | ||
column++; | ||
} | ||
prevCharCode = charCode; | ||
} | ||
var info = { | ||
line: line, | ||
column: column | ||
}; | ||
posMap.set(pos, info); | ||
return info; | ||
} | ||
handler.onopentag = function(name, attribs){ | ||
@@ -115,10 +170,14 @@ htmlparser.DomHandler.prototype.onopentag.call(this, name, attribs); | ||
var element = this._tagStack[this._tagStack.length - 1]; | ||
element.info = { | ||
attrs: attrInfo, | ||
start: posToLineColumn(html, parser.startIndex, 1), | ||
startContent: posToLineColumn(html, parser.endIndex) | ||
}; | ||
attrInfo = {}; | ||
if (options.location) | ||
{ | ||
element.info = { | ||
attrs: attrInfo, | ||
start: posToLineColumn(html, parser.startIndex), | ||
startContent: posToLineColumn(html, parser.endIndex + 1) | ||
}; | ||
attrInfo = {}; | ||
} | ||
switch (name) | ||
@@ -143,21 +202,24 @@ { | ||
handler.onclosetag = function(){ | ||
var element = this._tagStack[this._tagStack.length - 1]; | ||
element.info.endContent = posToLineColumn(html, parser.startIndex); | ||
element.info.end = posToLineColumn(html, parser.endIndex, 1); | ||
if (options.location) | ||
{ | ||
handler.onclosetag = function(){ | ||
var element = this._tagStack[this._tagStack.length - 1]; | ||
element.info.endContent = posToLineColumn(html, parser.startIndex); | ||
element.info.end = posToLineColumn(html, parser.endIndex); | ||
htmlparser.DomHandler.prototype.onclosetag.call(this); | ||
}; | ||
htmlparser.DomHandler.prototype.onclosetag.call(this); | ||
}; | ||
parser.onattribdata = function(){ | ||
htmlparser.Parser.prototype.onattribdata.apply(this, arguments); | ||
parser.onattribdata = function(){ | ||
htmlparser.Parser.prototype.onattribdata.apply(this, arguments); | ||
var startIndex = parser._tokenizer._sectionStart; | ||
var endIndex = parser._tokenizer._index; | ||
var startIndex = parser._tokenizer._sectionStart; | ||
var endIndex = parser._tokenizer._index; | ||
attrInfo[this._attribname] = { | ||
start: posToLineColumn(html, startIndex, 1), | ||
end: posToLineColumn(html, endIndex, 1) | ||
attrInfo[this._attribname] = { | ||
start: posToLineColumn(html, startIndex), | ||
end: posToLineColumn(html, endIndex) | ||
}; | ||
}; | ||
}; | ||
} | ||
@@ -164,0 +226,0 @@ // parse html |
{ | ||
"name": "basisjs-tools-ast", | ||
"version": "1.0.3", | ||
"description": "Tool set for basisjs-tools to work with various AST", | ||
"version": "1.0.4", | ||
"description": "Tool set for basisjs-tools to work with various types of AST", | ||
"homepage": "https://github.com/basisjs/basisjs-tools-ast", | ||
@@ -14,7 +14,14 @@ "author": "Roman Dvornov <rdvornov@gmail.com>", | ||
"engines": { | ||
"node": ">=0.8.0" | ||
"node": ">=0.12.0" | ||
}, | ||
"scripts": { | ||
"test": "mocha" | ||
}, | ||
"dependencies": { | ||
"htmlparser2": "3.8.3" | ||
}, | ||
"devDependencies": { | ||
"chai": "^3.4.0", | ||
"mocha": "^2.3.3" | ||
}, | ||
"files": [ | ||
@@ -21,0 +28,0 @@ "lib", |
@@ -1,1 +0,5 @@ | ||
TODO | ||
[![NPM version](https://img.shields.io/npm/v/basisjs-tools-ast.svg)](https://www.npmjs.com/package/basisjs-tools-ast) | ||
[![Dependency Status](https://img.shields.io/david/basisjs/basisjs-tools-ast.svg)](https://david-dm.org/basisjs/basisjs-tools-ast) | ||
[![Build Status](https://travis-ci.org/basisjs/basisjs-tools-ast.svg?branch=master)](https://travis-ci.org/basisjs/basisjs-tools-ast) | ||
Tool set to work with AST: `HTML`, `CSS`, `JavaScript` and [basis.js](https://github.com/basisjs/basisjs) templates. |
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
186893
17
5495
6
2