diff --git a/README.md b/README.md index 0264fc3..a36af84 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ Blocks are expressed as: - **type**: either `js`, `css` or `remove` - **alternate search path**: (optional) By default the input files are relative to the treated file. Alternate search path allows one to change that - **path**: the file path of the optimized file, the target output -- **parameters**: extra parameters that should be added to the tag +- **parameters**: extra parameters that should be added to the tag. By default `rel="stylesheet"` attribute is added to css link tag, your can overwrite it by passing your own rel parameter, e.g. `rel="preload"` An example of this in completed form can be seen below: diff --git a/lib/refManager.js b/lib/refManager.js index d723ef2..6bc641d 100644 --- a/lib/refManager.js +++ b/lib/refManager.js @@ -16,17 +16,25 @@ module.exports = { transformCSSRefs: function (block, target, attbs) { var ref = ''; + var rel = 'rel="stylesheet" '; // css link element regular expression // TODO: Determine if 'href' attribute is present. var regcss = /|\))/gmi; + // rel attribute regular expression + var regrel = /(^|\s)rel=/i; + // if rel exists in attributes, set the default one empty + if (regrel.test(attbs)) { + rel = ''; + } + // Check to see if there are any css references at all. if (block.search(regcss) !== -1) { if (attbs) { - ref = ''; + ref = ''; } else { - ref = ''; + ref = ''; } } diff --git a/test/test.js b/test/test.js index 38e1e02..b395204 100644 --- a/test/test.js +++ b/test/test.js @@ -144,6 +144,12 @@ describe('html-ref-replace', function() { expect(result[1]).to.eql({ css: { '/css/combined.css': { 'assets': [ '/css/one.css', '/css/two.css' ] }}}); }); + it('should replace css blocks with rel', function() { + var result = useRef(fread(djoin('testfiles/15-rel.html'))); + expect(result[0]).to.equal(fread(djoin('testfiles/15-rel-expected.html'))); + expect(result[1]).to.eql({ css: { '/css/combined.css': { 'assets': [ '/css/one.css', '/css/two.css' ] }}}); + }); + it('should reserve IE conditional comments', function() { var result = useRef(fread(djoin('testfiles/16.html'))); expect(result[0]).to.equal(fread(djoin('testfiles/16-expected.html'))); diff --git a/test/testfiles/15-rel-expected.html b/test/testfiles/15-rel-expected.html new file mode 100644 index 0000000..7e87b5a --- /dev/null +++ b/test/testfiles/15-rel-expected.html @@ -0,0 +1,5 @@ + +
+ + + \ No newline at end of file diff --git a/test/testfiles/15-rel.html b/test/testfiles/15-rel.html new file mode 100644 index 0000000..02f36d4 --- /dev/null +++ b/test/testfiles/15-rel.html @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file