| <!DOCTYPE html> |
| <!-- |
| Copyright (C) 2016 The Android Open Source Project |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
| --> |
| |
| <meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes"> |
| <title>gr-editable-label</title> |
| |
| <script src="../../../bower_components/webcomponentsjs/webcomponents-lite.min.js"></script> |
| <script src="../../../bower_components/web-component-tester/browser.js"></script> |
| |
| <link rel="import" href="gr-formatted-text.html"> |
| |
| <script>void(0);</script> |
| |
| <test-fixture id="basic"> |
| <template> |
| <gr-formatted-text></gr-formatted-text> |
| </template> |
| </test-fixture> |
| |
| <script> |
| suite('gr-formatted-text tests', function() { |
| var element; |
| var sandbox; |
| |
| function assertBlock(result, index, type, text) { |
| assert.equal(result[index].type, type); |
| assert.equal(result[index].text, text); |
| } |
| |
| function assertListBlock(result, resultIndex, itemIndex, text) { |
| assert.equal(result[resultIndex].type, 'list'); |
| assert.equal(result[resultIndex].items[itemIndex], text); |
| } |
| |
| setup(function() { |
| element = fixture('basic'); |
| sandbox = sinon.sandbox.create(); |
| }); |
| |
| teardown(function() { |
| sandbox.restore(); |
| }); |
| |
| test('parse null undefined and empty', function() { |
| assert.lengthOf(element._computeBlocks(null), 0); |
| assert.lengthOf(element._computeBlocks(undefined), 0); |
| assert.lengthOf(element._computeBlocks(''), 0); |
| }); |
| |
| test('parse simple', function() { |
| var comment = 'Para1'; |
| var result = element._computeBlocks(comment); |
| assert.lengthOf(result, 1); |
| assertBlock(result, 0, 'paragraph', comment); |
| }); |
| |
| test('parse multiline para', function() { |
| var comment = 'Para 1\nStill para 1'; |
| var result = element._computeBlocks(comment); |
| assert.lengthOf(result, 1); |
| assertBlock(result, 0, 'paragraph', comment); |
| }); |
| |
| test('parse para break', function() { |
| var comment = 'Para 1\n\nPara 2\n\nPara 3'; |
| var result = element._computeBlocks(comment); |
| assert.lengthOf(result, 3); |
| assertBlock(result, 0, 'paragraph', 'Para 1'); |
| assertBlock(result, 1, 'paragraph', 'Para 2'); |
| assertBlock(result, 2, 'paragraph', 'Para 3'); |
| }); |
| |
| test('parse quote', function() { |
| var comment = '> Quote text'; |
| var result = element._computeBlocks(comment); |
| assert.lengthOf(result, 1); |
| assert.equal(result[0].type, 'quote'); |
| assert.lengthOf(result[0].blocks, 1); |
| assertBlock(result[0].blocks, 0, 'paragraph', 'Quote text'); |
| }); |
| |
| test('parse quote lead space', function() { |
| var comment = ' > Quote text'; |
| var result = element._computeBlocks(comment); |
| assert.lengthOf(result, 1); |
| assert.equal(result[0].type, 'quote'); |
| assert.lengthOf(result[0].blocks, 1); |
| assertBlock(result[0].blocks, 0, 'paragraph', 'Quote text'); |
| }); |
| |
| test('parse excludes empty', function() { |
| var comment = 'Para 1\n\n\n\nPara 2'; |
| var result = element._computeBlocks(comment); |
| assert.lengthOf(result, 2); |
| assertBlock(result, 0, 'paragraph', 'Para 1'); |
| assertBlock(result, 1, 'paragraph', 'Para 2'); |
| }); |
| |
| test('parse multiline quote', function() { |
| var comment = '> Quote line 1\n> Quote line 2\n > Quote line 3\n'; |
| var result = element._computeBlocks(comment); |
| assert.lengthOf(result, 1); |
| assert.equal(result[0].type, 'quote'); |
| assert.lengthOf(result[0].blocks, 1); |
| assertBlock(result[0].blocks, 0, 'paragraph', |
| 'Quote line 1\nQuote line 2\nQuote line 3\n'); |
| }); |
| |
| test('parse pre', function() { |
| var comment = ' Four space indent.'; |
| var result = element._computeBlocks(comment); |
| assert.lengthOf(result, 1); |
| assertBlock(result, 0, 'pre', comment); |
| }); |
| |
| test('parse one space pre', function() { |
| var comment = ' One space indent.\n Another line.'; |
| var result = element._computeBlocks(comment); |
| assert.lengthOf(result, 1); |
| assertBlock(result, 0, 'pre', comment); |
| }); |
| |
| test('parse tab pre', function() { |
| var comment = '\tOne tab indent.\n\tAnother line.\n Yet another!'; |
| var result = element._computeBlocks(comment); |
| assert.lengthOf(result, 1); |
| assertBlock(result, 0, 'pre', comment); |
| }); |
| |
| test('parse intermediate leading whitespace pre', function() { |
| var comment = 'No indent.\n\tNonzero indent.\nNo indent again.'; |
| var result = element._computeBlocks(comment); |
| assert.lengthOf(result, 1); |
| assertBlock(result, 0, 'pre', comment); |
| }); |
| |
| test('parse star list', function() { |
| var comment = '* Item 1\n* Item 2\n* Item 3'; |
| var result = element._computeBlocks(comment); |
| assert.lengthOf(result, 1); |
| assertListBlock(result, 0, 0, 'Item 1'); |
| assertListBlock(result, 0, 1, 'Item 2'); |
| assertListBlock(result, 0, 2, 'Item 3'); |
| }); |
| |
| test('parse dash list', function() { |
| var comment = '- Item 1\n- Item 2\n- Item 3'; |
| var result = element._computeBlocks(comment); |
| assert.lengthOf(result, 1); |
| assertListBlock(result, 0, 0, 'Item 1'); |
| assertListBlock(result, 0, 1, 'Item 2'); |
| assertListBlock(result, 0, 2, 'Item 3'); |
| }); |
| |
| test('parse mixed list', function() { |
| var comment = '- Item 1\n* Item 2\n- Item 3\n* Item 4'; |
| var result = element._computeBlocks(comment); |
| assert.lengthOf(result, 1); |
| assertListBlock(result, 0, 0, 'Item 1'); |
| assertListBlock(result, 0, 1, 'Item 2'); |
| assertListBlock(result, 0, 2, 'Item 3'); |
| assertListBlock(result, 0, 3, 'Item 4'); |
| }); |
| |
| test('parse mixed block types', function() { |
| var comment = 'Paragraph\nacross\na\nfew\nlines.' + |
| '\n\n' + |
| '> Quote\n> across\n> not many lines.' + |
| '\n\n' + |
| 'Another paragraph' + |
| '\n\n' + |
| '* Series\n* of\n* list\n* items' + |
| '\n\n' + |
| 'Yet another paragraph' + |
| '\n\n' + |
| '\tPreformatted text.' + |
| '\n\n' + |
| 'Parting words.'; |
| var result = element._computeBlocks(comment); |
| assert.lengthOf(result, 7); |
| assertBlock(result, 0, 'paragraph', 'Paragraph\nacross\na\nfew\nlines.'); |
| |
| assert.equal(result[1].type, 'quote'); |
| assert.lengthOf(result[1].blocks, 1); |
| assertBlock(result[1].blocks, 0, 'paragraph', |
| 'Quote\nacross\nnot many lines.'); |
| |
| assertBlock(result, 2, 'paragraph', 'Another paragraph'); |
| assertListBlock(result, 3, 0, 'Series'); |
| assertListBlock(result, 3, 1, 'of'); |
| assertListBlock(result, 3, 2, 'list'); |
| assertListBlock(result, 3, 3, 'items'); |
| assertBlock(result, 4, 'paragraph', 'Yet another paragraph'); |
| assertBlock(result, 5, 'pre', '\tPreformatted text.'); |
| assertBlock(result, 6, 'paragraph', 'Parting words.'); |
| }); |
| |
| test('bullet list 1', function() { |
| var comment = 'A\n\n* line 1\n* 2nd line'; |
| var result = element._computeBlocks(comment); |
| assert.lengthOf(result, 2); |
| assertBlock(result, 0, 'paragraph', 'A'); |
| assertListBlock(result, 1, 0, 'line 1'); |
| assertListBlock(result, 1, 1, '2nd line'); |
| }); |
| |
| test('bullet list 2', function() { |
| var comment = 'A\n\n* line 1\n* 2nd line\n\nB'; |
| var result = element._computeBlocks(comment); |
| assert.lengthOf(result, 3); |
| assertBlock(result, 0, 'paragraph', 'A'); |
| assertListBlock(result, 1, 0, 'line 1'); |
| assertListBlock(result, 1, 1, '2nd line'); |
| assertBlock(result, 2, 'paragraph', 'B'); |
| }); |
| |
| test('bullet list 3', function() { |
| var comment = '* line 1\n* 2nd line\n\nB'; |
| var result = element._computeBlocks(comment); |
| assert.lengthOf(result, 2); |
| assertListBlock(result, 0, 0, 'line 1'); |
| assertListBlock(result, 0, 1, '2nd line'); |
| assertBlock(result, 1, 'paragraph', 'B'); |
| }); |
| |
| test('bullet list 4', function() { |
| var comment = 'To see this bug, you have to:\n' + |
| '* Be on IMAP or EAS (not on POP)\n' + |
| '* Be very unlucky\n'; |
| var result = element._computeBlocks(comment); |
| assert.lengthOf(result, 2); |
| assertBlock(result, 0, 'paragraph', 'To see this bug, you have to:'); |
| assertListBlock(result, 1, 0, 'Be on IMAP or EAS (not on POP)'); |
| assertListBlock(result, 1, 1, 'Be very unlucky'); |
| }); |
| |
| test('bullet list 5', function() { |
| var comment = 'To see this bug,\n' + |
| 'you have to:\n' + |
| '* Be on IMAP or EAS (not on POP)\n' + |
| '* Be very unlucky\n'; |
| var result = element._computeBlocks(comment); |
| assert.lengthOf(result, 2); |
| assertBlock(result, 0, 'paragraph', 'To see this bug, you have to:'); |
| assertListBlock(result, 1, 0, 'Be on IMAP or EAS (not on POP)'); |
| assertListBlock(result, 1, 1, 'Be very unlucky'); |
| }); |
| |
| test('dash list 1', function() { |
| var comment = 'A\n\n- line 1\n- 2nd line'; |
| var result = element._computeBlocks(comment); |
| assert.lengthOf(result, 2); |
| assertBlock(result, 0, 'paragraph', 'A'); |
| assertListBlock(result, 1, 0, 'line 1'); |
| assertListBlock(result, 1, 1, '2nd line'); |
| }); |
| |
| test('dash list 2', function() { |
| var comment = 'A\n\n- line 1\n- 2nd line\n\nB'; |
| var result = element._computeBlocks(comment); |
| assert.lengthOf(result, 3); |
| assertBlock(result, 0, 'paragraph', 'A'); |
| assertListBlock(result, 1, 0, 'line 1'); |
| assertListBlock(result, 1, 1, '2nd line'); |
| assertBlock(result, 2, 'paragraph', 'B'); |
| }); |
| |
| test('dash list 3', function() { |
| var comment = '- line 1\n- 2nd line\n\nB'; |
| var result = element._computeBlocks(comment); |
| assert.lengthOf(result, 2); |
| assertListBlock(result, 0, 0, 'line 1'); |
| assertListBlock(result, 0, 1, '2nd line'); |
| assertBlock(result, 1, 'paragraph', 'B'); |
| }); |
| |
| test('pre format 1', function() { |
| var comment = 'A\n\n This is pre\n formatted'; |
| var result = element._computeBlocks(comment); |
| assert.lengthOf(result, 2); |
| assertBlock(result, 0, 'paragraph', 'A'); |
| assertBlock(result, 1, 'pre', ' This is pre\n formatted'); |
| }); |
| |
| test('pre format 2', function() { |
| var comment = 'A\n\n This is pre\n formatted\n\nbut this is not'; |
| var result = element._computeBlocks(comment); |
| assert.lengthOf(result, 3); |
| assertBlock(result, 0, 'paragraph', 'A'); |
| assertBlock(result, 1, 'pre', ' This is pre\n formatted'); |
| assertBlock(result, 2, 'paragraph', 'but this is not'); |
| }); |
| |
| test('pre format 3', function() { |
| var comment = 'A\n\n Q\n <R>\n S\n\nB'; |
| var result = element._computeBlocks(comment); |
| assert.lengthOf(result, 3); |
| assertBlock(result, 0, 'paragraph', 'A'); |
| assertBlock(result, 1, 'pre', ' Q\n <R>\n S'); |
| assertBlock(result, 2, 'paragraph', 'B'); |
| }); |
| |
| test('pre format 4', function() { |
| var comment = ' Q\n <R>\n S\n\nB'; |
| var result = element._computeBlocks(comment); |
| assert.lengthOf(result, 2); |
| assertBlock(result, 0, 'pre', ' Q\n <R>\n S'); |
| assertBlock(result, 1, 'paragraph', 'B'); |
| }); |
| |
| test('quote 1', function() { |
| var comment = '> I\'m happy\n > with quotes!\n\nSee above.'; |
| var result = element._computeBlocks(comment); |
| assert.lengthOf(result, 2); |
| assert.equal(result[0].type, 'quote'); |
| assert.lengthOf(result[0].blocks, 1); |
| assertBlock(result[0].blocks, 0, 'paragraph', 'I\'m happy\nwith quotes!'); |
| assertBlock(result, 1, 'paragraph', 'See above.'); |
| }); |
| |
| test('quote 2', function() { |
| var comment = 'See this said:\n\n > a quoted\n > string block\n\nOK?'; |
| var result = element._computeBlocks(comment); |
| assert.lengthOf(result, 3); |
| assertBlock(result, 0, 'paragraph', 'See this said:'); |
| assert.equal(result[1].type, 'quote'); |
| assert.lengthOf(result[1].blocks, 1); |
| assertBlock(result[1].blocks, 0, 'paragraph', 'a quoted\nstring block'); |
| assertBlock(result, 2, 'paragraph', 'OK?'); |
| }); |
| |
| test('nested quotes', function() { |
| var comment = ' > > prior\n > \n > next\n'; |
| var result = element._computeBlocks(comment); |
| assert.lengthOf(result, 1); |
| assert.equal(result[0].type, 'quote'); |
| assert.lengthOf(result[0].blocks, 2); |
| assert.equal(result[0].blocks[0].type, 'quote'); |
| assert.lengthOf(result[0].blocks[0].blocks, 1); |
| assertBlock(result[0].blocks[0].blocks, 0, 'paragraph', 'prior'); |
| assertBlock(result[0].blocks, 1, 'paragraph', 'next\n'); |
| }); |
| |
| test('getTextContent', function() { |
| var comment = 'Paragraph\n\n pre\n\n* List\n* Of\n* Items\n\n> Quote'; |
| element.content = comment; |
| var result = element.getTextContent(); |
| var expected = 'Paragraph\n\n pre\n\nList\nOf\nItems\n\nQuote'; |
| assert.equal(result, expected); |
| }); |
| |
| test('_contentOrConfigChanged not called without config', function() { |
| var contentStub = sandbox.stub(element, '_contentChanged'); |
| var contentConfigStub = sandbox.stub(element, '_contentOrConfigChanged'); |
| element.content = 'some text'; |
| assert.isTrue(contentStub.called); |
| assert.isFalse(contentConfigStub.called); |
| }); |
| |
| test('_contentOrConfigChanged called with config', function() { |
| var contentStub = sandbox.stub(element, '_contentChanged'); |
| var contentConfigStub = sandbox.stub(element, '_contentOrConfigChanged'); |
| element.content = 'some text'; |
| element.config = {}; |
| assert.isTrue(contentStub.called); |
| assert.isTrue(contentConfigStub.called); |
| }); |
| }); |
| </script> |