def Parse(loader, module_name, module_dir_name, is_component, parser_results):
    res = module.ModuleDependencyMetadata()
    if is_component:
        return res

    if parser_results.has_decl is False:
        raise Exception('%s must have <!DOCTYPE html>' % module_name)

    # External script references..
    for href in parser_results.scripts_external:
        resource = _HRefToResource(loader,
                                   module_name,
                                   module_dir_name,
                                   href,
                                   tag_for_err_msg='<script src="%s">' % href)
        res.dependent_raw_script_relative_paths.append(
            resource.unix_style_relative_path)

    # External imports. Mostly the same as <script>, but we know its a module.
    for href in parser_results.imports:
        if not href.endswith('.html'):
            raise Exception(
                'In %s, the <link rel="import" href="%s"> must point at a '
                'file with an html suffix' % (module_name, href))

        resource = _HRefToResource(
            loader,
            module_name,
            module_dir_name,
            href,
            tag_for_err_msg='<link rel="import" href="%s">' % href)
        res.dependent_module_names.append(resource.name)

    # Validate the in-line scripts.
    for inline_script in parser_results.inline_scripts:
        stripped_text = inline_script.stripped_contents
        try:
            js_utils.ValidateUsesStrictMode('_', stripped_text)
        except:
            raise Exception('%s has an inline script tag that is missing '
                            'a \'use strict\' directive.' % module_name)

    # Style sheets
    for href in parser_results.stylesheets:
        resource = _HRefToResource(
            loader,
            module_name,
            module_dir_name,
            href,
            tag_for_err_msg='<link rel="stylesheet" href="%s">' % href)
        res.style_sheet_names.append(resource.name)

    return res
예제 #2
0
def Parse(module_name, stripped_text):
  """Parses the tvcm.require* lines in the module and returns module.ModuleDependencyMetadata.

  Args:
    stripped_text: Javascript source code with comments stripped out.

  Raises:
    DepsException: The name of a resource was not formatted properly.
  """
  res = module.ModuleDependencyMetadata()
  if module_name != 'tvcm':
    res.dependent_module_names.append('tvcm')
  if not module_name:
    raise Exception("Module.name must be set.")

  rest = stripped_text
  while True:
    # Search for require statements in the rest of the file.
    m_r = re.search("""tvcm\s*\.\s*require\((["'])(.+?)\\1\)""",
                    rest, re.DOTALL)
    m_s = re.search("""tvcm\s*\.\s*requireStylesheet\((["'])(.+?)\\1\)""",
                    rest, re.DOTALL)
    m_t = re.search("""tvcm\s*\.\s*requireTemplate\((["'])(.+?)\\1\)""",
                    rest, re.DOTALL)
    m_irs = re.search("""tvcm\s*\.\s*requireRawScript\((["'])(.+?)\\1\)""",
                    rest, re.DOTALL)
    matches = [m for m in [m_r, m_s, m_t, m_irs] if m]

    # Figure out which was first.
    matches.sort(key=lambda x: x.start())
    if len(matches):
      m = matches[0]
    else:
      break

    if m == m_r:
      dependent_module_name = m.group(2)
      if '/' in dependent_module_name:
        raise module.DepsException('Slashes are not allowed in module names. '
                                   "Use '.' instead: %s" % dependent_module_name)
      if dependent_module_name.endswith('js'):
        raise module.DepsException("module names shouldn't end with .js"
                                   'The module system will append that for you: %s' %
                                   dependent_module_name)
      res.dependent_module_names.append(dependent_module_name)
    elif m == m_s:
      style_sheet_name = m.group(2)
      if '/' in style_sheet_name:
        raise module.DepsException('Slashes are not allowed in style sheet names. '
                                   "Use '.' instead: %s" % style_sheet_name)
      if style_sheet_name.endswith('.css'):
        raise module.DepsException('Style sheets should not end in .css. '
                                  'The module system will append that for you: %s' %
                                  style_sheet_name)
      res.style_sheet_names.append(style_sheet_name)
    elif m == m_t:
      html_template_name = m.group(2)
      if '/' in html_template_name:
        raise module.DepsException('Slashes are not allowed in html template names. '
                                   "Use '.' instead: %s" % html_template_name)
      if html_template_name.endswith('.html'):
        raise module.DepsException(
            'HTML templates resource names should not include extension. '
            'The module system will append that for you.' %
            html_template_name)
      res.html_template_names.append(html_template_name)
    elif m == m_irs:
      name = m.group(2)
      res.dependent_raw_script_relative_paths.append(name)

    rest = rest[m.end():]

  return res