This repository has been archived by the owner on Dec 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrebase.js
119 lines (110 loc) · 2.86 KB
/
rebase.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* Rebase.js
* */
/**
* rebase
* @param content{String}
* @param options{Object}
* @param references{String} [optional]
* */
var rebase = module.exports = function( content, options ){
if( !options ) return content
for( var scope in options ){
var rewriter = rebase[scope]
scope = options[scope]
if( rewriter && scope ) {
if ( scope.forEach ) {
scope.forEach(function( rule ){
if ( rule.tag ) {
content = rebase.tag(content, rule.tag, rule.attr, rule.base, rule.rebase)
}
else {
content = rewriter(content, rule.base, rule.rebase)
}
})
}
else {
for( var base in scope ){
content = rewriter(content, base, scope[base])
}
}
}
}
return content
}
/**
* "any string"
* */
rebase.string = function( content, search, replace ){
search = new RegExp("^"+search)
return content.replace(/('|")((?:\\\1|.)*?)\1/g, function( match, str, inside ){
if ( search.test(inside) ) {
inside = inside.replace(search, replace)
}
return str + inside + str
})
}
/**
* <tag attr="">
* */
var tag = rebase.tag = function( content, tag, attr, search, replace ){
tag = new RegExp("<"+tag+"([^>]+)>", 'g') // capture attribute space
attr = new RegExp("("+attr+')\\s*=\\s*"\\s*([^"]+)\\s*"') // capture attribute name and value
search = new RegExp("^"+search)
return content.replace(tag, function( match ){
return match.replace(attr, function( match, attr, value ){
if ( search.test(value) ) {
return attr+'="'+value.replace(search, replace)+'"'
}
else return match
})
})
}
/**
* <script type="text/javascript" src=""></script>
* */
rebase.script = function( content, search, replace ){
return tag(content, "script", "src", search, replace)
}
/**
* <link rel="stylesheet" href=""/>
* */
rebase.link = function( content, search, replace ){
return tag(content, "link", "href", search, replace)
}
/**
* <a href=""></a>
* */
rebase.a = function( content, search, replace ){
return tag(content, "a", "href", search, replace)
}
/**
* <img src="" alt=""/>
* */
rebase.img = function( content, search, replace ){
return tag(content, "img", "src", search, replace)
}
/**
* background-image: url("");
* */
rebase.url = function( content, search, replace ){
search = new RegExp("^"+search)
return content.replace(/url\(\s*['"]?([^'"]+?)['"]?\s*\)/g, function( match, url ){
if ( search.test(url) ) {
return "url("+url.replace(search, replace)+")"
}
else return match
})
}
/**
* @import "";
* */
rebase.imports = function( content, search, replace ){
search = new RegExp("^"+search)
return content.replace(/@import\s+"([^")]+)"/g, function( match, url ){
if ( search.test(url) ) {
return "@import \""+url.replace(search, replace)+"\""
}
else return match
})
}