Skip to content

Commit

Permalink
public api regex
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergii.Kliuchnyk committed Mar 14, 2016
1 parent ac4ed32 commit fa8dcfa
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 14 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,11 @@ console.log(sanitized);
* @param {Function} [callbacks.cdata] Takes the content of the CDATA
* @param {Function} [callbacks.xmlProlog] Takes no arguments
* @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)

/**
* Parses the HTML contained in the given file asynchronously.
Expand Down
15 changes: 13 additions & 2 deletions src/context.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
exports.create = function(raw, options) {
exports.create = function(raw, options, regex) {
var index = 0;
var context = {
text: '',
Expand Down Expand Up @@ -78,5 +78,16 @@ exports.create = function(raw, options) {
};
});

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;
}
};
20 changes: 11 additions & 9 deletions src/parser.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
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;
if (context.current === '=' || context.peekIgnoreWhitespace() === '=') {
context.readRegex(/\s*=\s*/);
Expand Down Expand Up @@ -30,7 +29,7 @@ function readAttributes(context, isXml) {

var next = context.current;
while (!context.isEof() && !isClosingToken()) {
if (nameRegex.test(next)) {
if (context.regex.attribute.test(next)) {
readAttribute(context);
next = context.current;
}
Expand Down Expand Up @@ -68,7 +67,7 @@ function readCloserForOpenedElement(context, name) {
}

function parseOpenElement(context) {
var name = context.readRegex(nameRegex);
var name = context.readRegex(context.regex.name);
context.callbacks.openElement(name);
readAttributes(context, false);
readCloserForOpenedElement(context, name);
Expand All @@ -90,7 +89,7 @@ function parseOpenElement(context) {
}

function parseEndElement(context) {
var name = context.readRegex(nameRegex);
var name = context.readRegex(context.regex.name);
context.callbacks.closeElement(name);
context.readRegex(/.*?(?:>|$)/);
}
Expand Down Expand Up @@ -150,7 +149,7 @@ function parseNext(context) {
buffer += context.read();
if (context.current === '/') {
buffer += context.read();
if (nameRegex.test(context.current)) {
if (context.regex.name.test(context.current)) {
callbackText(context);
parseEndElement(context);
} else {
Expand Down Expand Up @@ -182,7 +181,7 @@ function parseNext(context) {
context.read();
appendText(buffer, context);
}
} else if (nameRegex.test(context.current)) {
} else if (context.regex.name.test(context.current)) {
callbackText(context);
parseOpenElement(context);
} else {
Expand Down Expand Up @@ -213,10 +212,13 @@ function parseNext(context) {
* @param {Function} [callbacks.cdata] Takes the content of the CDATA
* @param {Function} [callbacks.xmlProlog] Takes no arguments
* @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 {
parseNext(context);
} while (!context.isEof());
Expand Down
21 changes: 21 additions & 0 deletions tests/attribute-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,25 @@ describe('attributes', function() {
openCount.should.equal(1);
attrCount.should.equal(1);
});

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);
});
});
4 changes: 2 additions & 2 deletions tests/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ var htmlParser = require('../src/parser.js');

exports.parser = htmlParser;

exports.parseString = function(string, options) {
htmlParser.parse(string, options);
exports.parseString = function(string, options, regex) {
htmlParser.parse(string, options, regex);
};

0 comments on commit fa8dcfa

Please sign in to comment.