Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Semantify contents

  • Loading branch information...
commit 1a43b742b4f5ec9d39ec4414298cd60e40a6ef61 1 parent abbd169
@timjb timjb authored
Showing with 86 additions and 29 deletions.
  1. +81 −29 proper.js
  2. +5 −0 stylesheets/page.css
View
110 proper.js
@@ -326,33 +326,90 @@
// presentational elements (e.g. `<b>`) with their semantic counterparts
// (e.g. `<strong>`).
function semantifyContents(node) {
- function getCorrectTagName(node) {
- var tagName = node.nodeName.toLowerCase();
-
- if (tagName === 'i') return 'em';
- if (tagName === 'b') return 'strong';
- if (tagName === 'font') return 'code';
- return tagName;
+ var clone = $(node).clone();
+
+ function replace(presentational, semantic) {
+ clone.find(presentational).each(function () {
+ $(this).replaceWith($(document.createElement(semantic)).html($(this).html()));
+ });
}
+ replace('i', 'em');
+ replace('b', 'strong');
+ replace('.proper-code', 'code');
+ replace('div', 'p');
+ //replace('span', 'span');
- if (node.nodeType === Node.TEXT_NODE) {
- return escape(node.data);
- } else if (node.nodeType === Node.ELEMENT_NODE) {
- var tagName = getCorrectTagName(node);
-
- if (tagName === 'br') return '<br />';
-
- var result = tagName === 'a' ? '<a href="'+escape(node.href)+'">'
- : '<'+tagName+'>';
- var children = node.childNodes;
+ clone.find('span').each(function () {
+ /*var child;
+ while (child = this.firstChild) {
+ $(child).remove().insertBefore(this);
+ }
+ $(this).remove();*/
+ if (this.firstChild) {
+ $(this.firstChild).unwrap();
+ }
+ });
+
+ clone.find('p, ul, ol').each(function () {
+ while ($(this).parent().is('p')) {
+ $(this).unwrap();
+ }
+ });
+
+ // Fix nested lists
+ clone.find('ul > ul, ul > ol, ol > ul, ol > ol').each(function () {
+ if ($(this).prev()) {
+ $(this).prev().append(this);
+ } else {
+ $(this).wrap($('<li />'));
+ }
+ });
+
+ (function () {
+ var currentP = [];
+ function wrapInP() {
+ if (currentP.length) {
+ var p = $('<p />').insertBefore(currentP[0]);
+ for (var i = 0, l = currentP.length; i < l; i++) {
+ $(currentP[i]).remove().appendTo(p);
+ }
+ currentP = [];
+ }
+ }
+ // _.clone is necessary because it turns the `childNodes` live
+ // dom collection into a static array.
+ var children = _.clone(clone.get(0).childNodes);
for (var i = 0, l = children.length; i < l; i++) {
- result += semantifyContents(children[i]);
+ var child = children[i];
+ if (!$(child).is('p, ul, ol') &&
+ !(child.nodeType === Node.TEXT_NODE && (/^\s*$/).exec(child.data))) {
+ currentP.push(child);
+ } else {
+ wrapInP();
+ }
}
- result += '</'+tagName+'>';
- return result;
- } else {
- return '';
- }
+ wrapInP();
+ })();
+
+ // Remove unnecessary br's
+ clone.find('br').each(function () {
+ if (this.parentNode.lastChild === this) {
+ $(this).remove();
+ }
+ });
+
+ // Remove all spans
+ clone.find('span').each(function () {
+ $(this).children().first().unwrap();
+ });
+
+ clone.children().each(function () {
+ if (!$(this).is('p,ul,ol')) {
+ $(this).wrap($('<p />'));
+ }
+ });
+
+ return clone.html();
}
// Update the control buttons' state.
@@ -580,12 +637,7 @@
if (options.markup) {
if (!activeElement) return '';
-
- var result = '';
- _.each($(activeElement).get(0).childNodes, function (child) {
- result += semantifyContents(child);
- })
- return result;
+ return semantifyContents($(activeElement));
} else {
if (options.multiline) {
return _.stripTags($(activeElement).html().replace(/<div>/g, '\n')
View
5 stylesheets/page.css
@@ -94,3 +94,8 @@ code, .proper-code {
}
.content[contenteditable=true] { border-color: #aaa; }
.no-markup[contenteditable=true] { border-color: #aaa; }
+
+.content p, .content div, .content ul, .content ol { margin: 1em 0; }
+.content ul ul, .content ul ol, .content ol ul, .content ol ol { margin: 0; }
+/*.content p:first-child, .content div:first-child { margin-top: 0; }
+.content p:last-child, .content div:last-child { margin-bottom: 0; }*/
Please sign in to comment.
Something went wrong with that request. Please try again.