var CucumberHTML = {}; CucumberHTML.DOMFormatter = function(rootNode) { var currentUri; var currentFeature; var currentElement; var currentSteps; var currentStepIndex; var currentStep; var $templates = $(CucumberHTML.templates); this.uri = function(uri) { currentUri = uri; }; this.feature = function(feature) { currentFeature = blockElement(rootNode, feature, 'feature'); }; this.background = function(background) { currentElement = featureElement(background, 'background'); currentStepIndex = 1; }; this.scenario = function(scenario) { currentElement = featureElement(scenario, 'scenario'); currentStepIndex = 1; }; this.scenarioOutline = function(scenarioOutline) { currentElement = featureElement(scenarioOutline, 'scenario_outline'); currentStepIndex = 1; }; this.step = function(step) { var stepElement = $('.step', $templates).clone(); stepElement.appendTo(currentSteps); populate(stepElement, step, 'step'); if (step.doc_string) { docString = $('.doc_string', $templates).clone(); docString.appendTo(stepElement); // TODO: use a syntax highlighter based on the content_type docString.text(step.doc_string.value); } if (step.rows) { dataTable = $('.data_table', $templates).clone(); dataTable.appendTo(stepElement); var tBody = dataTable.find('tbody'); $.each(step.rows, function(index, row) { var tr = $('').appendTo(tBody); $.each(row.cells, function(index, cell) { var td = $('' + cell + '').appendTo(tBody); }); }); } }; this.examples = function(examples) { var examplesElement = blockElement(currentElement.children('details'), examples, 'examples'); var examplesTable = $('.examples_table', $templates).clone(); examplesTable.appendTo(examplesElement.children('details')); $.each(examples.rows, function(index, row) { var parent = index == 0 ? examplesTable.find('thead') : examplesTable.find('tbody'); var tr = $('').appendTo(parent); $.each(row.cells, function(index, cell) { var td = $('' + cell + '').appendTo(tr); }); }); }; this.match = function(match) { currentStep = currentSteps.find('li:nth-child(' + currentStepIndex + ')'); currentStepIndex++; }; this.result = function(result) { currentStep.addClass(result.status); if (result.status == 'failed') { populateStepError(currentStep, result.error_message); } currentElement.addClass(result.status); var isLastStep = currentSteps.find('li:nth-child(' + currentStepIndex + ')').length == 0; if (isLastStep) { if (currentSteps.find('.failed').length == 0) { // No failed steps. Collapse it. currentElement.find('details').removeAttr('open'); } else { currentElement.find('details').attr('open', 'open'); } } }; this.embedding = function(mimeType, data) { if (mimeType.match(/^image\//)) { currentStep.append(''); } else if (mimeType.match(/^video\//)) { currentStep.append(''); } else if (mimeType.match(/^text\//)) { this.write(data); } }; this.write = function(text) { currentStep.append('
' + text + '
'); }; this.before = function(before) { if(before.status != 'passed') { currentElement = featureElement({keyword: 'Before', name: '', description: ''}, 'before'); currentStepIndex = 1; populateStepError($('details', currentElement), before.error_message); } }; this.after = function(after) { if(after.status != 'passed') { currentElement = featureElement({keyword: 'After', name: '', description: ''}, 'after'); currentStepIndex++; populateStepError($('details', currentElement), after.error_message); } }; function featureElement(statement, itemtype) { var e = blockElement(currentFeature.children('details'), statement, itemtype); currentSteps = $('.steps', $templates).clone(); currentSteps.appendTo(e.children('details')); return e; } function blockElement(parent, statement, itemtype) { var e = $('.blockelement', $templates).clone(); e.appendTo(parent); return populate(e, statement, itemtype); } function populate(e, statement, itemtype) { populateTags(e, statement.tags); populateComments(e, statement.comments); e.find('.keyword').text(statement.keyword); e.find('.name').text(statement.name); e.find('.description').text(statement.description); e.attr('itemtype', 'http://cukes.info/microformat/' + itemtype); e.addClass(itemtype); return e; } function populateComments(e, comments) { if (comments !== undefined) { var commentsNode = $('.comments', $templates).clone().prependTo(e.find('.header')); $.each(comments, function(index, comment) { var commentNode = $('.comment', $templates).clone().appendTo(commentsNode); commentNode.text(comment.value); }); } } function populateTags(e, tags) { if (tags !== undefined) { var tagsNode = $('.tags', $templates).clone().prependTo(e.find('.header')); $.each(tags, function(index, tag) { var tagNode = $('.tag', $templates).clone().appendTo(tagsNode); tagNode.text(tag.name); }); } } function populateStepError(e, error) { if (error !== undefined) { errorNode = $('.error', $templates).clone().appendTo(e); errorNode.text(error); } } }; CucumberHTML.templates = '
\
\
\ \ Keyword: This is the block name\ \
The description goes here
\
\
\ \
    \ \
      \
    1. KeywordName
    2. \
    \ \
    \
    \
      
    \
    \
      \
        \
        \
      
    \ \ \ \ \
    \ \
    \ \
    \
    \ \
    \
    \
    '; if (typeof module !== 'undefined') { module.exports = CucumberHTML; } else if (typeof define !== 'undefined') { define([], function() { return CucumberHTML; }); }