Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Add @litejs/dom + update generateHtml method (#1825)" #1839

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@
"@gravity-ui/ui-logger": "^1.1.0",
"@gravity-ui/uikit": "^6.31.0",
"@jest/types": "^29.6.3",
"@litejs/dom": "^24.8.0",
"@microsoft/fetch-event-source": "^2.0.1",
"@playwright/test": "^1.48.2",
"@statoscope/cli": "^5.28.2",
Expand Down
13 changes: 0 additions & 13 deletions src/@types/light-js-dom.d.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,6 @@ export class Table extends React.PureComponent<TableProps, TableState> {
nullBeforeNumbers
// To avoid drawing No Data line when after pagination there is only a footer
renderEmptyRow={onlyFooterExists ? () => null : undefined}
onError={(error) => {
throw error;
}}
/>
);
}
Expand Down
62 changes: 4 additions & 58 deletions src/ui/libs/DatalensChartkit/modules/html-generator/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type {HtmlElement} from '@litejs/dom';
import {document as sandboxDocument} from '@litejs/dom';
import escape from 'lodash/escape';

import type {ChartKitHtmlItem} from '../../../../../shared';
Expand All @@ -24,13 +22,13 @@ type GenerateHtmlOptions = {
tooltipId?: string;
};

function jsonToHtml(
item?: ChartKitHtmlItem | string | (ChartKitHtmlItem | string)[],
export function generateHtml(
item?: ChartKitHtmlItem | ChartKitHtmlItem[] | string,
options: GenerateHtmlOptions = {},
): string {
if (item) {
if (Array.isArray(item)) {
return item.map((it) => jsonToHtml(it, options)).join('');
return item.map((it) => generateHtml(it, options)).join('');
}

if (typeof item === 'string') {
Expand Down Expand Up @@ -103,62 +101,10 @@ function jsonToHtml(
themeStyle = getThemeStyle(theme, dataThemeId);
}

elem.innerHTML = `${themeStyle}${jsonToHtml(content, nextOptions)}`;
elem.innerHTML = `${themeStyle}${generateHtml(content, nextOptions)}`;

return elem.outerHTML;
}

return '';
}

function nodeToJson(node: HtmlElement) {
if (!node?.tagName) {
return node.textContent ?? '';
}

const attrs: string[] | undefined = node.attributes?.names();
const style = node?.getAttribute('style');

let content: any = Array.from(node.childNodes).map((childNode) =>
nodeToJson(childNode as HtmlElement),
);
if (content?.length === 1) {
content = content[0];
}

const result: ChartKitHtmlItem = {
tag: node.tagName.toLowerCase(),
content,
attributes: attrs?.reduce((acc, attr) => {
return {
...acc,
[attr]: node?.getAttribute(attr),
};
}, {}),
style: style
? Object.fromEntries(style?.split(';').map((rule) => rule.split(':')))
: undefined,
};
return result;
}

function convertHtmlToJson(value: string) {
const fragment = sandboxDocument.createElement('div');
fragment.innerHTML = value;
const elements = Array.from(fragment.childNodes) as HtmlElement[];

if (elements.length > 1) {
return elements.map(nodeToJson);
}

return elements.length ? nodeToJson(elements[0]) : '';
}

export function generateHtml(item?: ChartKitHtmlItem | ChartKitHtmlItem[] | string): string {
if (typeof item === 'string') {
const json = convertHtmlToJson(item);
return jsonToHtml(json);
}

return jsonToHtml(item);
}
Loading