def fix_links_with_target_blank(source_string_path): """Takes in a grd(p) path, finds all <a> tags with target _blank and makes sure they have rel attribute with noopener and noreferrer values""" source_xml_tree = lxml.etree.parse(source_string_path) for elem in source_xml_tree.xpath('//message'): fix_links_with_target_blank_in_element(elem) write_xml_file_from_tree(source_string_path, source_xml_tree)
def generate_overrides_and_replace_strings(source_string_path): fix_links_with_target_blank(source_string_path) original_xml_tree_with_branding_fixes = etree.parse(source_string_path) update_braveified_grd_tree_override(original_xml_tree_with_branding_fixes, True) write_braveified_grd_override(source_string_path) modified_xml_tree = etree.parse(source_string_path) original_messages = original_xml_tree_with_branding_fixes.xpath( '//message') modified_messages = modified_xml_tree.xpath('//message') assert len(original_messages) == len(modified_messages) for i in range(0, len(original_messages)): if textify(original_messages[i]) == textify(modified_messages[i]): modified_messages[i].getparent().remove(modified_messages[i]) # Remove uneeded things from the override grds nodes_to_remove = [ '//outputs', '//comment()', ] for xpath_expr in nodes_to_remove: nodes = modified_xml_tree.xpath(xpath_expr) for n in nodes: if n.getparent() is not None: n.getparent().remove(n) parts = modified_xml_tree.xpath('//part') for part in parts: override_file = get_override_file_path(part.attrib['file']) # Check for the special case of brave_stings.grd: if (os.path.basename(source_string_path) == 'brave_strings.grd' and override_file == 'settings_chromium_strings_override.grdp'): override_file = 'settings_brave_strings_override.grdp' if os.path.exists( os.path.join(os.path.dirname(source_string_path), override_file)): part.attrib['file'] = override_file else: # No grdp override here, carry on part.getparent().remove(part) files = modified_xml_tree.xpath('//file') for f in files: f.attrib['path'] = get_override_file_path(f.attrib['path']) # Write out an override file that is a duplicate of the original file but with strings that # are shared with Chrome stripped out. filename = os.path.basename(source_string_path) (basename, ext) = filename.split('.') override_string_path = get_override_file_path(source_string_path) modified_messages = modified_xml_tree.xpath('//message') modified_parts = modified_xml_tree.xpath('//part') if len(modified_messages) > 0 or len(modified_parts) > 0: write_xml_file_from_tree(override_string_path, modified_xml_tree)