tools/android/checkxmlstyle/checkxmlstyle.py
def _CheckTextAppearance(input_api, output_api):
  """Checks text attributes are only used for text appearance styles in XMLs."""
  text_attributes = [
      'android:textColor', 'android:textSize', 'android:textStyle',
      'android:fontFamily', 'android:textAllCaps']
  namespace = {'android': 'http://schemas.android.com/apk/res/android'}
  errors = []
  for f in IncludedFiles(input_api):
    root = ET.fromstring(input_api.ReadFile(f))
    # Check if there are text attributes defined outside text appearances.
    for attribute in text_attributes:
      # Get style name that contains text attributes but is not text appearance.
      invalid_styles = []
      for style in root.findall('style') + root.findall('.//style'):
        name = style.get('name')
        is_text_appearance = helpers.TEXT_APPEARANCE_STYLE_PATTERN.search(name)
        item = style.find(".//item[@name='"+attribute+"']")
        if is_text_appearance is None and item is not None:
          invalid_styles.append(name)
      # Append error messages.
      contents = input_api.ReadFile(f)
      style_count = 0
      widget_count = len(root.findall('[@'+attribute+']', namespace)) + len(
          root.findall('.//*[@'+attribute+']', namespace))
      for line_number, line in enumerate(contents.splitlines(False)):
        # Error for text attributes in non-text-appearance style.
        if (style_count < len(invalid_styles) and
            invalid_styles[style_count] in line):
          errors.append('  %s:%d contains attribute %s\n    \t%s' % (
              f.LocalPath(), line_number+1, attribute, line.strip()))
          style_count += 1
        # Error for text attributes in layout.
        if widget_count > 0 and attribute in line:
          errors.append('  %s:%d contains attribute %s\n    \t%s' % (
              f.LocalPath(), line_number+1, attribute, line.strip()))
          widget_count -= 1
  # TODO(huayinz): Change the path on the error message to the corresponding
  # styles.xml when this check applies to all resource directories.
  if errors:
    return [
        output_api.PresubmitError(
            '''
  Android Text Appearance Check failed:
    Your modified files contain Android text attributes defined outside
    text appearance styles, listed below.

    It is recommended to use the pre-defined text appearance styles in
      src/ui/android/java/res/values-v17/styles.xml

    And to use
      android:textAppearance="@style/SomeTextAppearance"
    in the XML layout whenever possible.

    If your text appearance absolutely has to deviate from the existing
    pre-defined text appearance style, you will need UX approval for adding a
    new text appearance style.

    If your approved text appearance style is a common text appreance style,
    please define it in src/ui/android/java/res/values-v17/styles.xml.

    Otherwise, if your approved text appearance is feature-specific, in
    chrome/android/java/res/values*/styles.xml, please define
      <style name="TextAppearance.YourTextAppearanceName>
        <item name="android:textColor">...</item>
        <item name="android:textSize">...</item>
        ...
      </style>

    Please contact arminaforoughi@chromium.org for UX approval, and
    src/chrome/android/java/res/OWNERS for questions.
    See https://crbug.com/775198 for more information.
  ''', errors)
    ]
  return []
Checks text attributes are only used for text appearance styles in XMLs.
Checks text attributes are only used for text appearance styles in XMLs.
Checks text attributes are only used for text appearance styles in XMLs .
def _CheckTextAppearance(input_api, output_api):
  text_attributes = [
      'android:textColor', 'android:textSize', 'android:textStyle',
      'android:fontFamily', 'android:textAllCaps']
  namespace = {'android': 'http://schemas.android.com/apk/res/android'}
  errors = []
  for f in IncludedFiles(input_api):
    root = ET.fromstring(input_api.ReadFile(f))
    for attribute in text_attributes:
      invalid_styles = []
      for style in root.findall('style') + root.findall('.//style'):
        name = style.get('name')
        is_text_appearance = helpers.TEXT_APPEARANCE_STYLE_PATTERN.search(name)
        item = style.find(".//item[@name='"+attribute+"']")
        if is_text_appearance is None and item is not None:
          invalid_styles.append(name)
      contents = input_api.ReadFile(f)
      style_count = 0
      widget_count = len(root.findall('[@'+attribute+']', namespace)) + len(
          root.findall('.//*[@'+attribute+']', namespace))
      for line_number, line in enumerate(contents.splitlines(False)):
        if (style_count < len(invalid_styles) and
            invalid_styles[style_count] in line):
          errors.append('  %s:%d contains attribute %s\n    \t%s' % (
              f.LocalPath(), line_number+1, attribute, line.strip()))
          style_count += 1
        if widget_count > 0 and attribute in line:
          errors.append('  %s:%d contains attribute %s\n    \t%s' % (
              f.LocalPath(), line_number+1, attribute, line.strip()))
          widget_count -= 1
  if errors:
    return [
        output_api.PresubmitError(
            '''
  Android Text Appearance Check failed:
    Your modified files contain Android text attributes defined outside
    text appearance styles, listed below.
    It is recommended to use the pre-defined text appearance styles in
      src/ui/android/java/res/values-v17/styles.xml
    And to use
      android:textAppearance="@style/SomeTextAppearance"
    in the XML layout whenever possible.
    If your text appearance absolutely has to deviate from the existing
    pre-defined text appearance style, you will need UX approval for adding a
    new text appearance style.
    If your approved text appearance style is a common text appreance style,
    please define it in src/ui/android/java/res/values-v17/styles.xml.
    Otherwise, if your approved text appearance is feature-specific, in
    chrome/android/java/res/values*/styles.xml, please define
      <style name="TextAppearance.YourTextAppearanceName>
        <item name="android:textColor">...</item>
        <item name="android:textSize">...</item>
        ...
      </style>
    Please contact arminaforoughi@chromium.org for UX approval, and
    src/chrome/android/java/res/OWNERS for questions.
    See https://crbug.com/775198 for more information.
  ''', errors)
    ]
  return []
