Skip to content

Commit

Permalink
Special handling for link tags that aren’t stylesheets
Browse files Browse the repository at this point in the history
  • Loading branch information
dvonlehman committed Mar 15, 2016
1 parent d9ae0bf commit 2bed276
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
18 changes: 16 additions & 2 deletions lib/attribute-modifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ var _ = require('lodash');

var absoluteUrlRe = /^http[s]?:\/\//i;
var validSrcAttributeTags = ['iframe', 'img', 'script', 'audio', 'video', 'embed', 'input'];
var linkRelAttributeUrlValues = ['alternate', 'help', 'license', 'next', 'prev', 'search'];
var inlineCssUrlRe = /url\(["']?(.*?)["']?\)/;

module.exports = function(options) {
var customAttribute = require('./custom-attribute')(options.attrPrefix);

return function(tagName, attribs) {
// Strip off any extra leading slashes from anchor tags
if (tagName === 'a' && attribs.href) {
if (isHrefHyperlink(tagName, attribs)) {
attribs.href = updateHrefAttribute(attribs.href);
}

Expand All @@ -20,7 +21,7 @@ module.exports = function(options) {
var srcAttr;
if (attribs.src) {
srcAttr = 'src';
} else if (tagName === 'link' && attribs.href) {
} else if (isResourceLink(tagName, attribs)) {
srcAttr = 'href';
} else {
return;
Expand Down Expand Up @@ -137,6 +138,19 @@ module.exports = function(options) {
return items.join('/');
}

function isResourceLink(tagName, attribs) {
if (tagName !== 'link' || !attribs.href) return false;
if (!_.includes(linkRelAttributeUrlValues, attribs.rel)) return true;
return false;
}

// Determine if this tag has an href attribute that is a hyperlink to another URL.
function isHrefHyperlink(tagName, attribs) {
if (tagName === 'a' && attribs.href) return true;
if (tagName === 'link' && _.includes(linkRelAttributeUrlValues, attribs.rel)) return true;
return false;
}

function stripExtraLeadingSlash(pathAttr) {
// If there is a leading double slash, detect if this is a valid
// non-protocol path like //fonts.google.com/ or a rogue double-slash like
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "htmlprep",
"version": "1.3.0",
"version": "1.4.0",
"description": "High-performance streaming HTML pre-processor designed to run in middleware.",
"main": "index.js",
"scripts": {
Expand Down
16 changes: 16 additions & 0 deletions test/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,4 +272,20 @@ describe('htmlprep attributes', function() {
done();
});
});

it('updates rss link attributes', function(done) {
var html = '<html><link href="https://__baseurl__/index.xml" rel="alternate" type="application/rss+xml" title="RSS feed"/></html>';

var opts = {
baseUrlPlaceholder: 'https://__baseurl__',
baseUrl: 'https://mysite.com'
};

run(html, opts, function(err, output) {
if (err) return done(err);

assert.equal(output, '<html><link href="https://mysite.com/index.xml" rel="alternate" type="application/rss+xml" title="RSS feed"/></html>');
done();
});
});
});

0 comments on commit 2bed276

Please sign in to comment.