var parse5 = require('parse5/index'); import {ListWrapper, StringMapWrapper} from '../src/facade/collection'; import {DomAdapter, setRootDomAdapter} from '../platform_browser_private'; import {isPresent, isBlank, global, Type, setValueOnPath, DateWrapper} from '../src/facade/lang'; import {BaseException} from '../src/facade/exceptions'; import {SelectorMatcher, CssSelector} from '../compiler_private'; import {XHR} from '@angular/compiler'; var parser: any /** TODO #9100 */ = null; var serializer: any /** TODO #9100 */ = null; var treeAdapter: any /** TODO #9100 */ = null; var _attrToPropMap: {[key: string]: string} = { 'class': 'className', 'innerHtml': 'innerHTML', 'readonly': 'readOnly', 'tabindex': 'tabIndex', }; var defDoc: any /** TODO #9100 */ = null; var mapProps = ['attribs', 'x-attribsNamespace', 'x-attribsPrefix']; function _notImplemented(methodName: any /** TODO #9100 */) { return new BaseException('This method is not implemented in Parse5DomAdapter: ' + methodName); } /* tslint:disable:requireParameterType */ export class Parse5DomAdapter extends DomAdapter { static makeCurrent() { parser = new parse5.Parser(parse5.TreeAdapters.htmlparser2); serializer = new parse5.Serializer(parse5.TreeAdapters.htmlparser2); treeAdapter = parser.treeAdapter; setRootDomAdapter(new Parse5DomAdapter()); } hasProperty(element: any /** TODO #9100 */, name: string): boolean { return _HTMLElementPropertyList.indexOf(name) > -1; } // TODO(tbosch): don't even call this method when we run the tests on server side // by not using the DomRenderer in tests. Keeping this for now to make tests happy... setProperty(el: /*element*/ any, name: string, value: any) { if (name === 'innerHTML') { this.setInnerHTML(el, value); } else if (name === 'className') { el.attribs['class'] = el.className = value; } else { el[name] = value; } } // TODO(tbosch): don't even call this method when we run the tests on server side // by not using the DomRenderer in tests. Keeping this for now to make tests happy... getProperty(el: /*element*/ any, name: string): any { return el[name]; } logError(error: any /** TODO #9100 */) { console.error(error); } log(error: any /** TODO #9100 */) { console.log(error); } logGroup(error: any /** TODO #9100 */) { console.error(error); } logGroupEnd() {} getXHR(): Type { return XHR; } get attrToPropMap() { return _attrToPropMap; } query(selector: any /** TODO #9100 */) { throw _notImplemented('query'); } querySelector(el: any /** TODO #9100 */, selector: string): any { return this.querySelectorAll(el, selector)[0]; } querySelectorAll(el: any /** TODO #9100 */, selector: string): any[] { var res: any[] /** TODO #9100 */ = []; var _recursive = (result: any /** TODO #9100 */, node: any /** TODO #9100 */, selector: any /** TODO #9100 */, matcher: any /** TODO #9100 */) => { var cNodes = node.childNodes; if (cNodes && cNodes.length > 0) { for (var i = 0; i < cNodes.length; i++) { var childNode = cNodes[i]; if (this.elementMatches(childNode, selector, matcher)) { result.push(childNode); } _recursive(result, childNode, selector, matcher); } } }; var matcher = new SelectorMatcher(); matcher.addSelectables(CssSelector.parse(selector)); _recursive(res, el, selector, matcher); return res; } elementMatches( node: any /** TODO #9100 */, selector: string, matcher: any /** TODO #9100 */ = null): boolean { if (this.isElementNode(node) && selector === '*') { return true; } var result = false; if (selector && selector.charAt(0) == '#') { result = this.getAttribute(node, 'id') == selector.substring(1); } else if (selector) { var result = false; if (matcher == null) { matcher = new SelectorMatcher(); matcher.addSelectables(CssSelector.parse(selector)); } var cssSelector = new CssSelector(); cssSelector.setElement(this.tagName(node)); if (node.attribs) { for (var attrName in node.attribs) { cssSelector.addAttribute(attrName, node.attribs[attrName]); } } var classList = this.classList(node); for (var i = 0; i < classList.length; i++) { cssSelector.addClassName(classList[i]); } matcher.match( cssSelector, function(selector: any /** TODO #9100 */, cb: any /** TODO #9100 */) { result = true; }); } return result; } on(el: any /** TODO #9100 */, evt: any /** TODO #9100 */, listener: any /** TODO #9100 */) { var listenersMap: {[k: /*any*/ string]: any} = el._eventListenersMap; if (isBlank(listenersMap)) { var listenersMap: {[k: /*any*/ string]: any} = StringMapWrapper.create(); el._eventListenersMap = listenersMap; } var listeners = StringMapWrapper.get(listenersMap, evt); if (isBlank(listeners)) { listeners = []; } listeners.push(listener); StringMapWrapper.set(listenersMap, evt, listeners); } onAndCancel( el: any /** TODO #9100 */, evt: any /** TODO #9100 */, listener: any /** TODO #9100 */): Function { this.on(el, evt, listener); return () => { ListWrapper.remove(StringMapWrapper.get(el._eventListenersMap, evt), listener); }; } dispatchEvent(el: any /** TODO #9100 */, evt: any /** TODO #9100 */) { if (isBlank(evt.target)) { evt.target = el; } if (isPresent(el._eventListenersMap)) { var listeners: any = StringMapWrapper.get(el._eventListenersMap, evt.type); if (isPresent(listeners)) { for (var i = 0; i < listeners.length; i++) { listeners[i](evt); } } } if (isPresent(el.parent)) { this.dispatchEvent(el.parent, evt); } if (isPresent(el._window)) { this.dispatchEvent(el._window, evt); } } createMouseEvent(eventType: any /** TODO #9100 */): Event { return this.createEvent(eventType); } createEvent(eventType: string): Event { var evt = { type: eventType, defaultPrevented: false, preventDefault: () => { (evt).defaultPrevented = true; } }; return evt; } preventDefault(evt: any /** TODO #9100 */) { evt.returnValue = false; } isPrevented(evt: any /** TODO #9100 */): boolean { return isPresent(evt.returnValue) && !evt.returnValue; } getInnerHTML(el: any /** TODO #9100 */): string { return serializer.serialize(this.templateAwareRoot(el)); } getTemplateContent(el: any /** TODO #9100 */): Node { return null; // no