OLD | NEW |
(Empty) | |
| 1 /** |
| 2 * @license |
| 3 * Copyright (c) 2014 The Polymer Project Authors. All rights reserved. |
| 4 * This code may only be used under the BSD style license found at http://polyme
r.github.io/LICENSE.txt |
| 5 * The complete set of authors may be found at http://polymer.github.io/AUTHORS.
txt |
| 6 * The complete set of contributors may be found at http://polymer.github.io/CON
TRIBUTORS.txt |
| 7 * Code distributed by Google as part of the polymer project is also |
| 8 * subject to an additional IP rights grant found at http://polymer.github.io/PA
TENTS.txt |
| 9 */ |
| 10 |
| 11 var path = require('path'); |
| 12 var constants = require('./constants.js'); |
| 13 var utils = require('./utils.js'); |
| 14 var setTextContent = utils.setTextContent; |
| 15 var getTextContent = utils.getTextContent; |
| 16 var searchAll = utils.searchAll; |
| 17 |
| 18 function resolvePaths($, input, output, abspath) { |
| 19 var assetPath; |
| 20 if (abspath) { |
| 21 assetPath = rebasePath(input, abspath); |
| 22 } else { |
| 23 assetPath = path.relative(output, input); |
| 24 } |
| 25 // make sure assetpath is a folder, but not root! |
| 26 if (assetPath) { |
| 27 assetPath = utils.unixPath(assetPath) + '/'; |
| 28 } |
| 29 // resolve attributes |
| 30 searchAll($, constants.URL_ATTR_SEL).each(function() { |
| 31 var el = $(this); |
| 32 constants.URL_ATTR.forEach(function(a) { |
| 33 var val = el.attr(a); |
| 34 if (val) { |
| 35 if (val.search(constants.URL_TEMPLATE) < 0) { |
| 36 if (a === 'style') { |
| 37 el.attr(a, rewriteURL(input, output, val, abspath)); |
| 38 } else { |
| 39 el.attr(a, rewriteRelPath(input, output, val, abspath)); |
| 40 } |
| 41 } |
| 42 } |
| 43 }); |
| 44 }); |
| 45 searchAll($, constants.CSS).each(function() { |
| 46 var el = $(this); |
| 47 var text = rewriteURL(input, output, getTextContent(el), abspath); |
| 48 setTextContent(el, text); |
| 49 }); |
| 50 searchAll($, constants.ELEMENTS).each(function() { |
| 51 $(this).attr('assetpath', assetPath); |
| 52 }); |
| 53 } |
| 54 |
| 55 function rebasePath(absolutePath, baselinePath) { |
| 56 var absBase = new RegExp('^' + utils.escapeForRegExp(baselinePath)); |
| 57 return absolutePath.replace(absBase, ''); |
| 58 } |
| 59 |
| 60 function rewriteRelPath(inputPath, outputPath, rel, abspath) { |
| 61 if (constants.ABS_URL.test(rel)) { |
| 62 return rel; |
| 63 } |
| 64 |
| 65 var abs = path.resolve(inputPath, rel); |
| 66 |
| 67 if (abspath) { |
| 68 return utils.unixPath(rebasePath(abs, abspath)); |
| 69 } |
| 70 |
| 71 var relPath = path.relative(outputPath, abs); |
| 72 return utils.unixPath(relPath); |
| 73 } |
| 74 |
| 75 function rewriteURL(inputPath, outputPath, cssText, abspath) { |
| 76 return cssText.replace(constants.URL, function(match) { |
| 77 var path = match.replace(/["']/g, "").slice(4, -1); |
| 78 path = rewriteRelPath(inputPath, outputPath, path, abspath); |
| 79 return 'url("' + path + '")'; |
| 80 }); |
| 81 } |
| 82 |
| 83 exports.resolvePaths = resolvePaths; |
| 84 exports.rewriteRelPath = rewriteRelPath; |
| 85 exports.rewriteURL = rewriteURL; |
OLD | NEW |