Lettering.js icon indicating copy to clipboard operation
Lettering.js copied to clipboard

Added custom data attributes, character escape function, and character class for kerning pairs

Open alienresident opened this issue 11 years ago • 6 comments

Changes to LetteringJS

  • Copies the text to custom data attributes of the span.
    • Uses the method to create data attribute i.e. data-char, data-word and data-line
  • Escapes the text before copying it using an 'escape' function. Makes sure that common HTML attributes are properly escaped: quotation marks, ampersands, less than or greater than symbols.
  • Only if the 'char' method is being used it add a addition class with the letter value. Adds a class char - item value i.e. If the letter is a it's adds a class of char-a.
    • you can escape non-alphanumeric characters in css using \ i.e class="char-&" can be styled .char-\& see CSS character escape sequences for more details.
    • This allows you create kerning pairs. You can create kerning pair rules for all the character combos. You won't need to know the content beforehand. Use case could be on a CMS where you won't know the headings beforehand but want strict kerning rules.

Use cases (codepens)

Collection of examples LetteringJS Collection

Incorporates Ideas/Work from Other Pull Requests

#41 Add a character-specific class to letters
#26 Add data-content='item' attribute to the generated span tags
#6 Added additional classes

I am sure I have missed something and this approach may be overly simplistic.

Thanks, Mark

alienresident avatar Nov 08 '13 04:11 alienresident

Thanks for this commit. Really pulling things together. Let's figure this out b/c there's quite a few requests that do the same thing but they've all become a little too complex.

char-* classes

The .char- class seems a bit redundant. It seems you could style kerning pairs with a simple:

/* Edit: Updated example. It's too early. Need more coffee */
[data-char="a"] + [data-char="b"] {
  /* do something */
}

Escaped characters

* Does escaping `&` mean that we'd end up with `char-&` or `char-&`? What's the best behavior? * Is escaping `&` sort of an XHTML thing?

EDIT: Re-re-read and looked at your char-\& example. Suppose that's fine but explaining escaped chars might be sorta difficult to designer'y types.

  • Do we need quotes?

Non-latin languages

I think you've solved it the best with a limited escape set, but are we sure this problem is solved fairly well?

Next steps

What might make sense is schedule this for a v0.7 branch. We can even go so far as to write some tests to make sure features, etc are preserved. Sound good?

davatron5000 avatar Nov 08 '13 16:11 davatron5000

Hi Dave, Thanks for the quick feedback I've made some quick changes.

Removed the .char- classes and the if statement.

Removed the unnecessary .char- classes and the if statement. I also updated the Kerning Pairs demo using the [data-char="a"] + [data-char="b"]. Great idea and it's so elegant.

Escape Characters

The escape function in the JS just escapes the copied text so that it doesn't break the html when copied to the data attribute.
Without the .char- classes you no longer need to do the hacky css escapes .char-\& you can use [data-char="&"] the quotes around escape the & already (without the quotes you need to [data-char=\&]) Here's a codepen demoing the escape characters.

  • You DO need to escape the " quote [data-char="\""].
  • Need to come up with a solution for space character [data-char=" "] or [data-char="\ "] doesn't work.

Non-latin languages and Next Steps

Needs a lot more testing for non-latin characters!
v0.7 branch makes sense. Let me know how I can help!

alienresident avatar Nov 08 '13 17:11 alienresident

Need to come up with a solution for space character [data-char=" "] or [data-char="\ "] doesn't work.

It might be a v1 thing since it breaks current functionality, but maybe not-wrapping spaces is a better solution. I know we've run into it not producing the correct results: http://fishslapsababy.com/2013/01/09/stuff-with-friends/ (it's a blog for kids, go easy :smile:) might make sense to deprecate wrapping spaces.

EDIT: On second thought, removing space-wrapping may actually break kerning pairs since two words would be next to each other. :frowning:

davatron5000 avatar Nov 08 '13 17:11 davatron5000

I was breaking the space character!

In the escape function I was breaking the space character! .replace(/ /g, ' ') was removed from line 20
I was changing space to   (which isn't the right character anyway). Now [data-char="\ "] works. I updated the codepen.

alienresident avatar Nov 08 '13 18:11 alienresident

Opened up example/index.html

<div id="demo3" class="demo">
  <p>
    <span class="line1" data-line="This is an amazing">This is an amazing</span>
    <span class="line2" data-line=" Revolution in Typography. "> Revolution in Typography. </span>
    <span class="line3" data-line=" The possibilities are endless: "> The possibilities are endless: </span>
    <span class="line4" data-line=" Coloring, Vertical spacing, and Kerning."> Coloring, Vertical spacing, and Kerning.</span>
    </p>
</div>

Looks like we might have to $.trim() some whitespace from in the lettering('lines') method. This actually came up in #42 as well. So let's add that to the milestone. Get that wrapped up in a separate commit.

davatron5000 avatar Nov 23 '13 03:11 davatron5000

Bookmarking: Escape function from #7 might be useful here for a complete escaping setup (if we still need it in the data-char attribute)

https://github.com/davatron5000/Lettering.js/blob/80d53da7ea753af9836923dc09a23171e35161fe/jquery.lettering.js#L14-L30

davatron5000 avatar Nov 26 '13 18:11 davatron5000