From 8647886de3b25a5567dfc3fbaac11d1823b1cb48 Mon Sep 17 00:00:00 2001 From: Johann du Toit Date: Mon, 27 Feb 2017 00:19:58 +0200 Subject: [PATCH] Added the rule with docs and test suite --- docs/bgsound.md | 23 +++++++++++ lib/rules/bgsound.js | 57 +++++++++++++++++++++++++++ lib/rules/index.js | 3 +- samples/bgsound.missing.html | 5 +++ samples/bgsound.present.html | 6 +++ test/bgsound.js | 76 ++++++++++++++++++++++++++++++++++++ 6 files changed, 169 insertions(+), 1 deletion(-) create mode 100644 docs/bgsound.md create mode 100644 lib/rules/bgsound.js create mode 100644 samples/bgsound.missing.html create mode 100644 samples/bgsound.present.html create mode 100644 test/bgsound.js diff --git a/docs/bgsound.md b/docs/bgsound.md new file mode 100644 index 0000000..fe4a0a8 --- /dev/null +++ b/docs/bgsound.md @@ -0,0 +1,23 @@ +Sounds on websites have rough history. While playing sounds can give more options to grab a users attention, using `` is generally seen as very **BAD** form. + +There are various ways to play sounds using Javscript that use common standards which can be used rather. + +The big issue with using `` is that users have no way to control when the sound starts and no way to stop it. + +# How do I fix this ? + +Playing audio can happen straight from plain javascript in most popular browsers: + +``` +var audio = new Audio('sound.mp3'); +audio.play(); +``` + +Frameworks such as [howler.js](http://goldfirestudios.com/blog/104/howler.js-Modern-Web-Audio-Javascript-Library) also exist that provides more features in multiple browsers. + +# Resources + +* [howler.js](http://goldfirestudios.com/blog/104/howler.js-Modern-Web-Audio-Javascript-Library) +* [HTMLAudioElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLAudioElement) +* [Playing audio with Javascript](http://stackoverflow.com/questions/9419263/playing-audio-with-javascript) +* [Is using bgsound a bad idea](https://www.thecodingforums.com/threads/is-using-bgsound-a-bad-idea.687818/) diff --git a/lib/rules/bgsound.js b/lib/rules/bgsound.js new file mode 100644 index 0000000..d346cd8 --- /dev/null +++ b/lib/rules/bgsound.js @@ -0,0 +1,57 @@ +// modules +var S = require('string'); +var _ = require('underscore'); +var cheerio = require('cheerio'); + +// actual hashes that check the content +var swfCodePattern = new RegExp(/"(.*?).swf"|'(.*?).swf'/gi); + +// expose the actual function +module.exports = exports = function(payload, fn){ + + // get the data + var data = payload.getData(); + + // get the page content to start using it + payload.getPageContent(function(err, content) { + + // check for errors, else stop + if(err) return fn(err); + + // check if content was given + if(S(content || '').isEmpty() == true) { + + // done + return fn(null); + + } + + // load in our cheerio context + $ = cheerio.load(content); + + // get all the object tags + $('bgsound').each(function(i, elem) { + + // add the rule + payload.addRule({ + + type: 'error', + message: 'Do not use the element', + key: 'bgsound' + + }, { + + message: '$ is very much looked down on', + type: 'text', + identifiers: [ 'bgsound' ] + + }); + + }); + + // add all our rules + fn(null); + + }); + +}; \ No newline at end of file diff --git a/lib/rules/index.js b/lib/rules/index.js index b66b7ef..cc8e70b 100644 --- a/lib/rules/index.js +++ b/lib/rules/index.js @@ -2,6 +2,7 @@ module.exports = exports = [ require('./w3c'), - require('./plugins/flash') + require('./plugins/flash'), + require('./bgsound') ]; diff --git a/samples/bgsound.missing.html b/samples/bgsound.missing.html new file mode 100644 index 0000000..dcb5313 --- /dev/null +++ b/samples/bgsound.missing.html @@ -0,0 +1,5 @@ + + +

No annoying sound in the background ...

+ + \ No newline at end of file diff --git a/samples/bgsound.present.html b/samples/bgsound.present.html new file mode 100644 index 0000000..7b00504 --- /dev/null +++ b/samples/bgsound.present.html @@ -0,0 +1,6 @@ + + +

Some annoying sound in the background ...

+ + + \ No newline at end of file diff --git a/test/bgsound.js b/test/bgsound.js new file mode 100644 index 0000000..a09117d --- /dev/null +++ b/test/bgsound.js @@ -0,0 +1,76 @@ +// modules +var assert = require('assert') +var _ = require('underscore') +var fs = require('fs'); +var passmarked = require('passmarked'); +var pluginFunc = require('../lib/rules/plugins/applets'); + +describe('bgsound', function(){ + + // handle the settings + it('Should not return a error error if is present', function(done){ + + // create a dummy payload + payload = passmarked.createPayload( + { + + url: 'http://example.com' + + }, + {}, + fs.readFileSync('./samples/bgsound.missing.html').toString()); + + // handle the stream + pluginFunc(payload, function(err){ + + // check for a error + if(err) assert.fail(err); + + // get the rules + var rules = payload.getRules(); + + // check if we got any rules back ... + if(rules.length > 0) + assert.fail('Was not expecting a error'); + + // done + done(); + + }); + + }); + + // handle the settings + it('Should not return a error error if is missing', function(done){ + + // create a dummy payload + payload = passmarked.createPayload( + { + + url: 'http://example.com' + + }, + {}, + fs.readFileSync('./samples/bgsound.missing.html').toString()); + + // handle the stream + pluginFunc(payload, function(err){ + + // check for a error + if(err) assert.fail(err); + + // get the rules + var rules = payload.getRules(); + + // check if we got any rules back ... + if(rules.length > 0) + assert.fail('Was expecting an error'); + + // done + done(); + + }); + + }); + +}); \ No newline at end of file