Skip to content

Commit 3054c05

Browse files
committed
feat(logmethod): add logNgHTML method to console
1 parent 4c54bdb commit 3054c05

6 files changed

Lines changed: 125 additions & 13 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { ComponentFixture } from '@angular/core/testing';
2+
import { DebugElement } from '@angular/core';
3+
import { highlight, Theme } from 'pretty-html-log';
4+
import { prettyPrintFixture } from './pretty-fixture';
5+
import {
6+
prettyPrintDebugElement,
7+
prettyPrintDebugElements
8+
} from './pretty-debugElement';
9+
import {
10+
prettyPrintHtmlEleemnt,
11+
prettyPrintHtmlElements
12+
} from './pretty-htmlelement';
13+
14+
export type NgHTMLElement<T> =
15+
| ComponentFixture<T>
16+
| DebugElement
17+
| DebugElement[]
18+
| HTMLElement
19+
| HTMLElement[];
20+
21+
export const logNgHTML = <T>(
22+
ngHTMLElement: NgHTMLElement<T>,
23+
theme?: Theme
24+
) => {
25+
if (ngHTMLElement instanceof ComponentFixture) {
26+
prettyPrintFixture<T>(ngHTMLElement);
27+
return;
28+
}
29+
30+
if (Array.isArray(ngHTMLElement)) {
31+
if (ngHTMLElement[0] instanceof DebugElement) {
32+
prettyPrintDebugElements(ngHTMLElement as DebugElement[]);
33+
return;
34+
}
35+
36+
if (ngHTMLElement[0] instanceof HTMLElement) {
37+
prettyPrintHtmlElements(ngHTMLElement as HTMLElement[]);
38+
return;
39+
}
40+
}
41+
42+
if (ngHTMLElement instanceof DebugElement) {
43+
prettyPrintDebugElement(ngHTMLElement);
44+
return;
45+
}
46+
47+
if (ngHTMLElement instanceof HTMLElement) {
48+
prettyPrintHtmlEleemnt(ngHTMLElement);
49+
return;
50+
}
51+
console.log(
52+
'@angular-extension/pretty-html-log: Unknown type - try to format anyway'
53+
);
54+
console.log(highlight(ngHTMLElement as any, theme));
55+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { ComponentFixture } from '@angular/core/testing';
2+
import { highlight, Theme } from 'pretty-html-log';
3+
import { DebugElement } from '@angular/core';
4+
5+
export const prettyPrintDebugElement = (
6+
debugElement: DebugElement,
7+
theme?: Theme
8+
): void => {
9+
console.log(prettyDebugelement(debugElement, theme));
10+
};
11+
12+
export const prettyPrintDebugElements = (
13+
debugElements: DebugElement[],
14+
theme?: Theme
15+
): void => {
16+
for (let i = 0; i < debugElements.length; i++) {
17+
console.log(`--------- Degubelement ${i} -----------
18+
${prettyDebugelement(debugElements[i], theme)}
19+
`);
20+
}
21+
};
22+
23+
export const prettyDebugelement = (
24+
debugElement: DebugElement,
25+
theme?: Theme
26+
): string => {
27+
return highlight(debugElement.nativeElement.innerHTML, theme);
28+
};
Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { highlight } from 'pretty-html-log';
2-
import { Theme } from 'pretty-html-log';
31
import { ComponentFixture } from '@angular/core/testing';
2+
import { highlight, Theme } from 'pretty-html-log';
43

54
export const prettyPrintFixture = <T>(
65
fixture: ComponentFixture<T>,
@@ -15,13 +14,3 @@ export const prettyFixture = <T>(
1514
): string => {
1615
return highlight(fixture.debugElement.nativeElement.innerHTML, theme);
1716
};
18-
19-
(() => {
20-
(console as any).logFixture = prettyPrintFixture;
21-
})();
22-
23-
declare global {
24-
interface Console {
25-
logFixture: <T>(fixture: ComponentFixture<T>, theme?: Theme) => void;
26-
}
27-
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { Theme } from 'pretty-html-log';
2+
import { highlight } from 'pretty-html-log';
3+
4+
export const prettyPrintHtmlEleemnt = (
5+
htmlElement: HTMLElement,
6+
theme?: Theme
7+
): void => {
8+
console.log(prettyHtmlElement(htmlElement, theme));
9+
};
10+
11+
export const prettyPrintHtmlElements = (
12+
htmlElements: HTMLElement[],
13+
theme?: Theme
14+
): void => {
15+
for (let i = 0; i < htmlElements.length; i++) {
16+
console.log(`--------- HTMLElement ${i} -----------
17+
${prettyHtmlElement(htmlElements[i], theme)}
18+
`);
19+
}
20+
};
21+
22+
export const prettyHtmlElement = (
23+
htmlElement: HTMLElement,
24+
theme?: Theme
25+
): string => {
26+
return highlight(htmlElement.innerHTML, theme);
27+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { ComponentFixture } from '@angular/core/testing';
2+
import { Theme } from 'pretty-html-log';
3+
import { logNgHTML, NgHTMLElement } from './logNgHTML';
4+
5+
(() => {
6+
(console as any).logNgHTML = logNgHTML;
7+
})();
8+
9+
declare global {
10+
interface Console {
11+
logNgHTML: <T>(ngHTMLElement: any, theme?: Theme) => void;
12+
}
13+
}

projects/pretty-html-log/src/public-api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
* Public API Surface of pretty-html-log
33
*/
44

5-
export * from './lib/pretty-html-log';
5+
export * from './lib/setup-console-methods';

0 commit comments

Comments
 (0)