Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

docs(ngProp, ngOn) #16627

Merged
merged 1 commit into from
Aug 3, 2018
Merged

docs(ngProp, ngOn) #16627

merged 1 commit into from
Aug 3, 2018

Conversation

Narretz
Copy link
Contributor

@Narretz Narretz commented Jul 12, 2018

What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)

What is the current behavior? (You can also link to an open issue here)

What is the new behavior (if this is a feature change)?

Does this PR introduce a breaking change?

Please check if the PR fulfills these requirements

  • The commit message follows our guidelines
  • Fix/Feature: Docs have been added/updated
  • Fix/Feature: Tests have been added; existing tests pass

Other information:

* @element ANY
*
* @description
* The `ngOn` directive adds an event listener to a DOM element, and evaluates an expression
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we should mention that it uses jqLite.on and not the native addEventListener. I'm not sure how we refer to the internal jqLite var in docs though. angular.element?

* malicious code.
* For this reason, `ngProp` requires trusted expression content via
* Strict Contextual Escaping with the {@link ng.$sce $sce service}. Practically this means
* that different properties require different trusted content types
Copy link
Contributor

Choose a reason for hiding this comment

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

For this reason, ngProp makes use of the Strict Contextual Escaping mechanisms in AngularJS
to ensure that, for security sensitive properties, the developer either marks the value as "trusted"
or applies an appropriate sanitization strategy to the value before it is assigned to the property.

*
* ### Sanitization
*
* By default, `$sce` will throw an error if it detects potentially dangerous content. However,
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure this is accurate. It will, by default, throw an error if you try to write a non-trusted value to a security sensitive property, such as innerHTML.

* malicious code.
* For this reason, `ngProp` applies Strict Contextual Escaping with the {@link ng.$sce $sce service}.
* This means vulnerable properties require their content to be "trusted", based on the
* context of the property. For example, the `iframe[src]` property is in the `RESOURCE_URL` context and
Copy link
Contributor

Choose a reason for hiding this comment

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

Given that this directive is about properties and not attributes, we should probably not put iframe[src], instead I think we should put iframe.src.

* For this reason, `ngProp` applies Strict Contextual Escaping with the {@link ng.$sce $sce service}.
* This means vulnerable properties require their content to be "trusted", based on the
* context of the property. For example, the `iframe[src]` property is in the `RESOURCE_URL` context and
* `innerHTML` is in the `HTML` context.
Copy link
Contributor

Choose a reason for hiding this comment

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

, which requires that values written to this property are trusted as a RESOURCE_URL. This can be
set explicitly by calling $sce.trustAsResourceUrl(value) on the value that is trusted before passing
it to the ng-prop-src directive. In some cases you can rely upon automatic sanitization of untrusted
values - see below.

*
* By default, `$sce` will throw an error if it detects untrusted content, and will not bind the
* content.
* However, if you include the {@link ngSanitize ngSanitize module}, it will try to sanitize the
Copy link
Contributor

Choose a reason for hiding this comment

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

ngSanitize is only related to HTML values. The URL sanitizers are built into the core ng module.

* However, if you include the {@link ngSanitize ngSanitize module}, it will try to sanitize the
* potentially dangerous content, e.g. strip non-whitelisted tags and attributes when binding to
* `innerHTML`.
*
Copy link
Contributor

Choose a reason for hiding this comment

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

The autogenerated "usage" block for this directive is not helpful or accurate.
If you add a @usa ge tag then this will be used instead, which allows you to provide an appropriate usage for this "special" directive.

*
* Since HTML attributes are case-insensitive, camelCase properties like `innerHTML` must be escaped.
* AngularJS uses the underscore (_) in front of a character to indicate that it is uppercase, so
* `innerHTML` must be written as `ng-prop-inner_h_t_m_l="expression"`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Might be worth noting here that usually you would use ngBindHtml for this.

*
* <div class="prop-unit">
* ... so that actually dangerous content cannot be executed:
* <div class="prop-binding" ng-prop-inner_h_t_m_l="$ctrl.unsafeContent"></div>
Copy link
Contributor

Choose a reason for hiding this comment

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

Won't this one cause an error and break the example app?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, it logs an error to the console, but doesn't stop execution

Copy link
Contributor

Choose a reason for hiding this comment

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

I guess it kills the current digest?

* often use
* [custom events](https://developer.mozilla.org/docs/Web/Guide/Events/Creating_and_triggering_events)
* that are fired like normal DOM events.
*
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it is worth mentioning camelCasing here as well for custom events that have uppercase characters in them.

* The `ngOn` directive adds an event listener to a DOM element, and evaluates an expression
* when the event is fired.
* `ngOn` allows adding listeners for arbitrary events by including
* the event name in the attribute, e.g. `ng-on-drop="onDrop()"` executes the 'onDrop' expression
Copy link
Contributor

Choose a reason for hiding this comment

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

strictly the expression is onDrop()

Copy link
Contributor

@petebacondarwin petebacondarwin left a comment

Choose a reason for hiding this comment

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

A couple of random suggestions...
Do we want to make more of the potential for use with Custom Elements?
Also do we want to draw a comparison with Angular's [prop] and (event) bindings?

The docs are written as if ngProp and ngOn were regular directives,
which makes them easier to find.
@Narretz Narretz merged commit af3b71e into angular:master Aug 3, 2018
@Narretz Narretz deleted the docs-prop-event branch August 3, 2018 10:17
Narretz added a commit that referenced this pull request Aug 3, 2018
The docs are written as if ngProp and ngOn were regular directives,
which makes them easier to find.

Closes #16627
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants