Skip to content

Commit

Permalink
Merge pull request #986 from 18F/913-prevent-marker-edits
Browse files Browse the repository at this point in the history
Prevent list markers from being edited.
  • Loading branch information
cmc333333 authored Feb 2, 2018
2 parents 6178e46 + a4e9e8f commit 101062c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 35 deletions.
24 changes: 8 additions & 16 deletions api/document/js/__tests__/parse-doc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,22 +84,14 @@ describe('parseDoc()', () => {
expect(result.content.childCount).toBe(2);
const lis = [result.content.child(0), result.content.child(1)];
expect(lis.map(li => li.type.name)).toEqual(['listitem', 'listitem']);
expect(lis.map(li => li.content.childCount)).toEqual([2, 2]);

const markers = lis.map(li => li.content.child(0));
expect(markers.map(m => m.type.name)).toEqual(
['listitemMarker', 'listitemMarker']);
expect(markers.map(m => m.textContent)).toEqual(['aaa', 'bbb']);

const [body1, body2] = lis.map(li => li.content.child(1));
expect(body1.type.name).toBe('listitemBody');
expect(body1.content.childCount).toBe(1);
expect(body1.content.child(0).type.name).toBe('para');

expect(body2.type.name).toBe('listitemBody');
expect(body2.content.childCount).toBe(2);
expect(body2.content.child(0).type.name).toBe('para');
expect(body2.content.child(1).type.name).toBe('para');
expect(lis.map(li => li.attrs.marker)).toEqual(['aaa', 'bbb']);

expect(lis[0].content.childCount).toBe(1);
expect(lis[0].content.child(0).type.name).toBe('para');

expect(lis[1].content.childCount).toBe(2);
expect(lis[1].content.child(0).type.name).toBe('para');
expect(lis[1].content.child(1).type.name).toBe('para');
});

describe('unimplementedNode', () => {
Expand Down
23 changes: 10 additions & 13 deletions api/document/js/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ const listSchemaNodes: { [name: string]: NodeSpec } = {
toDOM: () => ['ol', { class: 'node-list' }, 0],
},
listitem: {
content: 'listitemMarker listitemBody',
toDOM: () => ['li', { class: 'node-list-item' }, 0],
},
listitemMarker: {
content: 'text+',
toDOM: () => ['span', { class: 'list-item-marker' }, 0],
},
listitemBody: {
attrs: {
marker: { default: '1.' },
},
content: 'block+',
toDOM: () => ['div', { class: 'list-item-text' }, 0],
toDOM: node => [
'li',
{ class: 'node-list-item' },
['span', { class: 'list-item-marker' }, node.attrs.marker],
['div', { class: 'list-item-text' }, 0],
],
},
};

Expand Down Expand Up @@ -78,10 +78,7 @@ export const factory = {
list: (children?: Node[]) =>
schema.nodes.list.create({}, children || []),
listitem: (marker: string, children?: Node[]) =>
schema.nodes.listitem.create({}, [
schema.nodes.listitemMarker.create({}, schema.text(marker)),
schema.nodes.listitemBody.create({}, children || []),
]),
schema.nodes.listitem.create({ marker }, children || []),
para: (textContent: string | Node[], children?: Node[]) =>
schema.nodes.para.create({}, [schema.nodes.inline.create(
{},
Expand Down
8 changes: 2 additions & 6 deletions api/document/js/serialize-doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,9 @@ const NODE_CONVERTERS = {
{ content: convertTexts(node.content) },
),
listitem(node) {
// Note the existence of these two nodes is ensured by the schema
const markerNode = node.content.child(0);
const body = node.content.child(1);

const children: ApiNode[] = [];
body.forEach(child => children.push(serializeDoc(child)));
const marker = markerNode.textContent;
node.content.forEach(child => children.push(serializeDoc(child)));
const marker = node.attrs.marker;
const typeEmblem = marker.replace(/[^a-zA-Z0-9]/, '');
return apiFactory.node(
node.type.name,
Expand Down

0 comments on commit 101062c

Please sign in to comment.