OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Custom binding for the declarativeContent API. | 5 // Custom binding for the declarativeContent API. |
6 | 6 |
7 var binding = require('binding').Binding.create('declarativeContent'); | 7 var binding = require('binding').Binding.create('declarativeContent'); |
8 | 8 |
9 var utils = require('utils'); | 9 var utils = require('utils'); |
10 var validate = require('schemaUtils').validate; | 10 var validate = require('schemaUtils').validate; |
| 11 var canonicalizeCompoundSelector = |
| 12 requireNative('css_natives').CanonicalizeCompoundSelector; |
11 | 13 |
12 binding.registerCustomHook( function(api) { | 14 binding.registerCustomHook( function(api) { |
13 var declarativeContent = api.compiledApi; | 15 var declarativeContent = api.compiledApi; |
14 | 16 |
15 // Returns the schema definition of type |typeId| defined in |namespace|. | 17 // Returns the schema definition of type |typeId| defined in |namespace|. |
16 function getSchema(typeId) { | 18 function getSchema(typeId) { |
17 return utils.lookup(api.schema.types, | 19 return utils.lookup(api.schema.types, |
18 'id', | 20 'id', |
19 'declarativeContent.' + typeId); | 21 'declarativeContent.' + typeId); |
20 } | 22 } |
21 | 23 |
22 // Helper function for the constructor of concrete datatypes of the | 24 // Helper function for the constructor of concrete datatypes of the |
23 // declarative content API. | 25 // declarative content API. |
24 // Makes sure that |this| contains the union of parameters and | 26 // Makes sure that |this| contains the union of parameters and |
25 // {'instanceType': 'declarativeContent.' + typeId} and validates the | 27 // {'instanceType': 'declarativeContent.' + typeId} and validates the |
26 // generated union dictionary against the schema for |typeId|. | 28 // generated union dictionary against the schema for |typeId|. |
27 function setupInstance(instance, parameters, typeId) { | 29 function setupInstance(instance, parameters, typeId) { |
28 for (var key in parameters) { | 30 for (var key in parameters) { |
29 if ($Object.hasOwnProperty(parameters, key)) { | 31 if ($Object.hasOwnProperty(parameters, key)) { |
30 instance[key] = parameters[key]; | 32 instance[key] = parameters[key]; |
31 } | 33 } |
32 } | 34 } |
33 instance.instanceType = 'declarativeContent.' + typeId; | 35 instance.instanceType = 'declarativeContent.' + typeId; |
34 var schema = getSchema(typeId); | 36 var schema = getSchema(typeId); |
35 validate([instance], [schema]); | 37 validate([instance], [schema]); |
36 } | 38 } |
37 | 39 |
| 40 function canonicalizeCssSelectors(selectors) { |
| 41 for (var i = 0; i < selectors.length; i++) { |
| 42 var canonicalizedSelector = canonicalizeCompoundSelector(selectors[i]); |
| 43 if (canonicalizedSelector == '') { |
| 44 throw new Error( |
| 45 'Element of \'css\' array must be a ' + |
| 46 'list of valid compound selectors: ' + |
| 47 selectors[i]); |
| 48 } |
| 49 selectors[i] = canonicalizedSelector; |
| 50 } |
| 51 } |
| 52 |
38 // Setup all data types for the declarative content API. | 53 // Setup all data types for the declarative content API. |
39 declarativeContent.PageStateMatcher = function(parameters) { | 54 declarativeContent.PageStateMatcher = function(parameters) { |
40 setupInstance(this, parameters, 'PageStateMatcher'); | 55 setupInstance(this, parameters, 'PageStateMatcher'); |
| 56 if ($Object.hasOwnProperty(this, 'css')) { |
| 57 canonicalizeCssSelectors(this.css); |
| 58 } |
41 }; | 59 }; |
42 declarativeContent.ShowPageAction = function(parameters) { | 60 declarativeContent.ShowPageAction = function(parameters) { |
43 setupInstance(this, parameters, 'ShowPageAction'); | 61 setupInstance(this, parameters, 'ShowPageAction'); |
44 }; | 62 }; |
45 }); | 63 }); |
46 | 64 |
47 exports.binding = binding.generate(); | 65 exports.binding = binding.generate(); |
OLD | NEW |