Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,46 @@ Results in:
<script type="text/javascript" src="scripts/this.js"></script>
<script type="text/javascript" src="scripts/that.js"></script>
```
#### options.strictCssXML

Type: `Boolean`
Defaul: `false`

Adds `/` self closure to the `link` tag to make it strict XML compliant to prevent some XML parsers from failing to properly parse and validate the document.

```html
<!-- build:css css/combined.css -->
<link href="css/one.css" rel="stylesheet">
<link href="css/two.css" rel="stylesheet">
<!-- endbuild -->
```

```html
<link rel="stylesheet" href="css/combined.css"/>
```

#### options.strictScriptXML

Type: `Boolean`
Defaul: `false`

Adds `//<![CDATA[ //]]>` block to the script tag to prevent some XML parsers from changing the empty tag (`<script></script>`) into a self-closed tag (`<script />`). This can be an issue with pre-processed HTML in JSPX.

```html
<!-- build:js scripts/combined.js -->
<script type="text/javascript" src="scripts/this.js"></script>
<script type="text/javascript" src="scripts/that.js"></script>
<!-- endbuild -->
```

```html
<script type="text/javascript" src="scripts/combined.js">//<![CDATA[ //]]></script>
```

## Contributing

See the [CONTRIBUTING Guidelines](https://github.com/jonkemp/useref/blob/master/CONTRIBUTING.md)

## License

MIT © [Jonathan Kemp](http://jonkemp.com)
MIT © [Jonathan Kemp](http://jonkemp.com)
30 changes: 26 additions & 4 deletions lib/refManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ module.exports = {
bb.type = props.type;
bb.attbs = props.attbs;
bb.alternateSearchPaths = props.alternateSearchPaths;
bb.strictCssXML =
options && options.strictCssXML;
bb.strictScriptXML =
options && options.strictScriptXML;
},

transformCSSRefs: function (block, target, attbs) {
Expand All @@ -21,12 +25,21 @@ module.exports = {
// TODO: Determine if 'href' attribute is present.
var regcss = /<?link.*?(?:>|\))/gmi;

// end closing tag for the link element
var closingTag = '>';

// Check to see if there are any css references at all.
if (block.search(regcss) !== -1) {
if (bb && bb.strictCssXML) {
// make end closing tag self closing
// if strict XML type needed
closingTag = '/>';
}

if (attbs) {
ref = '<link rel="stylesheet" href="' + target + '" ' + attbs + '>';
ref = '<link rel="stylesheet" href="' + target + '" ' + attbs + closingTag;
} else {
ref = '<link rel="stylesheet" href="' + target + '">';
ref = '<link rel="stylesheet" href="' + target + '"' + closingTag;
}
}

Expand All @@ -40,12 +53,21 @@ module.exports = {
// TODO: Detect 'src' attribute.
var regscript = /<?script\(?\b[^<]*(?:(?!<\/script>|\))<[^<]*)*(?:<\/script>|\))/gmi;

// general closing for a script tag
var closingTag = '</script>';

// Check to see if there are any js references at all.
if (block.search(regscript) !== -1) {
if (bb && bb.strictScriptXML) {
// append cdata block to script element
// if strict XML type needed
closingTag = '//<![CDATA[ //]]>' + closingTag;
}

if (attbs) {
ref = '<script src="' + target + '" ' + attbs + '></script>';
ref = '<script src="' + target + '" ' + attbs + '>' + closingTag;
} else {
ref = '<script src="' + target + '"></script>';
ref = '<script src="' + target + '">' + closingTag;
}
}

Expand Down
13 changes: 13 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,4 +248,17 @@ describe('html-ref-replace', function() {
}
});
});


it('should replace reference in css block and return replaced files', function() {
var result = useRef(fread(djoin('testfiles/28-strictCssXML.html')), { strictCssXML: true });
expect(result[0]).to.equal(fread(djoin('testfiles/28-strictCssXML-expected.html')));
expect(result[1]).to.eql({ css: { '/css/combined.css': { 'assets': [ '/css/one.css', '/css/two.css' ] }}});
});

it('should replace reference in js block and return replaced files', function() {
var result = useRef(fread(djoin('testfiles/29-strictScriptXML.html')), { strictScriptXML: true });
expect(result[0]).to.equal(fread(djoin('testfiles/29-strictScriptXML-expected.html')));
expect(result[1]).to.eql({ js: { 'scripts/combined.concat.min.js': { 'assets': [ 'scripts/this.js', 'scripts/that.js' ] }}});
});
});
12 changes: 6 additions & 6 deletions test/testfiles/16-win-expected.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<html>
<head>
<!--[if lt IE 9]>
<script src="scripts/combined.js"></script>
<![endif]-->
</head>
<html>
<head>
<!--[if lt IE 9]>
<script src="scripts/combined.js"></script>
<![endif]-->
</head>
</html>
18 changes: 9 additions & 9 deletions test/testfiles/16-win.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<html>
<head>
<!-- build:js scripts/combined.js -->
<!--[if lt IE 9]>
<script type="text/javascript" src="scripts/this.js"></script>
<script type="text/javascript" src="scripts/that.js"></script>
<![endif]-->
<!-- endbuild -->
</head>
<html>
<head>
<!-- build:js scripts/combined.js -->
<!--[if lt IE 9]>
<script type="text/javascript" src="scripts/this.js"></script>
<script type="text/javascript" src="scripts/that.js"></script>
<![endif]-->
<!-- endbuild -->
</head>
</html>
5 changes: 5 additions & 0 deletions test/testfiles/28-strictCssXML-expected.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<head>
<link rel="stylesheet" href="/css/combined.css"/>
</head>
</html>
8 changes: 8 additions & 0 deletions test/testfiles/28-strictCssXML.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>
<!-- build:css /css/combined.css -->
<link href="/css/one.css" rel="stylesheet">
<link href="/css/two.css" rel="stylesheet">
<!-- endbuild -->
</head>
</html>
6 changes: 6 additions & 0 deletions test/testfiles/29-strictScriptXML-expected.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html>
<head></head>
<body>
<script src="scripts/combined.concat.min.js">//<![CDATA[ //]]></script>
</body>
</html>
9 changes: 9 additions & 0 deletions test/testfiles/29-strictScriptXML.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html>
<head></head>
<body>
<!-- build:js scripts/combined.concat.min.js -->
<script type="text/javascript" src="scripts/this.js"></script>
<script type="text/javascript" src="scripts/that.js"></script>
<!-- endbuild -->
</body>
</html>