Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fluent-dom: Simplify DOM Overlays #71

Merged
merged 1 commit into from Aug 31, 2017

Conversation

stasm
Copy link
Contributor

@stasm stasm commented Aug 28, 2017

This is a WIP. I'm working on a second commit which changes the underlying logic even further.

This PR reduces the complexity of the overlay logic by making the following changes:

  • Only children of the white-listed types are allowed now. It's not possible anymore to put elements of other types in the source HTML to make exceptions.

  • The identity of the source element's children is explicitly not kept anymore. This allows us to treat the translation DocumentFragment as the reference for iteration over child nodes.

  • The overlay function is also no longer recursive. Any nested HTML will be lost and only its textContent will be preserved.

@stasm stasm changed the title fluent-dom: Overlay only allowed elements fluent-dom: Simplify DOM Overlays Aug 30, 2017
@stasm stasm requested review from zbraniecki and Pike August 30, 2017 16:39
Copy link
Contributor

@Pike Pike left a comment

Choose a reason for hiding this comment

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

I only have a few cosmetic changes that don't require another round of review, so approving.

* The goal of overlay is to move the children of `translationElement`
* into `sourceElement` such that `sourceElement`'s own children are not
* replaced, but only have their text nodes and their attributes modified.
* Sanitize the `translationElement` according to the following rules:
Copy link
Contributor

Choose a reason for hiding this comment

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

translationFragment 'cause that's what the paramater is.

const result = translationElement.ownerDocument.createDocumentFragment();

// Take one node from translationElement at a time and check it against
function overlay(translationFragment, sourceElement) {
Copy link
Contributor

Choose a reason for hiding this comment

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

sanitzeWithLove

}
// If a child of the same type exists in sourceElement, use it as the base
// for the resultChild.
const sourceChild = popChild(sourceElement, childNode.localName);
Copy link
Contributor

Choose a reason for hiding this comment

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

I know I said the opposite, but how about shiftElementOfName ?

for (const attr of Array.from(translationElement.attributes)) {
if (isAttrNameAllowed(attr.name, sourceElement)) {
sourceElement.setAttribute(attr.name, attr.value);
// Explicitly ignore nested HTML.
Copy link
Contributor

Choose a reason for hiding this comment

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

This comment means well, but you still have to know what the code does per spec.

How about // Serialize to a single TextNode

assert.equal(element.innerHTML, 'FOO <em>BAR</em> BAZ');
});

test('allowed element with nested HTML', function() {
Copy link
Contributor

Choose a reason for hiding this comment

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

... with forbidden nested HTML'.

I read this as "allowed ..yadda yadda"

Make DOM overlay logic less complex and more predictable by making the
following changes:

  - Only children of the white-listed types are allowed now. It's not possible
    anymore to put elements of other types in the source HTML to make exceptions.

  - The identity of the source element's children is explicitly not kept
    anymore. This allows us to treat the translation DocumentFragment as the
    reference for iteration over child nodes.

  - The overlay function is also no longer recursive. Any nested HTML
    will be lost and only its textContent will be preserved.
@stasm stasm merged commit 96c890f into projectfluent:master Aug 31, 2017
@stasm stasm deleted the overlay-only-allowed branch August 31, 2017 00:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants