|
|
|
|
|
|
|
|
|
|
|
|
|
import type {AnyParseNode} from "./parseNode"; |
|
|
|
|
|
|
|
|
|
const contains = function<T>(list: Array<T>, elem: T): boolean { |
|
return list.indexOf(elem) !== -1; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
const deflt = function<T>(setting: T | void, defaultIfUndefined: T): * { |
|
return setting === undefined ? defaultIfUndefined : setting; |
|
}; |
|
|
|
|
|
|
|
const uppercase = /([A-Z])/g; |
|
const hyphenate = function(str: string): string { |
|
return str.replace(uppercase, "-$1").toLowerCase(); |
|
}; |
|
|
|
const ESCAPE_LOOKUP = { |
|
"&": "&", |
|
">": ">", |
|
"<": "<", |
|
"\"": """, |
|
"'": "'", |
|
}; |
|
|
|
const ESCAPE_REGEX = /[&><"']/g; |
|
|
|
|
|
|
|
|
|
function escape(text: mixed): string { |
|
return String(text).replace(ESCAPE_REGEX, match => ESCAPE_LOOKUP[match]); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
const getBaseElem = function(group: AnyParseNode): AnyParseNode { |
|
if (group.type === "ordgroup") { |
|
if (group.body.length === 1) { |
|
return getBaseElem(group.body[0]); |
|
} else { |
|
return group; |
|
} |
|
} else if (group.type === "color") { |
|
if (group.body.length === 1) { |
|
return getBaseElem(group.body[0]); |
|
} else { |
|
return group; |
|
} |
|
} else if (group.type === "font") { |
|
return getBaseElem(group.body); |
|
} else { |
|
return group; |
|
} |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
const isCharacterBox = function(group: AnyParseNode): boolean { |
|
const baseElem = getBaseElem(group); |
|
|
|
|
|
return baseElem.type === "mathord" || |
|
baseElem.type === "textord" || |
|
baseElem.type === "atom"; |
|
}; |
|
|
|
export const assert = function<T>(value: ?T): T { |
|
if (!value) { |
|
throw new Error('Expected non-null, but got ' + String(value)); |
|
} |
|
return value; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
export const protocolFromUrl = function(url: string): string | null { |
|
|
|
|
|
|
|
|
|
const protocol = /^[\x00-\x20]*([^\\/#?]*?)(:|�*58|�*3a|&colon)/i |
|
.exec(url); |
|
if (!protocol) { |
|
return "_relative"; |
|
} |
|
|
|
if (protocol[2] !== ":") { |
|
return null; |
|
} |
|
|
|
|
|
if (!/^[a-zA-Z][a-zA-Z0-9+\-.]*$/.test(protocol[1])) { |
|
return null; |
|
} |
|
|
|
return protocol[1].toLowerCase(); |
|
}; |
|
|
|
export default { |
|
contains, |
|
deflt, |
|
escape, |
|
hyphenate, |
|
getBaseElem, |
|
isCharacterBox, |
|
protocolFromUrl, |
|
}; |
|
|