2014-09-19 16:38:37 -07:00
|
|
|
library angular.core.facade.dom;
|
2014-09-18 14:56:38 -07:00
|
|
|
|
|
|
|
import 'dart:html';
|
2015-01-13 13:06:09 -08:00
|
|
|
import 'dart:js' show JsObject, context;
|
2014-09-18 14:56:38 -07:00
|
|
|
|
2015-01-07 21:58:40 -08:00
|
|
|
export 'dart:html' show DocumentFragment, Node, Element, TemplateElement, Text, document, location, window;
|
2014-09-19 16:38:37 -07:00
|
|
|
|
2014-11-11 17:33:47 -08:00
|
|
|
// TODO(tbosch): Is there a builtin one? Why is Dart
|
|
|
|
// removing unknown elements by default?
|
|
|
|
class IdentitySanitizer implements NodeTreeSanitizer {
|
|
|
|
void sanitizeTree(Node node) {}
|
|
|
|
}
|
|
|
|
|
2015-01-13 13:06:09 -08:00
|
|
|
var _window = context['window'];
|
|
|
|
var _gc = context['gc'];
|
|
|
|
|
|
|
|
gc() {
|
|
|
|
if (_gc != null) {
|
|
|
|
_gc.apply(const []);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-11 17:33:47 -08:00
|
|
|
final identitySanitizer = new IdentitySanitizer();
|
|
|
|
|
2014-09-18 14:56:38 -07:00
|
|
|
class DOM {
|
|
|
|
static query(selector) {
|
2014-10-02 12:27:01 -07:00
|
|
|
return document.querySelector(selector);
|
2014-09-18 14:56:38 -07:00
|
|
|
}
|
2014-11-07 14:30:04 -08:00
|
|
|
static Element querySelector(el, String selector) {
|
|
|
|
return el.querySelector(selector);
|
|
|
|
}
|
2014-10-27 11:47:13 -04:00
|
|
|
static ElementList querySelectorAll(el, String selector) {
|
|
|
|
return el.querySelectorAll(selector);
|
|
|
|
}
|
2014-09-18 14:56:38 -07:00
|
|
|
static on(element, event, callback) {
|
|
|
|
element.addEventListener(event, callback);
|
|
|
|
}
|
|
|
|
static getInnerHTML(el) {
|
|
|
|
return el.innerHtml;
|
|
|
|
}
|
2014-11-11 17:33:47 -08:00
|
|
|
static getOuterHTML(el) {
|
|
|
|
return el.outerHtml;
|
|
|
|
}
|
2014-09-28 13:55:01 -07:00
|
|
|
static setInnerHTML(el, value) {
|
2014-09-18 14:56:38 -07:00
|
|
|
el.innerHtml = value;
|
|
|
|
}
|
2014-10-27 11:47:13 -04:00
|
|
|
static Node firstChild(el) {
|
|
|
|
return el.firstChild;
|
|
|
|
}
|
2014-12-11 13:58:26 -08:00
|
|
|
static Node nextSibling(el) {
|
|
|
|
return el.nextNode;
|
|
|
|
}
|
2014-11-11 17:33:47 -08:00
|
|
|
static Element parentElement(el) {
|
|
|
|
return el.parent;
|
|
|
|
}
|
2014-10-27 11:47:13 -04:00
|
|
|
static List<Node> childNodes(el) {
|
|
|
|
return el.childNodes;
|
|
|
|
}
|
2015-01-02 14:23:59 -08:00
|
|
|
static childNodesAsList(el) {
|
|
|
|
return childNodes(el).toList();
|
|
|
|
}
|
2014-12-23 10:45:20 -08:00
|
|
|
static clearNodes(el) {
|
|
|
|
el.nodes = [];
|
|
|
|
}
|
2014-11-11 17:33:47 -08:00
|
|
|
static appendChild(el, node) {
|
|
|
|
el.append(node);
|
|
|
|
}
|
2014-11-21 15:13:01 -08:00
|
|
|
static removeChild(el, node) {
|
|
|
|
node.remove();
|
|
|
|
}
|
2015-01-02 14:23:59 -08:00
|
|
|
static insertBefore(el, node) {
|
|
|
|
el.parentNode.insertBefore(node, el);
|
|
|
|
}
|
|
|
|
static insertAllBefore(el, nodes) {
|
|
|
|
el.parentNode.insertAllBefore(nodes, el);
|
|
|
|
}
|
2014-11-21 15:13:01 -08:00
|
|
|
static insertAfter(el, node) {
|
|
|
|
el.parentNode.insertBefore(node, el.nextNode);
|
|
|
|
}
|
2014-12-05 17:44:00 -08:00
|
|
|
static getText(Element el) {
|
|
|
|
return el.text;
|
|
|
|
}
|
2014-09-26 11:20:08 -07:00
|
|
|
static setText(Text text, String value) {
|
|
|
|
text.text = value;
|
|
|
|
}
|
2014-09-28 13:55:01 -07:00
|
|
|
static createTemplate(html) {
|
2014-11-11 17:33:47 -08:00
|
|
|
var t = new TemplateElement();
|
|
|
|
t.setInnerHtml(html, treeSanitizer:identitySanitizer);
|
2014-09-28 13:55:01 -07:00
|
|
|
return t;
|
|
|
|
}
|
2014-11-07 14:30:04 -08:00
|
|
|
static createElement(tagName, [doc=null]) {
|
|
|
|
if (doc == null) doc = document;
|
|
|
|
return doc.createElement(tagName);
|
|
|
|
}
|
2015-01-02 14:23:59 -08:00
|
|
|
static createScriptTag(String attrName, String attrValue, [doc=null]) {
|
|
|
|
if (doc == null) doc = document;
|
|
|
|
var el = doc.createElement("SCRIPT");
|
|
|
|
el.setAttribute(attrName, attrValue);
|
|
|
|
return el;
|
|
|
|
}
|
2014-09-28 20:02:32 -07:00
|
|
|
static clone(Node node) {
|
|
|
|
return node.clone(true);
|
|
|
|
}
|
2014-12-09 10:31:19 -08:00
|
|
|
static hasProperty(Element element, String name) {
|
|
|
|
return new JsObject.fromBrowserObject(element).hasProperty(name);
|
2014-11-11 17:33:47 -08:00
|
|
|
}
|
2014-10-10 20:44:55 -07:00
|
|
|
static getElementsByClassName(Element element, String name) {
|
|
|
|
return element.getElementsByClassName(name);
|
|
|
|
}
|
|
|
|
static getElementsByTagName(Element element, String name) {
|
|
|
|
return element.querySelectorAll(name);
|
|
|
|
}
|
2014-11-11 17:33:47 -08:00
|
|
|
static List classList(Element element) {
|
|
|
|
return element.classes.toList();
|
|
|
|
}
|
|
|
|
static addClass(Element element, classname) {
|
|
|
|
element.classes.add(classname);
|
|
|
|
}
|
|
|
|
static hasClass(Element element, classname) {
|
|
|
|
return element.classes.contains(classname);
|
|
|
|
}
|
2015-01-02 14:23:59 -08:00
|
|
|
static String tagName(Element element) {
|
|
|
|
return element.tagName;
|
|
|
|
}
|
2014-11-11 17:33:47 -08:00
|
|
|
static attributeMap(Element element) {
|
|
|
|
return element.attributes;
|
|
|
|
}
|
2015-01-02 14:23:59 -08:00
|
|
|
static getAttribute(Element element, String attribute) {
|
|
|
|
return element.getAttribute(attribute);
|
|
|
|
}
|
2014-11-14 10:37:42 -08:00
|
|
|
static Node templateAwareRoot(Element el) {
|
|
|
|
return el is TemplateElement ? el.content : el;
|
|
|
|
}
|
2014-11-07 14:30:04 -08:00
|
|
|
static HtmlDocument createHtmlDocument() {
|
|
|
|
return document.implementation.createHtmlDocument('fakeTitle');
|
|
|
|
}
|
|
|
|
static HtmlDocument defaultDoc() {
|
|
|
|
return document;
|
|
|
|
}
|
2015-01-02 14:23:59 -08:00
|
|
|
static bool elementMatches(n, String selector) {
|
|
|
|
return n is Element && n.matches(selector);
|
|
|
|
}
|
2014-09-26 11:20:08 -07:00
|
|
|
}
|