|
| 1 | +describe('highlightText', function() { |
| 2 | + |
| 3 | + // Helper Methods |
| 4 | + // -------------- |
| 5 | + |
| 6 | + var createParagraphWithTextNodes = function(firstPart, parts) { |
| 7 | + var textNode, part; |
| 8 | + var elem = $('<p>'+ firstPart +'</p>')[0]; |
| 9 | + for (var i=1; i<arguments.length; i++) { |
| 10 | + part = arguments[i]; |
| 11 | + textNode = document.createTextNode(part); |
| 12 | + elem.appendChild(textNode); |
| 13 | + } |
| 14 | + return elem; |
| 15 | + }; |
| 16 | + |
| 17 | + var iterateOverElement = function(elem, regex) { |
| 18 | + var matches = highlightText.find(elem, regex); |
| 19 | + var range = highlightText.getRange(elem); |
| 20 | + highlightText.iterate(range, matches); |
| 21 | + }; |
| 22 | + |
| 23 | + |
| 24 | + describe('minimal case', function() { |
| 25 | + |
| 26 | + beforeEach(function() { |
| 27 | + this.textNode = $('<div>a</div>')[0]; |
| 28 | + this.regex = /a/g; |
| 29 | + }); |
| 30 | + |
| 31 | + it('extracts the text', function(){ |
| 32 | + var range = highlightText.getRange(this.textNode); |
| 33 | + var text = highlightText.extractText(range); |
| 34 | + expect(text).toEqual('a'); |
| 35 | + }); |
| 36 | + |
| 37 | + it('finds the letter "a"', function() { |
| 38 | + var matches = highlightText.find(this.textNode, this.regex); |
| 39 | + var firstMatch = matches[0]; |
| 40 | + expect(firstMatch.search).toEqual('a'); |
| 41 | + expect(firstMatch.matchIndex).toEqual(0); |
| 42 | + expect(firstMatch.startIndex).toEqual(0); |
| 43 | + expect(firstMatch.endIndex).toEqual(1); |
| 44 | + }); |
| 45 | + |
| 46 | + it('does not find the letter "b"', function() { |
| 47 | + var matches = highlightText.find(this.textNode, /b/g); |
| 48 | + expect(matches.length).toEqual(0); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + describe('Some juice.', function() { |
| 53 | + |
| 54 | + beforeEach(function() { |
| 55 | + this.textNode = $('<div>Some juice.</div>')[0]; |
| 56 | + this.regex = /juice/g; |
| 57 | + }); |
| 58 | + |
| 59 | + it('finds the word "juice"', function() { |
| 60 | + var matches = highlightText.find(this.textNode, this.regex); |
| 61 | + var firstMatch = matches[0]; |
| 62 | + expect(firstMatch.search).toEqual('juice'); |
| 63 | + expect(firstMatch.matchIndex).toEqual(0); |
| 64 | + expect(firstMatch.startIndex).toEqual(5); |
| 65 | + expect(firstMatch.endIndex).toEqual(10); |
| 66 | + }); |
| 67 | + |
| 68 | + }); |
| 69 | + |
| 70 | + describe('iterator', function() { |
| 71 | + |
| 72 | + beforeEach(function() { |
| 73 | + this.wrapWord = sinon.stub(highlightText, 'wrapWord'); |
| 74 | + }); |
| 75 | + |
| 76 | + afterEach(function() { |
| 77 | + this.wrapWord.restore(); |
| 78 | + }); |
| 79 | + |
| 80 | + it('finds a letter that it is own text node', function() { |
| 81 | + var elem = createParagraphWithTextNodes('a', 'b', 'c'); |
| 82 | + iterateOverElement(elem, /b/g); |
| 83 | + var portions = this.wrapWord.firstCall.args[0]; |
| 84 | + |
| 85 | + expect(portions.length).toEqual(1); |
| 86 | + expect(portions[0].text).toEqual('b'); |
| 87 | + expect(portions[0].offset).toEqual(0); |
| 88 | + expect(portions[0].length).toEqual(1); |
| 89 | + expect(portions[0].lastPortion).toEqual(true); |
| 90 | + }); |
| 91 | + |
| 92 | + it('finds a letter that is in a text node with a letter before', function() { |
| 93 | + var elem = createParagraphWithTextNodes('a', 'xb', 'c'); |
| 94 | + iterateOverElement(elem, /b/g); |
| 95 | + var portions = this.wrapWord.firstCall.args[0]; |
| 96 | + |
| 97 | + expect(portions.length).toEqual(1); |
| 98 | + expect(portions[0].text).toEqual('b'); |
| 99 | + expect(portions[0].offset).toEqual(1); |
| 100 | + expect(portions[0].length).toEqual(1); |
| 101 | + expect(portions[0].lastPortion).toEqual(true); |
| 102 | + }); |
| 103 | + |
| 104 | + it('finds a letter that is in a text node with a letter after', function() { |
| 105 | + var elem = createParagraphWithTextNodes('a', 'bx', 'c'); |
| 106 | + iterateOverElement(elem, /b/g); |
| 107 | + var portions = this.wrapWord.firstCall.args[0]; |
| 108 | + |
| 109 | + expect(portions.length).toEqual(1); |
| 110 | + expect(portions[0].text).toEqual('b'); |
| 111 | + expect(portions[0].offset).toEqual(0); |
| 112 | + expect(portions[0].length).toEqual(1); |
| 113 | + expect(portions[0].lastPortion).toEqual(true); |
| 114 | + }); |
| 115 | + |
| 116 | + it('finds two letters that span over two text nodes', function() { |
| 117 | + var elem = createParagraphWithTextNodes('a', 'b', 'c'); |
| 118 | + iterateOverElement(elem, /bc/g); |
| 119 | + var portions = this.wrapWord.firstCall.args[0]; |
| 120 | + |
| 121 | + expect(portions.length).toEqual(2); |
| 122 | + expect(portions[0].text).toEqual('b'); |
| 123 | + expect(portions[0].lastPortion).toEqual(false); |
| 124 | + |
| 125 | + expect(portions[1].text).toEqual('c'); |
| 126 | + expect(portions[1].lastPortion).toEqual(true); |
| 127 | + }); |
| 128 | + |
| 129 | + it('finds three letters that span over three text nodes', function() { |
| 130 | + var elem = createParagraphWithTextNodes('a', 'b', 'c'); |
| 131 | + iterateOverElement(elem, /abc/g); |
| 132 | + var portions = this.wrapWord.firstCall.args[0]; |
| 133 | + |
| 134 | + expect(portions.length).toEqual(3); |
| 135 | + expect(portions[0].text).toEqual('a'); |
| 136 | + expect(portions[1].text).toEqual('b'); |
| 137 | + expect(portions[2].text).toEqual('c'); |
| 138 | + }); |
| 139 | + |
| 140 | + it('finds a word that is partially contained in two text nodes', function() { |
| 141 | + var elem = createParagraphWithTextNodes('a', 'bxx', 'xxe'); |
| 142 | + iterateOverElement(elem, /xxxx/g); |
| 143 | + var portions = this.wrapWord.firstCall.args[0]; |
| 144 | + |
| 145 | + expect(portions.length).toEqual(2); |
| 146 | + expect(portions[0].text).toEqual('xx'); |
| 147 | + expect(portions[0].offset).toEqual(1); |
| 148 | + expect(portions[0].length).toEqual(2); |
| 149 | + expect(portions[0].lastPortion).toEqual(false); |
| 150 | + |
| 151 | + expect(portions[1].text).toEqual('xx'); |
| 152 | + expect(portions[1].offset).toEqual(0); |
| 153 | + expect(portions[1].length).toEqual(2); |
| 154 | + expect(portions[1].lastPortion).toEqual(true); |
| 155 | + }); |
| 156 | + |
| 157 | + }); |
| 158 | + |
| 159 | + describe('wrapWord', function() { |
| 160 | + |
| 161 | + it('wraps a word in a single text node', function() { |
| 162 | + var elem = $('<div>Some juice.</div>')[0]; |
| 163 | + var matches = highlightText.find(elem, /juice/g); |
| 164 | + var range = highlightText.getRange(elem); |
| 165 | + highlightText.iterate(range, matches); |
| 166 | + expect(range.commonAncestorContainer.outerHTML) |
| 167 | + .toEqual('<div>Some <span data-awesome="crazy">juice</span>.</div>') |
| 168 | + }); |
| 169 | + |
| 170 | + it('wraps a word with a partial <em> element', function() { |
| 171 | + var elem = $('<div>Some jui<em>ce</em>.</div>')[0]; |
| 172 | + var matches = highlightText.find(elem, /juice/g); |
| 173 | + var range = highlightText.getRange(elem); |
| 174 | + highlightText.iterate(range, matches); |
| 175 | + expect(range.commonAncestorContainer.outerHTML) |
| 176 | + .toEqual('<div>Some <span data-awesome="crazy">jui</span><em><span data-awesome="crazy">ce</span></em>.</div>') |
| 177 | + }); |
| 178 | + }); |
| 179 | + |
| 180 | +}); |
0 commit comments