Grabbed dndbeyond's source code
``` ~/go/bin/sourcemapper -output ddb -jsurl https://media.dndbeyond.com/character-app/static/js/main.90aa78c5.js ```
This commit is contained in:
+583
@@ -0,0 +1,583 @@
|
||||
import { StyleSheet } from '@emotion/sheet';
|
||||
import { dealloc, alloc, next, token, from, peek, delimit, slice, position, RULESET, combine, match, serialize, copy, replace, WEBKIT, MOZ, MS, KEYFRAMES, DECLARATION, hash, charat, strlen, indexof, stringify, COMMENT, rulesheet, middleware, compile } from 'stylis';
|
||||
import '@emotion/weak-memoize';
|
||||
import '@emotion/memoize';
|
||||
|
||||
var identifierWithPointTracking = function identifierWithPointTracking(begin, points, index) {
|
||||
var previous = 0;
|
||||
var character = 0;
|
||||
|
||||
while (true) {
|
||||
previous = character;
|
||||
character = peek(); // &\f
|
||||
|
||||
if (previous === 38 && character === 12) {
|
||||
points[index] = 1;
|
||||
}
|
||||
|
||||
if (token(character)) {
|
||||
break;
|
||||
}
|
||||
|
||||
next();
|
||||
}
|
||||
|
||||
return slice(begin, position);
|
||||
};
|
||||
|
||||
var toRules = function toRules(parsed, points) {
|
||||
// pretend we've started with a comma
|
||||
var index = -1;
|
||||
var character = 44;
|
||||
|
||||
do {
|
||||
switch (token(character)) {
|
||||
case 0:
|
||||
// &\f
|
||||
if (character === 38 && peek() === 12) {
|
||||
// this is not 100% correct, we don't account for literal sequences here - like for example quoted strings
|
||||
// stylis inserts \f after & to know when & where it should replace this sequence with the context selector
|
||||
// and when it should just concatenate the outer and inner selectors
|
||||
// it's very unlikely for this sequence to actually appear in a different context, so we just leverage this fact here
|
||||
points[index] = 1;
|
||||
}
|
||||
|
||||
parsed[index] += identifierWithPointTracking(position - 1, points, index);
|
||||
break;
|
||||
|
||||
case 2:
|
||||
parsed[index] += delimit(character);
|
||||
break;
|
||||
|
||||
case 4:
|
||||
// comma
|
||||
if (character === 44) {
|
||||
// colon
|
||||
parsed[++index] = peek() === 58 ? '&\f' : '';
|
||||
points[index] = parsed[index].length;
|
||||
break;
|
||||
}
|
||||
|
||||
// fallthrough
|
||||
|
||||
default:
|
||||
parsed[index] += from(character);
|
||||
}
|
||||
} while (character = next());
|
||||
|
||||
return parsed;
|
||||
};
|
||||
|
||||
var getRules = function getRules(value, points) {
|
||||
return dealloc(toRules(alloc(value), points));
|
||||
}; // WeakSet would be more appropriate, but only WeakMap is supported in IE11
|
||||
|
||||
|
||||
var fixedElements = /* #__PURE__ */new WeakMap();
|
||||
var compat = function compat(element) {
|
||||
if (element.type !== 'rule' || !element.parent || // positive .length indicates that this rule contains pseudo
|
||||
// negative .length indicates that this rule has been already prefixed
|
||||
element.length < 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
var value = element.value,
|
||||
parent = element.parent;
|
||||
var isImplicitRule = element.column === parent.column && element.line === parent.line;
|
||||
|
||||
while (parent.type !== 'rule') {
|
||||
parent = parent.parent;
|
||||
if (!parent) return;
|
||||
} // short-circuit for the simplest case
|
||||
|
||||
|
||||
if (element.props.length === 1 && value.charCodeAt(0) !== 58
|
||||
/* colon */
|
||||
&& !fixedElements.get(parent)) {
|
||||
return;
|
||||
} // if this is an implicitly inserted rule (the one eagerly inserted at the each new nested level)
|
||||
// then the props has already been manipulated beforehand as they that array is shared between it and its "rule parent"
|
||||
|
||||
|
||||
if (isImplicitRule) {
|
||||
return;
|
||||
}
|
||||
|
||||
fixedElements.set(element, true);
|
||||
var points = [];
|
||||
var rules = getRules(value, points);
|
||||
var parentRules = parent.props;
|
||||
|
||||
for (var i = 0, k = 0; i < rules.length; i++) {
|
||||
for (var j = 0; j < parentRules.length; j++, k++) {
|
||||
element.props[k] = points[i] ? rules[i].replace(/&\f/g, parentRules[j]) : parentRules[j] + " " + rules[i];
|
||||
}
|
||||
}
|
||||
};
|
||||
var removeLabel = function removeLabel(element) {
|
||||
if (element.type === 'decl') {
|
||||
var value = element.value;
|
||||
|
||||
if ( // charcode for l
|
||||
value.charCodeAt(0) === 108 && // charcode for b
|
||||
value.charCodeAt(2) === 98) {
|
||||
// this ignores label
|
||||
element["return"] = '';
|
||||
element.value = '';
|
||||
}
|
||||
}
|
||||
};
|
||||
var ignoreFlag = 'emotion-disable-server-rendering-unsafe-selector-warning-please-do-not-use-this-the-warning-exists-for-a-reason';
|
||||
|
||||
var isIgnoringComment = function isIgnoringComment(element) {
|
||||
return element.type === 'comm' && element.children.indexOf(ignoreFlag) > -1;
|
||||
};
|
||||
|
||||
var createUnsafeSelectorsAlarm = function createUnsafeSelectorsAlarm(cache) {
|
||||
return function (element, index, children) {
|
||||
if (element.type !== 'rule' || cache.compat) return;
|
||||
var unsafePseudoClasses = element.value.match(/(:first|:nth|:nth-last)-child/g);
|
||||
|
||||
if (unsafePseudoClasses) {
|
||||
var isNested = !!element.parent; // in nested rules comments become children of the "auto-inserted" rule and that's always the `element.parent`
|
||||
//
|
||||
// considering this input:
|
||||
// .a {
|
||||
// .b /* comm */ {}
|
||||
// color: hotpink;
|
||||
// }
|
||||
// we get output corresponding to this:
|
||||
// .a {
|
||||
// & {
|
||||
// /* comm */
|
||||
// color: hotpink;
|
||||
// }
|
||||
// .b {}
|
||||
// }
|
||||
|
||||
var commentContainer = isNested ? element.parent.children : // global rule at the root level
|
||||
children;
|
||||
|
||||
for (var i = commentContainer.length - 1; i >= 0; i--) {
|
||||
var node = commentContainer[i];
|
||||
|
||||
if (node.line < element.line) {
|
||||
break;
|
||||
} // it is quite weird but comments are *usually* put at `column: element.column - 1`
|
||||
// so we seek *from the end* for the node that is earlier than the rule's `element` and check that
|
||||
// this will also match inputs like this:
|
||||
// .a {
|
||||
// /* comm */
|
||||
// .b {}
|
||||
// }
|
||||
//
|
||||
// but that is fine
|
||||
//
|
||||
// it would be the easiest to change the placement of the comment to be the first child of the rule:
|
||||
// .a {
|
||||
// .b { /* comm */ }
|
||||
// }
|
||||
// with such inputs we wouldn't have to search for the comment at all
|
||||
// TODO: consider changing this comment placement in the next major version
|
||||
|
||||
|
||||
if (node.column < element.column) {
|
||||
if (isIgnoringComment(node)) {
|
||||
return;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
unsafePseudoClasses.forEach(function (unsafePseudoClass) {
|
||||
console.error("The pseudo class \"" + unsafePseudoClass + "\" is potentially unsafe when doing server-side rendering. Try changing it to \"" + unsafePseudoClass.split('-child')[0] + "-of-type\".");
|
||||
});
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
var isImportRule = function isImportRule(element) {
|
||||
return element.type.charCodeAt(1) === 105 && element.type.charCodeAt(0) === 64;
|
||||
};
|
||||
|
||||
var isPrependedWithRegularRules = function isPrependedWithRegularRules(index, children) {
|
||||
for (var i = index - 1; i >= 0; i--) {
|
||||
if (!isImportRule(children[i])) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}; // use this to remove incorrect elements from further processing
|
||||
// so they don't get handed to the `sheet` (or anything else)
|
||||
// as that could potentially lead to additional logs which in turn could be overhelming to the user
|
||||
|
||||
|
||||
var nullifyElement = function nullifyElement(element) {
|
||||
element.type = '';
|
||||
element.value = '';
|
||||
element["return"] = '';
|
||||
element.children = '';
|
||||
element.props = '';
|
||||
};
|
||||
|
||||
var incorrectImportAlarm = function incorrectImportAlarm(element, index, children) {
|
||||
if (!isImportRule(element)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (element.parent) {
|
||||
console.error("`@import` rules can't be nested inside other rules. Please move it to the top level and put it before regular rules. Keep in mind that they can only be used within global styles.");
|
||||
nullifyElement(element);
|
||||
} else if (isPrependedWithRegularRules(index, children)) {
|
||||
console.error("`@import` rules can't be after other rules. Please put your `@import` rules before your other rules.");
|
||||
nullifyElement(element);
|
||||
}
|
||||
};
|
||||
|
||||
/* eslint-disable no-fallthrough */
|
||||
|
||||
function prefix(value, length) {
|
||||
switch (hash(value, length)) {
|
||||
// color-adjust
|
||||
case 5103:
|
||||
return WEBKIT + 'print-' + value + value;
|
||||
// animation, animation-(delay|direction|duration|fill-mode|iteration-count|name|play-state|timing-function)
|
||||
|
||||
case 5737:
|
||||
case 4201:
|
||||
case 3177:
|
||||
case 3433:
|
||||
case 1641:
|
||||
case 4457:
|
||||
case 2921: // text-decoration, filter, clip-path, backface-visibility, column, box-decoration-break
|
||||
|
||||
case 5572:
|
||||
case 6356:
|
||||
case 5844:
|
||||
case 3191:
|
||||
case 6645:
|
||||
case 3005: // mask, mask-image, mask-(mode|clip|size), mask-(repeat|origin), mask-position, mask-composite,
|
||||
|
||||
case 6391:
|
||||
case 5879:
|
||||
case 5623:
|
||||
case 6135:
|
||||
case 4599:
|
||||
case 4855: // background-clip, columns, column-(count|fill|gap|rule|rule-color|rule-style|rule-width|span|width)
|
||||
|
||||
case 4215:
|
||||
case 6389:
|
||||
case 5109:
|
||||
case 5365:
|
||||
case 5621:
|
||||
case 3829:
|
||||
return WEBKIT + value + value;
|
||||
// appearance, user-select, transform, hyphens, text-size-adjust
|
||||
|
||||
case 5349:
|
||||
case 4246:
|
||||
case 4810:
|
||||
case 6968:
|
||||
case 2756:
|
||||
return WEBKIT + value + MOZ + value + MS + value + value;
|
||||
// flex, flex-direction
|
||||
|
||||
case 6828:
|
||||
case 4268:
|
||||
return WEBKIT + value + MS + value + value;
|
||||
// order
|
||||
|
||||
case 6165:
|
||||
return WEBKIT + value + MS + 'flex-' + value + value;
|
||||
// align-items
|
||||
|
||||
case 5187:
|
||||
return WEBKIT + value + replace(value, /(\w+).+(:[^]+)/, WEBKIT + 'box-$1$2' + MS + 'flex-$1$2') + value;
|
||||
// align-self
|
||||
|
||||
case 5443:
|
||||
return WEBKIT + value + MS + 'flex-item-' + replace(value, /flex-|-self/, '') + value;
|
||||
// align-content
|
||||
|
||||
case 4675:
|
||||
return WEBKIT + value + MS + 'flex-line-pack' + replace(value, /align-content|flex-|-self/, '') + value;
|
||||
// flex-shrink
|
||||
|
||||
case 5548:
|
||||
return WEBKIT + value + MS + replace(value, 'shrink', 'negative') + value;
|
||||
// flex-basis
|
||||
|
||||
case 5292:
|
||||
return WEBKIT + value + MS + replace(value, 'basis', 'preferred-size') + value;
|
||||
// flex-grow
|
||||
|
||||
case 6060:
|
||||
return WEBKIT + 'box-' + replace(value, '-grow', '') + WEBKIT + value + MS + replace(value, 'grow', 'positive') + value;
|
||||
// transition
|
||||
|
||||
case 4554:
|
||||
return WEBKIT + replace(value, /([^-])(transform)/g, '$1' + WEBKIT + '$2') + value;
|
||||
// cursor
|
||||
|
||||
case 6187:
|
||||
return replace(replace(replace(value, /(zoom-|grab)/, WEBKIT + '$1'), /(image-set)/, WEBKIT + '$1'), value, '') + value;
|
||||
// background, background-image
|
||||
|
||||
case 5495:
|
||||
case 3959:
|
||||
return replace(value, /(image-set\([^]*)/, WEBKIT + '$1' + '$`$1');
|
||||
// justify-content
|
||||
|
||||
case 4968:
|
||||
return replace(replace(value, /(.+:)(flex-)?(.*)/, WEBKIT + 'box-pack:$3' + MS + 'flex-pack:$3'), /s.+-b[^;]+/, 'justify') + WEBKIT + value + value;
|
||||
// (margin|padding)-inline-(start|end)
|
||||
|
||||
case 4095:
|
||||
case 3583:
|
||||
case 4068:
|
||||
case 2532:
|
||||
return replace(value, /(.+)-inline(.+)/, WEBKIT + '$1$2') + value;
|
||||
// (min|max)?(width|height|inline-size|block-size)
|
||||
|
||||
case 8116:
|
||||
case 7059:
|
||||
case 5753:
|
||||
case 5535:
|
||||
case 5445:
|
||||
case 5701:
|
||||
case 4933:
|
||||
case 4677:
|
||||
case 5533:
|
||||
case 5789:
|
||||
case 5021:
|
||||
case 4765:
|
||||
// stretch, max-content, min-content, fill-available
|
||||
if (strlen(value) - 1 - length > 6) switch (charat(value, length + 1)) {
|
||||
// (m)ax-content, (m)in-content
|
||||
case 109:
|
||||
// -
|
||||
if (charat(value, length + 4) !== 45) break;
|
||||
// (f)ill-available, (f)it-content
|
||||
|
||||
case 102:
|
||||
return replace(value, /(.+:)(.+)-([^]+)/, '$1' + WEBKIT + '$2-$3' + '$1' + MOZ + (charat(value, length + 3) == 108 ? '$3' : '$2-$3')) + value;
|
||||
// (s)tretch
|
||||
|
||||
case 115:
|
||||
return ~indexof(value, 'stretch') ? prefix(replace(value, 'stretch', 'fill-available'), length) + value : value;
|
||||
}
|
||||
break;
|
||||
// position: sticky
|
||||
|
||||
case 4949:
|
||||
// (s)ticky?
|
||||
if (charat(value, length + 1) !== 115) break;
|
||||
// display: (flex|inline-flex)
|
||||
|
||||
case 6444:
|
||||
switch (charat(value, strlen(value) - 3 - (~indexof(value, '!important') && 10))) {
|
||||
// stic(k)y
|
||||
case 107:
|
||||
return replace(value, ':', ':' + WEBKIT) + value;
|
||||
// (inline-)?fl(e)x
|
||||
|
||||
case 101:
|
||||
return replace(value, /(.+:)([^;!]+)(;|!.+)?/, '$1' + WEBKIT + (charat(value, 14) === 45 ? 'inline-' : '') + 'box$3' + '$1' + WEBKIT + '$2$3' + '$1' + MS + '$2box$3') + value;
|
||||
}
|
||||
|
||||
break;
|
||||
// writing-mode
|
||||
|
||||
case 5936:
|
||||
switch (charat(value, length + 11)) {
|
||||
// vertical-l(r)
|
||||
case 114:
|
||||
return WEBKIT + value + MS + replace(value, /[svh]\w+-[tblr]{2}/, 'tb') + value;
|
||||
// vertical-r(l)
|
||||
|
||||
case 108:
|
||||
return WEBKIT + value + MS + replace(value, /[svh]\w+-[tblr]{2}/, 'tb-rl') + value;
|
||||
// horizontal(-)tb
|
||||
|
||||
case 45:
|
||||
return WEBKIT + value + MS + replace(value, /[svh]\w+-[tblr]{2}/, 'lr') + value;
|
||||
}
|
||||
|
||||
return WEBKIT + value + MS + value + value;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
var prefixer = function prefixer(element, index, children, callback) {
|
||||
if (element.length > -1) if (!element["return"]) switch (element.type) {
|
||||
case DECLARATION:
|
||||
element["return"] = prefix(element.value, element.length);
|
||||
break;
|
||||
|
||||
case KEYFRAMES:
|
||||
return serialize([copy(element, {
|
||||
value: replace(element.value, '@', '@' + WEBKIT)
|
||||
})], callback);
|
||||
|
||||
case RULESET:
|
||||
if (element.length) return combine(element.props, function (value) {
|
||||
switch (match(value, /(::plac\w+|:read-\w+)/)) {
|
||||
// :read-(only|write)
|
||||
case ':read-only':
|
||||
case ':read-write':
|
||||
return serialize([copy(element, {
|
||||
props: [replace(value, /:(read-\w+)/, ':' + MOZ + '$1')]
|
||||
})], callback);
|
||||
// :placeholder
|
||||
|
||||
case '::placeholder':
|
||||
return serialize([copy(element, {
|
||||
props: [replace(value, /:(plac\w+)/, ':' + WEBKIT + 'input-$1')]
|
||||
}), copy(element, {
|
||||
props: [replace(value, /:(plac\w+)/, ':' + MOZ + '$1')]
|
||||
}), copy(element, {
|
||||
props: [replace(value, /:(plac\w+)/, MS + 'input-$1')]
|
||||
})], callback);
|
||||
}
|
||||
|
||||
return '';
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
var defaultStylisPlugins = [prefixer];
|
||||
|
||||
var createCache = function createCache(options) {
|
||||
var key = options.key;
|
||||
|
||||
if (process.env.NODE_ENV !== 'production' && !key) {
|
||||
throw new Error("You have to configure `key` for your cache. Please make sure it's unique (and not equal to 'css') as it's used for linking styles to your cache.\n" + "If multiple caches share the same key they might \"fight\" for each other's style elements.");
|
||||
}
|
||||
|
||||
if (key === 'css') {
|
||||
var ssrStyles = document.querySelectorAll("style[data-emotion]:not([data-s])"); // get SSRed styles out of the way of React's hydration
|
||||
// document.head is a safe place to move them to(though note document.head is not necessarily the last place they will be)
|
||||
// note this very very intentionally targets all style elements regardless of the key to ensure
|
||||
// that creating a cache works inside of render of a React component
|
||||
|
||||
Array.prototype.forEach.call(ssrStyles, function (node) {
|
||||
// we want to only move elements which have a space in the data-emotion attribute value
|
||||
// because that indicates that it is an Emotion 11 server-side rendered style elements
|
||||
// while we will already ignore Emotion 11 client-side inserted styles because of the :not([data-s]) part in the selector
|
||||
// Emotion 10 client-side inserted styles did not have data-s (but importantly did not have a space in their data-emotion attributes)
|
||||
// so checking for the space ensures that loading Emotion 11 after Emotion 10 has inserted some styles
|
||||
// will not result in the Emotion 10 styles being destroyed
|
||||
var dataEmotionAttribute = node.getAttribute('data-emotion');
|
||||
|
||||
if (dataEmotionAttribute.indexOf(' ') === -1) {
|
||||
return;
|
||||
}
|
||||
document.head.appendChild(node);
|
||||
node.setAttribute('data-s', '');
|
||||
});
|
||||
}
|
||||
|
||||
var stylisPlugins = options.stylisPlugins || defaultStylisPlugins;
|
||||
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
// $FlowFixMe
|
||||
if (/[^a-z-]/.test(key)) {
|
||||
throw new Error("Emotion key must only contain lower case alphabetical characters and - but \"" + key + "\" was passed");
|
||||
}
|
||||
}
|
||||
|
||||
var inserted = {};
|
||||
var container;
|
||||
var nodesToHydrate = [];
|
||||
|
||||
{
|
||||
container = options.container || document.head;
|
||||
Array.prototype.forEach.call( // this means we will ignore elements which don't have a space in them which
|
||||
// means that the style elements we're looking at are only Emotion 11 server-rendered style elements
|
||||
document.querySelectorAll("style[data-emotion^=\"" + key + " \"]"), function (node) {
|
||||
var attrib = node.getAttribute("data-emotion").split(' '); // $FlowFixMe
|
||||
|
||||
for (var i = 1; i < attrib.length; i++) {
|
||||
inserted[attrib[i]] = true;
|
||||
}
|
||||
|
||||
nodesToHydrate.push(node);
|
||||
});
|
||||
}
|
||||
|
||||
var _insert;
|
||||
|
||||
var omnipresentPlugins = [compat, removeLabel];
|
||||
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
omnipresentPlugins.push(createUnsafeSelectorsAlarm({
|
||||
get compat() {
|
||||
return cache.compat;
|
||||
}
|
||||
|
||||
}), incorrectImportAlarm);
|
||||
}
|
||||
|
||||
{
|
||||
var currentSheet;
|
||||
var finalizingPlugins = [stringify, process.env.NODE_ENV !== 'production' ? function (element) {
|
||||
if (!element.root) {
|
||||
if (element["return"]) {
|
||||
currentSheet.insert(element["return"]);
|
||||
} else if (element.value && element.type !== COMMENT) {
|
||||
// insert empty rule in non-production environments
|
||||
// so @emotion/jest can grab `key` from the (JS)DOM for caches without any rules inserted yet
|
||||
currentSheet.insert(element.value + "{}");
|
||||
}
|
||||
}
|
||||
} : rulesheet(function (rule) {
|
||||
currentSheet.insert(rule);
|
||||
})];
|
||||
var serializer = middleware(omnipresentPlugins.concat(stylisPlugins, finalizingPlugins));
|
||||
|
||||
var stylis = function stylis(styles) {
|
||||
return serialize(compile(styles), serializer);
|
||||
};
|
||||
|
||||
_insert = function insert(selector, serialized, sheet, shouldCache) {
|
||||
currentSheet = sheet;
|
||||
|
||||
if (process.env.NODE_ENV !== 'production' && serialized.map !== undefined) {
|
||||
currentSheet = {
|
||||
insert: function insert(rule) {
|
||||
sheet.insert(rule + serialized.map);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
stylis(selector ? selector + "{" + serialized.styles + "}" : serialized.styles);
|
||||
|
||||
if (shouldCache) {
|
||||
cache.inserted[serialized.name] = true;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
var cache = {
|
||||
key: key,
|
||||
sheet: new StyleSheet({
|
||||
key: key,
|
||||
container: container,
|
||||
nonce: options.nonce,
|
||||
speedy: options.speedy,
|
||||
prepend: options.prepend,
|
||||
insertionPoint: options.insertionPoint
|
||||
}),
|
||||
nonce: options.nonce,
|
||||
inserted: inserted,
|
||||
registered: {},
|
||||
insert: _insert
|
||||
};
|
||||
cache.sheet.hydrate(nodesToHydrate);
|
||||
return cache;
|
||||
};
|
||||
|
||||
export { createCache as default };
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
export var MS = '-ms-'
|
||||
export var MOZ = '-moz-'
|
||||
export var WEBKIT = '-webkit-'
|
||||
|
||||
export var COMMENT = 'comm'
|
||||
export var RULESET = 'rule'
|
||||
export var DECLARATION = 'decl'
|
||||
|
||||
export var PAGE = '@page'
|
||||
export var MEDIA = '@media'
|
||||
export var IMPORT = '@import'
|
||||
export var CHARSET = '@charset'
|
||||
export var VIEWPORT = '@viewport'
|
||||
export var SUPPORTS = '@supports'
|
||||
export var DOCUMENT = '@document'
|
||||
export var NAMESPACE = '@namespace'
|
||||
export var KEYFRAMES = '@keyframes'
|
||||
export var FONT_FACE = '@font-face'
|
||||
export var COUNTER_STYLE = '@counter-style'
|
||||
export var FONT_FEATURE_VALUES = '@font-feature-values'
|
||||
export var LAYER = '@layer'
|
||||
+108
@@ -0,0 +1,108 @@
|
||||
import {MS, MOZ, WEBKIT, RULESET, KEYFRAMES, DECLARATION} from './Enum.js'
|
||||
import {match, charat, substr, strlen, sizeof, replace, combine} from './Utility.js'
|
||||
import {copy, tokenize} from './Tokenizer.js'
|
||||
import {serialize} from './Serializer.js'
|
||||
import {prefix} from './Prefixer.js'
|
||||
|
||||
/**
|
||||
* @param {function[]} collection
|
||||
* @return {function}
|
||||
*/
|
||||
export function middleware (collection) {
|
||||
var length = sizeof(collection)
|
||||
|
||||
return function (element, index, children, callback) {
|
||||
var output = ''
|
||||
|
||||
for (var i = 0; i < length; i++)
|
||||
output += collection[i](element, index, children, callback) || ''
|
||||
|
||||
return output
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {function} callback
|
||||
* @return {function}
|
||||
*/
|
||||
export function rulesheet (callback) {
|
||||
return function (element) {
|
||||
if (!element.root)
|
||||
if (element = element.return)
|
||||
callback(element)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {object} element
|
||||
* @param {number} index
|
||||
* @param {object[]} children
|
||||
* @param {function} callback
|
||||
*/
|
||||
export function prefixer (element, index, children, callback) {
|
||||
if (element.length > -1)
|
||||
if (!element.return)
|
||||
switch (element.type) {
|
||||
case DECLARATION: element.return = prefix(element.value, element.length, children)
|
||||
return
|
||||
case KEYFRAMES:
|
||||
return serialize([copy(element, {value: replace(element.value, '@', '@' + WEBKIT)})], callback)
|
||||
case RULESET:
|
||||
if (element.length)
|
||||
return combine(element.props, function (value) {
|
||||
switch (match(value, /(::plac\w+|:read-\w+)/)) {
|
||||
// :read-(only|write)
|
||||
case ':read-only': case ':read-write':
|
||||
return serialize([copy(element, {props: [replace(value, /:(read-\w+)/, ':' + MOZ + '$1')]})], callback)
|
||||
// :placeholder
|
||||
case '::placeholder':
|
||||
return serialize([
|
||||
copy(element, {props: [replace(value, /:(plac\w+)/, ':' + WEBKIT + 'input-$1')]}),
|
||||
copy(element, {props: [replace(value, /:(plac\w+)/, ':' + MOZ + '$1')]}),
|
||||
copy(element, {props: [replace(value, /:(plac\w+)/, MS + 'input-$1')]})
|
||||
], callback)
|
||||
}
|
||||
|
||||
return ''
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {object} element
|
||||
* @param {number} index
|
||||
* @param {object[]} children
|
||||
*/
|
||||
export function namespace (element) {
|
||||
switch (element.type) {
|
||||
case RULESET:
|
||||
element.props = element.props.map(function (value) {
|
||||
return combine(tokenize(value), function (value, index, children) {
|
||||
switch (charat(value, 0)) {
|
||||
// \f
|
||||
case 12:
|
||||
return substr(value, 1, strlen(value))
|
||||
// \0 ( + > ~
|
||||
case 0: case 40: case 43: case 62: case 126:
|
||||
return value
|
||||
// :
|
||||
case 58:
|
||||
if (children[++index] === 'global')
|
||||
children[index] = '', children[++index] = '\f' + substr(children[index], index = 1, -1)
|
||||
// \s
|
||||
case 32:
|
||||
return index === 1 ? '' : value
|
||||
default:
|
||||
switch (index) {
|
||||
case 0: element = value
|
||||
return sizeof(children) > 1 ? '' : value
|
||||
case index = sizeof(children) - 1: case 2:
|
||||
return index === 2 ? value + element + element : value + element
|
||||
default:
|
||||
return value
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
+191
@@ -0,0 +1,191 @@
|
||||
import {COMMENT, RULESET, DECLARATION} from './Enum.js'
|
||||
import {abs, charat, trim, from, sizeof, strlen, substr, append, replace, indexof} from './Utility.js'
|
||||
import {node, char, prev, next, peek, caret, alloc, dealloc, delimit, whitespace, escaping, identifier, commenter} from './Tokenizer.js'
|
||||
|
||||
/**
|
||||
* @param {string} value
|
||||
* @return {object[]}
|
||||
*/
|
||||
export function compile (value) {
|
||||
return dealloc(parse('', null, null, null, [''], value = alloc(value), 0, [0], value))
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} value
|
||||
* @param {object} root
|
||||
* @param {object?} parent
|
||||
* @param {string[]} rule
|
||||
* @param {string[]} rules
|
||||
* @param {string[]} rulesets
|
||||
* @param {number[]} pseudo
|
||||
* @param {number[]} points
|
||||
* @param {string[]} declarations
|
||||
* @return {object}
|
||||
*/
|
||||
export function parse (value, root, parent, rule, rules, rulesets, pseudo, points, declarations) {
|
||||
var index = 0
|
||||
var offset = 0
|
||||
var length = pseudo
|
||||
var atrule = 0
|
||||
var property = 0
|
||||
var previous = 0
|
||||
var variable = 1
|
||||
var scanning = 1
|
||||
var ampersand = 1
|
||||
var character = 0
|
||||
var type = ''
|
||||
var props = rules
|
||||
var children = rulesets
|
||||
var reference = rule
|
||||
var characters = type
|
||||
|
||||
while (scanning)
|
||||
switch (previous = character, character = next()) {
|
||||
// (
|
||||
case 40:
|
||||
if (previous != 108 && charat(characters, length - 1) == 58) {
|
||||
if (indexof(characters += replace(delimit(character), '&', '&\f'), '&\f') != -1)
|
||||
ampersand = -1
|
||||
break
|
||||
}
|
||||
// " ' [
|
||||
case 34: case 39: case 91:
|
||||
characters += delimit(character)
|
||||
break
|
||||
// \t \n \r \s
|
||||
case 9: case 10: case 13: case 32:
|
||||
characters += whitespace(previous)
|
||||
break
|
||||
// \
|
||||
case 92:
|
||||
characters += escaping(caret() - 1, 7)
|
||||
continue
|
||||
// /
|
||||
case 47:
|
||||
switch (peek()) {
|
||||
case 42: case 47:
|
||||
append(comment(commenter(next(), caret()), root, parent), declarations)
|
||||
break
|
||||
default:
|
||||
characters += '/'
|
||||
}
|
||||
break
|
||||
// {
|
||||
case 123 * variable:
|
||||
points[index++] = strlen(characters) * ampersand
|
||||
// } ; \0
|
||||
case 125 * variable: case 59: case 0:
|
||||
switch (character) {
|
||||
// \0 }
|
||||
case 0: case 125: scanning = 0
|
||||
// ;
|
||||
case 59 + offset: if (ampersand == -1) characters = replace(characters, /\f/g, '')
|
||||
if (property > 0 && (strlen(characters) - length))
|
||||
append(property > 32 ? declaration(characters + ';', rule, parent, length - 1) : declaration(replace(characters, ' ', '') + ';', rule, parent, length - 2), declarations)
|
||||
break
|
||||
// @ ;
|
||||
case 59: characters += ';'
|
||||
// { rule/at-rule
|
||||
default:
|
||||
append(reference = ruleset(characters, root, parent, index, offset, rules, points, type, props = [], children = [], length), rulesets)
|
||||
|
||||
if (character === 123)
|
||||
if (offset === 0)
|
||||
parse(characters, root, reference, reference, props, rulesets, length, points, children)
|
||||
else
|
||||
switch (atrule === 99 && charat(characters, 3) === 110 ? 100 : atrule) {
|
||||
// d l m s
|
||||
case 100: case 108: case 109: case 115:
|
||||
parse(value, reference, reference, rule && append(ruleset(value, reference, reference, 0, 0, rules, points, type, rules, props = [], length), children), rules, children, length, points, rule ? props : children)
|
||||
break
|
||||
default:
|
||||
parse(characters, reference, reference, reference, [''], children, 0, points, children)
|
||||
}
|
||||
}
|
||||
|
||||
index = offset = property = 0, variable = ampersand = 1, type = characters = '', length = pseudo
|
||||
break
|
||||
// :
|
||||
case 58:
|
||||
length = 1 + strlen(characters), property = previous
|
||||
default:
|
||||
if (variable < 1)
|
||||
if (character == 123)
|
||||
--variable
|
||||
else if (character == 125 && variable++ == 0 && prev() == 125)
|
||||
continue
|
||||
|
||||
switch (characters += from(character), character * variable) {
|
||||
// &
|
||||
case 38:
|
||||
ampersand = offset > 0 ? 1 : (characters += '\f', -1)
|
||||
break
|
||||
// ,
|
||||
case 44:
|
||||
points[index++] = (strlen(characters) - 1) * ampersand, ampersand = 1
|
||||
break
|
||||
// @
|
||||
case 64:
|
||||
// -
|
||||
if (peek() === 45)
|
||||
characters += delimit(next())
|
||||
|
||||
atrule = peek(), offset = length = strlen(type = characters += identifier(caret())), character++
|
||||
break
|
||||
// -
|
||||
case 45:
|
||||
if (previous === 45 && strlen(characters) == 2)
|
||||
variable = 0
|
||||
}
|
||||
}
|
||||
|
||||
return rulesets
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} value
|
||||
* @param {object} root
|
||||
* @param {object?} parent
|
||||
* @param {number} index
|
||||
* @param {number} offset
|
||||
* @param {string[]} rules
|
||||
* @param {number[]} points
|
||||
* @param {string} type
|
||||
* @param {string[]} props
|
||||
* @param {string[]} children
|
||||
* @param {number} length
|
||||
* @return {object}
|
||||
*/
|
||||
export function ruleset (value, root, parent, index, offset, rules, points, type, props, children, length) {
|
||||
var post = offset - 1
|
||||
var rule = offset === 0 ? rules : ['']
|
||||
var size = sizeof(rule)
|
||||
|
||||
for (var i = 0, j = 0, k = 0; i < index; ++i)
|
||||
for (var x = 0, y = substr(value, post + 1, post = abs(j = points[i])), z = value; x < size; ++x)
|
||||
if (z = trim(j > 0 ? rule[x] + ' ' + y : replace(y, /&\f/g, rule[x])))
|
||||
props[k++] = z
|
||||
|
||||
return node(value, root, parent, offset === 0 ? RULESET : type, props, children, length)
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} value
|
||||
* @param {object} root
|
||||
* @param {object?} parent
|
||||
* @return {object}
|
||||
*/
|
||||
export function comment (value, root, parent) {
|
||||
return node(value, root, parent, COMMENT, from(char()), substr(value, 2, -2), 0)
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} value
|
||||
* @param {object} root
|
||||
* @param {object?} parent
|
||||
* @param {number} length
|
||||
* @return {object}
|
||||
*/
|
||||
export function declaration (value, root, parent, length) {
|
||||
return node(value, root, parent, DECLARATION, substr(value, 0, length), substr(value, length + 1, -1), length)
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
import {IMPORT, LAYER, COMMENT, RULESET, DECLARATION, KEYFRAMES} from './Enum.js'
|
||||
import {strlen, sizeof} from './Utility.js'
|
||||
|
||||
/**
|
||||
* @param {object[]} children
|
||||
* @param {function} callback
|
||||
* @return {string}
|
||||
*/
|
||||
export function serialize (children, callback) {
|
||||
var output = ''
|
||||
var length = sizeof(children)
|
||||
|
||||
for (var i = 0; i < length; i++)
|
||||
output += callback(children[i], i, children, callback) || ''
|
||||
|
||||
return output
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {object} element
|
||||
* @param {number} index
|
||||
* @param {object[]} children
|
||||
* @param {function} callback
|
||||
* @return {string}
|
||||
*/
|
||||
export function stringify (element, index, children, callback) {
|
||||
switch (element.type) {
|
||||
case LAYER: if (element.children.length) break
|
||||
case IMPORT: case DECLARATION: return element.return = element.return || element.value
|
||||
case COMMENT: return ''
|
||||
case KEYFRAMES: return element.return = element.value + '{' + serialize(element.children, callback) + '}'
|
||||
case RULESET: element.value = element.props.join(',')
|
||||
}
|
||||
|
||||
return strlen(children = serialize(element.children, callback)) ? element.return = element.value + '{' + children + '}' : ''
|
||||
}
|
||||
+246
@@ -0,0 +1,246 @@
|
||||
import {from, trim, charat, strlen, substr, append, assign} from './Utility.js'
|
||||
|
||||
export var line = 1
|
||||
export var column = 1
|
||||
export var length = 0
|
||||
export var position = 0
|
||||
export var character = 0
|
||||
export var characters = ''
|
||||
|
||||
/**
|
||||
* @param {string} value
|
||||
* @param {object | null} root
|
||||
* @param {object | null} parent
|
||||
* @param {string} type
|
||||
* @param {string[] | string} props
|
||||
* @param {object[] | string} children
|
||||
* @param {number} length
|
||||
*/
|
||||
export function node (value, root, parent, type, props, children, length) {
|
||||
return {value: value, root: root, parent: parent, type: type, props: props, children: children, line: line, column: column, length: length, return: ''}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {object} root
|
||||
* @param {object} props
|
||||
* @return {object}
|
||||
*/
|
||||
export function copy (root, props) {
|
||||
return assign(node('', null, null, '', null, null, 0), root, {length: -root.length}, props)
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {number}
|
||||
*/
|
||||
export function char () {
|
||||
return character
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {number}
|
||||
*/
|
||||
export function prev () {
|
||||
character = position > 0 ? charat(characters, --position) : 0
|
||||
|
||||
if (column--, character === 10)
|
||||
column = 1, line--
|
||||
|
||||
return character
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {number}
|
||||
*/
|
||||
export function next () {
|
||||
character = position < length ? charat(characters, position++) : 0
|
||||
|
||||
if (column++, character === 10)
|
||||
column = 1, line++
|
||||
|
||||
return character
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {number}
|
||||
*/
|
||||
export function peek () {
|
||||
return charat(characters, position)
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {number}
|
||||
*/
|
||||
export function caret () {
|
||||
return position
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} begin
|
||||
* @param {number} end
|
||||
* @return {string}
|
||||
*/
|
||||
export function slice (begin, end) {
|
||||
return substr(characters, begin, end)
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} type
|
||||
* @return {number}
|
||||
*/
|
||||
export function token (type) {
|
||||
switch (type) {
|
||||
// \0 \t \n \r \s whitespace token
|
||||
case 0: case 9: case 10: case 13: case 32:
|
||||
return 5
|
||||
// ! + , / > @ ~ isolate token
|
||||
case 33: case 43: case 44: case 47: case 62: case 64: case 126:
|
||||
// ; { } breakpoint token
|
||||
case 59: case 123: case 125:
|
||||
return 4
|
||||
// : accompanied token
|
||||
case 58:
|
||||
return 3
|
||||
// " ' ( [ opening delimit token
|
||||
case 34: case 39: case 40: case 91:
|
||||
return 2
|
||||
// ) ] closing delimit token
|
||||
case 41: case 93:
|
||||
return 1
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} value
|
||||
* @return {any[]}
|
||||
*/
|
||||
export function alloc (value) {
|
||||
return line = column = 1, length = strlen(characters = value), position = 0, []
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {any} value
|
||||
* @return {any}
|
||||
*/
|
||||
export function dealloc (value) {
|
||||
return characters = '', value
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} type
|
||||
* @return {string}
|
||||
*/
|
||||
export function delimit (type) {
|
||||
return trim(slice(position - 1, delimiter(type === 91 ? type + 2 : type === 40 ? type + 1 : type)))
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} value
|
||||
* @return {string[]}
|
||||
*/
|
||||
export function tokenize (value) {
|
||||
return dealloc(tokenizer(alloc(value)))
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} type
|
||||
* @return {string}
|
||||
*/
|
||||
export function whitespace (type) {
|
||||
while (character = peek())
|
||||
if (character < 33)
|
||||
next()
|
||||
else
|
||||
break
|
||||
|
||||
return token(type) > 2 || token(character) > 3 ? '' : ' '
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string[]} children
|
||||
* @return {string[]}
|
||||
*/
|
||||
export function tokenizer (children) {
|
||||
while (next())
|
||||
switch (token(character)) {
|
||||
case 0: append(identifier(position - 1), children)
|
||||
break
|
||||
case 2: append(delimit(character), children)
|
||||
break
|
||||
default: append(from(character), children)
|
||||
}
|
||||
|
||||
return children
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} index
|
||||
* @param {number} count
|
||||
* @return {string}
|
||||
*/
|
||||
export function escaping (index, count) {
|
||||
while (--count && next())
|
||||
// not 0-9 A-F a-f
|
||||
if (character < 48 || character > 102 || (character > 57 && character < 65) || (character > 70 && character < 97))
|
||||
break
|
||||
|
||||
return slice(index, caret() + (count < 6 && peek() == 32 && next() == 32))
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} type
|
||||
* @return {number}
|
||||
*/
|
||||
export function delimiter (type) {
|
||||
while (next())
|
||||
switch (character) {
|
||||
// ] ) " '
|
||||
case type:
|
||||
return position
|
||||
// " '
|
||||
case 34: case 39:
|
||||
if (type !== 34 && type !== 39)
|
||||
delimiter(character)
|
||||
break
|
||||
// (
|
||||
case 40:
|
||||
if (type === 41)
|
||||
delimiter(type)
|
||||
break
|
||||
// \
|
||||
case 92:
|
||||
next()
|
||||
break
|
||||
}
|
||||
|
||||
return position
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} type
|
||||
* @param {number} index
|
||||
* @return {number}
|
||||
*/
|
||||
export function commenter (type, index) {
|
||||
while (next())
|
||||
// //
|
||||
if (type + character === 47 + 10)
|
||||
break
|
||||
// /*
|
||||
else if (type + character === 42 + 42 && peek() === 47)
|
||||
break
|
||||
|
||||
return '/*' + slice(index, position - 1) + '*' + from(type === 47 ? type : next())
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} index
|
||||
* @return {string}
|
||||
*/
|
||||
export function identifier (index) {
|
||||
while (!token(peek()))
|
||||
next()
|
||||
|
||||
return slice(index, position)
|
||||
}
|
||||
+115
@@ -0,0 +1,115 @@
|
||||
/**
|
||||
* @param {number}
|
||||
* @return {number}
|
||||
*/
|
||||
export var abs = Math.abs
|
||||
|
||||
/**
|
||||
* @param {number}
|
||||
* @return {string}
|
||||
*/
|
||||
export var from = String.fromCharCode
|
||||
|
||||
/**
|
||||
* @param {object}
|
||||
* @return {object}
|
||||
*/
|
||||
export var assign = Object.assign
|
||||
|
||||
/**
|
||||
* @param {string} value
|
||||
* @param {number} length
|
||||
* @return {number}
|
||||
*/
|
||||
export function hash (value, length) {
|
||||
return charat(value, 0) ^ 45 ? (((((((length << 2) ^ charat(value, 0)) << 2) ^ charat(value, 1)) << 2) ^ charat(value, 2)) << 2) ^ charat(value, 3) : 0
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} value
|
||||
* @return {string}
|
||||
*/
|
||||
export function trim (value) {
|
||||
return value.trim()
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} value
|
||||
* @param {RegExp} pattern
|
||||
* @return {string?}
|
||||
*/
|
||||
export function match (value, pattern) {
|
||||
return (value = pattern.exec(value)) ? value[0] : value
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} value
|
||||
* @param {(string|RegExp)} pattern
|
||||
* @param {string} replacement
|
||||
* @return {string}
|
||||
*/
|
||||
export function replace (value, pattern, replacement) {
|
||||
return value.replace(pattern, replacement)
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} value
|
||||
* @param {string} search
|
||||
* @return {number}
|
||||
*/
|
||||
export function indexof (value, search) {
|
||||
return value.indexOf(search)
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} value
|
||||
* @param {number} index
|
||||
* @return {number}
|
||||
*/
|
||||
export function charat (value, index) {
|
||||
return value.charCodeAt(index) | 0
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} value
|
||||
* @param {number} begin
|
||||
* @param {number} end
|
||||
* @return {string}
|
||||
*/
|
||||
export function substr (value, begin, end) {
|
||||
return value.slice(begin, end)
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} value
|
||||
* @return {number}
|
||||
*/
|
||||
export function strlen (value) {
|
||||
return value.length
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {any[]} value
|
||||
* @return {number}
|
||||
*/
|
||||
export function sizeof (value) {
|
||||
return value.length
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {any} value
|
||||
* @param {any[]} array
|
||||
* @return {any}
|
||||
*/
|
||||
export function append (value, array) {
|
||||
return array.push(value), value
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string[]} array
|
||||
* @param {function} callback
|
||||
* @return {string}
|
||||
*/
|
||||
export function combine (array, callback) {
|
||||
return array.map(callback).join('')
|
||||
}
|
||||
Reference in New Issue
Block a user