Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
prototype_css_generator.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2#
3# Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
4# for details. All rights reserved. Use of this source code is governed by a
5# BSD-style license that can be found in the LICENSE file.
6"""Generates CssStyleDeclaration extension with all property getters and setters
7from css property definitions defined in WebKit."""
8
9import tempfile, os, re
10
11COMMENT_LINE_PREFIX = ' * '
12SOURCE_PATH = 'CSSPropertyNames.in'
13OUTPUT_FILE = 'prototype_css_properties.dart'
14
15# These are the properties that are supported on all Dart project supported
16# browsers as camelCased names on the CssStyleDeclaration.
17# Note that we do not use the MDN for compatibility info here.
18BROWSER_PATHS = [
19 'cssProperties.CSS21.txt', # Remove when we have samples from all browsers.
20 'cssProperties.ie9.txt',
21 'cssProperties.ie10.txt',
22 'cssProperties.ie11.txt',
23 'cssProperties.ff36.txt',
24 'cssProperties.chrome40.txt',
25 'cssProperties.safari-7.1.3.txt',
26 'cssProperties.mobileSafari-8.2.txt',
27 'cssProperties.iPad4Air.onGoogleSites.txt',
28]
29
30# Supported annotations for any specific CSS properties.
31annotated = {
32 'transition':
33 '''
34 @SupportedBrowser(SupportedBrowser.CHROME)
35 @SupportedBrowser(SupportedBrowser.FIREFOX)
36 @SupportedBrowser(SupportedBrowser.IE, '10')
37 @SupportedBrowser(SupportedBrowser.SAFARI)'''
38}
39
40
41class Error:
42
43 def __init__(self, message):
44 self.message = message
45
46 def __repr__(self):
47 return self.message
48
49
50def camelCaseName(name):
51 """Convert a CSS property name to a lowerCamelCase name."""
52 name = name.replace('-webkit-', '')
53 words = []
54 for word in name.split('-'):
55 if words:
56 words.append(word.title())
57 else:
58 words.append(word)
59 return ''.join(words)
60
61
62def dashifyName(camelName):
63
64 def fix(match):
65 return '-' + match.group(0).lower()
66
67 return re.sub(r'[A-Z]', fix, camelName)
68
69
70def isCommentLine(line):
71 return line.strip() == '' or line.startswith('#') or line.startswith('//')
72
73
74def readCssProperties(filename):
75 data = open(filename).readlines()
76 data = sorted([d.strip() for d in set(data) if not isCommentLine(d)])
77 return data
78
79
80def main():
81 data = open(SOURCE_PATH).readlines()
82 data = [d.strip() for d in data if not isCommentLine(d) and not '=' in d]
83
84 browser_props = [set(readCssProperties(file)) for file in BROWSER_PATHS]
85 universal_properties = set.intersection(*browser_props)
86 universal_properties = universal_properties.difference(['cssText'])
87 universal_properties = universal_properties.intersection(
88 list(map(camelCaseName, data)))
89
90 output_file = open(OUTPUT_FILE, 'w')
91 output_file.write("""
92/// Exposing all the extra CSS property getters and setters.
93@JS()
94library dart.css_properties;
95
96import 'dart:_js_annotations';
97import 'dart:_js_bindings' as js_bindings;
98import 'dart:html_common';
99
100@JS()
101@staticInterop
102class CssStyleDeclaration implements js_bindings.CSSStyleDeclaration {}
103
104extension CssStyleDeclarationView on CssStyleDeclaration {
105 // dart:html requires a `String?` type for `value`.
106 external Object setProperty(String property, String? value,
107 [String? priority = '']);
108
109 // ##### Universal property getters and setters #####
110 """)
111
112 for camelName in sorted(universal_properties):
113 property = dashifyName(camelName)
114 output_file.write("""
115 /** Gets the value of "%s" */
116 String get %s => this._%s;
117
118 /** Sets the value of "%s" */
119 set %s(String? value) {
120 _%s = value == null ? '' : value;
121 }
122
123 @JS('%s')
124 external String get _%s;
125
126 @JS('%s')
127 external set _%s(String value);
128 """ % (property, camelName, camelName, property, camelName, camelName,
129 camelName, camelName, camelName, camelName))
130
131 output_file.write("""
132
133 // ##### Non-universal property getters and setters #####
134
135""")
136
137 property_lines = []
138
139 seen = set()
140 for prop in sorted(data, key=camelCaseName):
141 camel_case_name = camelCaseName(prop)
142 upper_camel_case_name = camel_case_name[0].upper() + camel_case_name[1:]
143 css_name = prop.replace('-webkit-', '')
144 base_css_name = prop.replace('-webkit-', '')
145
146 if base_css_name in seen or base_css_name.startswith(
147 '-internal') or camel_case_name in universal_properties:
148 continue
149 seen.add(base_css_name)
150
151 comment = ' /** %s the value of "' + base_css_name + '" */'
152 property_lines.append('\n')
153 property_lines.append(comment % 'Gets')
154 if base_css_name in annotated:
155 property_lines.append(annotated[base_css_name])
156 property_lines.append("""
157 String get %s =>
158 getPropertyValue('%s');
159
160""" % (camel_case_name, css_name))
161
162 property_lines.append(comment % 'Sets')
163 if base_css_name in annotated:
164 property_lines.append(annotated[base_css_name])
165 property_lines.append("""
166 set %s(String value) {
167 setProperty('%s', value, '');
168 }
169""" % (camel_case_name, css_name))
170
171 output_file.write(''.join(property_lines))
172 output_file.write('}\n')
173 output_file.close()
174
175
176if __name__ == '__main__':
177 main()
static void readlines(const void *data, size_t size, F f)
Definition editor.cpp:30
Definition main.py:1