Skip to content
This repository was archived by the owner on Jan 23, 2020. It is now read-only.

Commit 801faff

Browse files
committed
Merge pull request #24 from neraliu/get-tag-api
add the api of getCurrentTagIndex and getCurrentTag
2 parents 7088344 + d0812dc commit 801faff

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/context-parser.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,12 +332,35 @@ FastParser.prototype.afterWalk = function (i, input) {};
332332

333333
/**
334334
* @function FastParser#getStartTagName
335+
* @depreciated Replace it by getCurrentTagIndex and getCurrentTag
335336
*
336337
* @returns {string} The current handling start tag name
337338
*
338339
*/
339340
FastParser.prototype.getStartTagName = function() {
340-
return this.tags[0].toLowerCase();
341+
return this.tags[0] !== undefined? this.tags[0].toLowerCase() : undefined;
342+
};
343+
344+
/**
345+
* @function FastParser#getCurrentTagIndex
346+
*
347+
* @returns {integer} The current handling tag Idx
348+
*
349+
*/
350+
FastParser.prototype.getCurrentTagIndex = function() {
351+
return this.tagIdx;
352+
};
353+
354+
/**
355+
* @function FastParser#getCurrentTag
356+
*
357+
* @params {integer} The tag Idx
358+
*
359+
* @returns {string} The current tag name indexed by tag Idx
360+
*
361+
*/
362+
FastParser.prototype.getCurrentTag = function(tagIdx) {
363+
return tagIdx === 0 || tagIdx === 1? (this.tags[tagIdx] !== undefined? this.tags[tagIdx].toLowerCase():undefined) : undefined;
341364
};
342365

343366
/**

tests/unit/run-functions-spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,26 @@ Authors: Nera Liu <neraliu@yahoo-inc.com>
216216

217217
});
218218

219+
describe('#getCurrentTagIndex and #getCurrentTag', function(){
220+
221+
it('should return correct tag name/index', function(){
222+
223+
[ { html: "<div class='classname' style='color:red'></div>", tag0: 'div', tag1: 'div', index: 1},
224+
{ html: "<div class='classname' style='color:red'></div> ", tag0: 'div', tag1: 'div', index: 1},
225+
{ html: "<div class='classname' style='color:red'></div><img>", tag0: 'img', tag1: 'div', index: 0},
226+
{ html: "<div class='classname' style='color:red'></div><img ", tag0: 'img', tag1: 'div', index: 0},
227+
{ html: "<div class='classname' style='color:red'></div><img></im", tag0: 'img', tag1: 'im', index: 1},
228+
{ html: "<div class='classname' style='color:red'></div><img></img ",tag0: 'img', tag1: 'img', index: 1},
229+
].forEach(function(testObj) {
230+
var p1 = new Parser(config);
231+
p1.contextualize(testObj.html);
232+
assert.equal(p1.getCurrentTag(0), testObj.tag0);
233+
assert.equal(p1.getCurrentTag(1), testObj.tag1);
234+
assert.equal(p1.getCurrentTagIndex(), testObj.index);
235+
});
236+
});
237+
});
238+
219239
describe('#Constructor', function(){
220240

221241
it('should support multiple instances', function(){

0 commit comments

Comments
 (0)