|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright (c) 2016 The Polymer Project Authors. All rights reserved. |
| 4 | + * This code may only be used under the BSD style license found at |
| 5 | + * http://polymer.github.io/LICENSE.txt |
| 6 | + * The complete set of authors may be found at |
| 7 | + * http://polymer.github.io/AUTHORS.txt |
| 8 | + * The complete set of contributors may be found at |
| 9 | + * http://polymer.github.io/CONTRIBUTORS.txt |
| 10 | + * Code distributed by Google as part of the polymer project is also |
| 11 | + * subject to an additional IP rights grant found at |
| 12 | + * http://polymer.github.io/PATENTS.txt |
| 13 | + */ |
| 14 | + |
| 15 | +import {assert} from 'chai'; |
| 16 | + |
| 17 | +import {Analyzer} from '../../analyzer'; |
| 18 | +import {HtmlVisitor} from '../../html/html-document'; |
| 19 | +import {HtmlCustomElementReferenceScanner, HtmlElementReferenceScanner} from '../../html/html-element-reference-scanner'; |
| 20 | +import {HtmlParser} from '../../html/html-parser'; |
| 21 | +import {SourceRange} from '../../model/model'; |
| 22 | +import {WarningPrinter} from '../../warning/warning-printer'; |
| 23 | + |
| 24 | +suite('HtmlElementReferenceScanner', () => { |
| 25 | + |
| 26 | + suite('scan()', () => { |
| 27 | + let scanner: HtmlElementReferenceScanner; |
| 28 | + |
| 29 | + setup(() => { |
| 30 | + scanner = new HtmlElementReferenceScanner(); |
| 31 | + }); |
| 32 | + |
| 33 | + test('finds element references', async() => { |
| 34 | + const contents = `<html><head></head> |
| 35 | + <body> |
| 36 | + <div>Foo</div> |
| 37 | + <x-foo></x-foo> |
| 38 | + <div> |
| 39 | + <x-bar></x-bar> |
| 40 | + </div> |
| 41 | + </body></html>`; |
| 42 | + |
| 43 | + const document = new HtmlParser().parse(contents, 'test-document.html'); |
| 44 | + let visit = async(visitor: HtmlVisitor) => document.visit([visitor]); |
| 45 | + |
| 46 | + const features = await scanner.scan(document, visit); |
| 47 | + |
| 48 | + assert.deepEqual( |
| 49 | + features.map(f => f.tagName), |
| 50 | + ['html', 'head', 'body', 'div', 'x-foo', 'div', 'x-bar']); |
| 51 | + }); |
| 52 | + }); |
| 53 | +}); |
| 54 | + |
| 55 | +suite('HtmlCustomElementReferenceScanner', () => { |
| 56 | + |
| 57 | + suite('scan()', () => { |
| 58 | + let scanner: HtmlCustomElementReferenceScanner; |
| 59 | + let contents = ''; |
| 60 | + const loader = {canLoad: () => true, load: () => Promise.resolve(contents)}; |
| 61 | + const warningPrinter = new WarningPrinter( |
| 62 | + null as any, {analyzer: new Analyzer({urlLoader: loader})}); |
| 63 | + |
| 64 | + async function getUnderlinedText(sourceRange: SourceRange|undefined) { |
| 65 | + if (!sourceRange) { |
| 66 | + return 'No source range produced'; |
| 67 | + } |
| 68 | + return '\n' + await warningPrinter.getUnderlinedText(sourceRange); |
| 69 | + } |
| 70 | + |
| 71 | + setup(() => { |
| 72 | + scanner = new HtmlCustomElementReferenceScanner(); |
| 73 | + }); |
| 74 | + |
| 75 | + test('finds custom element references', async() => { |
| 76 | + contents = `<html><body> |
| 77 | + <div>Foo</div> |
| 78 | + <x-foo a=5 b="test" c></x-foo> |
| 79 | + <div> |
| 80 | + <x-bar></x-bar> |
| 81 | + </div> |
| 82 | + <h1>Bar</h1> |
| 83 | + </body></html>`; |
| 84 | + |
| 85 | + const document = new HtmlParser().parse(contents, 'test-document.html'); |
| 86 | + let visit = async(visitor: HtmlVisitor) => document.visit([visitor]); |
| 87 | + |
| 88 | + const features = await scanner.scan(document, visit); |
| 89 | + |
| 90 | + assert.deepEqual(features.map(f => f.tagName), ['x-foo', 'x-bar']); |
| 91 | + |
| 92 | + assert.deepEqual( |
| 93 | + features[0].attributes.map(a => [a.name, a.value]), |
| 94 | + [['a', '5'], ['b', 'test'], ['c', '']]); |
| 95 | + |
| 96 | + const sourceRanges = await Promise.all( |
| 97 | + features.map(async f => await getUnderlinedText(f.sourceRange))); |
| 98 | + |
| 99 | + assert.deepEqual(sourceRanges, [ |
| 100 | + ` |
| 101 | + <x-foo a=5 b="test" c></x-foo> |
| 102 | + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`, |
| 103 | + ` |
| 104 | + <x-bar></x-bar> |
| 105 | + ~~~~~~~~~~~~~~~` |
| 106 | + ]); |
| 107 | + |
| 108 | + const attrRanges = await Promise.all(features.map( |
| 109 | + async f => await Promise.all(f.attributes.map( |
| 110 | + async a => await getUnderlinedText(a.sourceRange))))); |
| 111 | + |
| 112 | + assert.deepEqual(attrRanges, [ |
| 113 | + [ |
| 114 | + ` |
| 115 | + <x-foo a=5 b="test" c></x-foo> |
| 116 | + ~~~`, |
| 117 | + ` |
| 118 | + <x-foo a=5 b="test" c></x-foo> |
| 119 | + ~~~~~~~~`, |
| 120 | + ` |
| 121 | + <x-foo a=5 b="test" c></x-foo> |
| 122 | + ~` |
| 123 | + ], |
| 124 | + [] |
| 125 | + ]); |
| 126 | + |
| 127 | + const attrNameRanges = await Promise.all(features.map( |
| 128 | + async f => await Promise.all(f.attributes.map( |
| 129 | + async a => await getUnderlinedText(a.nameSourceRange))))); |
| 130 | + |
| 131 | + assert.deepEqual(attrNameRanges, [ |
| 132 | + [ |
| 133 | + ` |
| 134 | + <x-foo a=5 b="test" c></x-foo> |
| 135 | + ~`, |
| 136 | + ` |
| 137 | + <x-foo a=5 b="test" c></x-foo> |
| 138 | + ~`, |
| 139 | + ` |
| 140 | + <x-foo a=5 b="test" c></x-foo> |
| 141 | + ~` |
| 142 | + ], |
| 143 | + [] |
| 144 | + ]); |
| 145 | + |
| 146 | + const attrValueRanges = await Promise.all(features.map( |
| 147 | + async f => await Promise.all(f.attributes.map( |
| 148 | + async a => await getUnderlinedText(a.valueSourceRange))))); |
| 149 | + |
| 150 | + assert.deepEqual(attrValueRanges, [ |
| 151 | + [ |
| 152 | + ` |
| 153 | + <x-foo a=5 b="test" c></x-foo> |
| 154 | + ~`, |
| 155 | + ` |
| 156 | + <x-foo a=5 b="test" c></x-foo> |
| 157 | + ~~~~~~`, |
| 158 | + `No source range produced` |
| 159 | + ], |
| 160 | + [] |
| 161 | + ]); |
| 162 | + }); |
| 163 | + |
| 164 | + }); |
| 165 | + |
| 166 | +}); |
0 commit comments