Skip to content
This repository was archived by the owner on Jan 4, 2019. It is now read-only.

Commit 7d5256f

Browse files
committed
Handle indirect prototype references in Polymer invocation
Fixes #82 Element name could not be inferred error on paper-button
1 parent ece25a0 commit 7d5256f

File tree

4 files changed

+47
-30
lines changed

4 files changed

+47
-30
lines changed

lib/constants.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ module.exports = {
2222
JS_SRC: JS.split(',').map(function(s){ return s + '[src]'; }).join(','),
2323
JS_INLINE: JS.split(',').map(function(s) { return s + ':not([src])'; }).join(','),
2424
CSS: 'style:not([type]), style[type="text/css"]',
25-
// Output match is [ 'Polymer(', NAME_OF_ELEMENT OR undefined, '{' OR ')' ]
26-
POLYMER_INVOCATION: /Polymer\(([^,{]+)?(?:,\s*)?({|\))/
25+
// Output match is [ 'Polymer(', NAME_OF_ELEMENT OR undefined, ',', { or )
26+
POLYMER_INVOCATION: /Polymer\(([^,{]+)?(,\s*)?({|\))/
2727
};

lib/utils.js

+18
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
var path = require('path');
77

8+
var constants = require('./constants.js');
89
module.exports = {
910
// directly update the textnode child of <style>
1011
// equivalent to <style>.textContent
@@ -37,5 +38,22 @@ module.exports = {
3738
inpath = inpath.split(path.sep).join('/');
3839
}
3940
return inpath;
41+
},
42+
processPolymerInvocation: function(elementName, invocation) {
43+
var name = invocation[1] || '';
44+
var split = invocation[2] || '';
45+
var trailing = invocation[3];
46+
var nameIsString = /^['"]/.test(name);
47+
if (!split) {
48+
// assume "name" is actually the prototype if it is not a string literal
49+
if (!name || (name && !nameIsString)) {
50+
trailing = name + trailing;
51+
name = '\'' + elementName + '\'';
52+
}
53+
if (trailing !== ')') {
54+
split = ',';
55+
}
56+
}
57+
return 'Polymer(' + name + split + trailing;
4058
}
4159
};

lib/vulcan.js

+3-10
Original file line numberDiff line numberDiff line change
@@ -226,16 +226,9 @@ function handleMainDocument() {
226226
var parentElement = el.closest('polymer-element').get(0);
227227
if (parentElement) {
228228
var match = constants.POLYMER_INVOCATION.exec(content);
229-
// skip Polymer() calls that have the tag name
230-
if (match && !match[1]) {
231-
// get element name
232-
var name = $(parentElement).attr('name');
233-
// build the named Polymer invocation
234-
var namedInvocation = 'Polymer(\'' + name + '\'' + (match[2] === '{' ? ',{' : ')');
235-
content = content.replace(match[0], namedInvocation);
236-
if (options.verbose) {
237-
console.log(match[0], '->', namedInvocation);
238-
}
229+
if (match) {
230+
var invocation = utils.processPolymerInvocation(elementName, match);
231+
content.replace(match[0], invocation);
239232
setTextContent(el, content);
240233
}
241234
}

test/test.js

+24-18
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,6 @@ suite('constants', function() {
4141
assert('url("foo.html")'.match(url), 'double quote');
4242
});
4343

44-
test('Polymer Invocation', function() {
45-
var polymer = constants.POLYMER_INVOCATION;
46-
47-
function test(invocation, expected, msg) {
48-
var matches = polymer.exec(invocation);
49-
assert(matches, 'polymer invocation found');
50-
if (expected) {
51-
var name = String(matches[1]).replace(/['"]/g, '');
52-
assert.equal(name, expected);
53-
}
54-
}
55-
56-
test('Polymer(\'core-input\', {})', 'core-input', 'full');
57-
test('Polymer()', null, 'none');
58-
test('Polymer({})', null, 'partial');
59-
60-
});
61-
6244
});
6345

6446
suite('Path Resolver', function() {
@@ -158,4 +140,28 @@ suite('constants', function() {
158140
});
159141

160142
});
143+
144+
suite('Utils', function() {
145+
var utils = require('../lib/utils.js');
146+
147+
test('Polymer Invocation', function() {
148+
var polymer = constants.POLYMER_INVOCATION;
149+
150+
function test(invocation, expected, msg) {
151+
var matches = polymer.exec(invocation);
152+
assert(matches, 'polymer invocation found');
153+
var replacement = utils.processPolymerInvocation('core-input', matches);
154+
var actual = invocation.replace(matches[0], replacement);
155+
assert.strictEqual(actual, expected, msg);
156+
}
157+
158+
test('Polymer(\'core-input\', {})', 'Polymer(\'core-input\', {})', 'full');
159+
test('Polymer(\'core-input\')', 'Polymer(\'core-input\')', 'name-only');
160+
test('Polymer()', 'Polymer(\'core-input\')', 'none');
161+
test('Polymer({})', 'Polymer(\'core-input\',{})', 'object-only');
162+
test('Polymer(p)', 'Polymer(\'core-input\',p)', 'indirect');
163+
164+
});
165+
166+
});
161167
});

0 commit comments

Comments
 (0)