Commit Graph

61 Commits

Author SHA1 Message Date
Kevin Old f0ea2b5979 update to use spyOn for console.warn #2749 2015-01-26 21:51:42 -06:00
Paul O’Shannessy df64a67b7f codemod "use strict" to 'use strict' for better linting 2015-01-13 15:26:32 -08:00
Paul O’Shannessy 7863175cd9 Remove LegacyImmutableObject
We don't use it and it's not part of the build so nobody else is using
it. You should probably use immutable instead anyway.
2015-01-07 11:37:52 -08:00
Lee Byron a3608d27ab [traverseAllChildren] fix out-of-scope var use.
Dear ES6 gods, bring us `let` soon.

This fixes an issue where non-keyed iterables are used as children and the value of `i` would be undefined because its used out of scope. This adds a separately scoped iteration index value and appropriately increments it as iteration continues. Doi.

While I'm in there, make the usage of falsey `nameSoFar` more obvious and more consistent with the existing usage on L115

Test plan: first wrote a test covering this previously untested path. Saw an identical issue as was experienced in development environment. Then ensured test passed after this diff.
2014-11-03 21:19:28 -05:00
Lee Byron c07ea0ba34 Allow iterables in traverseAllChildren
This lets you use any kind of iterable as a container of react child elements. It also preserves keys when possible if the iterable is keyed.

 * Use an ES6 Map or Set to hold children.
 * Use an Immutable-js collection to hold children.
 * Use a function* which yields children.

Concretely, this removes the necessary conversion to JS objects if you're mapping over Immutable-js collections to get children, i.e.:

```
<div>
  {myImmutable.map(val => <span>{val}</span>).toJS()
</div>
```

Now we can remove the `toJS()` and the intermediate allocation along with it.

This also cleans up the traverseAllChildrenImpl method.
2014-10-21 15:18:53 -07:00
Sebastian Markbage 8210beeef4 Hide Object.assign polyfill behind a module
Because the JS community's polyfilling infrastructure sucks and we'll
have to fix it for them before we require this.

JSX spread uses React.__spread
(which might get special behavior for key/ref, not sure yet)

This never uses the native implementation and throws for prototype chains.
Once the native implementations are faster, we'll start using them.
2014-10-16 09:21:10 -07:00
Paul O’Shannessy dcf415c2b9 BSD + PATENTS 2014-10-10 13:34:07 -07:00
Sebastian Markbage 348af57842 Drop the @jsx docblock from unit tests
This is no longer necessary in our transforms.
2014-10-08 12:46:45 -07:00
Sebastian Markbage 096360db03 Use Object.assign instead of merge, mergeInto, mixInto and copyProperties
This makes it easier to contribute without having to learn a bunch of
slightly different helpers and remember their slightly different
signatures and semantics.

We'll probably start using ES7 spread properties instead of merge in the
future when at least one more transpiler supports it.
2014-10-08 11:32:40 -07:00
Cheng Lou 48e901f8ae [RFC] Use accumulateInto to save even more allocation
Trying to make the event a bit more performant for events.

Feel free to reject this because the API inevitably isn't great. It's good for perf though, and since we're only using `accumulate` in very restrained places, I think we're fine.

`accumulateInto` is `accumulate` that mutates more and allocates less. I kept `accumulate` in case we want that in the future.
2014-08-20 13:10:50 -07:00
Sebastian Markbage 3818656f70 Use Object.assign in merge/mergeInto
Relax the argument type checks. Currently we throw for non-objects and terminals
but Object.assign does a coercion to Object instead. It also allows merging
Arrays as if they are objects.

This also relaxes the check for dependents such as ImmutableObject. This sucks
but it will allow us to use a fast code path to native Object.assign.

We always have the option of adding warnings to Object.assign or static type
checks.

I'm keeping the null check. Object.assign throws for null checks.

We'll also start returning the result of coercions just like Object.assign.
2014-07-18 22:01:36 -07:00
Sebastian Markbage 5aab0bddaa Move key/ref off props and prepare for new descriptor factories
Breaking changes

- key/ref are no longer accessible on props but they are accessible on the
  descriptors. This means that parents/owners can access it but not the
  component itself.

