Skip to content

Commit 2186261

Browse files
committed
issue #104. add test case && bugfix.
1 parent b410ed6 commit 2186261

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

lib/util/replace-tag-attribute.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
*/
2929
module.exports = exports = function (content, tag, attribute, valueReplacer, condition) {
3030
var attrReg = new RegExp('(' + attribute + ')=([\'"])([^\'"]+)\\2');
31-
var tagReg = new RegExp('<' + tag + '([^>]+)', 'g');
31+
var tagReg = new RegExp('<' + tag + '(("[^"]*"|\'[^\']*\'|[^\'">])*)', 'g');
3232
function replacer(match, attrStr) {
3333
if (typeof condition === 'function' && !condition(match.slice(1))) {
3434
return match;

test/replace-tag-attribute.spec.js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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

Comments
 (0)