From 18a2cd1edc751415b8db6d792bfc4c5f5359f597 Mon Sep 17 00:00:00 2001 From: Kasper Isager Date: Wed, 10 Feb 2021 11:35:05 +0100 Subject: [PATCH] Progress on source view component --- .../alfa-interviewer-console/package.json | 1 + .../src/component/source.tsx | 182 ++++++++++++++++++ .../test/console.spec.tsx | 28 +++ .../alfa-interviewer-console/tsconfig.json | 13 +- 4 files changed, 222 insertions(+), 2 deletions(-) create mode 100644 packages/alfa-interviewer-console/src/component/source.tsx create mode 100644 packages/alfa-interviewer-console/test/console.spec.tsx diff --git a/packages/alfa-interviewer-console/package.json b/packages/alfa-interviewer-console/package.json index a93d646dae..e21c0d8b59 100644 --- a/packages/alfa-interviewer-console/package.json +++ b/packages/alfa-interviewer-console/package.json @@ -19,6 +19,7 @@ ], "dependencies": { "@siteimprove/alfa-act": "^0.10.0", + "@siteimprove/alfa-dom": "^0.10.0", "@siteimprove/alfa-future": "^0.10.0", "@siteimprove/alfa-interviewer": "^0.10.0", "@siteimprove/alfa-option": "^0.10.0", diff --git a/packages/alfa-interviewer-console/src/component/source.tsx b/packages/alfa-interviewer-console/src/component/source.tsx new file mode 100644 index 0000000000..e6b61f3eeb --- /dev/null +++ b/packages/alfa-interviewer-console/src/component/source.tsx @@ -0,0 +1,182 @@ +import React, { FunctionComponent } from "react"; + +import * as ink from "ink"; + +import * as dom from "@siteimprove/alfa-dom"; + +export const Source: FunctionComponent = ({ source }) => ( + +); + +export namespace Source { + export interface Props { + source: dom.Node; + } +} + +const Node: FunctionComponent = ({ source }) => { + if (dom.Element.isElement(source)) { + return ; + } + + if (dom.Attribute.isAttribute(source)) { + return ; + } + + if (dom.Text.isText(source)) { + return ; + } + + if (dom.Document.isDocument(source)) { + return ; + } + + if (dom.Type.isType(source)) { + return ; + } + + if (dom.Shadow.isShadow(source)) { + return ; + } + + return null; +}; + +namespace Node { + export interface Props { + source: dom.Node; + } +} + +const Document: FunctionComponent = ({ source }) => { + const children = source.children(); + + return ( + + #document + + + {children.isEmpty() + ? null + : children.map((child, index) => )} + + + ); +}; + +namespace Document { + export interface Props { + source: dom.Document; + } +} + +const Type: React.FunctionComponent = ({ source }) => ( + + {""} + +); + +namespace Type { + export interface Props { + source: dom.Type; + } +} + +const Shadow: React.FunctionComponent = ({ source }) => { + const children = source.children(); + + return ( + + #shadow-root ({source.mode}) + + + {children.isEmpty() + ? null + : children.map((child, index) => )} + + + ); +}; + +namespace Shadow { + export interface Props { + source: dom.Shadow; + } +} + +const Element: React.FunctionComponent = ({ source }) => { + const children = source.children({ + composed: true, + nested: true, + }); + + return ( + + + {"<"} + {source.name} + + {[...source.attributes].map((attribute, index) => ( + + + + ))} + + {">"} + + + + {children.isEmpty() + ? null + : children.map((child, index) => )} + + + {source.isVoid() ? null : ( + + {" + {source.name} + {">"} + + )} + + ); +}; + +namespace Element { + export interface Props { + source: dom.Element; + } +} + +const Attribute: React.FunctionComponent = ({ source }) => ( + + {source.name} + {'="'} + {source.value} + {'"'} + +); + +namespace Attribute { + export interface Props { + source: dom.Attribute; + } +} + +const Text: React.FunctionComponent = ({ source }) => { + const { data } = source; + + if (data.trim() === "") { + return null; + } + + return "{data}"; +}; + +namespace Text { + export interface Props { + source: dom.Text; + } +} diff --git a/packages/alfa-interviewer-console/test/console.spec.tsx b/packages/alfa-interviewer-console/test/console.spec.tsx new file mode 100644 index 0000000000..0e2549f038 --- /dev/null +++ b/packages/alfa-interviewer-console/test/console.spec.tsx @@ -0,0 +1,28 @@ +import React from "react"; + +import { render } from "ink"; + +import { h } from "@siteimprove/alfa-dom/h"; + +import { Source } from "../src/component/source"; + +render( + +); diff --git a/packages/alfa-interviewer-console/tsconfig.json b/packages/alfa-interviewer-console/tsconfig.json index 53a1894549..b496a4c6d5 100644 --- a/packages/alfa-interviewer-console/tsconfig.json +++ b/packages/alfa-interviewer-console/tsconfig.json @@ -3,13 +3,22 @@ "extends": "../tsconfig.json", "compilerOptions": { "esModuleInterop": true, - "jsxFactory": "React.createElement" + "jsxFactory": "React.createElement", + "jsxFragmentFactory": "React.Fragment" }, - "files": ["src/console.tsx", "src/index.ts"], + "files": [ + "src/component/source.tsx", + "src/console.tsx", + "src/index.ts", + "test/console.spec.tsx" + ], "references": [ { "path": "../alfa-act" }, + { + "path": "../alfa-dom" + }, { "path": "../alfa-future" },