about summary refs log blame commit diff stats
path: root/src/html/script.nim
blob: 61066abe1150a45dae4bae8c46848ed84136c30c (plain) (tree)
1
2
3
4
5
6
7
8

                     
                     



                        
                                         












































                                                                        
                                                                     





                                                                    
                                                                 




                                             
                
import js/javascript
import loader/request
import types/referrer
import types/url

type
  ParserMetadata* = enum
    pmParserInserted, pmNotParserInserted

  ScriptType* = enum
    NO_SCRIPTTYPE, CLASSIC, MODULE, IMPORTMAP

  ScriptResultType* = enum
    RESULT_NULL, RESULT_SCRIPT, RESULT_IMPORT_MAP_PARSE, RESULT_FETCHING

type
  EnvironmentSettings* = ref object
    scripting*: bool
    moduleMap*: ModuleMap

  Script* = object
    #TODO setings
    baseURL*: URL
    options*: ScriptOptions
    mutedErrors*: bool
    #TODO parse error/error to rethrow
    record*: JSValue

  ScriptOptions* = object
    nonce*: string
    integrity*: string
    parserMetadata*: ParserMetadata
    credentialsMode*: CredentialsMode
    referrerPolicy*: Option[ReferrerPolicy]
    renderBlocking*: bool

  ScriptResult* = ref object
    case t*: ScriptResultType
    of RESULT_NULL:
      discard
    of RESULT_SCRIPT:
      script*: Script
    of RESULT_FETCHING:
      discard
    of RESULT_IMPORT_MAP_PARSE:
      discard #TODO

  ModuleMapEntry = object
    key: tuple[url, moduleType: string]
    value*: ScriptResult

  ModuleMap* = seq[ModuleMapEntry]

proc find*(moduleMap: ModuleMap; url: URL; moduleType: string): int =
  let surl = $url
  for i, entry in moduleMap:
    if entry.key.moduleType == moduleType and entry.key.url == surl:
      return i
  return -1

func fetchDestinationFromModuleType*(default: RequestDestination;
    moduleType: string): RequestDestination =
  if moduleType == "json":
    return RequestDestination.JSON
  if moduleType == "css":
    return RequestDestination.STYLE
  return default