-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1283 from wordpress-mobile/release/1.19.0
Release/1.19.0
- Loading branch information
Showing
19 changed files
with
258 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...ters/StringAttributesToAttributes/Implementations/SubscriptStringAttributeConverter.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import Foundation | ||
import UIKit | ||
|
||
|
||
/// Converts the subscript style information from string attributes and aggregates it into an | ||
/// existing array of element nodes. | ||
/// | ||
open class SubscriptStringAttributeConverter: StringAttributeConverter { | ||
|
||
private let toggler = HTMLStyleToggler(defaultElement: .sub, cssAttributeMatcher: NeverCSSAttributeMatcher()) | ||
|
||
public func convert( | ||
attributes: [NSAttributedString.Key: Any], | ||
andAggregateWith elementNodes: [ElementNode]) -> [ElementNode] { | ||
|
||
var elementNodes = elementNodes | ||
|
||
// We add the representation right away, if it exists... as it could contain attributes beyond just this | ||
// style. The enable and disable methods below can modify this as necessary. | ||
// | ||
if let representation = attributes[NSAttributedString.Key.subHtmlRepresentation] as? HTMLRepresentation, | ||
case let .element(representationElement) = representation.kind { | ||
|
||
elementNodes.append(representationElement.toElementNode()) | ||
} | ||
|
||
if shouldEnable(for: attributes) { | ||
return toggler.enable(in: elementNodes) | ||
} else { | ||
return toggler.disable(in: elementNodes) | ||
} | ||
} | ||
|
||
// MARK: - Style Detection | ||
|
||
func shouldEnable(for attributes: [NSAttributedString.Key : Any]) -> Bool { | ||
return hasTraits(for: attributes) | ||
} | ||
|
||
func hasTraits(for attributes: [NSAttributedString.Key : Any]) -> Bool { | ||
guard let baselineOffset = attributes[.baselineOffset] as? NSNumber else { | ||
return false | ||
} | ||
|
||
return baselineOffset.intValue < 0; | ||
} | ||
} | ||
|
48 changes: 48 additions & 0 deletions
48
...rs/StringAttributesToAttributes/Implementations/SuperscriptStringAttributeConverter.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import Foundation | ||
import UIKit | ||
|
||
|
||
/// Converts the superscript style information from string attributes and aggregates it into an | ||
/// existing array of element nodes. | ||
/// | ||
open class SuperscriptStringAttributeConverter: StringAttributeConverter { | ||
|
||
private let toggler = HTMLStyleToggler(defaultElement: .sup, cssAttributeMatcher: NeverCSSAttributeMatcher()) | ||
|
||
public func convert( | ||
attributes: [NSAttributedString.Key: Any], | ||
andAggregateWith elementNodes: [ElementNode]) -> [ElementNode] { | ||
|
||
var elementNodes = elementNodes | ||
|
||
// We add the representation right away, if it exists... as it could contain attributes beyond just this | ||
// style. The enable and disable methods below can modify this as necessary. | ||
// | ||
if let representation = attributes[NSAttributedString.Key.supHtmlRepresentation] as? HTMLRepresentation, | ||
case let .element(representationElement) = representation.kind { | ||
|
||
elementNodes.append(representationElement.toElementNode()) | ||
} | ||
|
||
if shouldEnable(for: attributes) { | ||
return toggler.enable(in: elementNodes) | ||
} else { | ||
return toggler.disable(in: elementNodes) | ||
} | ||
} | ||
|
||
// MARK: - Style Detection | ||
|
||
func shouldEnable(for attributes: [NSAttributedString.Key : Any]) -> Bool { | ||
return hasTraits(for: attributes) | ||
} | ||
|
||
func hasTraits(for attributes: [NSAttributedString.Key : Any]) -> Bool { | ||
guard let baselineOffset = attributes[.baselineOffset] as? NSNumber else { | ||
return false | ||
} | ||
|
||
return baselineOffset.intValue > 0; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
Aztec/Classes/Formatters/Implementations/SubscriptFormatter.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import UIKit | ||
|
||
class SubscriptFormatter: StandardAttributeFormatter { | ||
|
||
init() { | ||
super.init(attributeKey: .baselineOffset, | ||
attributeValue: NSNumber(-4), | ||
htmlRepresentationKey: .subHtmlRepresentation) | ||
} | ||
|
||
override func apply(to attributes: [NSAttributedString.Key: Any], andStore representation: HTMLRepresentation?) -> [NSAttributedString.Key: Any] { | ||
var resultingAttributes = super.apply(to: attributes, andStore: representation) | ||
guard let currentFont = attributes[.font] as? UIFont else { | ||
return resultingAttributes | ||
} | ||
let font = UIFont(descriptor: currentFont.fontDescriptor, size: currentFont.pointSize - 2) | ||
resultingAttributes[.font] = font | ||
return resultingAttributes | ||
} | ||
|
||
override func remove(from attributes: [NSAttributedString.Key: Any]) -> [NSAttributedString.Key: Any] { | ||
var resultingAttributes = super.remove(from: attributes) | ||
|
||
guard let currentFont = attributes[.font] as? UIFont else { | ||
return resultingAttributes | ||
} | ||
let font = UIFont(descriptor: currentFont.fontDescriptor, size: currentFont.pointSize + 2) | ||
resultingAttributes[.font] = font | ||
|
||
return resultingAttributes | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
Aztec/Classes/Formatters/Implementations/SuperscriptFormatter.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import UIKit | ||
|
||
class SuperscriptFormatter: StandardAttributeFormatter { | ||
|
||
init() { | ||
super.init(attributeKey: .baselineOffset, | ||
attributeValue: NSNumber(4), | ||
htmlRepresentationKey: .supHtmlRepresentation) | ||
} | ||
|
||
override func apply(to attributes: [NSAttributedString.Key: Any], andStore representation: HTMLRepresentation?) -> [NSAttributedString.Key: Any] { | ||
var resultingAttributes = super.apply(to: attributes, andStore: representation) | ||
guard let currentFont = attributes[.font] as? UIFont else { | ||
return resultingAttributes | ||
} | ||
let font = UIFont(descriptor: currentFont.fontDescriptor, size: currentFont.pointSize - 2) | ||
resultingAttributes[.font] = font | ||
return resultingAttributes | ||
} | ||
|
||
override func remove(from attributes: [NSAttributedString.Key: Any]) -> [NSAttributedString.Key: Any] { | ||
var resultingAttributes = super.remove(from: attributes) | ||
|
||
guard let currentFont = attributes[.font] as? UIFont else { | ||
return resultingAttributes | ||
} | ||
let font = UIFont(descriptor: currentFont.fontDescriptor, size: currentFont.pointSize + 2) | ||
resultingAttributes[.font] = font | ||
|
||
return resultingAttributes | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.