Last active
April 26, 2018 16:13
-
-
Save loomismilitia/d9262a662a0f78985a1f1966fa634344 to your computer and use it in GitHub Desktop.
Angular WYSIWYG utils
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* WYSIWYG utils | |
* | |
* @namespace WYSIWYGUtils | |
*/ | |
(function() { | |
"use strict"; | |
angular | |
.module('wysiwyg.utils', []) | |
.factory('WYSIWYGUtils', WYSIWYGUtils); | |
/** | |
* @namespace WYSIWYGUtils | |
* @desc Simple wysiwyg editor utilities. | |
* @memberOf WYSIWYGUtils | |
*/ | |
function WYSIWYGUtils() { | |
return { | |
/** | |
* Decreases all occurrences of font-size by the provide factor for the given html string | |
* @param html HTML string to search for font-size attributes | |
* @param factor Value to subtract on the font-size | |
* @returns {String} html string with updated font-size value | |
*/ | |
decreaseFontSize: function (html, factor) { | |
if(!html || !factor){ | |
return ""; | |
} | |
return applyExpression(html, factor, subtract); | |
}, | |
/** | |
* Increases all occurrences of font-size by the provide factor for the given html string | |
* @param html HTML string to search for font-size attributes | |
* @param factor Value to add on the font-size | |
* @returns {String} html string with updated font-size value | |
*/ | |
increaseFontSize: function (html, factor) { | |
if(!html || !factor){ | |
return ""; | |
} | |
return applyExpression(html, factor, add); | |
} | |
}; | |
/** | |
* Apply regex expression | |
* @param html html string | |
* @param factor value to add or subtract in font-size | |
* @param operation add or subtract function | |
* @returns {String} html string with updated font-size value | |
*/ | |
function applyExpression(html, factor, operation) { | |
// $1 == (font-size:) | |
// $2 == (value \m/) | |
// $3 == (px;) | |
var expression = /(font-size:\s*)(\d+)(\s*px;)/g; | |
return html.replace(expression, function(match, $1, $2, $3){ | |
return $1 + operation(parseInt($2), factor) + $3; | |
}); | |
} | |
/** | |
* Add value | |
* @param value value | |
* @param factor amount to add | |
* @returns {Integer} sum of value plus factor | |
*/ | |
function add(value, factor) { | |
return value + factor; | |
} | |
/** | |
* Subtract value | |
* @param value value | |
* @param factor amount to subtract | |
* @returns {Integer} sum of value minus factor | |
*/ | |
function subtract(value, factor) { | |
return value - factor; | |
} | |
} | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
describe('wysiwyg.utils factory', function() { | |
var WYSIWYGUtils; | |
// Before each test load our wysiwyg.utils module | |
beforeEach(module('wysiwyg.utils')); | |
// beforeEach(angular.mock.module('wysiwyg.utils')); | |
// Before each test set our injected WYSIWYGUtils factory (_WYSIWYGUtils_) to our local WYSIWYGUtils variable | |
beforeEach(inject(function(_WYSIWYGUtils_){ | |
WYSIWYGUtils = _WYSIWYGUtils_; | |
})); | |
// A simple test to verify the WYSIWYGUtils factory exists | |
it('should exist', function() { | |
expect(WYSIWYGUtils).toBeDefined(); | |
}); | |
it('should increase the font-size value by a factor of 2', function() { | |
var factor = 2; | |
var html = '<span style="font-size: 6px;"></span><span style="font-size: 10px;"></span>'; | |
var expected = '<span style="font-size: 8px;"></span><span style="font-size: 12px;"></span>'; | |
expect(expected).toEqual(WYSIWYGUtils.increaseFontSize(html, factor)); | |
// expect(expected).toBe('technical death metal \m/'); // this test should fail | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment