UNPKG

@markuplint/ml-spec

Version:

Types and schema that specs of the Markup languages for markuplint

28 lines (27 loc) 1.02 kB
import { getNS } from './get-ns.js'; const cache = new Map(); const namespaceURIMap = { html: 'http://www.w3.org/1999/xhtml', svg: 'http://www.w3.org/2000/svg', mml: 'http://www.w3.org/1998/Math/MathML', xlink: 'http://www.w3.org/1999/xlink', }; export function resolveNamespace(name, namespaceURI = 'http://www.w3.org/1999/xhtml') { const cached = cache.get(name + namespaceURI); if (cached) { return cached; } const [_explicitNS, _localName] = name.split(':'); const explicitNS = _localName ? _explicitNS : null; const localName = _localName ?? _explicitNS ?? ''; const namespace = ['html', 'svg', 'mml', 'xlink'].find(_ns => _ns === (explicitNS || getNS(namespaceURI ?? null))) || 'html'; const result = { localNameWithNS: `${namespace === 'html' ? '' : `${namespace}:`}${localName}`, localName, namespace, namespaceURI: namespaceURIMap[namespace], }; cache.set(name + namespaceURI, result); return result; }