|
| 1 | +/** |
| 2 | + * @file 测试一下 replace-tag-attribute 功能是否正常 |
| 3 | + * @author Firede([email protected]) |
| 4 | + */ |
| 5 | + |
| 6 | +var replaceTagAttribute = require('../lib/util/replace-tag-attribute'); |
| 7 | + |
| 8 | +describe('replace-tag-attribute', function () { |
| 9 | + |
| 10 | + var expectValue = '<head><script src="/root/path/file.js" type="text/javascript"></script></head>'; |
| 11 | + |
| 12 | + it('default', function () { |
| 13 | + var value = replaceTagAttribute( |
| 14 | + '<head><script src="${root}/path/file.js" type="text/javascript"></script></head>', |
| 15 | + 'script', |
| 16 | + 'src', |
| 17 | + function (val) { |
| 18 | + return val.replace('${root}', '/root'); |
| 19 | + } |
| 20 | + ); |
| 21 | + |
| 22 | + expect(value).toBe(expectValue); |
| 23 | + }); |
| 24 | + |
| 25 | + it('attribute value include `<`', function () { |
| 26 | + var value = replaceTagAttribute( |
| 27 | + '<head><script src="<$root>/path/file.js" type="text/javascript"></script></head>', |
| 28 | + 'script', |
| 29 | + 'src', |
| 30 | + function (val) { |
| 31 | + return val.replace('<$root>', '/root'); |
| 32 | + } |
| 33 | + ); |
| 34 | + |
| 35 | + expect(value).toBe(expectValue); |
| 36 | + }); |
| 37 | + |
| 38 | + it('script-x should not matched', function () { |
| 39 | + // FIXME 这个 case 暂时还通不过 |
| 40 | + // 当搜索 tag 为 script 时,会匹配到以 script 为前缀的所有 tag |
| 41 | + // 要先确定当前有没有误用的情况,再修复此问题 |
| 42 | + var textScriptX = '<head><script-x src="${root}/path/file.js" type="text/javascript"></script></head>'; |
| 43 | + var value = replaceTagAttribute( |
| 44 | + textScriptX, |
| 45 | + 'script', |
| 46 | + 'src', |
| 47 | + function (val) { |
| 48 | + return val.replace('${root}', '/root'); |
| 49 | + } |
| 50 | + ); |
| 51 | + console.log(value); |
| 52 | + expect(value).toBe(textScriptX); |
| 53 | + }); |
| 54 | + |
| 55 | +}); |
0 commit comments