export function androidParsePattern()

in js/src/android-parse.ts [30:61]


export function androidParsePattern(
  src: string,
  onError: (error: ParseError) => void
): Pattern {
  const entities: Record<string, string> = {}
  const safe = src.replace(/&([a-z][a-z0-9_]*);/gi, (match, name) => {
    if (_xmlEntities.has(name)) return match
    const key = `_entity_${++_xmlEntityKey}_`
    entities[key] = name
    return key
  })
  const doc = new DOMParser().parseFromString(
    `<string xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">${safe}</string>`,
    'text/xml'
  )
  const root = doc.querySelector('string')
  const error = doc.querySelector('parsererror')
  if (!root || error) {
    const errMsg = 'android: ' + (error?.textContent ?? 'XML parser error')
    onError(new ParseError(errMsg))
    return []
  }

  if (root.childElementCount === 0 && resoureRef.test(root.textContent ?? '')) {
    // https://developer.android.com/guide/topics/resources/providing-resources#ResourcesFromXml
    return [{ _: root.textContent!, fn: 'reference' }]
  }

  const flat = flattenElements(root)
  const spaced = parseQuotes(flat)
  return Array.from(parseInline(spaced, entities))
}