Swap empty divs for br, fix parsing of blocks with font styles

This commit is contained in:
Ben Gotow 2018-01-16 08:46:22 -08:00
parent ca489342be
commit fba8c5a45f
8 changed files with 38 additions and 14 deletions

View file

@ -12,7 +12,7 @@ export function currentSignatureId(body) {
} }
export function applySignature(body, signature) { export function applySignature(body, signature) {
let additionalWhitespace = '<div></div>'; let additionalWhitespace = '<br/>';
// Remove any existing signature in the body // Remove any existing signature in the body
let newBody = body; let newBody = body;

View file

@ -47,8 +47,7 @@ describe('SignatureComposerDropdown', function signatureComposerDropdown() {
); );
ReactTestUtils.Simulate.mouseDown(this.signature); ReactTestUtils.Simulate.mouseDown(this.signature);
expect(this.button.props.session.changes.add).toHaveBeenCalledWith({ expect(this.button.props.session.changes.add).toHaveBeenCalledWith({
body: `${this.button.props.draft body: `${this.button.props.draft.body}<br/><signature id="2">${sigToAdd.body}</signature>`,
.body}<div></div><signature id="2">${sigToAdd.body}</signature>`,
}); });
}); });
it('finds and removes the signature when no signature is clicked and there is a current signature', () => { it('finds and removes the signature when no signature is clicked and there is a current signature', () => {

View file

@ -38,11 +38,11 @@ describe('SignatureComposerExtension', function signatureComposerExtension() {
SignatureComposerExtension.prepareNewDraft({ draft: a }); SignatureComposerExtension.prepareNewDraft({ draft: a });
expect(a.body).toEqual( expect(a.body).toEqual(
`This is a test! <div></div><signature id="1">${TEST_SIGNATURE.body}</signature><div class="gmail_quote">Hello world</div>` `This is a test! <br/><signature id="1">${TEST_SIGNATURE.body}</signature><div class="gmail_quote">Hello world</div>`
); );
SignatureComposerExtension.prepareNewDraft({ draft: b }); SignatureComposerExtension.prepareNewDraft({ draft: b });
expect(b.body).toEqual( expect(b.body).toEqual(
`This is a another test.<div></div><signature id="1">${TEST_SIGNATURE.body}</signature>` `This is a another test.<br/><signature id="1">${TEST_SIGNATURE.body}</signature>`
); );
}); });

View file

@ -28,7 +28,7 @@ const NoIconUrl = {
performSendAction() {}, performSendAction() {},
}; };
fdescribe('SendActionButton', function describeBlock() { describe('SendActionButton', function describeBlock() {
beforeEach(() => { beforeEach(() => {
spyOn(Actions, 'sendDraft'); spyOn(Actions, 'sendDraft');
this.isValidDraft = jasmine.createSpy('isValidDraft'); this.isValidDraft = jasmine.createSpy('isValidDraft');

View file

@ -55,7 +55,7 @@
} }
} }
blockquote { blockquote {
color: purple; color: #500050;
} }
&:hover { &:hover {
border: 1px dashed @border-color-divider; border: 1px dashed @border-color-divider;

View file

@ -161,6 +161,14 @@ const rules = [
config = BLOCK_CONFIG.code; config = BLOCK_CONFIG.code;
} }
// div elements that are entirely empty and have no meaningful-looking styles applied
// would probably just add extra whitespace
if (tagName === 'div' && !el.hasChildNodes()) {
const s = (el.getAttribute('style') || '').toLowerCase();
if (!s.includes('background') && !s.includes('margin') && !s.includes('padding')) {
return;
}
}
// return block // return block
if (config) { if (config) {
return { return {

View file

@ -7,6 +7,8 @@ import {
hasMark, hasMark,
} from './toolbar-component-factories'; } from './toolbar-component-factories';
import BaseBlockPlugins from './base-block-plugins';
export const DEFAULT_FONT_SIZE = 2; export const DEFAULT_FONT_SIZE = 2;
export const DEFAULT_FONT_OPTIONS = [ export const DEFAULT_FONT_OPTIONS = [
{ name: 'Small', value: 1 }, { name: 'Small', value: 1 },
@ -194,13 +196,28 @@ const rules = [
} }
if (marks.length) { if (marks.length) {
// convert array of marks into a tree. If the marks are on a BLOCK
// tagname, also nest the marks within the block node that would
// have been created, since the block will not be created if we return
// a value.
let block = null;
for (const plugin of BaseBlockPlugins) {
if (block) break;
if (!plugin.rules) continue;
for (const { deserialize } of plugin.rules) {
block = deserialize(el, next);
if (block) {
break;
}
}
}
const root = marks[0]; const root = marks[0];
let tail = root; let tail = root;
for (let x = 1; x < marks.length; x++) { for (let x = 1; x < marks.length; x++) {
tail.nodes = [marks[x]]; tail.nodes = [marks[x]];
tail = tail.nodes[0]; tail = tail.nodes[0];
} }
tail.nodes = next(el.childNodes); tail.nodes = block ? [block] : next(el.childNodes);
return root; return root;
} }
}, },

View file

@ -34,7 +34,7 @@ class DraftFactory {
async createDraft(fields = {}) { async createDraft(fields = {}) {
const account = this._accountForNewDraft(); const account = this._accountForNewDraft();
const defaults = { const defaults = {
body: '<div></div>', body: '<br/>',
subject: '', subject: '',
version: 0, version: 0,
unread: false, unread: false,
@ -164,15 +164,15 @@ class DraftFactory {
accountId: message.accountId, accountId: message.accountId,
replyToHeaderMessageId: message.headerMessageId, replyToHeaderMessageId: message.headerMessageId,
body: ` body: `
<div></div> <br/>
<div></div> <br/>
<div class="gmail_quote_attribution">${DOMUtils.escapeHTMLCharacters( <div class="gmail_quote_attribution">${DOMUtils.escapeHTMLCharacters(
message.replyAttributionLine() message.replyAttributionLine()
)}</div> )}</div>
<blockquote class="gmail_quote" <blockquote class="gmail_quote"
style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"> style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
${prevBody} ${prevBody}
<div></div> <br/>
</blockquote> </blockquote>
`, `,
}); });
@ -200,7 +200,7 @@ class DraftFactory {
accountId: message.accountId, accountId: message.accountId,
forwardedHeaderMessageId: message.headerMessageId, forwardedHeaderMessageId: message.headerMessageId,
body: ` body: `
<div></div> <br/>
<div class="gmail_quote"> <div class="gmail_quote">
<br> <br>
---------- Forwarded message --------- ---------- Forwarded message ---------
@ -208,7 +208,7 @@ class DraftFactory {
${fields.join('<br>')} ${fields.join('<br>')}
<br><br> <br><br>
${body} ${body}
<div></div> <br/>
</div> </div>
`, `,
}); });