Skip to content

Conversation

@jleandroperez
Copy link
Contributor

Details:

This PR introduces no new functionality. We're:

  1. Encapsulating the linkedImage parsing mechanism, into it's own method
  2. Moving the textAttachment constant over to a String extension, for convenience
  3. Adding a small method to ElementNode (and few unit tests!)

To test:

  1. Verify that the unit tests are green
  2. Add a linked image <a href="www.wordpress.com"><img src="www.something.com/img"></a>
  3. Try toggling back and forth, and verity that the HTML is preserved

Needs Review: @diegoreymendez
Thanks in advance!

extension String {
/// String containing the NSTextAttachment Character
///
static let textAttachment = String(UnicodeScalar(NSAttachmentCharacter)!)
Copy link
Contributor

Choose a reason for hiding this comment

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

Loved this little touch!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I absolutely LOVE 'extensible enums' (that's this trick's name!)


/// Verifies that `onlyChildr` returns the receiver's only child, if it's type matches with the specified one.
///
func testOnlyChildReturnsSingleChildrenIfItRepresentsAnImage() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Great to have this tests on.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you sir!

Copy link
Contributor

@SergioEstevao SergioEstevao left a comment

Choose a reason for hiding this comment

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

Great work here!

}


/// Returns the ElementNode instance, whenever there's a *single* children, of the specified nodeType (or nil otherwise).
Copy link
Contributor

@diegoreymendez diegoreymendez Nov 30, 2017

Choose a reason for hiding this comment

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

As "children" is the plural form, we should change to "child" here and in the lines below.

/// If true, returns the '.img' node.


/// Whenever the `element`'s nodeType is `a` (link!), and there's a single children of the `.img` type, this method will return the
Copy link
Contributor

Choose a reason for hiding this comment

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

/s/children/child

}


/// Verifies that `onlyChildr` returns the receiver's only child, if it's type matches with the specified one.
Copy link
Contributor

Choose a reason for hiding this comment

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

/s/onlyChildr/onlyChild

Want to avoid flooding the PR but search for other occurrences below.

guard children.count == 1,
let onlyChild = children.first as? ElementNode,
onlyChild.isNodeType(type)
else {
Copy link
Contributor

Choose a reason for hiding this comment

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

Closing { goes in the same line as the declaration preceding it.

From https://github.com/wordpress-mobile/swift-style-guide#braces:

Opening braces should be preceded by a single space and on the same line as the declaration. The exception to this rule is when enclosed within parentheses: no preceding space is required.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Our guidelines don't fully apply to this statement, because the guard clause has multiple lines / conditions.

Moving the else to the preceding line, would have the sad effect of forcing us to further increase the indentation level of the snippet that goes inside the else.

IMO this way we truly improve readability, and avoid increasing indentation.
I'll submit a formal request to add this to our guideline!

Copy link
Contributor

@diegoreymendez diegoreymendez Nov 30, 2017

Choose a reason for hiding this comment

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

I still think this looks very inconsistent with how we format the rest of our code, as we don't separate else statements from preceding declarations anywhere else.

The fact that auto-indent adds an extra tab should not affect how we format the code.

That said, as this won't affect the logic in the code, we can merge as it is.

///
/// - Returns: the requested children (if it's the only children in the collection, and if the type matches), or nil otherwise.
///
func onlyChild(ofType type: StandardElementType) -> ElementNode? {
Copy link
Contributor

Choose a reason for hiding this comment

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

In order to aim for single-responsibility would you mind splitting this method into two steps (retrieval, filtering)?

This'd help ensure our methods are easier to compose, and also easier to read and understand.

Example:

/// This one returns first child found, if it's the only one.
///
func onlyChild() -> ElementNode? {
    guard children.count == 1 else {
        return nil
    }

    return children.first as? ElementNode
}

/// This one returns the first child found, if it's the only one and it matches the specified type.
///
func onlyChild(ofType type: StandardElementType) -> ElementNode? {
    guard let child = onlyChild(),
        child.isNodeType(type) else {
            return nil
    }

    return child
}

Copy link
Contributor

@diegoreymendez diegoreymendez left a comment

Choose a reason for hiding this comment

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

Just one more missing "children" replacement, but otherwise approved.

Nice work.

///
/// - Parameter type: Type of the 'single child' node to be retrieved.
///
/// - Returns: the requested children (if it's the only children in the collection, and if the type matches), or nil otherwise.
Copy link
Contributor

Choose a reason for hiding this comment

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

/s/children/child

@jleandroperez
Copy link
Contributor Author

Thank you both!

@jleandroperez jleandroperez merged commit 0eb6838 into develop Dec 1, 2017
@jleandroperez jleandroperez deleted the issue/linked-image-cleanup branch December 1, 2017 00:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants