Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Fix #552: Added SwiftDomainParser to get eTLD+1 and manage textAlignm…
Browse files Browse the repository at this point in the history
…ent (#869)
  • Loading branch information
danishjafri88 authored and jhreis committed Aug 8, 2019
1 parent 4bf518f commit f5d3d99
Show file tree
Hide file tree
Showing 11 changed files with 3,442 additions and 986 deletions.
4 changes: 4 additions & 0 deletions Client.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@
4F514FD41ACD8F2C0022D7EA /* HistoryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4F514FD31ACD8F2C0022D7EA /* HistoryTests.swift */; };
4F97573B1AFA6F37006ECC24 /* readerContent.html in Resources */ = {isa = PBXBuildFile; fileRef = 4F9757391AFA6F37006ECC24 /* readerContent.html */; };
55A747171DC46FC400CE1B57 /* HomePageUITest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55A747161DC46FC400CE1B57 /* HomePageUITest.swift */; };
59350AFB22612D95004D7445 /* DisplayTextFieldTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 59350AFA22612D95004D7445 /* DisplayTextFieldTest.swift */; };
592F521E2217327C0078395E /* HttpCookieExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 592F521D2217327B0078395E /* HttpCookieExtension.swift */; };
5953AAEF2226E9D800A92DE1 /* HttpCookieExtensionTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5953AAEE2226E9D800A92DE1 /* HttpCookieExtensionTest.swift */; };
595E0EDB21CAD97C00813D49 /* CookieControl.js in Resources */ = {isa = PBXBuildFile; fileRef = 595E0EDA21CAD97C00813D49 /* CookieControl.js */; };
Expand Down Expand Up @@ -1617,6 +1618,7 @@
5929A9F221B6AF03005ECF8D /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/Localizable.strings; sourceTree = "<group>"; };
5929A9F321B6AF0F005ECF8D /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/Shared.strings; sourceTree = "<group>"; };
5929A9F421B6AF18005ECF8D /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = "<group>"; };
59350AFA22612D95004D7445 /* DisplayTextFieldTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DisplayTextFieldTest.swift; sourceTree = "<group>"; };
592F521D2217327B0078395E /* HttpCookieExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HttpCookieExtension.swift; sourceTree = "<group>"; };
59347A7822E22F2000C38919 /* pt-BR */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "pt-BR"; path = "pt-BR.lproj/BraveShared.strings"; sourceTree = "<group>"; };
5953AAEE2226E9D800A92DE1 /* HttpCookieExtensionTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HttpCookieExtensionTest.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -4222,6 +4224,7 @@
D8EFFA251FF702A8001D3A09 /* NavigationRouterTests.swift */,
2795274921A890EB00921AA1 /* FingerprintingProtectionTests.swift */,
0A4214E821A6EBCF006B8E39 /* SafeBrowsingTests.swift */,
59350AFA22612D95004D7445 /* DisplayTextFieldTest.swift */,
5953AAEE2226E9D800A92DE1 /* HttpCookieExtensionTest.swift */,
F939FBFD22A596B900D9CD3F /* U2FTests.swift */,
5E9288C922DF864C007BE7A6 /* TabSessionTests.swift */,
Expand Down Expand Up @@ -6057,6 +6060,7 @@
0A4BEFDE221F03C80005551A /* NetworkManagerTests.swift in Sources */,
39236E721FCC600200A38F1B /* TabEventHandlerTests.swift in Sources */,
2F13E79B1AC0C02700D75081 /* StringExtensionsTests.swift in Sources */,
59350AFB22612D95004D7445 /* DisplayTextFieldTest.swift in Sources */,
2FDB10931A9FBEC5006CF312 /* PrefsTests.swift in Sources */,
D8EFFA261FF702A8001D3A09 /* NavigationRouterTests.swift in Sources */,
F939FBFE22A596B900D9CD3F /* U2FTests.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,6 @@ extension WKNavigationAction {
}
}

extension URL {
/// Obtain a schemeless absolute string
fileprivate var schemelessAbsoluteString: String {
guard let scheme = self.scheme else { return absoluteString }
return absoluteString.replacingOccurrences(of: "\(scheme)://", with: "")
}
}

extension BrowserViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
if tabManager.selectedTab?.webView !== webView {
Expand Down
34 changes: 22 additions & 12 deletions Client/Frontend/Browser/Toolbars/UrlBar/TabLocationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class TabLocationView: UIView {
urlTextField.accessibilityActionsSource = self
urlTextField.font = UIConstants.DefaultChromeFont
urlTextField.backgroundColor = .clear

urlTextField.clipsToBounds = true
// Remove the default drop interaction from the URL text field so that our
// custom drop interaction on the BVC can accept dropped URLs.
if let dropInteraction = urlTextField.textDropInteraction {
Expand Down Expand Up @@ -341,15 +341,8 @@ class TabLocationView: UIView {
}

fileprivate func updateTextWithURL() {
if let host = url?.host, AppConstants.MOZ_PUNYCODE {
urlTextField.text = url?.absoluteString.replacingOccurrences(of: host, with: host.asciiHostToUTF8())
} else {
urlTextField.text = url?.absoluteString
}
// remove https:// (the scheme) from the url when displaying
if let scheme = url?.scheme, let range = url?.absoluteString.range(of: "\(scheme)://") {
urlTextField.text = url?.absoluteString.replacingCharacters(in: range, with: "")
}
(urlTextField as? DisplayTextField)?.hostString = url?.host ?? ""
urlTextField.text = url?.schemelessAbsoluteString.trim("/")
}
}

Expand Down Expand Up @@ -427,8 +420,10 @@ extension TabLocationView: TabEventHandler {
}
}

private class DisplayTextField: UITextField {
class DisplayTextField: UITextField {
weak var accessibilityActionsSource: AccessibilityActionsSource?
var hostString: String = ""
let pathPadding: CGFloat = 20.0

override var accessibilityCustomActions: [UIAccessibilityCustomAction]? {
get {
Expand All @@ -439,7 +434,22 @@ private class DisplayTextField: UITextField {
}
}

fileprivate override var canBecomeFirstResponder: Bool {
override var canBecomeFirstResponder: Bool {
return false
}

// This override is done in case the eTLD+1 string overflows the width of textField.
// In that case the textRect is adjusted to show right aligned and truncate left.
// Since this textField changes with WebView domain change, performance implications are low.
override func textRect(forBounds bounds: CGRect) -> CGRect {
var rect: CGRect = super.textRect(forBounds: bounds)

if let size: CGSize = (self.hostString as NSString?)?.size(withAttributes: [.font: self.font!]) {
if size.width > self.bounds.width {
rect.origin.x = rect.origin.x - (size.width + pathPadding - self.bounds.width)
rect.size.width = size.width + pathPadding
}
}
return rect
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ extension TopToolbarView: TabLocationViewDelegate {

var overlayText = locationText
// Make sure to use the result from topToolbarDisplayTextForURL as it is responsible for extracting out search terms when on a search page
if let text = locationText, let url = URL(string: text), let host = url.host, AppConstants.MOZ_PUNYCODE {
if let text = locationText, let url = URL(string: text), let host = url.host {
overlayText = url.absoluteString.replacingOccurrences(of: host, with: host.asciiHostToUTF8())
}
enterOverlayMode(overlayText, pasted: false, search: isSearchQuery)
Expand Down
13 changes: 2 additions & 11 deletions Client/Frontend/Browser/URIFixup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class URIFixup {
// However, we ensure that the scheme is one that is listed in
// the official URI scheme list, so that other such search phrases
// like "filetype:" are recognised as searches rather than URLs.
if let url = punycodedURL(escaped), url.schemeIsValid {
if let url = URL(string: escaped), url.schemeIsValid {
return url
}

Expand All @@ -35,19 +35,10 @@ class URIFixup {

// If there is a ".", prepend "http://" and try again. Since this
// is strictly an "http://" URL, we also require a host.
if let url = punycodedURL("http://\(escaped)"), url.host != nil {
if let url = URL(string: "http://\(escaped)"), url.host != nil {
return url
}

return nil
}

static func punycodedURL(_ string: String) -> URL? {
var components = URLComponents(string: string)
if AppConstants.MOZ_PUNYCODE {
let host = components?.host?.utf8HostToAscii()
components?.host = host
}
return components?.url
}
}
41 changes: 41 additions & 0 deletions ClientTests/DisplayTextFieldTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

@testable import Client
import XCTest

class DisplayTextFieldTest: XCTestCase {

func testRightAlignedTLD() {
let textField = DisplayTextField(frame: CGRect(width: 250, height: 44))

let urlCases: [URL] = [URL(string: "https://www.google.com")!,
URL(string: "http://myaccountsecure.testmycase.co.uk/asd?a=1")!,
URL(string: "http://testmycase.co.uk/something/path/asd?a=1")!,
URL(string: "http://myaccountsecure.secured.testmycase.co.uk/asd?a=1")!,
URL(string: "http://myaccountsecure.testmycase.co.uk")!]
urlCases.forEach({textField.assertWidth(text: $0.schemelessAbsoluteString, hostString: $0.host ?? "")})

}


}


extension DisplayTextField {
func assertWidth(text: String, hostString: String) {
self.hostString = hostString
self.text = text
let rect = self.textRect(forBounds: self.bounds)
if rect.width > self.bounds.width, let upperBound = text.range(of: hostString)?.upperBound {
let pathString = text[upperBound...]
let widthPath = (pathString as NSString).size(withAttributes: [.font: self.font!]).width
let widthText = (text as NSString).size(withAttributes: [.font: self.font!]).width

let test1 = rect.width + widthPath - pathPadding == widthText
let test2 = rect.origin.x == self.bounds.width - widthText + widthPath - pathPadding
XCTAssert(test1 && test2)
}
}
}
6 changes: 0 additions & 6 deletions ClientTests/SearchTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,6 @@ class SearchTests: XCTestCase {
checkInvalidURL("data://https://www.example.com,fake example.com")
}

func testURIFixupPunyCode() {
checkValidURL("http://创业咖啡.中国/", afterFixup: "http://xn--vhq70hq9bhxa.xn--fiqs8s/")
checkValidURL("创业咖啡.中国", afterFixup: "http://xn--vhq70hq9bhxa.xn--fiqs8s")
checkValidURL(" 创业咖啡.中国 ", afterFixup: "http://xn--vhq70hq9bhxa.xn--fiqs8s")
}

fileprivate func checkValidURL(_ beforeFixup: String, afterFixup: String) {
XCTAssertEqual(URIFixup.getURL(beforeFixup)!.absoluteString, afterFixup)
}
Expand Down
14 changes: 0 additions & 14 deletions Shared/AppConstants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,6 @@ public struct AppConstants {
#endif
}()

/// Enables support for International Domain Names (IDN)
/// Disabled because of https://bugzilla.mozilla.org/show_bug.cgi?id=1312294
public static let MOZ_PUNYCODE: Bool = {
#if MOZ_CHANNEL_RELEASE
return false
#elseif MOZ_CHANNEL_BETA
return false
#elseif MOZ_CHANNEL_FENNEC
return true
#else
return true
#endif
}()

/// The maximum length of a URL stored by Firefox. Shared with Places on desktop.
public static let DB_URL_LENGTH_MAX = 65536

Expand Down
5 changes: 5 additions & 0 deletions Shared/Extensions/StringExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ extension String {
}
return trimmed
}

//Minimize trimming effort for characterset based on string
public func trim(_ charactersInString: String) -> String {
return self.trimmingCharacters(in: CharacterSet(charactersIn: charactersInString))
}

/// Adds a newline at the closest space from the middle of a string.
/// Example turning "Mark as Read" into "Mark as\n Read"
Expand Down
6 changes: 6 additions & 0 deletions Shared/Extensions/URLExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,12 @@ extension URL {

return nil
}

// Obtain a schemeless absolute string
public var schemelessAbsoluteString: String {
guard let scheme = self.scheme else { return absoluteString }
return absoluteString.replacingOccurrences(of: "\(scheme)://", with: "")
}

/**
Returns the base domain from a given hostname. The base domain name is defined as the public domain suffix
Expand Down
Loading

0 comments on commit f5d3d99

Please sign in to comment.