- Descriptor factories are now plain functions and you can't rely on the
  prototype or constructors of descriptors to identify the component type.

Existing descriptor factories are now wrapped in a legacy factory. Currently it
does nothing but it will give us a hook to track callers to factories that are
not using JSX but just invoking the function directly. It also proxies static
methods/properties to the underlying class. The newer factories don't have this
feature.

ReactTextComponent has it's own little factory because it's props is not an
object. This is a detail and will go away once ReactTextComponent no longer
needs descriptors.
2014-07-18 22:01:36 -07:00
Cheng Lou e8e08127c5 Merge pull request #1461 from spicyj/no-esc-slash
Don't escape slash; it's unnecessary
2014-07-03 11:05:16 -07:00
Guangqiang Dong 1aa9cc6a8b add count() method to ReactChildren
added ReactChildren.count() to count the number of children, and a test case.
2014-06-30 20:54:57 -07:00
Ben Alpert 0d3622714c Don't escape slash; it's unnecessary
Fixes #1444.

Test Plan: grunt fasttest
2014-04-28 23:22:03 -03:00
Jan Kassens 514f5fb98b fix warnings in cloneWithProps test
Don't test refs in this case, already testing warning above
2014-04-23 14:14:45 -07:00
Ben Alpert f923933ef3 Make code spacing more consistent
I'm a little surprised we don't have lint rules for this.
2014-04-13 14:25:11 -07:00
Ben Alpert 216fcdeb42 Ignore children with clashing keys
Fixes #566.
2014-04-07 13:50:42 -07:00
Kunal Mehta e505e47e01 Separate immutable project
This moves Immutable and Immutable object into the new `immutable` project.
2014-03-18 15:03:26 -07:00
Kunal Mehta 0cec4af8d7 Sync latest Immutable changes 2014-03-18 14:57:04 -07:00
Sebastian Markbage 7bbdcdba96 Reassign variable of rendered component
The component that gets passed into renderComponent isn't guaranteed to be the
instance that gets mounted. We want to clone the instance.

Unit tests need to reason about the mounted instance. The first code mod changes:

  ReactTestUtils.renderIntoDocument(<identifier>)

into

  <identifier> = ReactTestUtils.renderIntoDocument(<identifier>)

Using this scripts:

  scripts/bin/codemod -m -d ~/www --extensions js \
   '^(\s*)ReactTestUtils\.renderIntoDocument\(\s*([$a-zA-Z0-9_]+)\s*\)' \
   '\1\2 = ReactTestUtils.renderIntoDocument(\2)'

In the second case I do the same for React.renderComponent. However, there are
alot more unnecessary matches so I only codemod if the same identifier occurs
later in the file.

  scripts/bin/codemod -m -d ~/www --extensions js \
   '^(\s*)React.renderComponent\(\s*([$a-zA-Z0-9_]+)\s*?,(.*?\n?.*?\s\2\b)' \
   '\1\2 = React.renderComponent(\2,\3'

And one more for ReactMount.renderComponent used by internals.

  scripts/bin/codemod -m -d ~/www --extensions js \
   '^(\s*)ReactMount.renderComponent\(\s*([$a-zA-Z0-9_]+)\s*?,(.*?\n?.*?\s\2\b)' \
   '\1\2 = ReactMount.renderComponent(\2,\3'

This still matches many unnecessary cases where the second occurance of the
identifier is a redeclaration or comment. But this code mod doesn't hurt in
those cases.

