-
Notifications
You must be signed in to change notification settings - Fork 593
/
Copy pathxml.ts
30 lines (25 loc) · 1.1 KB
/
xml.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/// <reference path="../built/pxtlib.d.ts" />
export function getBlocksWithType(parent: Document | Element, type: string) {
return getChildrenWithAttr(parent, "block", "type", type).concat(getChildrenWithAttr(parent, "shadow", "type", type));
}
export function getChildrenWithAttr(parent: Document | Element, tag: string, attr: string, value: string) {
return pxt.Util.toArray(parent.getElementsByTagName(tag)).filter(b => b.getAttribute(attr) === value);
}
export function getFirstChildWithAttr(parent: Document | Element, tag: string, attr: string, value: string) {
const res = getChildrenWithAttr(parent, tag, attr, value);
return res.length ? res[0] : undefined;
}
export function getDirectChildren(parent: Element, tag: string) {
const res: Element[] = [];
for (let i = 0; i < parent.childNodes.length; i++) {
const n = parent.childNodes.item(i) as Element;
if (n.tagName === tag) {
res.push(n);
}
}
return res;
}
export function cleanOuterHTML(el: HTMLElement): string {
// remove IE11 junk
return el.outerHTML.replace(/^<\?[^>]*>/, '');
}