@garfish/utils
Advanced tools
Comparing version 0.0.35-alpha.3 to 0.0.35
export declare const __GARFISH_FLAG__: unique symbol; | ||
export declare const __MockBody__ = "__garfishmockbody__"; | ||
export declare const __MockHead__ = "__garfishmockhead__"; |
@@ -7,1 +7,2 @@ export * from './utils'; | ||
export * from './container'; | ||
export * from './templateParse'; |
@@ -45,12 +45,17 @@ const objectToString = Object.prototype.toString; | ||
const processError = (error, fn) => { | ||
if (typeof error === 'string') { | ||
error = `${warnPrefix}: ${error}\n\n`; | ||
fn(error, true); | ||
} | ||
else if (error instanceof Error) { | ||
if (!error.message.startsWith(warnPrefix)) { | ||
error.message = `${warnPrefix}: ${error.message}`; | ||
try { | ||
if (typeof error === 'string') { | ||
error = `${warnPrefix}: ${error}\n\n`; | ||
fn(error, true); | ||
} | ||
fn(error, false); | ||
else if (error instanceof Error) { | ||
if (!error.message.startsWith(warnPrefix)) { | ||
error.message = `${warnPrefix}: ${error.message}`; | ||
} | ||
fn(error, false); | ||
} | ||
} | ||
catch (e) { | ||
fn(error, typeof error === 'string'); | ||
} | ||
}; | ||
@@ -60,2 +65,3 @@ function warn(msg) { | ||
const warnMsg = isString ? e : e.message; | ||
if (false) ; | ||
console.warn(warnMsg); | ||
@@ -684,2 +690,4 @@ }); | ||
const __GARFISH_FLAG__ = Symbol.for('__GARFISH_FLAG__'); | ||
const __MockBody__ = '__garfishmockbody__'; | ||
const __MockHead__ = '__garfishmockhead__'; | ||
@@ -819,2 +827,79 @@ function parseContentType(input) { | ||
export { DOMApis, __GARFISH_FLAG__, __extends, assert, callTestCallback, computeErrorUrl, computeStackTraceFromStackProp, createAppContainer, createKey, deepMerge, def, error, evalWithEnv, filterAndWrapEventListener, findTarget, getRenderNode, hasOwn, inBrowser, internFunc, isAbsolute, isCss, isHtml, isJs, isObject, isPlainObject, isPrimitive, isPromise, makeMap, nextTick, noop, objectToString, parseContentType, remove, setDocCurrentScript, sourceListTags, sourceNode, toBoolean, transformUrl, unique, validURL, warn }; | ||
var ElementType; | ||
(function (ElementType) { | ||
ElementType[ElementType["TEXT"] = 3] = "TEXT"; | ||
ElementType[ElementType["COMMENT"] = 8] = "COMMENT"; | ||
ElementType[ElementType["ELEMENT"] = 1] = "ELEMENT"; | ||
})(ElementType || (ElementType = {})); | ||
function Attributes({ name, value }) { | ||
this.key = name; | ||
this.value = value; | ||
} | ||
const generateAttributes = (el) => { | ||
const list = []; | ||
const attrs = el.attributes; | ||
const len = attrs.length; | ||
if (len > 0) { | ||
// Optimize for the most common cases | ||
if (len === 1) { | ||
list[0] = new Attributes(attrs[0]); | ||
} | ||
else if (len === 2) { | ||
list[0] = new Attributes(attrs[0]); | ||
list[1] = new Attributes(attrs[1]); | ||
} | ||
else { | ||
for (let i = 0; i < len; i++) { | ||
list[i] = new Attributes(attrs[i]); | ||
} | ||
} | ||
} | ||
return list; | ||
}; | ||
const createElement = (el, filter) => { | ||
switch (el.nodeType) { | ||
case ElementType.TEXT: | ||
return { | ||
type: 'text', | ||
content: el.textContent, | ||
}; | ||
case ElementType.COMMENT: | ||
return { | ||
type: 'comment', | ||
content: el.textContent, | ||
}; | ||
case ElementType.ELEMENT: | ||
return filter({ | ||
type: 'element', | ||
tagName: el.tagName.toLowerCase(), | ||
attributes: generateAttributes(el), | ||
children: Array.from(el.childNodes).map((node) => { | ||
return createElement(node, filter); | ||
}), | ||
}); | ||
default: | ||
(process.env.NODE_ENV !== "production") && warn(`Invalid node type "${el.nodeType}"`); | ||
} | ||
}; | ||
// 1M text takes about time 60ms | ||
function templateParse(code, tags) { | ||
let astTree = []; | ||
const htmlNode = document.createElement('html'); | ||
const collectionEls = {}; | ||
const filter = (el) => { | ||
if (tags.includes(el.tagName)) { | ||
collectionEls[el.tagName].push(el); | ||
} | ||
return el; | ||
}; | ||
htmlNode.innerHTML = code; | ||
for (const tag of tags) { | ||
collectionEls[tag] = []; | ||
} | ||
astTree = Array.from(htmlNode.childNodes).map((node) => { | ||
return createElement(node, filter); | ||
}); | ||
return [astTree, collectionEls]; | ||
} | ||
export { DOMApis, __GARFISH_FLAG__, __MockBody__, __MockHead__, __extends, assert, callTestCallback, computeErrorUrl, computeStackTraceFromStackProp, createAppContainer, createKey, deepMerge, def, error, evalWithEnv, filterAndWrapEventListener, findTarget, getRenderNode, hasOwn, inBrowser, internFunc, isAbsolute, isCss, isHtml, isJs, isObject, isPlainObject, isPrimitive, isPromise, makeMap, nextTick, noop, objectToString, parseContentType, remove, setDocCurrentScript, sourceListTags, sourceNode, templateParse, toBoolean, transformUrl, unique, validURL, warn }; |
{ | ||
"name": "@garfish/utils", | ||
"version": "0.0.35-alpha.3", | ||
"version": "0.0.35", | ||
"description": "utils module.", | ||
@@ -40,3 +40,3 @@ "keywords": [ | ||
}, | ||
"gitHead": "4772634da31463994a4f45466175bc28d70cc2dc" | ||
"gitHead": "f6b04041cc3efa2755b6dd560c98843f7b2b5419" | ||
} |
275852
116
6303