From f54999226c2aa3e7c04db49dc5c26de4ad2a67d5 Mon Sep 17 00:00:00 2001 From: Hugo Giraudel Date: Thu, 7 Jul 2016 12:22:23 +0200 Subject: [PATCH] Remove jQuery dependency --- js/halfstyle.js | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/js/halfstyle.js b/js/halfstyle.js index 2bad267..08353d7 100644 --- a/js/halfstyle.js +++ b/js/halfstyle.js @@ -3,29 +3,31 @@ * Copyright 2014 Arbel Hakopian * Licensed under MIT (https://github.com/arbelh/HalfStyle/blob/master/license.md) */ -jQuery(function($) { - var halfstyle_text, halfstyle_chars, $halfstyle_el, halfstyle_i, halfstyle_output, halfstyle_style; +(function () { + var nodes = document.querySelectorAll('.textToHalfStyle'); - // Iterate over all class occurrences - $('.textToHalfStyle').each(function(idx, halfstyle_el) { - $halfstyle_el = $(halfstyle_el); - halfstyle_style = $halfstyle_el.data('halfstyle'); - halfstyle_text = $halfstyle_el.text(); - halfstyle_chars = halfstyle_text.split(''); + Array.prototype.forEach.call(nodes, function (node, index) { + var halfStyle = node.getAttribute('data-halfstyle'); + var text = node.innerText || node.textContent; + var chars = text.split(''); + var styles = [ + 'position: absolute !important', + 'clip: rect(1px 1px 1px 1px)', + 'clip: rect(1px, 1px, 1px, 1px)' + ].join(';'); - // Set the screen-reader text - $halfstyle_el.html('' + halfstyle_text + ''); + var output = '' + text + ''; - // Reset output for appending - halfstyle_output = ''; + for (var i = 0; i < chars.length; i++) { + var attributes = [ + 'aria-hidden="true"', + 'class="halfStyle ' + halfStyle + '"', + 'data-content="' + chars[i] + '"' + ].join(' '); - // Iterate over all chars in the text - for (halfstyle_i = 0; halfstyle_i < halfstyle_chars.length; halfstyle_i++) { - // Create a styled element for each character and append to container - halfstyle_output += ''; + output += '' + chars[i] + ''; } - - // Write to DOM only once - $halfstyle_el.append(halfstyle_output); + + node.innerHTML = output; }); -}); \ No newline at end of file +}());