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
112 changes: 58 additions & 54 deletions src/collab.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,65 +148,69 @@
html = EMPTY_DOC;
}
const tree = fromHtml(html, { fragment: true });
const main = tree.children.find((child) => child.tagName === 'main') || { children: [] };
fixImageLinks(main);
removeComments(main);
(main.children || []).forEach((parent) => {
if (parent.tagName === 'div' && parent.children) {
const children = [];
let modified = false;
parent.children.forEach((child) => {
if (child.tagName === 'div' && child.properties.className?.length > 0) {
modified = true;
blockToTable(child, children);
} else if (child.tagName === 'da-loc-deleted' || child.tagName === 'da-loc-added') {
modified = true;
const locChildren = [];
child.children.forEach((locChild) => {
if (locChild.tagName === 'div' && locChild.properties.className?.length > 0) {
blockToTable(locChild, locChildren);
} else {
locChildren.push(locChild);
}
});
const main = tree.children.find((child) => child.tagName === 'main');
console.log('tree', tree);

Check warning on line 152 in src/collab.js

View workflow job for this annotation

GitHub Actions / Running tests (20.x)

Unexpected console statement
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Guessing these logs are intentional?

console.log('main', main);

Check warning on line 153 in src/collab.js

View workflow job for this annotation

GitHub Actions / Running tests (20.x)

Unexpected console statement
if (main) {
fixImageLinks(main);
removeComments(main);
(main.children || []).forEach((parent) => {
if (parent.tagName === 'div' && parent.children) {
const children = [];
let modified = false;
parent.children.forEach((child) => {
if (child.tagName === 'div' && child.properties.className?.length > 0) {
modified = true;
blockToTable(child, children);
} else if (child.tagName === 'da-loc-deleted' || child.tagName === 'da-loc-added') {
modified = true;
const locChildren = [];
child.children.forEach((locChild) => {
if (locChild.tagName === 'div' && locChild.properties.className?.length > 0) {
blockToTable(locChild, locChildren);
} else {
locChildren.push(locChild);
}
});
// eslint-disable-next-line no-param-reassign
child.children = locChildren;
children.push(child);
} else {
children.push(child);
}
});
if (modified) {
// eslint-disable-next-line no-param-reassign
child.children = locChildren;
children.push(child);
} else {
children.push(child);
parent.children = children;
}
});
if (modified) {
// eslint-disable-next-line no-param-reassign
parent.children = children;
}
}
});
convertSectionBreak(main);
let count = 0;
main.children = main.children.flatMap((node) => {
const result = [];
if (node.tagName === 'div') {
if (count > 0) {
result.push({
type: 'element', tagName: 'p', children: [], properties: {},
});
result.push({
type: 'element', tagName: 'hr', children: [], properties: {},
});
result.push({
type: 'element', tagName: 'p', children: [], properties: {},
});
result.push(...node.children);
});
convertSectionBreak(main);
let count = 0;
main.children = main.children.flatMap((node) => {
const result = [];
if (node.tagName === 'div') {
if (count > 0) {
result.push({
type: 'element', tagName: 'p', children: [], properties: {},
});
result.push({
type: 'element', tagName: 'hr', children: [], properties: {},
});
result.push({
type: 'element', tagName: 'p', children: [], properties: {},
});
result.push(...node.children);
} else {
result.push(node);
}
count += 1;
} else {
result.push(node);
}
count += 1;
} else {
result.push(node);
}
return result;
});
return result;
});
}
const handler2 = {
get(target, prop) {
const source = target;
Expand Down Expand Up @@ -277,7 +281,7 @@
},
};

const json = DOMParser.fromSchema(getSchema()).parse(new Proxy(main, handler2));
const json = DOMParser.fromSchema(getSchema()).parse(new Proxy(main || tree, handler2));
prosemirrorToYXmlFragment(json, ydoc.getXmlFragment('prosemirror'));
}

Expand Down
6 changes: 3 additions & 3 deletions test/collab.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -595,12 +595,12 @@ assert.equal(result, html);
assert.equal(collapseWhitespace(result), collapseWhitespace(EMPTY_DOC));
});

it('can parse no main', async () => {
const html = '<body><header></header><footer></footer></body>';
it('can parse no main - results should remain unchanged - doc2aem wraps content into main', async () => {
const html = '<body><div><p>Hello</p></div><footer><p>World</p></footer></body>';
const yDoc = new Y.Doc();
aem2doc(html, yDoc);
const result = doc2aem(yDoc);
assert.equal(collapseWhitespace(result), collapseWhitespace(EMPTY_DOC));
assert.equal(collapseWhitespace(result), collapseWhitespace('<body><header></header><main><div><p>Hello</p><p>World</p></div></main><footer></footer></body>'));
});
});

Expand Down