From 50025832947a2121e999c68fdc81dccb5ef0cf9e Mon Sep 17 00:00:00 2001 From: Chirayu Krishnappa Date: Thu, 11 Sep 2014 23:38:49 -0700 Subject: [PATCH] fix(resource_url_resolver): fix DOM parsing for Safari Closes #1439 --- lib/core_dom/resource_url_resolver.dart | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/lib/core_dom/resource_url_resolver.dart b/lib/core_dom/resource_url_resolver.dart index f40a31c8f..9c124f63a 100644 --- a/lib/core_dom/resource_url_resolver.dart +++ b/lib/core_dom/resource_url_resolver.dart @@ -15,6 +15,10 @@ import 'package:di/annotations.dart'; import 'package:angular/core_dom/type_to_uri_mapper.dart'; +class _NullTreeSanitizer implements NodeTreeSanitizer { + void sanitizeTree(dom.Node node) {} +} + @Injectable() class ResourceUrlResolver { static final RegExp cssUrlRegexp = new RegExp(r'''(\burl\((?:[\s]+)?)(['"]?)([^]*)(\2(?:[\s]+)?\))'''); @@ -32,14 +36,27 @@ class ResourceUrlResolver { ResourceUrlResolver(this._uriMapper, this._config); + static final NodeTreeSanitizer _nullTreeSanitizer = new _NullTreeSanitizer(); + + static Node _parseHtmlString(String html) { + HtmlDocument doc = new DomParser().parseFromString( + "$html", "text/html"); + if (doc != null) { + return doc.body; + } + // Workaround for Safari (can't parse HTML documents via the DomParser) + doc = document.implementation.createHtmlDocument(""); + doc.body.setInnerHtml(html, treeSanitizer: _nullTreeSanitizer); + return doc.body; + } + String resolveHtml(String html, [Uri baseUri]) { if (baseUri == null) { return html; } - HtmlDocument document = new DomParser().parseFromString( - "$html", "text/html"); - _resolveDom(document.body, baseUri); - return document.body.innerHtml; + Node node = _parseHtmlString(html); + _resolveDom(node, baseUri); + return node.innerHtml; } /**