Skip to content
This repository has been archived by the owner on Apr 4, 2019. It is now read-only.

Adds Node-compatible protocolForURL #437

Merged
merged 2 commits into from
Dec 8, 2015
Merged

Conversation

tomdale
Copy link
Collaborator

@tomdale tomdale commented Dec 3, 2015

The current implementation of the DOM helper’s protocolForURL relies
on the implicit parsing and normalization that the browser’s DOM does
when setting the A element’s href property.

However, the DOM helper is often used with subset DOM implementations
like simple-dom. In those cases, we cannot rely on this normalization.

This commit detects when the DOM Helper is running in Node and uses
the built-in URL package to parse the protocol instead.

/cc @wycats @chancancode @rwjblue @stefanpenner

@tomdale
Copy link
Collaborator Author

tomdale commented Dec 3, 2015

It looks like CJS commits were disabled in 0c51a42 but no rationale for why was provided in that commit. Presumably it was for a good reason, but it means I couldn't write a test for this case.

I think it's OK because this is implicitly tested in Ember anyway and this implementation of the DOM Helper is EOL once Glimmer2 lands.

}
// In Node.js environments, we can't rely on the DOM correctly parsing URLs.
// Instead, we'll fall back to Node's built-in URL parsing.
if (typeof process === 'object' && process + '' === '[object process]') {
Copy link
Collaborator

Choose a reason for hiding this comment

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

does this work in node-webkit?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

cc @wycats 👀

Copy link
Collaborator

Choose a reason for hiding this comment

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

process is likely not something you want to expose on the sandbox context just for an is node check.

@stefanpenner
Copy link
Collaborator

It looks like CJS commits were disabled in 0c51a42 but no rationale for why was provided in that commit. Presumably it was for a good reason, but it means I couldn't write a test for this case.

They where breaking the build, I did not have time to fix them, and we needed to get some critical work out to ember. I intended to address this, unfortunately have not had a chance.

Maybe someone else can.

@chancancode
Copy link
Member

seems good! 👍

@tomdale
Copy link
Collaborator Author

tomdale commented Dec 4, 2015

@stefanpenner

Maybe someone else can

Basically EOL at this point, not sure it's worth anyone putting effort in here that could be put into glimmer.

@stefanpenner
Copy link
Collaborator

Basically EOL at this point, not sure it's worth anyone putting effort in here that could be put into glimmer.

@tomdale c

@tomdale tomdale force-pushed the node-protocol-for-url branch from 68cf674 to 38019b6 Compare December 6, 2015 16:30
@tomdale
Copy link
Collaborator Author

tomdale commented Dec 6, 2015

@stefanpenner @krisselden @chancancode Replaced detecting the environment with a feature detection. How does this approach look?

@stefanpenner
Copy link
Collaborator

@tomdale I would feel better doing the test before the prototype member is set, rather then replacing the SharedFunction during invocation. Is this possible, or is it lazy fora specific reason?

// Otherwise, we need to fall back to our own URL parsing.
// Global `require` is shadowed by Ember's loader so we have to use the fully
// qualified `module.require`.
URL = module.require('url');
Copy link
Collaborator

Choose a reason for hiding this comment

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

what if there is no require? Might be nice to fail fast then (might be aligned with my comment bellow about doing this before setting up the prototype.

The current implementation of the DOM helper’s `protocolForURL` relies
on the implicit parsing and normalization that the browser’s DOM does
when setting the A element’s `href` property.

However, the DOM helper is often used with subset DOM implementations
like simple-dom. In those cases, we cannot rely on this normalization.

This commit detects when URL parsing doesn’t happen when setting the
`href` property on an `A` element and falls back to using Node’s
`URL` package.
@tomdale tomdale force-pushed the node-protocol-for-url branch from 38019b6 to 7f7ceeb Compare December 6, 2015 23:55
@tomdale
Copy link
Collaborator Author

tomdale commented Dec 7, 2015

@chancancode @stefanpenner I updated the implementation to install the methods on the instance, and also added support for raw HTML sections. That last change let's us remove all of the monkeypatches from Ember/FastBoot.

When running in the browser, we rely on the browser’s built-in HTML
parsing for cases when users provide unescaped HTML content as dynamic
values in their templates (i.e., `{{{unsafeHTML}}}`). Because the final
output is a DOM tree, we ask the browser to parse the HTML and turn it
into a document fragment, then insert into the appropriate location in
the rendered element.

However, for server-side rendering, the final output is serialized HTML,
not a DOM tree. In the case of raw HTML provided by the user, we’d be
doing extra work to go from raw HTML -> parsed DOM -> serialized HTML.
(That we’d need an HTML parser also adds complexity.)

This patch allows us to detect an extension to the DOM API we’ve added
to SimpleDOM that allows us to insert a node in the DOM tree that means
“raw HTML” (similar to CDATA). When we go to serialize the DOM tree, any
raw HTML nodes are written as the HTML that was provided when created.
@tomdale tomdale force-pushed the node-protocol-for-url branch from ec88eed to e92d8c9 Compare December 8, 2015 14:56
@tomdale
Copy link
Collaborator Author

tomdale commented Dec 8, 2015

Gonna merge this, @stefanpenner let me know if you've got any issues and I will fix.

tomdale added a commit that referenced this pull request Dec 8, 2015
Adds Node-compatible protocolForURL
@tomdale tomdale merged commit f853743 into master Dec 8, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants