Mozilla Nederland LogoDe Nederlandse
Mozilla gemeenschap

Jeff Walden: Working on the JS engine, Episode V

Mozilla planet - wo, 03/12/2014 - 21:40

From a stack trace for a crash:

20:12:01 INFO - 2!bool js::DependentAddPtr<js::HashSet<js::ReadBarriered<js::UnownedBaseShape*>, js::StackBaseShape, js::SystemAllocPolicy> >::add<JS::RootedGeneric<js::StackBaseShape*>, js::UnownedBaseShape*>(js::ExclusiveContext const*, js::HashSet<js::ReadBarriered<js::UnownedBaseShape*>, js::StackBaseShape, js::SystemAllocPolicy>&, JS::RootedGeneric<js::StackBaseShape*> const&, js::UnownedBaseShape* const&) [HashTable.h:3ba384952a02 : 372 + 0x4]

If you can figure out where in that mess the actual method name is without staring at this for at least 15 seconds, I salute you. (Note that when I saw this originally, it wasn’t line-wrapped, making it even less readable.)

I’m not sure how this could be presented better, given the depth and breadth of template use in the class, in the template parameters to that class, in the method, and in the method arguments here.

Categorieën: Mozilla-nl planet

Wladimir Palant: Dumbing down HTML content for AMO

Mozilla planet - wo, 03/12/2014 - 18:10

If you are publishing extensions on AMO then you might have the same problem: how do I keep content synchronous between my website and extension descriptions on AMO? It could have been simple: take the HTML code from your website, copy it into the extension description and save. Unfortunately, usually this won’t produce useful results. The biggest issue: AMO doesn’t understand HTML paragraphs and will strip them out (along with most other tags). Instead it will turn each line break in your HTML code into a hard line break.

Luckily, a fairly simple script can do the conversion and make sure your text still looks somewhat okayish. Here is what I’ve come up with for myself:

#!/usr/bin/env python import sys import re data = # Normalize whitespace data = re.sub(r'\s+', ' ', data) # Insert line breaks after block tags data = re.sub(r'<(ul|/ul|ol|/ol|blockquote|/blockquote|/li)\b[^<>]*>\s*', '<\\1>\n', data) # Headers aren't supported, turn them into bold text data = re.sub(r'<h(\d)\b[^<>]*>(.*?)</h\1>\s*', '<b>\\2</b>\n\n', data) # Convert paragraphs into line breaks data = re.sub(r'<p\b[^<>]*>\s*', '', data) data = re.sub(r'</p>\s*', '\n\n', data) # Convert hard line breaks into line breaks data = re.sub(r'<br\b[^<>]*>\s*', '\n', data) # Remove any leading or trailing whitespace data = data.strip() print data

This script expects the original HTML code from standard input and will print the result to standard output. The conversions performed are sufficient for my needs, your mileage may vary — e.g. because you aren’t closing paragraph tags or because relative links are used that need resolving. I’m not intending to design some universal solution, you are free to add more logic to the script as needed.

Edit: Alternatively you can use the equivalent JavaScript code:

var textareas = document.getElementsByTagName("textarea"); for (var i = 0; i < textareas.length; i++) { if (window.getComputedStyle(textareas[i], "").display == "none") continue; data = textareas[i].value; // Normalize whitespace data = data.replace(/\s+/g, " "); // Insert line breaks after block tags data = data.replace(/<(ul|\/ul|ol|\/ol|blockquote|\/blockquote|\/li)\b[^<>]*>\s*/g, "<$1>\n"); // Headers aren't supported, turn them into bold text data = data.replace(/<h(\d)\b[^<>]*>(.*?)<\/h\1>\s*/g, "<b>$2</b>\n\n"); // Convert paragraphs into line breaks data = data.replace(/<p\b[^<>]*>\s*/g, ""); data = data.replace(/<\/p>\s*/g, "\n\n"); // Convert hard line breaks into line breaks data = data.replace(/<br\b[^<>]*>\s*/, "\n"); // Remove any leading or trailing whitespace data = data.trim(); textareas[i].value = data; }

This one will convert the text in all visible text areas. You can either run it on AMO pages via Scratchpad or turn it into a bookmarklet (replace !function by void function in the result of this bookmarklet generator, to make sure it works correctly in Firefox).

Categorieën: Mozilla-nl planet