From 10934a8617272ca941d5384e26f42eea20c96ee0 Mon Sep 17 00:00:00 2001 From: Steve Orvell Date: Tue, 14 Jan 2014 08:54:11 -0800 Subject: [PATCH] factor out path and add scope stub. --- html-imports.js | 2 + src/path.js | 181 ++++++++++++++++++++++++++++++++++++++++++++++++ src/scope.js | 6 ++ 3 files changed, 189 insertions(+) create mode 100644 src/path.js create mode 100644 src/scope.js diff --git a/html-imports.js b/html-imports.js index ccc038b..2477ffb 100644 --- a/html-imports.js +++ b/html-imports.js @@ -9,6 +9,8 @@ var thisFile = 'html-imports.js'; var scopeName = 'HTMLImports'; var modules = [ + 'src/scope.js', + 'src/path.js', 'src/HTMLImports.js', 'src/Parser.js', 'src/boot.js' diff --git a/src/path.js b/src/path.js new file mode 100644 index 0000000..7c16717 --- /dev/null +++ b/src/path.js @@ -0,0 +1,181 @@ +/* + * Copyright 2013 The Polymer Authors. All rights reserved. + * Use of this source code is governed by a BSD-style + * license that can be found in the LICENSE file. + */ + +(function(scope) { + +var URL_ATTRS = ['href', 'src', 'action']; +var URL_ATTRS_SELECTOR = '[' + URL_ATTRS.join('],[') + ']'; +var URL_TEMPLATE_SEARCH = '{{.*}}'; +var CSS_URL_REGEXP = /(url\()([^)]*)(\))/g; +var CSS_IMPORT_REGEXP = /(@import[\s]*)([^;]*)(;)/g; + +var path = { + nodeUrl: function(node) { + var docUrl = path.documentUrlFromNode(node); + return path.resolveUrl(docUrl, path.hrefOrSrc(node)); + //return path.resolveUrl(path.documentURL, path.hrefOrSrc(node)); + }, + hrefOrSrc: function(node) { + return node.getAttribute("href") || node.getAttribute("src"); + }, + documentUrlFromNode: function(node) { + return path.getDocumentUrl(node.ownerDocument || node); + }, + getDocumentUrl: function(doc) { + var url = doc && + // TODO(sjmiles): ShadowDOMPolyfill intrusion + (doc._URL || (doc.impl && doc.impl._URL) + || doc.baseURI || doc.URL) + || ''; + // take only the left side if there is a # + return url.split('#')[0]; + }, + resolveUrl: function(baseUrl, url) { + if (this.isAbsUrl(url)) { + return url; + } + return this.compressUrl(this.urlToPath(baseUrl) + url); + }, + resolveRelativeUrl: function(baseUrl, url) { + if (this.isAbsUrl(url)) { + return url; + } + return this.makeDocumentRelPath(this.resolveUrl(baseUrl, url)); + }, + isAbsUrl: function(url) { + return /(^data:)|(^http[s]?:)|(^\/)/.test(url); + }, + urlToPath: function(baseUrl) { + var parts = baseUrl.split("/"); + parts.pop(); + parts.push(''); + return parts.join("/"); + }, + compressUrl: function(url) { + var search = ''; + var searchPos = url.indexOf('?'); + // query string is not part of the path + if (searchPos > -1) { + search = url.substring(searchPos); + url = url.substring(searchPos, 0); + } + var parts = url.split('/'); + for (var i=0, p; i