html-parser
Advanced tools
Comparing version 0.8.0 to 0.9.0
{ | ||
"name": "html-parser", | ||
"version": "0.8.0", | ||
"version": "0.9.0", | ||
"description": "HTML/XML parser with less explosions", | ||
@@ -16,3 +16,4 @@ "keywords": [ "html", "xml", "parser", "explosion" ], | ||
{ "name": "jdponomarev" }, | ||
{ "name": "fiatjaf" } | ||
{ "name": "fiatjaf" }, | ||
{ "name": "Sergii Kliuchnyk" } | ||
], | ||
@@ -30,4 +31,4 @@ | ||
"devDependencies": { | ||
"mocha": "2.0.1", | ||
"should": "4.3.0" | ||
"mocha": "2.4.5", | ||
"should": "8.2.2" | ||
}, | ||
@@ -34,0 +35,0 @@ |
@@ -105,4 +105,7 @@ # html-parser | ||
* @param {Function} [callbacks.text] Takes the value of the text node | ||
* @param {Object} [regex] | ||
* @param {RegExp} [regex.name] Regex for element name. Default is [a-zA-Z_][\w:\-\.]* | ||
* @param {RegExp} [regex.attribute] Regex for attribute name. Default is [a-zA-Z_][\w:\-\.]* | ||
*/ | ||
parse(htmlString, callbacks) | ||
parse(htmlString, callbacks, regex) | ||
@@ -109,0 +112,0 @@ /** |
@@ -1,2 +0,2 @@ | ||
exports.create = function(raw, options) { | ||
exports.create = function(raw, options, regex) { | ||
var index = 0; | ||
@@ -81,3 +81,14 @@ var context = { | ||
context.regex = { | ||
name: /[a-zA-Z_][\w:\-\.]*/, | ||
attribute: /[a-zA-Z_][\w:\-\.]*/ | ||
}; | ||
regex = regex || {}; | ||
for (var name in regex) { | ||
if (regex.hasOwnProperty(name)) { | ||
context.regex[name] = regex[name]; | ||
} | ||
} | ||
return context; | ||
} | ||
}; |
var parseContext = require('./context'); | ||
var nameRegex = /[a-zA-Z_][\w:\-\.]*/; | ||
function readAttribute(context) { | ||
var name = context.readRegex(nameRegex); | ||
var name = context.readRegex(context.regex.attribute); | ||
var value = null; | ||
@@ -33,3 +32,3 @@ if (context.current === '=' || context.peekIgnoreWhitespace() === '=') { | ||
while (!context.isEof() && !isClosingToken()) { | ||
if (nameRegex.test(next)) { | ||
if (context.regex.attribute.test(next)) { | ||
readAttribute(context); | ||
@@ -72,3 +71,3 @@ next = context.current; | ||
function parseOpenElement(context) { | ||
var name = context.readRegex(nameRegex); | ||
var name = context.readRegex(context.regex.name); | ||
context.callbacks.openElement(name); | ||
@@ -95,3 +94,3 @@ readAttributes(context, false); | ||
function parseEndElement(context) { | ||
var name = context.readRegex(nameRegex); | ||
var name = context.readRegex(context.regex.name); | ||
context.callbacks.closeElement(name); | ||
@@ -156,3 +155,3 @@ context.readRegex(/.*?(?:>|$)/); | ||
buffer += context.read(); | ||
if (nameRegex.test(context.current)) { | ||
if (context.regex.name.test(context.current)) { | ||
callbackText(context); | ||
@@ -189,3 +188,3 @@ parseEndElement(context); | ||
} | ||
} else if (nameRegex.test(context.current)) { | ||
} else if (context.regex.name.test(context.current)) { | ||
callbackText(context); | ||
@@ -221,6 +220,9 @@ parseOpenElement(context); | ||
* @param {Function} [callbacks.text] Takes the value of the text node | ||
* @param {Object} [regex] | ||
* @param {RegExp} [regex.name] Regex for element name. Default is [a-zA-Z_][\w:\-\.]* | ||
* @param {RegExp} [regex.attribute] Regex for attribute name. Default is [a-zA-Z_][\w:\-\.]* | ||
*/ | ||
exports.parse = function(htmlString, callbacks) { | ||
exports.parse = function(htmlString, callbacks, regex) { | ||
htmlString = htmlString.replace(/\r\n/g, '\n').replace(/\r/g, '\n'); | ||
var context = parseContext.create(htmlString, callbacks); | ||
var context = parseContext.create(htmlString, callbacks, regex); | ||
do { | ||
@@ -227,0 +229,0 @@ parseNext(context); |
@@ -175,2 +175,23 @@ var should = require('should'); | ||
}); | ||
it('with custom attribute regex', function() { | ||
var openCount = 0, attrCount = 0; | ||
helpers.parseString('<foo [bar]="baz">', { | ||
openElement: function(name) { | ||
name.should.equal('foo'); | ||
openCount++; | ||
}, | ||
attribute: function(name, value) { | ||
name.should.equal('[bar]'); | ||
value.should.equal('baz'); | ||
attrCount++; | ||
} | ||
}, { | ||
attribute: /\[[\w\]]*/ | ||
}); | ||
openCount.should.equal(1); | ||
attrCount.should.equal(1); | ||
}); | ||
}); |
@@ -5,4 +5,4 @@ var htmlParser = require('../src/parser.js'); | ||
exports.parseString = function(string, options) { | ||
htmlParser.parse(string, options); | ||
exports.parseString = function(string, options, regex) { | ||
htmlParser.parse(string, options, regex); | ||
}; |
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
48947
1458
150