| OLD | NEW |
| 1 <!-- | 1 <!-- |
| 2 // Copyright 2014 The Chromium Authors. All rights reserved. | 2 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 3 // Use of this source code is governed by a BSD-style license that can be | 3 // Use of this source code is governed by a BSD-style license that can be |
| 4 // found in the LICENSE file. | 4 // found in the LICENSE file. |
| 5 --> | 5 --> |
| 6 <import src="sky-binder.sky" as="binder" /> | 6 <import src="sky-binder.sky" as="binder" /> |
| 7 <script> | 7 <script> |
| 8 // TODO(esprehn): It would be nice if these were exposed by the platform so | 8 // TODO(esprehn): It would be nice if these were exposed by the platform so |
| 9 // the framework didn't need to hard code a list. | 9 // the framework didn't need to hard code a list. |
| 10 var defaultAttributesNames = new Set([ | 10 var globalAttributesNames = new Set([ |
| 11 'accesskey', | |
| 12 'alt', | |
| 13 'as', | |
| 14 'async', | |
| 15 'class', | 11 'class', |
| 16 'contenteditable', | 12 'contenteditable', |
| 17 'crossorigin', | |
| 18 'dir', | 13 'dir', |
| 19 'height', | |
| 20 'href', | |
| 21 'id', | 14 'id', |
| 22 'is', | |
| 23 'lang', | 15 'lang', |
| 24 'media', | |
| 25 'name', | |
| 26 'rel', | |
| 27 'select', | |
| 28 'sizes', | |
| 29 'spellcheck', | 16 'spellcheck', |
| 30 'src', | 17 'tabindex', |
| 31 'srcset', | |
| 32 'style', | 18 'style', |
| 33 'tabindex', | |
| 34 'title', | |
| 35 'type', | |
| 36 'width', | |
| 37 ]); | 19 ]); |
| 38 | 20 |
| 39 var attributeConverters = { | 21 var attributeConverters = { |
| 40 boolean: function(value) { | 22 boolean: function(value) { |
| 41 if (typeof value == 'string') | 23 if (typeof value == 'string') |
| 42 return value == 'true'; | 24 return value == 'true'; |
| 43 return !!value; | 25 return !!value; |
| 44 }, | 26 }, |
| 45 number: function(value) { | 27 number: function(value) { |
| 46 return Number(value); | 28 return Number(value); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 69 this.tagName = tagName; | 51 this.tagName = tagName; |
| 70 this.attributes = new Map(); | 52 this.attributes = new Map(); |
| 71 this.eventHandlers = new Map(); | 53 this.eventHandlers = new Map(); |
| 72 this.template = null; | 54 this.template = null; |
| 73 Object.preventExtensions(this); | 55 Object.preventExtensions(this); |
| 74 } | 56 } |
| 75 | 57 |
| 76 allowsAttribute(name) { | 58 allowsAttribute(name) { |
| 77 if (name.startsWith('data-')) | 59 if (name.startsWith('data-')) |
| 78 return true; | 60 return true; |
| 79 if (defaultAttributesNames.has(name)) | 61 if (globalAttributesNames.has(name)) |
| 80 return true; | 62 return true; |
| 81 if (this.attributes.has(name)) | 63 if (this.attributes.has(name)) |
| 82 return true; | 64 return true; |
| 83 return false; | 65 return false; |
| 84 } | 66 } |
| 85 | 67 |
| 86 defineAttribute(name, type) { | 68 defineAttribute(name, type) { |
| 87 var converter = attributeConverters[type]; | 69 var converter = attributeConverters[type]; |
| 88 | 70 |
| 89 if (!converter) { | 71 if (!converter) { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 112 | 94 |
| 113 addInstanceEventListeners(instance) { | 95 addInstanceEventListeners(instance) { |
| 114 for (var eventName of this.eventHandlers.keys()) { | 96 for (var eventName of this.eventHandlers.keys()) { |
| 115 instance.addEventListener(eventName, eventHandlerCallback); | 97 instance.addEventListener(eventName, eventHandlerCallback); |
| 116 } | 98 } |
| 117 } | 99 } |
| 118 } | 100 } |
| 119 | 101 |
| 120 var registrations = new Map(); | 102 var registrations = new Map(); |
| 121 | 103 |
| 122 function registerElement(tagName) { | 104 function registerElement(tagName, attributes) { |
| 123 if (registrations.has(tagName)) | 105 if (registrations.has(tagName)) |
| 124 throw new Error('tagName "' + tagName + '" registered twice.'); | 106 throw new Error('tagName "' + tagName + '" registered twice.'); |
| 125 var registration = new ElementRegistration(tagName); | 107 var registration = new ElementRegistration(tagName); |
| 108 for (var name in attributes) { |
| 109 registration.defineAttribute(name, attributes[name]); |
| 110 } |
| 126 registrations.set(tagName, registration); | 111 registrations.set(tagName, registration); |
| 127 return registration; | 112 return registration; |
| 128 } | 113 } |
| 129 | 114 |
| 130 function getRegistration(tagName) { | 115 function getRegistration(tagName) { |
| 131 return registrations.get(tagName); | 116 return registrations.get(tagName); |
| 132 } | 117 } |
| 133 | 118 |
| 134 function checkAttribute(tagName, attrName) { | 119 function checkAttribute(tagName, attrName) { |
| 135 var registration = getRegistration(tagName); | 120 var registration = getRegistration(tagName); |
| 136 | 121 |
| 137 if (!registration) | 122 if (!registration) |
| 138 return defaultAttributesNames.has(attrName); | 123 return globalAttributesNames.has(attrName); |
| 139 | 124 |
| 140 return registration.allowsAttribute(attrName); | 125 return registration.allowsAttribute(attrName); |
| 141 } | 126 } |
| 142 | 127 |
| 128 registerElement('img', { |
| 129 'width': 'number', |
| 130 'height': 'number', |
| 131 // TODO(esprehn): Sky probably doesn't want the crossorign attr. |
| 132 'crossorigin': 'string', |
| 133 'src': 'string', |
| 134 }); |
| 135 |
| 136 registerElement('import', { |
| 137 'as': 'string', |
| 138 'src': 'string', |
| 139 'async': 'boolean', |
| 140 }); |
| 141 |
| 142 registerElement('a', { |
| 143 'href': 'string', |
| 144 'async': 'boolean', |
| 145 }); |
| 146 |
| 147 registerElement('content', { |
| 148 'select': 'string', |
| 149 }); |
| 150 |
| 151 registerElement('style', { |
| 152 'media': 'string', |
| 153 }); |
| 154 |
| 155 registerElement('template', { |
| 156 'if': 'string', |
| 157 'repeat': 'string', |
| 158 }); |
| 159 |
| 143 module.exports = { | 160 module.exports = { |
| 144 registerElement: registerElement, | 161 registerElement: registerElement, |
| 145 getRegistration: getRegistration, | 162 getRegistration: getRegistration, |
| 146 checkAttribute: checkAttribute, | 163 checkAttribute: checkAttribute, |
| 147 }; | 164 }; |
| 148 </script> | 165 </script> |
| OLD | NEW |