| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright 2013 The Polymer Authors. All rights reserved. | |
| 3 * Use of this source code is governed by a BSD-style | |
| 4 * license that can be found in the LICENSE file. | |
| 5 */ | |
| 6 | |
| 7 (function(scope) { | |
| 8 | |
| 9 var IMPORT_LINK_TYPE = 'import'; | |
| 10 | |
| 11 // highlander object for parsing a document tree | |
| 12 | |
| 13 var importParser = { | |
| 14 selectors: [ | |
| 15 'link[rel=' + IMPORT_LINK_TYPE + ']', | |
| 16 'link[rel=stylesheet]', | |
| 17 'style', | |
| 18 'script:not([type])', | |
| 19 'script[type="text/javascript"]' | |
| 20 ], | |
| 21 map: { | |
| 22 link: 'parseLink', | |
| 23 script: 'parseScript', | |
| 24 style: 'parseGeneric' | |
| 25 }, | |
| 26 parse: function(inDocument) { | |
| 27 if (!inDocument.__importParsed) { | |
| 28 // only parse once | |
| 29 inDocument.__importParsed = true; | |
| 30 // all parsable elements in inDocument (depth-first pre-order traversal) | |
| 31 var elts = inDocument.querySelectorAll(importParser.selectors); | |
| 32 // for each parsable node type, call the mapped parsing method | |
| 33 forEach(elts, function(e) { | |
| 34 importParser[importParser.map[e.localName]](e); | |
| 35 }); | |
| 36 } | |
| 37 }, | |
| 38 parseLink: function(linkElt) { | |
| 39 if (isDocumentLink(linkElt)) { | |
| 40 if (linkElt.content) { | |
| 41 importParser.parse(linkElt.content); | |
| 42 } | |
| 43 } else { | |
| 44 this.parseGeneric(linkElt); | |
| 45 } | |
| 46 }, | |
| 47 parseGeneric: function(elt) { | |
| 48 if (needsMainDocumentContext(elt)) { | |
| 49 document.head.appendChild(elt); | |
| 50 } | |
| 51 }, | |
| 52 parseScript: function(scriptElt) { | |
| 53 if (needsMainDocumentContext(scriptElt)) { | |
| 54 // acquire code to execute | |
| 55 var code = (scriptElt.__resource || scriptElt.textContent).trim(); | |
| 56 if (code) { | |
| 57 // calculate source map hint | |
| 58 var moniker = scriptElt.__nodeUrl; | |
| 59 if (!moniker) { | |
| 60 var moniker = scope.path.documentUrlFromNode(scriptElt); | |
| 61 // there could be more than one script this url | |
| 62 var tag = '[' + Math.floor((Math.random()+1)*1000) + ']'; | |
| 63 // TODO(sjmiles): Polymer hack, should be pluggable if we need to allo
w | |
| 64 // this sort of thing | |
| 65 var matches = code.match(/Polymer\(['"]([^'"]*)/); | |
| 66 tag = matches && matches[1] || tag; | |
| 67 // tag the moniker | |
| 68 moniker += '/' + tag + '.js'; | |
| 69 } | |
| 70 // source map hint | |
| 71 code += "\n//# sourceURL=" + moniker + "\n"; | |
| 72 // evaluate the code | |
| 73 eval.call(window, code); | |
| 74 } | |
| 75 } | |
| 76 } | |
| 77 }; | |
| 78 | |
| 79 var forEach = Array.prototype.forEach.call.bind(Array.prototype.forEach); | |
| 80 | |
| 81 function isDocumentLink(elt) { | |
| 82 return elt.localName === 'link' | |
| 83 && elt.getAttribute('rel') === IMPORT_LINK_TYPE; | |
| 84 } | |
| 85 | |
| 86 function needsMainDocumentContext(node) { | |
| 87 // nodes can be moved to the main document: | |
| 88 // if they are in a tree but not in the main document and not children of <ele
ment> | |
| 89 return node.parentNode && !inMainDocument(node) | |
| 90 && !isElementElementChild(node); | |
| 91 } | |
| 92 | |
| 93 function inMainDocument(elt) { | |
| 94 return elt.ownerDocument === document || | |
| 95 // TODO(sjmiles): ShadowDOMPolyfill intrusion | |
| 96 elt.ownerDocument.impl === document; | |
| 97 } | |
| 98 | |
| 99 function isElementElementChild(elt) { | |
| 100 return elt.parentNode && elt.parentNode.localName === 'element'; | |
| 101 } | |
| 102 | |
| 103 // exports | |
| 104 | |
| 105 scope.parser = importParser; | |
| 106 | |
| 107 })(HTMLImports); | |
| OLD | NEW |