carene_waterman: An image of the Carina Nebula (Default)
[personal profile] carene_waterman
My second AO3 bookmarklet (StripEmpties) is a really simple tool to take out the extra blank paragraphs the AO3 parser puts in sometimes.

It was easy to code, fast to test, and I made it a little less error-prone by making sure the paragraphs hidden are only inside the chapters div.

Great.

Until I tried to post it to my GitHub page. The bookmarklet there is a link element with the javascript as the href, because a bookmarklet is really a link in your browser with the code as its URL. So you have to escape a bunch of characters that confuse the browser when it tries to write that html.

Which was all very simple until I had to get the code to compare the innerHTML of a paragraph to  . So I had to get my href and then the URL to not convert that to an actual unbreaking space.

You'd expect to be able to simply escape the ampersand with the HTML code and all would work out. No, somewhere either in GitHubs page creation system or in the browser that got converted to an actual unbreaking space.

I finally, after much trial and frustrating error, figured out that I needed to end up with the URL escape code for ampersand in my bookmarklet. so #26nbsp;. But just putting that in in the HTML for the href of the link ended up as an actual unbreaking space.

I had to fool the web page (or the browser, still not sure which) by putting in the HTML escape for the #, %, then the number 26 and so on, so: %26nbsp;, which the href and then the URL saw as #26nbsp; and could successfully perform the comparison with  .

Whew. That's a lot of escaping.

When I was first writing the code, I came across the advice that often what you really want is the textContent not the innerHTML, which seems like good advice, because the inner HTML includes any child elements that exist.

But I could not figure out how to test for an textContent that was an unbreaking space. I finally stumbled on it after I had posted the bookmarklet with the test for innerHTML.

You test textContent using the escaped character code in the hex, octal or unicode form.

So, do I change this? Maybe next time.
From:
Anonymous
OpenID
Identity URL: 
User
Account name:
Password:
If you don't have an account you can create one now.
Subject:
HTML doesn't work in the subject.

Message:

 
Links will be displayed as unclickable URLs to help prevent spam.

Profile

carene_waterman: An image of the Carina Nebula (Default)
carene_waterman

July 2014

S M T W T F S
  12345
6789101112
13141516171819
2021 22 2324 2526
2728293031  

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Sep. 25th, 2017 06:45 pm
Powered by Dreamwidth Studios