|
| 1 | +var CucumberHTML = {}; |
| 2 | + |
| 3 | +CucumberHTML.DOMFormatter = function(rootNode) { |
| 4 | + var currentUri; |
| 5 | + var currentFeature; |
| 6 | + var currentElement; |
| 7 | + var currentSteps; |
| 8 | + |
| 9 | + var currentStepIndex; |
| 10 | + var currentStep; |
| 11 | + var $templates = $(CucumberHTML.templates); |
| 12 | + |
| 13 | + this.uri = function(uri) { |
| 14 | + currentUri = uri; |
| 15 | + }; |
| 16 | + |
| 17 | + this.feature = function(feature) { |
| 18 | + currentFeature = blockElement(rootNode, feature, 'feature'); |
| 19 | + }; |
| 20 | + |
| 21 | + this.background = function(background) { |
| 22 | + currentElement = featureElement(background, 'background'); |
| 23 | + currentStepIndex = 1; |
| 24 | + }; |
| 25 | + |
| 26 | + this.scenario = function(scenario) { |
| 27 | + currentElement = featureElement(scenario, 'scenario'); |
| 28 | + currentStepIndex = 1; |
| 29 | + }; |
| 30 | + |
| 31 | + this.scenarioOutline = function(scenarioOutline) { |
| 32 | + currentElement = featureElement(scenarioOutline, 'scenario_outline'); |
| 33 | + currentStepIndex = 1; |
| 34 | + }; |
| 35 | + |
| 36 | + this.step = function(step) { |
| 37 | + var stepElement = $('.step', $templates).clone(); |
| 38 | + stepElement.appendTo(currentSteps); |
| 39 | + populate(stepElement, step, 'step'); |
| 40 | + |
| 41 | + if (step.doc_string) { |
| 42 | + docString = $('.doc_string', $templates).clone(); |
| 43 | + docString.appendTo(stepElement); |
| 44 | + // TODO: use a syntax highlighter based on the content_type |
| 45 | + docString.text(step.doc_string.value); |
| 46 | + } |
| 47 | + if (step.rows) { |
| 48 | + dataTable = $('.data_table', $templates).clone(); |
| 49 | + dataTable.appendTo(stepElement); |
| 50 | + var tBody = dataTable.find('tbody'); |
| 51 | + $.each(step.rows, function(index, row) { |
| 52 | + var tr = $('<tr></tr>').appendTo(tBody); |
| 53 | + $.each(row.cells, function(index, cell) { |
| 54 | + var td = $('<td>' + cell + '</td>').appendTo(tBody); |
| 55 | + }); |
| 56 | + }); |
| 57 | + } |
| 58 | + }; |
| 59 | + |
| 60 | + this.examples = function(examples) { |
| 61 | + var examplesElement = blockElement(currentElement.children('details'), examples, 'examples'); |
| 62 | + var examplesTable = $('.examples_table', $templates).clone(); |
| 63 | + examplesTable.appendTo(examplesElement.children('details')); |
| 64 | + |
| 65 | + $.each(examples.rows, function(index, row) { |
| 66 | + var parent = index == 0 ? examplesTable.find('thead') : examplesTable.find('tbody'); |
| 67 | + var tr = $('<tr></tr>').appendTo(parent); |
| 68 | + $.each(row.cells, function(index, cell) { |
| 69 | + var td = $('<td>' + cell + '</td>').appendTo(tr); |
| 70 | + }); |
| 71 | + }); |
| 72 | + }; |
| 73 | + |
| 74 | + this.match = function(match) { |
| 75 | + currentStep = currentSteps.find('li:nth-child(' + currentStepIndex + ')'); |
| 76 | + currentStepIndex++; |
| 77 | + }; |
| 78 | + |
| 79 | + this.result = function(result) { |
| 80 | + currentStep.addClass(result.status); |
| 81 | + if (result.error_message != '') { |
| 82 | + populateStepError(currentStep, result.error_message); |
| 83 | + } |
| 84 | + currentElement.addClass(result.status); |
| 85 | + var isLastStep = currentSteps.find('li:nth-child(' + currentStepIndex + ')').length == 0; |
| 86 | + if (isLastStep) { |
| 87 | + if (currentSteps.find('.failed').length == 0) { |
| 88 | + // No failed steps. Collapse it. |
| 89 | + currentElement.find('details').removeAttr('open'); |
| 90 | + } else { |
| 91 | + currentElement.find('details').attr('open', 'open'); |
| 92 | + } |
| 93 | + } |
| 94 | + }; |
| 95 | + |
| 96 | + this.embedding = function(mimeType, data) { |
| 97 | + if (currentStepIndex == 1) { |
| 98 | + this.dummyStep(); |
| 99 | + } |
| 100 | + if (mimeType.match(/^image\//)) |
| 101 | + { |
| 102 | + currentStep.append('<img src="' + data + '">'); |
| 103 | + } |
| 104 | + else if (mimeType.match(/^video\//)) |
| 105 | + { |
| 106 | + currentStep.append('<video src="' + data + '" type="' + mimeType + '" autobuffer controls>Your browser doesn\'t support video.</video>'); |
| 107 | + } |
| 108 | + else if (mimeType.match(/^text\//)) |
| 109 | + { |
| 110 | + this.write(data); |
| 111 | + } |
| 112 | + }; |
| 113 | + |
| 114 | + this.write = function(text) { |
| 115 | + if (currentStepIndex == 1) { |
| 116 | + this.dummyStep(); |
| 117 | + } |
| 118 | + currentStep.append('<pre class="embedded-text">' + text + '</pre>'); |
| 119 | + }; |
| 120 | + |
| 121 | + this.before = function(before) { |
| 122 | + this.handleHookResult(before); |
| 123 | + }; |
| 124 | + |
| 125 | + this.after = function(after) { |
| 126 | + this.handleHookResult(after); |
| 127 | + }; |
| 128 | + |
| 129 | + this.beforestep = function(beforestep) { |
| 130 | + this.handleHookResult(beforestep); |
| 131 | + }; |
| 132 | + |
| 133 | + this.afterstep = function(afterstep) { |
| 134 | + this.handleHookResult(afterstep); |
| 135 | + }; |
| 136 | + |
| 137 | + this.handleHookResult = function(hook) { |
| 138 | + if (hook.status != 'passed' && hook.error_message != '') { |
| 139 | + this.dummyStep(); |
| 140 | + currentStep.addClass(hook.status); |
| 141 | + currentElement.addClass(hook.status); |
| 142 | + populateStepError(currentStep, hook.error_message); |
| 143 | + } |
| 144 | + }; |
| 145 | + |
| 146 | + this.dummyStep = function() { |
| 147 | + var stepElement = $('.step', $templates).clone(); |
| 148 | + stepElement.appendTo(currentSteps); |
| 149 | + populate(stepElement, {keyword: '', name: ''}, 'step'); |
| 150 | + currentStep = currentSteps.find('li:nth-child(' + currentStepIndex + ')'); |
| 151 | + currentStepIndex++; |
| 152 | + }; |
| 153 | + |
| 154 | + function featureElement(statement, itemtype) { |
| 155 | + var e = blockElement(currentFeature.children('details'), statement, itemtype); |
| 156 | + |
| 157 | + currentSteps = $('.steps', $templates).clone(); |
| 158 | + currentSteps.appendTo(e.children('details')); |
| 159 | + |
| 160 | + return e; |
| 161 | + } |
| 162 | + |
| 163 | + function blockElement(parent, statement, itemtype) { |
| 164 | + var e = $('.blockelement', $templates).clone(); |
| 165 | + e.appendTo(parent); |
| 166 | + return populate(e, statement, itemtype); |
| 167 | + } |
| 168 | + |
| 169 | + function populate(e, statement, itemtype) { |
| 170 | + populateTags(e, statement.tags); |
| 171 | + populateComments(e, statement.comments); |
| 172 | + e.find('.keyword').text(statement.keyword); |
| 173 | + e.find('.name').text(statement.name); |
| 174 | + e.find('.description').text(statement.description); |
| 175 | + e.attr('itemtype', 'http://cukes.info/microformat/' + itemtype); |
| 176 | + e.addClass(itemtype); |
| 177 | + return e; |
| 178 | + } |
| 179 | + |
| 180 | + function populateComments(e, comments) { |
| 181 | + if (comments !== undefined) { |
| 182 | + var commentsNode = $('.comments', $templates).clone().prependTo(e.find('.header')); |
| 183 | + $.each(comments, function(index, comment) { |
| 184 | + var commentNode = $('.comment', $templates).clone().appendTo(commentsNode); |
| 185 | + commentNode.text(comment.value); |
| 186 | + }); |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + function populateTags(e, tags) { |
| 191 | + if (tags !== undefined) { |
| 192 | + var tagsNode = $('.tags', $templates).clone().prependTo(e.find('.header')); |
| 193 | + $.each(tags, function(index, tag) { |
| 194 | + var tagNode = $('.tag', $templates).clone().appendTo(tagsNode); |
| 195 | + tagNode.text(tag.name); |
| 196 | + }); |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + function populateStepError(e, error) { |
| 201 | + if (error !== undefined) { |
| 202 | + errorNode = $('.error', $templates).clone().appendTo(e); |
| 203 | + errorNode.text(error); |
| 204 | + } |
| 205 | + } |
| 206 | +}; |
| 207 | + |
| 208 | +CucumberHTML.templates = '<div>\ |
| 209 | + <section class="blockelement" itemscope>\ |
| 210 | + <details open>\ |
| 211 | + <summary class="header">\ |
| 212 | + <span class="keyword" itemprop="keyword">Keyword</span>: <span itemprop="name" class="name">This is the block name</span>\ |
| 213 | + </summary>\ |
| 214 | + <div itemprop="description" class="description">The description goes here</div>\ |
| 215 | + </details>\ |
| 216 | + </section>\ |
| 217 | +\ |
| 218 | + <ol class="steps"></ol>\ |
| 219 | +\ |
| 220 | + <ol>\ |
| 221 | + <li class="step"><div class="header"></div><span class="keyword" itemprop="keyword">Keyword</span><span class="name" itemprop="name">Name</span></li>\ |
| 222 | + </ol>\ |
| 223 | +\ |
| 224 | + <pre class="doc_string"></pre>\ |
| 225 | +\ |
| 226 | + <pre class="error"></pre>\ |
| 227 | +\ |
| 228 | + <table class="data_table">\ |
| 229 | + <tbody>\ |
| 230 | + </tbody>\ |
| 231 | + </table>\ |
| 232 | +\ |
| 233 | + <table class="examples_table">\ |
| 234 | + <thead></thead>\ |
| 235 | + <tbody></tbody>\ |
| 236 | + </table>\ |
| 237 | +\ |
| 238 | + <section class="embed">\ |
| 239 | + <img itemprop="screenshot" class="screenshot" />\ |
| 240 | + </section>\ |
| 241 | + <div class="tags"></div>\ |
| 242 | + <span class="tag"></span>\ |
| 243 | + <div class="comments"></div>\ |
| 244 | + <div class="comment"></div>\ |
| 245 | +<div>'; |
| 246 | + |
| 247 | +if (typeof module !== 'undefined') { |
| 248 | + module.exports = CucumberHTML; |
| 249 | +} else if (typeof define !== 'undefined') { |
| 250 | + define([], function() { return CucumberHTML; }); |
| 251 | +} |
0 commit comments