MCPcopy Create free account
hub / github.com/angular/angular / stringifyElement

Function stringifyElement

packages/private/testing/src/browser_utils.ts:45–96  ·  view source on GitHub ↗
(el: Element)

Source from the content-addressed store, hash-verified

43
44const _selfClosingTags = ['br', 'hr', 'input'];
45export function stringifyElement(el: Element): string {
46 let result = '';
47 if (getDOM().isElementNode(el)) {
48 const tagName = el.tagName.toLowerCase();
49
50 // Opening tag
51 result += `<${tagName}`;
52
53 // Attributes in an ordered way
54 const attributeMap = getAttributeMap(el);
55 const sortedKeys = Array.from(attributeMap.keys()).sort();
56 for (const key of sortedKeys) {
57 const lowerCaseKey = key.toLowerCase();
58 let attValue = attributeMap.get(key);
59
60 if (typeof attValue !== 'string') {
61 result += ` ${lowerCaseKey}`;
62 } else {
63 // Browsers order style rules differently. Order them alphabetically for consistency.
64 if (lowerCaseKey === 'style') {
65 attValue = attValue
66 .split(/; ?/)
67 .filter((s) => !!s)
68 .sort()
69 .map((s) => `${s};`)
70 .join(' ');
71 }
72
73 result += ` ${lowerCaseKey}="${attValue}"`;
74 }
75 }
76 result += '>';
77
78 // Children
79 const childrenRoot = templateAwareRoot(el);
80 const children = childrenRoot ? childrenRoot.childNodes : [];
81 for (let j = 0; j < children.length; j++) {
82 result += stringifyElement(children[j]);
83 }
84
85 // Closing tag
86 if (_selfClosingTags.indexOf(tagName) == -1) {
87 result += `</${tagName}>`;
88 }
89 } else if (isCommentNode(el)) {
90 result += `<!--${el.nodeValue}-->`;
91 } else {
92 result += el.textContent;
93 }
94
95 return result;
96}
97
98export function createNgZone(): NgZone {
99 return new NgZone({enableLongStackTrace: true, shouldCoalesceEventChangeDetection: false});

Callers 2

expectHtmlFunction · 0.90
toHtmlFunction · 0.90

Calls 11

getDOMFunction · 0.85
getAttributeMapFunction · 0.85
templateAwareRootFunction · 0.85
isCommentNodeFunction · 0.85
mapMethod · 0.80
indexOfMethod · 0.80
keysMethod · 0.65
getMethod · 0.65
joinMethod · 0.65
isElementNodeMethod · 0.45
filterMethod · 0.45

Tested by

no test coverage detected