rrdom (#613)
* create rrdom package * test(rrdom): add unit tests for polyfill.ts * fix(rrweb snapshot): type check errors Errors are caused by the declaration similarity of @types/mocha and @types/jest if we install both of them in the whole project. * Set tagNames to upper case by default This mirrors the `Element.tagName` implementation: ``` For DOM trees which represent HTML documents, the returned tag name is always in the canonical upper-case form. For example, tagName called on a <div> element returns "DIV". ``` https://developer.mozilla.org/en-US/docs/Web/API/Element/tagName * Add workspace file * VSCode settings for rrdom tests * Add basic test for RRDocument * Only setup jest tests for rrdom * mock Node type and Event type for nodejs environment * test(rrdom): add snapshot for document.test.ts * fix issue of nwsapi import and add unit tests for rrdom * fix: querySelectorAll returns nothing when querying elements with ids and classNames * fix: error of unit test for Event polyfill Since Event class is built in nodejs after v15.0.0 * add a dummy implementation of canvas * add style element support * add unit test for style element Co-authored-by: Justin Halsall <Juice10@users.noreply.github.com>
This commit is contained in:
40
packages/rrdom/src/style.ts
Normal file
40
packages/rrdom/src/style.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
export function parseCSSText(cssText: string): Record<string, string> {
|
||||
const res: Record<string, string> = {};
|
||||
const listDelimiter = /;(?![^(]*\))/g;
|
||||
const propertyDelimiter = /:(.+)/;
|
||||
cssText.split(listDelimiter).forEach(function (item) {
|
||||
if (item) {
|
||||
const tmp = item.split(propertyDelimiter);
|
||||
tmp.length > 1 && (res[camelize(tmp[0].trim())] = tmp[1].trim());
|
||||
}
|
||||
});
|
||||
return res;
|
||||
}
|
||||
|
||||
export function toCSSText(style: Record<string, string>): string {
|
||||
const properties = [];
|
||||
for (let name in style) {
|
||||
const value = style[name];
|
||||
if (typeof value !== 'string') continue;
|
||||
const normalizedName = hyphenate(name);
|
||||
properties.push(`${normalizedName}:${value};`);
|
||||
}
|
||||
return properties.join(' ');
|
||||
}
|
||||
|
||||
/**
|
||||
* Camelize a hyphen-delimited string.
|
||||
*/
|
||||
const camelizeRE = /-(\w)/g;
|
||||
export const camelize = (str: string): string => {
|
||||
return str.replace(camelizeRE, (_, c) => (c ? c.toUpperCase() : ''));
|
||||
};
|
||||
|
||||
/**
|
||||
* Hyphenate a camelCase string.
|
||||
*/
|
||||
const hyphenateRE = /\B([A-Z])/g;
|
||||
export const hyphenate = (str: string): string => {
|
||||
return str.replace(hyphenateRE, '-$1').toLowerCase();
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user