Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "docs-soap",
"version": "0.1.2",
"version": "0.1.3",
"description": "A utility for cleaning Google Docs clipboard content into valid HTML",
"author": "aem <amarkon895@gmail.com>",
"keywords": [
Expand Down
18 changes: 18 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export const styles = {
BOLD: '700',
ITALIC: 'italic',
UNDERLINE: 'underline',
STRIKETHROUGH: 'line-through',
SUPERSCRIPT: 'super',
SUBSCRIPT: 'sub'
};

export const elements = {
ANCHOR: 'a',
BOLD: 'strong',
ITALIC: 'em',
UNDERLINE: 'u',
STRIKETHROUGH: 'del',
SUPERSCRIPT: 'sup',
SUBSCRIPT: 'sub'
};
53 changes: 39 additions & 14 deletions src/docsSoap.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
const parseHTML = require('./parseHTML');

const styles = {
BOLD: '700',
ITALIC: 'italic',
UNDERLINE: 'underline'
};

const elements = {
BOLD: 'strong',
ITALIC: 'i',
UNDERLINE: 'u'
};
import { elements, styles } from './constants';
import parseHTML from './parseHTML';

const wrapNodeAnchor = (node, href) => {
const anchor = document.createElement('a');
const anchor = document.createElement(elements.ANCHOR);
anchor.href = href;
anchor.appendChild(node.cloneNode(true));
return anchor;
Expand All @@ -40,6 +29,15 @@ const applyBlockStyles = dirty => {
if (inner && inner.style && inner.style.textDecoration === styles.UNDERLINE) {
newNode = wrapNodeInline(newNode, elements.UNDERLINE);
}
if (inner && inner.style && inner.style.textDecoration === styles.STRIKETHROUGH) {
newNode = wrapNodeInline(newNode, elements.STRIKETHROUGH);
}
if (inner && inner.style && inner.style.verticalAlign === styles.SUPERSCRIPT) {
newNode = wrapNodeInline(newNode, elements.SUPERSCRIPT);
}
if (inner && inner.style && inner.style.verticalAlign === styles.SUBSCRIPT) {
newNode = wrapNodeInline(newNode, elements.SUBSCRIPT);
}
}
if (node.childNodes[0] && node.childNodes[0].style && node.childNodes[0].style.fontWeight === styles.BOLD) {
newNode = wrapNodeInline(newNode, elements.BOLD);
Expand All @@ -50,6 +48,15 @@ const applyBlockStyles = dirty => {
if (node.childNodes[0] && node.childNodes[0].style && node.childNodes[0].style.textDecoration === styles.UNDERLINE) {
newNode = wrapNodeInline(newNode, elements.UNDERLINE);
}
if (node.childNodes[0] && node.childNodes[0].style && node.childNodes[0].style.textDecoration === styles.STRIKETHROUGH) {
newNode = wrapNodeInline(newNode, elements.STRIKETHROUGH);
}
if (node.childNodes[0] && node.childNodes[0].style && node.childNodes[0].style.verticalAlign === styles.SUPERSCRIPT) {
newNode = wrapNodeInline(newNode, elements.SUPERSCRIPT);
}
if (node.childNodes[0] && node.childNodes[0].style && node.childNodes[0].style.verticalAlign === styles.SUBSCRIPT) {
newNode = wrapNodeInline(newNode, elements.SUBSCRIPT);
}
return newNode;
};

Expand All @@ -67,6 +74,15 @@ const applyInlineStyles = dirty => {
if (node.childNodes[0] && node.childNodes[0].style && node.childNodes[0].style.textDecoration === styles.UNDERLINE) {
newNode = wrapNodeInline(newNode, elements.UNDERLINE);
}
if (node.childNodes[0] && node.childNodes[0].style && node.childNodes[0].style.textDecoration === styles.STRIKETHROUGH) {
newNode = wrapNodeInline(newNode, elements.STRIKETHROUGH);
}
if (node.childNodes[0] && node.childNodes[0].style && node.childNodes[0].style.verticalAlign === styles.SUPERSCRIPT) {
newNode = wrapNodeInline(newNode, elements.SUPERSCRIPT);
}
if (node.childNodes[0] && node.childNodes[0].style && node.childNodes[0].style.verticalAlign === styles.SUBSCRIPT) {
newNode = wrapNodeInline(newNode, elements.SUBSCRIPT);
}
}
if (node.style && node.style.fontWeight === styles.BOLD) {
newNode = wrapNodeInline(newNode, elements.BOLD);
Expand All @@ -77,6 +93,15 @@ const applyInlineStyles = dirty => {
if (node.style && node.style.textDecoration === styles.UNDERLINE) {
newNode = wrapNodeInline(newNode, elements.UNDERLINE);
}
if (node.style && node.style.textDecoration === styles.STRIKETHROUGH) {
newNode = wrapNodeInline(newNode, elements.STRIKETHROUGH);
}
if (node.style && node.style.verticalAlign === styles.SUPERSCRIPT) {
newNode = wrapNodeInline(newNode, elements.SUPERSCRIPT);
}
if (node.style && node.style.verticalAlign === styles.SUBSCRIPT) {
newNode = wrapNodeInline(newNode, elements.SUBSCRIPT);
}
return newNode;
};

Expand Down
31 changes: 19 additions & 12 deletions test/docsSoapSpec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use es6';

import { elements } from '../src/constants'
import documents from './fixtures/documents';
import docsSoap from '../dist/index';
import expect from 'expect';
Expand All @@ -11,23 +12,29 @@ describe('Google Docs Converter', () => {

it('converts inline styles from google docs properly', () => {
const rawContents = parseHTML(documents.inlineStyles);
expect(rawContents.querySelectorAll('strong').length).toBe(0);
expect(rawContents.querySelectorAll('i').length).toBe(0);
expect(rawContents.querySelectorAll('u').length).toBe(0);
expect(rawContents.querySelectorAll(elements.BOLD).length).toBe(0);
expect(rawContents.querySelectorAll(elements.ITALIC).length).toBe(0);
expect(rawContents.querySelectorAll(elements.UNDERLINE).length).toBe(0);
expect(rawContents.querySelectorAll(elements.STRIKETHROUGH).length).toBe(0);
expect(rawContents.querySelectorAll(elements.SUPERSCRIPT).length).toBe(0);
expect(rawContents.querySelectorAll(elements.SUBSCRIPT).length).toBe(0);
const doc = parseHTML(docsSoap(documents.inlineStyles));
expect(doc.querySelectorAll('strong').length).toBe(1);
expect(doc.querySelectorAll('i').length).toBe(1);
expect(doc.querySelectorAll('u').length).toBe(1);
expect(doc.querySelectorAll(elements.BOLD).length).toBe(1);
expect(doc.querySelectorAll(elements.ITALIC).length).toBe(1);
expect(doc.querySelectorAll(elements.UNDERLINE).length).toBe(1);
expect(doc.querySelectorAll(elements.STRIKETHROUGH).length).toBe(1);
expect(doc.querySelectorAll(elements.SUPERSCRIPT).length).toBe(1);
expect(doc.querySelectorAll(elements.SUBSCRIPT).length).toBe(1);
});

it('converts links from google docs properly', () => {
const rawContents = parseHTML(documents.links);
expect(rawContents.querySelectorAll('strong').length).toBe(0);
expect(rawContents.querySelectorAll('i').length).toBe(0);
expect(rawContents.querySelectorAll('a').length).toBe(4);
expect(rawContents.querySelectorAll(elements.BOLD).length).toBe(0);
expect(rawContents.querySelectorAll(elements.ITALIC).length).toBe(0);
expect(rawContents.querySelectorAll(elements.ANCHOR).length).toBe(4);
const doc = parseHTML(docsSoap(documents.links));
expect(doc.querySelectorAll('strong').length).toBe(2);
expect(doc.querySelectorAll('i').length).toBe(2);
expect(doc.querySelectorAll('a').length).toBe(4);
expect(doc.querySelectorAll(elements.BOLD).length).toBe(2);
expect(doc.querySelectorAll(elements.ITALIC).length).toBe(2);
expect(doc.querySelectorAll(elements.ANCHOR).length).toBe(4);
});
});
2 changes: 1 addition & 1 deletion test/fixtures/documents.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"inlineStyles": "<b style='font-weight:normal;' id='docs-internal-guid-8947de85-9825-af91-ff3b-70358a99c571'><p dir='ltr' style='line-height:1.38;margin-top:0pt;margin-bottom:0pt;'><span style='font-size:14.666666666666666px;font-family:Arial;color:#000000;background-color:transparent;font-weight:700;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre-wrap;'>Some bold text</span></p><p dir='ltr' style='line-height:1.38;margin-top:0pt;margin-bottom:0pt;'><span style='font-size:14.666666666666666px;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:italic;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre-wrap;'>Some italicized text</span></p><span style='font-size:14.666666666666666px;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:underline;vertical-align:baseline;white-space:pre-wrap;'>Some underlined text</span></b>",
"inlineStyles": "<b style='font-weight:normal;' id='docs-internal-guid-8947de85-9847-a581-2846-773abcb0b8ff'><p dir='ltr' style='line-height:1.38;margin-top:0pt;margin-bottom:0pt;'><span style='font-size:14.666666666666666px;font-family:Arial;color:#000000;background-color:transparent;font-weight:700;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre-wrap;'>Some bold text</span></p><p dir='ltr' style='line-height:1.38;margin-top:0pt;margin-bottom:0pt;'><span style='font-size:14.666666666666666px;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:italic;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre-wrap;'>Some italicized text</span></p><p dir='ltr' style='line-height:1.38;margin-top:0pt;margin-bottom:0pt;'><span style='font-size:14.666666666666666px;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:underline;vertical-align:baseline;white-space:pre-wrap;'>Some underlined text</span></p><p dir='ltr' style='line-height:1.38;margin-top:0pt;margin-bottom:0pt;'><span style='font-size:14.666666666666666px;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:line-through;vertical-align:baseline;white-space:pre-wrap;'>Some strikethrough text</span></p><p dir='ltr' style='line-height:1.38;margin-top:0pt;margin-bottom:0pt;'><span style='font-size:8.799999999999999px;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:super;white-space:pre-wrap;'>Some superscript</span></p><span style='font-size:8.799999999999999px;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:sub;white-space:pre-wrap;'>Some subscript</span></b>",
"links": "<b style='font-weight:normal;' id='docs-internal-guid-839dd5dd-30a0-81b2-a59c-6b97288cf0ee'><p dir='ltr' style='line-height:1.38;margin-top:0pt;margin-bottom:0pt;'><a href='https://www.example.com' style='text-decoration:none;'><span style='font-size:14.666666666666666px;font-family:Arial;color:#1155cc;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre-wrap;'>this is a link</span></a></p><p dir='ltr' style='line-height:1.38;margin-top:0pt;margin-bottom:0pt;'><a href='https://www.example.com' style='text-decoration:none;'><span style='font-size:14.666666666666666px;font-family:Arial;color:#1155cc;background-color:transparent;font-weight:700;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre-wrap;'>this is a bold link</span></a></p><p dir='ltr' style='line-height:1.38;margin-top:0pt;margin-bottom:0pt;'><a href='https://www.example.com' style='text-decoration:none;'><span style='font-size:14.666666666666666px;font-family:Arial;color:#1155cc;background-color:transparent;font-weight:400;font-style:italic;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre-wrap;'>this is an italicized link</span></a></p><a href='https://www.example.com' style='text-decoration:none;'><span style='font-size:14.666666666666666px;font-family:Arial;color:#1155cc;background-color:transparent;font-weight:700;font-style:italic;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre-wrap;'>this is a bold, italicized link</span></a></b>"
}