Finally I have to do the same for:

  this.<identifier> = React.renderComponent(this.<identifier>,

This is a common pattern for production code but not tests. Some of these call
sites will likely break when we move to true descriptors.

  scripts/bin/codemod -m -d ~/www --extensions js \
   '^(\s*)React.renderComponent\((\s*)this\.([$a-zA-Z0-9\_\.]+)\s*?,' \
   '\1this.\3 = React.renderComponent(\2this.\3,'
2014-03-16 22:01:09 -07:00
Kunal Mehta 4ad320dd13 LegacyImmutableObject module
This copies the old implementation of ImmutableObject into LegacyImmutableObject.
2014-03-04 18:14:12 -08:00
Ben Newman 41d30bb7de Re-require cloneWithProps as well, for consistency.
And don't mock `emptyObject`, since that might actually replace it with a
mutable/non-frozen object.
2014-03-04 13:12:59 -05:00
Ben Newman 9c87aef67f Fix stale usage of emptyObject in cloneWithProps-test.
After we run `require('mock-modules').dumpCache()`, the object exported by
the `emptyObject` module will no longer be identical to previously
exported objects, so tests like `expect(component.refs).toBe(emptyObject)`
will fail.

Note that this behavior only manifests itself in tests, because of course
we do not call `dumpCache` in production code.

We could consider storing the `emptyObject` globally to thwart the effects
of `dumpCache`, but it's more idiomatic simply to re-`require` the latest
version of `emptyObject`.
2014-03-04 12:59:55 -05:00
Pete Hunt 6666538316 Unbreak refs
If no refs are rendered, `this.refs` is undefined. This is bad since it deopts & is hard to look for. Instead we should make `this.refs` an immutable empty object.
2014-03-03 15:06:57 -08:00
Cheng Lou 2900997b5f Fix transaction test comments
Probably got copy/pasted in.
2014-02-20 16:54:52 -08:00
Paul O’Shannessy 8a47813baa Update copyrights for 2014.
grep -rl 'Copyright 2013 Facebook' static_upstream | xargs perl -pi -w -e s/Copyright 2013 Facebook/Copyright 2013-2014 Facebook/g;'

Not going to check in a script to do this since it will just change every year.
Closes #1006
2014-02-18 17:06:43 -08:00
Paul O’Shannessy 8cf5882447 Merge pull request #1021 from spicyj/close-finally
Set isInTransaction to false even if close throws
2014-02-11 09:27:09 -08:00
Cheng Lou 9ae002503c Ensure a pooled class releases back into the pool an instance of that class
Also added tests for PooledClass.

Noticed that some places use `ReactReconcileTransaction.release(transaction)`, when `transaction` might be of another class, say `ReactServerRenderingTransaction` (still a Github PR). This catches that.
2014-02-07 13:51:14 -08:00
Pete Hunt 9730759322 Fix cloneWithProps() to allow overriding props
This is a clear bug.
2014-02-04 14:37:53 -08:00
Ben Alpert 17f602f924 Set isInTransaction to false even if close throws
If a transaction wrapper's closer throws (such as flushBatchedUpdates in ReactDefaultBatchingStrategy) then we still want to properly deinitialize the transaction by setting isInTransaction to false. Without this, we can't reuse the transaction object (in this case, all future batched updates would fail because nested transactions aren't allowed).
2014-02-04 12:03:29 -08:00
Pete Hunt 4cbc4b58f6 Support children and ref for cloneWithProps()
We're not handling these correctly.
2014-01-30 16:53:47 -08:00
Paul Shen 4a5a6ad733 Transfer the key prop in cloneWithProps
`cloneWithProps` uses `ReactPropTransferer`, which ignores the `key`
prop. See https://github.com/facebook/react/pull/713

However, this is not the case with `cloneWithProps` because when someone
is cloning a component and provides a key, they mean for the clone to
take it.
2014-01-23 13:48:39 -08:00
Andreas Svensson 559cd46181 Shortened generated "data-reactid" 2014-01-20 22:03:25 +01:00
Pete Hunt 1e980a146f Fix bug in cloneWithProps() 2014-01-06 18:35:54 -08:00
Pete Hunt cc005668b5 cloneWithProps()
what if you want to change the props of a child? This is my first attempt which lets you clone a child and transfer some custom props to it.

There is a fundamental issue with refs here. Since the component is cloned the ref will be broken. And since we can clone multiple times, it might not make sense to support repairing refs.
2014-01-02 19:46:50 -08:00
Andreas Svensson a6b888b214 Escape component keys used in reactid 2013-12-28 01:58:59 +01:00
Ben Alpert b0431a51ca Respect 'key' prop for object identity
Now when a `key` prop appears, its value is always honored. This means that in the root component or as an only child, changing key will cause remounting; in a `children` object, the `key` prop will be joined with the object key to form a two-part key.

Fixes #590.
2013-12-25 22:24:10 -07:00
Paul Shen 0fed861424 Move merge utility functions from utils to vendor/core 2013-11-05 17:17:47 -08:00
SanderSpies 5a13dd090d Standardize prop -> DOM attribute process
Allow more than strings and numbers to be used as attributes for DOM
nodes. This removes the special casing for `0` and `false` that was
being used in ReactDOMInput and ReactDOMTextarea.

Now we will just `toString` any object we try to insert into a DOM.

Closes #422, #372, #302
2013-10-15 11:38:25 -07:00
Sebastian Markbage 61b38b9f05 Explicit and Implicit Keys Need Separate Namespaces
There are certain cases where you can end up with a collision with an implicit
key (array index) if your explicit key prop is a number. They should use
different namespaces. Therefore I wrap explicit keys in curlies and implicit
array indices in brackets.

I added a simple test case, but another case came up on the mailing list. Where
undefined entries in an array actually results in an entry and therefore an
implicit key.
2013-08-23 14:05:18 -07:00
Tim Yung 669f4b867f Allow DOM Nodes in ImmutableObject
Currently, `ImmutableObject` will stack overflow while it tries to recurse and deep freeze all the properties of a DOM node.
2013-08-19 11:37:08 -07:00
James Ide d9511d817a Move utils out of React that aren't being used
Many of React's util functions are non-redundant with Facebook's core
libraries, so move them out of React into a central location (out of
this repo).

These files were not getting used by any part of React core, so didn't
actually belong here anyway.
2013-08-16 15:40:11 -07:00
Paul Shen 9ef4e74ba2 ReactChildren
Instead of changing `traverseAllChildren`, keep that around for perf
reasons (for the hot code path `flattenChildren`)

Introduce `ReactChildren.map` and `ReactChildren.forEach`
which mirrors `Array.prototype.map` and `Array.prototype.forEach`. This
involves a rename of `mapAllChildren`
2013-08-06 14:17:33 -07:00
Jordan Walke 492407bcc9 Fix OrderedMap.
Tim caught a bug. Squashing it so he can rebase on top of it.
2013-07-24 17:39:37 -07:00
Jordan Walke 5d4f903482 [React Children] Step Three: New test case for traverseChildren.
If we're going to build utilities off of this, we should have good test
coverage.
2013-07-16 11:36:53 -07:00
Jordan Walke 946029c921 [React Children] Step Two: Use traverseChildren to perform mapChildren
Use the new `traverseChildren` utility to perform `mapChildren`.
The goal is to get as close to the bavior of the semantics of
`Array.prototype.map`, but also in a way that understands deeply nested arrays
and objects.
2013-07-16 11:36:11 -07:00
Sebastian Markbage d50148591b Introduce a supported way to slice children
Introduces a counterpart to mapChildren. It excludes empty children just as
mapChildren for compatibility. With might introduce something like
sliceChildrenIncludingEmptyValues at some point.
2013-07-02 18:30:04 -07:00
CommitSyncScript dd61439061 Revert Flattening of Children
I still think the semantics of flattening children is valid but we'll
want to revert the flattening implementation while we solidify the
semantics and try another approach.

This reverts flattening so that this.props.children can once again be
either a single child, flat array or nested array.

mapChildren calls flattenChildren before doing anything else. This is
not the most efficient approach but I wanted to keep this inital diff
simple. It also ignores empty values for backwards compatibility.

We may want to try another approach where empty values are included
in the map.

Validation of keys is still done inside ReactComponent. Ideally I'd
like to extract that into a separate module but to avoid cyclic
dependencies, I'm keeping it in ReactComponent for now.
2013-07-01 13:01:52 -07:00
CommitSyncScript 15272f30f4 Don't keep the HTML escaped ID internally, only in HTML generation
A dynamic value can be provided as a key to a child. Either as part of an object
or key property. This becomes part of the component's ID.

We have to be careful to escape this key before inserting it into the DOM since
it could become a vulnerability. We fixed this by escaping just the keys.

However, the current implementation breaks when you used escaped keys. The
internal value is escaped and the value used by getAttributeNode and
getElementById are both unescaped.

This fixes that by keeping the unescaped value internally but escaping it right
before the HTML is generated (like any other attribute).

This is important since business logic IDs (that should be used as keys)
contains characters that need to be escaped.
2013-06-28 16:35:45 -07:00