def _CheckTextAppearance ( input_api , output_api ) : text_attributes = [ 'android:textColor' , 'android:textSize' , 'android:textStyle' , 'android:fontFamily' , 'android:textAllCaps' ] namespace = { 'android' : 'http://schemas.android.com/apk/res/android' } errors = [ ] for f in IncludedFiles ( input_api ) : root = ET . fromstring ( input_api . ReadFile ( f ) ) for attribute in text_attributes : invalid_styles = [ ] for style in root . findall ( 'style' ) + root . findall ( './/style' ) : name = style . get ( 'name' ) is_text_appearance = helpers . TEXT_APPEARANCE_STYLE_PATTERN . search ( name ) item = style . find ( ".//item[@name='" + attribute + "']" ) if is_text_appearance is None and item is not None : invalid_styles . append ( name ) contents = input_api . ReadFile ( f ) style_count = 0 widget_count = len ( root . findall ( '[@' + attribute + ']' , namespace ) ) + len ( root . findall ( './/*[@' + attribute + ']' , namespace ) ) for line_number , line in enumerate ( contents . splitlines ( False ) ) : if ( style_count < len ( invalid_styles ) and invalid_styles [ style_count ] in line ) : errors . append ( '  %s:%d contains attribute %s\n    \t%s' % ( f . LocalPath ( ) , line_number + 1 , attribute , line . strip ( ) ) ) style_count += 1 if widget_count > 0 and attribute in line : errors . append ( '  %s:%d contains attribute %s\n    \t%s' % ( f . LocalPath ( ) , line_number + 1 , attribute , line . strip ( ) ) ) widget_count -= 1 if errors : return [ output_api . PresubmitError ( '''
  Android Text Appearance Check failed:
    Your modified files contain Android text attributes defined outside
    text appearance styles, listed below.

    It is recommended to use the pre-defined text appearance styles in
      src/ui/android/java/res/values-v17/styles.xml

    And to use
      android:textAppearance="@style/SomeTextAppearance"
    in the XML layout whenever possible.

    If your text appearance absolutely has to deviate from the existing
    pre-defined text appearance style, you will need UX approval for adding a
    new text appearance style.

    If your approved text appearance style is a common text appreance style,
    please define it in src/ui/android/java/res/values-v17/styles.xml.

    Otherwise, if your approved text appearance is feature-specific, in
    chrome/android/java/res/values*/styles.xml, please define
      <style name="TextAppearance.YourTextAppearanceName>
        <item name="android:textColor">...</item>
        <item name="android:textSize">...</item>
        ...
      </style>

    Please contact arminaforoughi@chromium.org for UX approval, and
    src/chrome/android/java/res/OWNERS for questions.
    See https://crbug.com/775198 for more information.
  ''' , errors ) ] return [ ]
Checks text attributes are only used for text appearance styles in XMLs.
Checks text attributes are only used for text appearance styles in XMLs .
"""Checks text attributes are only used for text appearance styles in XMLs.""" # Check if there are text attributes defined outside text appearances. # Get style name that contains text attributes but is not text appearance. # Append error messages. # Error for text attributes in non-text-appearance style. # Error for text attributes in layout. # TODO(huayinz): Change the path on the error message to the corresponding # styles.xml when this check applies to all resource directories.
input_api
output_api