| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 | 2 |
| 3 # Copyright (C) 2013 Adobe Systems Incorporated. All rights reserved. | 3 # Copyright (C) 2013 Adobe Systems Incorporated. All rights reserved. |
| 4 # | 4 # |
| 5 # Redistribution and use in source and binary forms, with or without | 5 # Redistribution and use in source and binary forms, with or without |
| 6 # modification, are permitted provided that the following conditions | 6 # modification, are permitted provided that the following conditions |
| 7 # are met: | 7 # are met: |
| 8 # | 8 # |
| 9 # 1. Redistributions of source code must retain the above | 9 # 1. Redistributions of source code must retain the above |
| 10 # copyright notice, this list of conditions and the following | 10 # copyright notice, this list of conditions and the following |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 prefixed_properties.append(match.group(0)) | 66 prefixed_properties.append(match.group(0)) |
| 67 | 67 |
| 68 return prefixed_properties | 68 return prefixed_properties |
| 69 | 69 |
| 70 def convert_for_webkit(self, new_path, filename): | 70 def convert_for_webkit(self, new_path, filename): |
| 71 """ Converts a file's |contents| so it will function correctly in its |n
ew_path| in Webkit. | 71 """ Converts a file's |contents| so it will function correctly in its |n
ew_path| in Webkit. |
| 72 | 72 |
| 73 Returns the list of modified properties and the modified text if the fil
e was modifed, None otherwise.""" | 73 Returns the list of modified properties and the modified text if the fil
e was modifed, None otherwise.""" |
| 74 contents = self._filesystem.read_binary_file(filename) | 74 contents = self._filesystem.read_binary_file(filename) |
| 75 if filename.endswith('.css'): | 75 if filename.endswith('.css'): |
| 76 return self.convert_css(contents) | 76 return self.convert_css(contents, filename) |
| 77 return self.convert_html(new_path, contents) | 77 return self.convert_html(new_path, contents, filename) |
| 78 | 78 |
| 79 def convert_css(self, contents): | 79 def convert_css(self, contents, filename): |
| 80 return self.add_webkit_prefix_to_unprefixed_properties(contents) | 80 return self.add_webkit_prefix_to_unprefixed_properties(contents, filenam
e) |
| 81 | 81 |
| 82 def convert_html(self, new_path, contents): | 82 def convert_html(self, new_path, contents, filename): |
| 83 doc = BeautifulSoup(contents) | 83 doc = BeautifulSoup(contents) |
| 84 did_modify_paths = self.convert_testharness_paths(doc, new_path) | 84 did_modify_paths = self.convert_testharness_paths(doc, new_path, filenam
e) |
| 85 converted_properties_and_content = self.convert_prefixed_properties(doc) | 85 converted_properties_and_content = self.convert_prefixed_properties(doc,
filename) |
| 86 return converted_properties_and_content if (did_modify_paths or converte
d_properties_and_content[0]) else None | 86 return converted_properties_and_content if (did_modify_paths or converte
d_properties_and_content[0]) else None |
| 87 | 87 |
| 88 def convert_testharness_paths(self, doc, new_path): | 88 def convert_testharness_paths(self, doc, new_path, filename): |
| 89 """ Update links to testharness.js in the BeautifulSoup |doc| to point t
o the copy in |new_path|. | 89 """ Update links to testharness.js in the BeautifulSoup |doc| to point t
o the copy in |new_path|. |
| 90 | 90 |
| 91 Returns whether the document was modified.""" | 91 Returns whether the document was modified.""" |
| 92 | 92 |
| 93 # Look for the W3C-style path to any testharness files - scripts (.js) o
r links (.css) | 93 # Look for the W3C-style path to any testharness files - scripts (.js) o
r links (.css) |
| 94 pattern = re.compile('/resources/testharness') | 94 pattern = re.compile('/resources/testharness') |
| 95 script_tags = doc.findAll(src=pattern) | 95 script_tags = doc.findAll(src=pattern) |
| 96 link_tags = doc.findAll(href=pattern) | 96 link_tags = doc.findAll(href=pattern) |
| 97 testharness_tags = script_tags + link_tags | 97 testharness_tags = script_tags + link_tags |
| 98 | 98 |
| 99 if not testharness_tags: | 99 if not testharness_tags: |
| 100 return False | 100 return False |
| 101 | 101 |
| 102 resources_path = self.path_from_webkit_root('LayoutTests', 'resources') | 102 resources_path = self.path_from_webkit_root('LayoutTests', 'resources') |
| 103 resources_relpath = self._filesystem.relpath(resources_path, new_path) | 103 resources_relpath = self._filesystem.relpath(resources_path, new_path) |
| 104 | 104 |
| 105 for tag in testharness_tags: | 105 for tag in testharness_tags: |
| 106 # FIXME: We need to handle img, audio, video tags also. | 106 # FIXME: We need to handle img, audio, video tags also. |
| 107 attr = 'src' | 107 attr = 'src' |
| 108 if tag.name != 'script': | 108 if tag.name != 'script': |
| 109 attr = 'href' | 109 attr = 'href' |
| 110 | 110 |
| 111 if not attr in tag: |
| 112 # FIXME: Figure out what to do w/ invalid tags. For now, we retu
rn False |
| 113 # and leave the document unmodified, which means that it'll prob
ably fail to run. |
| 114 print "Error: missing an attr in %s" % filename |
| 115 return False |
| 116 |
| 111 old_path = tag[attr] | 117 old_path = tag[attr] |
| 112 new_tag = Tag(doc, tag.name, tag.attrs) | 118 new_tag = Tag(doc, tag.name, tag.attrs) |
| 113 new_tag[attr] = re.sub(pattern, resources_relpath + '/testharness',
old_path) | 119 new_tag[attr] = re.sub(pattern, resources_relpath + '/testharness',
old_path) |
| 114 | 120 |
| 115 self.replace_tag(tag, new_tag) | 121 self.replace_tag(tag, new_tag) |
| 116 | 122 |
| 117 return True | 123 return True |
| 118 | 124 |
| 119 def convert_prefixed_properties(self, doc): | 125 def convert_prefixed_properties(self, doc, filename): |
| 120 """ Searches a BeautifulSoup |doc| for any CSS properties requiring the
-webkit- prefix and converts them. | 126 """ Searches a BeautifulSoup |doc| for any CSS properties requiring the
-webkit- prefix and converts them. |
| 121 | 127 |
| 122 Returns the list of converted properties and the modified document as a
string """ | 128 Returns the list of converted properties and the modified document as a
string """ |
| 123 | 129 |
| 124 converted_properties = [] | 130 converted_properties = [] |
| 125 | 131 |
| 126 # Look for inline and document styles. | 132 # Look for inline and document styles. |
| 127 inline_styles = doc.findAll(style=re.compile('.*')) | 133 inline_styles = doc.findAll(style=re.compile('.*')) |
| 128 style_tags = doc.findAll('style') | 134 style_tags = doc.findAll('style') |
| 129 all_styles = inline_styles + style_tags | 135 all_styles = inline_styles + style_tags |
| 130 | 136 |
| 131 for tag in all_styles: | 137 for tag in all_styles: |
| 132 | 138 |
| 133 # Get the text whether in a style tag or style attribute. | 139 # Get the text whether in a style tag or style attribute. |
| 134 style_text = '' | 140 style_text = '' |
| 135 if tag.name == 'style': | 141 if tag.name == 'style': |
| 136 if not tag.contents: | 142 if not tag.contents: |
| 137 continue | 143 continue |
| 138 style_text = tag.contents[0] | 144 style_text = tag.contents[0] |
| 139 else: | 145 else: |
| 140 style_text = tag['style'] | 146 style_text = tag['style'] |
| 141 | 147 |
| 142 updated_style_text = self.add_webkit_prefix_to_unprefixed_properties
(style_text) | 148 updated_style_text = self.add_webkit_prefix_to_unprefixed_properties
(style_text, filename) |
| 143 | 149 |
| 144 # Rewrite tag only if changes were made. | 150 # Rewrite tag only if changes were made. |
| 145 if updated_style_text[0]: | 151 if updated_style_text[0]: |
| 146 converted_properties.extend(updated_style_text[0]) | 152 converted_properties.extend(updated_style_text[0]) |
| 147 | 153 |
| 148 new_tag = Tag(doc, tag.name, tag.attrs) | 154 new_tag = Tag(doc, tag.name, tag.attrs) |
| 149 new_tag.insert(0, updated_style_text[1]) | 155 new_tag.insert(0, updated_style_text[1]) |
| 150 | 156 |
| 151 self.replace_tag(tag, new_tag) | 157 self.replace_tag(tag, new_tag) |
| 152 | 158 |
| 153 return (converted_properties, doc.prettify()) | 159 return (converted_properties, doc.prettify()) |
| 154 | 160 |
| 155 def add_webkit_prefix_to_unprefixed_properties(self, text): | 161 def add_webkit_prefix_to_unprefixed_properties(self, text, filename): |
| 156 """ Searches |text| for instances of properties requiring the -webkit- p
refix and adds the prefix to them. | 162 """ Searches |text| for instances of properties requiring the -webkit- p
refix and adds the prefix to them. |
| 157 | 163 |
| 158 Returns the list of converted properties and the modified text.""" | 164 Returns the list of converted properties and the modified text.""" |
| 159 | 165 |
| 160 converted_properties = [] | 166 converted_properties = [] |
| 161 | 167 |
| 162 for prefixed_property in self.prefixed_properties: | 168 for prefixed_property in self.prefixed_properties: |
| 163 # FIXME: add in both the prefixed and unprefixed versions, rather th
an just replacing them? | 169 # FIXME: add in both the prefixed and unprefixed versions, rather th
an just replacing them? |
| 164 # That might allow the imported test to work in other browsers more
easily. | 170 # That might allow the imported test to work in other browsers more
easily. |
| 165 | 171 |
| 166 unprefixed_property = prefixed_property.replace('-webkit-', '') | 172 unprefixed_property = prefixed_property.replace('-webkit-', '') |
| 167 | 173 |
| 168 # Look for the various ways it might be in the CSS | 174 # Look for the various ways it might be in the CSS |
| 169 # Match the the property preceded by either whitespace or left curly
brace | 175 # Match the the property preceded by either whitespace or left curly
brace |
| 170 # or at the beginning of the string (for inline style attribute) | 176 # or at the beginning of the string (for inline style attribute) |
| 171 pattern = '([\s{]|^)' + unprefixed_property + '(\s+:|:)' | 177 pattern = '([\s{]|^)' + unprefixed_property + '(\s+:|:)' |
| 172 if re.search(pattern, text): | 178 if re.search(pattern, text): |
| 173 print 'converting %s -> %s' % (unprefixed_property, prefixed_pro
perty) | 179 print 'converting %s -> %s' % (unprefixed_property, prefixed_pro
perty) |
| 174 converted_properties.append(prefixed_property) | 180 converted_properties.append(prefixed_property) |
| 175 text = re.sub(pattern, prefixed_property + ':', text) | 181 text = re.sub(pattern, prefixed_property + ':', text) |
| 176 | 182 |
| 177 # FIXME: Handle the JS versions of these properties and GetComputedStyle
, too. | 183 # FIXME: Handle the JS versions of these properties and GetComputedStyle
, too. |
| 178 return (converted_properties, text) | 184 return (converted_properties, text) |
| 179 | 185 |
| 180 def replace_tag(self, old_tag, new_tag): | 186 def replace_tag(self, old_tag, new_tag): |
| 181 index = old_tag.parent.contents.index(old_tag) | 187 index = old_tag.parent.contents.index(old_tag) |
| 182 old_tag.parent.insert(index, new_tag) | 188 old_tag.parent.insert(index, new_tag) |
| 183 old_tag.extract() | 189 old_tag.extract() |
| OLD | NEW |