Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 22 additions & 20 deletions js/halfstyle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('<span style="position: absolute !important;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);">' + halfstyle_text + '</span>');
var output = '<span style="' + styles + '">' + text + '</span>';

// 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 += '<span aria-hidden="true" class="halfStyle ' + halfstyle_style + '" data-content="' + halfstyle_chars[halfstyle_i] + '">' + halfstyle_chars[halfstyle_i] + '</span>';
output += '<span ' + attributes + '>' + chars[i] + '</span>';
}

// Write to DOM only once
$halfstyle_el.append(halfstyle_output);

node.innerHTML = output;
});
});
}());