Title: [96480] trunk/Source
Revision
96480
Author
wei...@apple.com
Date
2011-10-02 17:40:43 -0700 (Sun, 02 Oct 2011)

Log Message

Strength reduce Mac only Frame::searchForLabelsBeforeElement and Frame::matchLabelsAgainstElement into callers
https://bugs.webkit.org/show_bug.cgi?id=69230

Reviewed by Anders Carlsson.

../WebCore: 

* WebCore.exp.in:
* page/Frame.h:
* page/mac/FrameMac.mm:

../WebKit/mac: 

* WebView/WebHTMLRepresentation.mm:
(regExpForLabels):
(searchForLabelsBeforeElement):
(matchLabelsAgainstString):
(matchLabelsAgainstElement):
(-[WebHTMLRepresentation searchForLabels:beforeElement:resultDistance:resultIsInCellAbove:]):
(-[WebHTMLRepresentation matchLabels:againstElement:]):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (96479 => 96480)


--- trunk/Source/WebCore/ChangeLog	2011-10-03 00:22:04 UTC (rev 96479)
+++ trunk/Source/WebCore/ChangeLog	2011-10-03 00:40:43 UTC (rev 96480)
@@ -1,3 +1,14 @@
+2011-10-02  Sam Weinig  <s...@webkit.org>
+
+        Strength reduce Mac only Frame::searchForLabelsBeforeElement and Frame::matchLabelsAgainstElement into callers
+        https://bugs.webkit.org/show_bug.cgi?id=69230
+
+        Reviewed by Anders Carlsson.
+
+        * WebCore.exp.in:
+        * page/Frame.h:
+        * page/mac/FrameMac.mm:
+
 2011-10-02  Zeno Albisser  <zeno.albis...@nokia.com>
 
         [Qt][WK2][Mac] WebKit2 does not build on mac after merge of Qt5 refactor branch.

Modified: trunk/Source/WebCore/WebCore.exp.in (96479 => 96480)


--- trunk/Source/WebCore/WebCore.exp.in	2011-10-03 00:22:04 UTC (rev 96479)
+++ trunk/Source/WebCore/WebCore.exp.in	2011-10-03 00:40:43 UTC (rev 96480)
@@ -136,6 +136,8 @@
 __ZN3WTF10StringImplcvP8NSStringEv
 __ZN3WTF6StringC1EP8NSString
 __ZN3WTF6StringC1EPK10__CFString
+__ZN7WebCore10ClientRectC1ERKNS_7IntRectE
+__ZN7WebCore10ClientRectC1Ev
 __ZN7WebCore10CredentialC1ERKN3WTF6StringES4_NS_21CredentialPersistenceE
 __ZN7WebCore10CredentialC1Ev
 __ZN7WebCore10FloatPointC1ERKNS_8IntPointE
@@ -146,14 +148,12 @@
 __ZN7WebCore10ScrollView17setUseFixedLayoutEb
 __ZN7WebCore10ScrollView18setFixedLayoutSizeERKNS_7IntSizeE
 __ZN7WebCore10ScrollView20setCanHaveScrollbarsEb
+__ZN7WebCore10ScrollView23setScrollbarsSuppressedEbb
 __ZN7WebCore10ScrollView24windowResizerRectChangedEv
 __ZN7WebCore10deleteFileERKN3WTF6StringE
 __ZN7WebCore10handCursorEv
 __ZN7WebCore10setCookiesEPNS_8DocumentERKNS_4KURLERKN3WTF6StringE
 __ZN7WebCore10toDocumentEN3JSC7JSValueE
-__ZN7WebCore10ClientRectC1Ev
-__ZN7WebCore10ClientRectC1ERKNS_7IntRectE
-__ZN7WebCore4toJSEPN3JSC9ExecStateEPNS_17JSDOMGlobalObjectEPNS_10ClientRectE
 __ZN7WebCore11BitmapImageC1EP7CGImagePNS_13ImageObserverE
 __ZN7WebCore11CachedFrame23cachedFramePlatformDataEv
 __ZN7WebCore11CachedFrame26setCachedFramePlatformDataEN3WTF10PassOwnPtrINS_23CachedFramePlatformDataEEE
@@ -211,12 +211,12 @@
 __ZN7WebCore11HistoryItemC1Ev
 __ZN7WebCore11HistoryItemD1Ev
 __ZN7WebCore11MemoryCache11setDisabledEb
+__ZN7WebCore11MemoryCache11setDisabledEb
 __ZN7WebCore11MemoryCache13getStatisticsEv
 __ZN7WebCore11MemoryCache13setCapacitiesEjjj
 __ZN7WebCore11MemoryCache14evictResourcesEv
 __ZN7WebCore11MemoryCache19getOriginsWithCacheERN3WTF7HashSetINS1_6RefPtrINS_14SecurityOriginEEENS_18SecurityOriginHashENS1_10HashTraitsIS5_EEEE
 __ZN7WebCore11MemoryCache25removeResourcesWithOriginEPNS_14SecurityOriginE
-__ZN7WebCore11MemoryCache11setDisabledEb
 __ZN7WebCore11RenderLayer19scrollRectToVisibleERKNS_7IntRectERKNS_15ScrollAlignmentES6_
 __ZN7WebCore11globalPointERK8_NSPointP8NSWindow
 __ZN7WebCore11memoryCacheEv
@@ -373,7 +373,6 @@
 __ZN7WebCore14ScrollableArea19willStartLiveResizeEv
 __ZN7WebCore14ScrollableArea28setScrollOffsetFromInternalsERKNS_8IntPointE
 __ZN7WebCore14ScrollbarTheme11nativeThemeEv
-__ZN7WebCore10ScrollView23setScrollbarsSuppressedEbb
 __ZN7WebCore14SecurityOrigin14threadsafeCopyEv
 __ZN7WebCore14SecurityOrigin16createFromStringERKN3WTF6StringE
 __ZN7WebCore14SecurityOrigin18setLocalLoadPolicyENS0_15LocalLoadPolicyE
@@ -488,8 +487,8 @@
 __ZN7WebCore16ThreadGlobalDataD1Ev
 __ZN7WebCore16VisibleSelection22expandUsingGranularityENS_15TextGranularityE
 __ZN7WebCore16VisibleSelectionC1EPKNS_5RangeENS_9EAffinityEb
+__ZN7WebCore16VisibleSelectionC1ERKNS_15VisiblePositionES3_b
 __ZN7WebCore16VisibleSelectionC1ERKNS_15VisiblePositionEb
-__ZN7WebCore16VisibleSelectionC1ERKNS_15VisiblePositionES3_b
 __ZN7WebCore16colorFromNSColorEP7NSColor
 __ZN7WebCore16createFullMarkupEPKNS_4NodeE
 __ZN7WebCore16createFullMarkupEPKNS_5RangeE
@@ -506,6 +505,8 @@
 __ZN7WebCore17GlyphPageTreeNode18treeGlyphPageCountEv
 __ZN7WebCore17HistoryController26saveDocumentAndScrollStateEv
 __ZN7WebCore17HistoryController33restoreScrollPositionAndViewStateEv
+__ZN7WebCore17RegularExpressionC1ERKN3WTF6StringENS1_19TextCaseSensitivityE
+__ZN7WebCore17RegularExpressionD1Ev
 __ZN7WebCore17cacheDOMStructureEPNS_17JSDOMGlobalObjectEPN3JSC9StructureEPKNS2_9ClassInfoE
 __ZN7WebCore17nameForCursorTypeENS_6Cursor4TypeE
 __ZN7WebCore17openTemporaryFileERKN3WTF6StringERi
@@ -710,6 +711,7 @@
 __ZN7WebCore4Page16setDefersLoadingEb
 __ZN7WebCore4Page17willMoveOffscreenEv
 __ZN7WebCore4Page18removeSchedulePairEN3WTF10PassRefPtrINS_12SchedulePairEEE
+__ZN7WebCore4Page18setPageScaleFactorEfRKNS_8IntPointE
 __ZN7WebCore4Page19visitedStateChangedEPNS_9PageGroupEy
 __ZN7WebCore4Page20setDeviceScaleFactorEf
 __ZN7WebCore4Page20unmarkAllTextMatchesEv
@@ -720,7 +722,6 @@
 __ZN7WebCore4Page31setCustomHTMLTokenizerChunkSizeEi
 __ZN7WebCore4Page31setCustomHTMLTokenizerTimeDelayEd
 __ZN7WebCore4Page32setMemoryCacheClientCallsEnabledEb
-__ZN7WebCore4Page18setPageScaleFactorEfRKNS_8IntPointE
 __ZN7WebCore4Page37setInLowQualityImageInterpolationModeEb
 __ZN7WebCore4Page6goBackEv
 __ZN7WebCore4Page8goToItemEPNS_11HistoryItemENS_13FrameLoadTypeE
@@ -733,6 +734,7 @@
 __ZN7WebCore4PathD1Ev
 __ZN7WebCore4coreEP20NSURLProtectionSpace
 __ZN7WebCore4coreEP28NSURLAuthenticationChallenge
+__ZN7WebCore4toJSEPN3JSC9ExecStateEPNS_17JSDOMGlobalObjectEPNS_10ClientRectE
 __ZN7WebCore4toJSEPN3JSC9ExecStateEPNS_17JSDOMGlobalObjectEPNS_19CSSStyleDeclarationE
 __ZN7WebCore4toJSEPN3JSC9ExecStateEPNS_17JSDOMGlobalObjectEPNS_5RangeE
 __ZN7WebCore4toJSEPN3JSC9ExecStateEPNS_17JSDOMGlobalObjectEPNS_8NodeListE
@@ -744,18 +746,16 @@
 __ZN7WebCore5Frame17setPageZoomFactorEf
 __ZN7WebCore5Frame17setTextZoomFactorEf
 __ZN7WebCore5Frame23visiblePositionForPointERKNS_8IntPointE
-__ZN7WebCore5Frame25matchLabelsAgainstElementEP7NSArrayPNS_7ElementE
-__ZNK7WebCore5Frame16frameScaleFactorEv
+__ZN7WebCore5Frame24searchForLabelsAboveCellEPNS_17RegularExpressionEPNS_20HTMLTableCellElementEPm
 __ZN7WebCore5Frame25setPageAndTextZoomFactorsEff
 __ZN7WebCore5Frame27resizePageRectsKeepingRatioERKNS_9FloatSizeES3_
-__ZN7WebCore5Frame28searchForLabelsBeforeElementEP7NSArrayPNS_7ElementEPmPb
 __ZN7WebCore5Frame6createEPNS_4PageEPNS_21HTMLFrameOwnerElementEPNS_17FrameLoaderClientE
 __ZN7WebCore5Frame7setViewEN3WTF10PassRefPtrINS_9FrameViewEEE
 __ZN7WebCore5Frame9nodeImageEPNS_4NodeE
 __ZN7WebCore5FrameD1Ev
-__ZN7WebCore5Image9nullImageEv
 __ZN7WebCore5Image12supportsTypeERKN3WTF6StringE
 __ZN7WebCore5Image20loadPlatformResourceEPKc
+__ZN7WebCore5Image9nullImageEv
 __ZN7WebCore5Range10selectNodeEPNS_4NodeERi
 __ZN7WebCore5Range14isPointInRangeEPNS_4NodeEiRi
 __ZN7WebCore5Range6createEN3WTF10PassRefPtrINS_8DocumentEEENS2_INS_4NodeEEEiS6_i
@@ -850,6 +850,7 @@
 __ZN7WebCore7TextRun22setAllowsRoundingHacksEb
 __ZN7WebCore7cookiesEPKNS_8DocumentERKNS_4KURLE
 __ZN7WebCore7nsColorERKNS_5ColorE
+__ZN7WebCore7replaceERN3WTF6StringERKNS_17RegularExpressionERKS1_
 __ZN7WebCore8Document11createRangeEv
 __ZN7WebCore8Document12updateLayoutEv
 __ZN7WebCore8Document13svgExtensionsEv
@@ -949,6 +950,7 @@
 __ZN7WebCore8toStringERKN3WTF6VectorINS_11ProxyServerELm0EEE
 __ZN7WebCore9DOMWindow30dispatchAllPendingUnloadEventsEv
 __ZN7WebCore9DOMWindow36dispatchAllPendingBeforeUnloadEventsEv
+__ZN7WebCore9FloatRectC1ERK6CGRect
 __ZN7WebCore9FloatRectC1ERK7_NSRect
 __ZN7WebCore9FloatRectC1ERKNS_7IntRectE
 __ZN7WebCore9FloatSizeC1ERKNS_7IntSizeE
@@ -967,9 +969,11 @@
 __ZN7WebCore9FrameView14setTransparentEb
 __ZN7WebCore9FrameView15setMarginHeightEi
 __ZN7WebCore9FrameView16setPaintBehaviorEj
+__ZN7WebCore9FrameView17paintControlTintsEv
 __ZN7WebCore9FrameView17setScrollPositionERKNS_8IntPointE
 __ZN7WebCore9FrameView17setTracksRepaintsEb
 __ZN7WebCore9FrameView18updateControlTintsEv
+__ZN7WebCore9FrameView19scrollElementToRectEPNS_7ElementERKNS_7IntRectE
 __ZN7WebCore9FrameView20enterCompositingModeEv
 __ZN7WebCore9FrameView21flushDeferredRepaintsEv
 __ZN7WebCore9FrameView22setBaseBackgroundColorERKNS_5ColorE
@@ -983,8 +987,6 @@
 __ZN7WebCore9FrameView38syncCompositingStateIncludingSubframesEv
 __ZN7WebCore9FrameView6createEPNS_5FrameE
 __ZN7WebCore9FrameView6createEPNS_5FrameERKNS_7IntSizeE
-__ZN7WebCore9FrameView17paintControlTintsEv
-__ZN7WebCore9FrameView19scrollElementToRectEPNS_7ElementERKNS_7IntRectE
 __ZN7WebCore9HTMLNames10listingTagE
 __ZN7WebCore9HTMLNames11textareaTagE
 __ZN7WebCore9HTMLNames13blockquoteTagE
@@ -1010,6 +1012,7 @@
 __ZN7WebCore9HTMLNames5trTagE
 __ZN7WebCore9HTMLNames5ulTagE
 __ZN7WebCore9HTMLNames6divTagE
+__ZN7WebCore9HTMLNames6idAttrE
 __ZN7WebCore9HTMLNames6imgTagE
 __ZN7WebCore9HTMLNames6preTagE
 __ZN7WebCore9HTMLNames7formTagE
@@ -1018,6 +1021,7 @@
 __ZN7WebCore9HTMLNames8frameTagE
 __ZN7WebCore9HTMLNames8hrefAttrE
 __ZN7WebCore9HTMLNames8inputTagE
+__ZN7WebCore9HTMLNames8nameAttrE
 __ZN7WebCore9HTMLNames8videoTagE
 __ZN7WebCore9HTMLNames9iframeTagE
 __ZN7WebCore9HTMLNames9objectTagE
@@ -1199,6 +1203,9 @@
 __ZNK7WebCore16VisibleSelection19rootEditableElementEv
 __ZNK7WebCore16VisibleSelection23isContentRichlyEditableEv
 __ZNK7WebCore16VisibleSelection5isAllENS_27EditingBoundaryCrossingRuleE
+__ZNK7WebCore17RegularExpression13matchedLengthEv
+__ZNK7WebCore17RegularExpression5matchERKN3WTF6StringEiPi
+__ZNK7WebCore17RegularExpression9searchRevERKN3WTF6StringE
 __ZNK7WebCore17ResourceErrorBase8lazyInitEv
 __ZNK7WebCore19AnimationController24numberOfActiveAnimationsEPNS_8DocumentE
 __ZNK7WebCore19ResourceRequestBase10httpMethodEv
@@ -1246,6 +1253,7 @@
 __ZNK7WebCore4KURLcvP5NSURLEv
 __ZNK7WebCore4Node14isDescendantOfEPKS0_
 __ZNK7WebCore4Node18getSubresourceURLsERN3WTF11ListHashSetINS_4KURLELm256ENS_8KURLHashEEE
+__ZNK7WebCore4Node20traversePreviousNodeEPKS0_
 __ZNK7WebCore4Node9nodeIndexEv
 __ZNK7WebCore4Page10pluginDataEv
 __ZNK7WebCore4Page15backForwardListEv
@@ -1256,6 +1264,7 @@
 __ZNK7WebCore5Frame14selectionImageEb
 __ZNK7WebCore5Frame15contentRendererEv
 __ZNK7WebCore5Frame15layerTreeAsTextEb
+__ZNK7WebCore5Frame16frameScaleFactorEv
 __ZNK7WebCore5Frame18documentTypeStringEv
 __ZNK7WebCore5Frame31displayStringModifiedByEncodingERKN3WTF6StringE
 __ZNK7WebCore5Frame8settingsEv
@@ -1323,10 +1332,10 @@
 __ZNK7WebCore8Document31displayStringModifiedByEncodingERKN3WTF6StringE
 __ZNK7WebCore8Document4bodyEv
 __ZNK7WebCore8Document4pageEv
+__ZNK7WebCore8Document4viewEv
 __ZNK7WebCore8Document6domainEv
 __ZNK7WebCore8Document6loaderEv
 __ZNK7WebCore8Document8settingsEv
-__ZNK7WebCore8Document4viewEv
 __ZNK7WebCore8IntPointcv7CGPointEv
 __ZNK7WebCore8IntPointcv8_NSPointEv
 __ZNK7WebCore8Position10downstreamENS_27EditingBoundaryCrossingRuleE
@@ -1337,7 +1346,6 @@
 __ZNK7WebCore8Position8upstreamENS_27EditingBoundaryCrossingRuleE
 __ZNK7WebCore9DOMWindow27pendingUnloadEventListenersEv
 __ZNK7WebCore9FloatQuad11boundingBoxEv
-__ZN7WebCore9FloatRectC1ERK6CGRect
 __ZNK7WebCore9FloatRectcv7_NSRectEv
 __ZNK7WebCore9FrameTree12traverseNextEPKNS_5FrameE
 __ZNK7WebCore9FrameTree14isDescendantOfEPKNS_5FrameE

Modified: trunk/Source/WebCore/page/Frame.h (96479 => 96480)


--- trunk/Source/WebCore/page/Frame.h	2011-10-03 00:22:04 UTC (rev 96479)
+++ trunk/Source/WebCore/page/Frame.h	2011-10-03 00:40:43 UTC (rev 96480)
@@ -195,9 +195,6 @@
         String matchLabelsAgainstElement(const Vector<String>& labels, Element*);
         
 #if PLATFORM(MAC)
-        NSString* searchForLabelsBeforeElement(NSArray* labels, Element*, size_t* resultDistance, bool* resultIsInCellAbove);
-        NSString* matchLabelsAgainstElement(NSArray* labels, Element*);
-
         NSImage* selectionImage(bool forceBlackText = false) const;
         NSImage* snapshotDragImage(Node*, NSRect* imageRect, NSRect* elementRect) const;
         NSImage* imageFromRect(NSRect) const;

Modified: trunk/Source/WebCore/page/mac/FrameMac.mm (96479 => 96480)


--- trunk/Source/WebCore/page/mac/FrameMac.mm	2011-10-03 00:22:04 UTC (rev 96479)
+++ trunk/Source/WebCore/page/mac/FrameMac.mm	2011-10-03 00:40:43 UTC (rev 96480)
@@ -63,194 +63,6 @@
 
 using namespace HTMLNames;
 
-// Either get cached regexp or build one that matches any of the labels.
-// The regexp we build is of the form:  (STR1|STR2|STRN)
-static RegularExpression* regExpForLabels(NSArray* labels)
-{
-    // All the ObjC calls in this method are simple array and string
-    // calls which we can assume do not raise exceptions
-
-
-    // Parallel arrays that we use to cache regExps.  In practice the number of expressions
-    // that the app will use is equal to the number of locales is used in searching.
-    static const unsigned int regExpCacheSize = 4;
-    static NSMutableArray* regExpLabels = nil;
-    DEFINE_STATIC_LOCAL(Vector<RegularExpression*>, regExps, ());
-    DEFINE_STATIC_LOCAL(RegularExpression, wordRegExp, ("\\w", TextCaseSensitive));
-
-    RegularExpression* result;
-    if (!regExpLabels)
-        regExpLabels = [[NSMutableArray alloc] initWithCapacity:regExpCacheSize];
-    CFIndex cacheHit = [regExpLabels indexOfObject:labels];
-    if (cacheHit != NSNotFound)
-        result = regExps.at(cacheHit);
-    else {
-        String pattern("(");
-        unsigned int numLabels = [labels count];
-        unsigned int i;
-        for (i = 0; i < numLabels; i++) {
-            String label = [labels objectAtIndex:i];
-
-            bool startsWithWordChar = false;
-            bool endsWithWordChar = false;
-            if (label.length() != 0) {
-                startsWithWordChar = wordRegExp.match(label.substring(0, 1)) >= 0;
-                endsWithWordChar = wordRegExp.match(label.substring(label.length() - 1, 1)) >= 0;
-            }
-            
-            if (i != 0)
-                pattern.append("|");
-            // Search for word boundaries only if label starts/ends with "word characters".
-            // If we always searched for word boundaries, this wouldn't work for languages
-            // such as Japanese.
-            if (startsWithWordChar)
-                pattern.append("\\b");
-            pattern.append(label);
-            if (endsWithWordChar)
-                pattern.append("\\b");
-        }
-        pattern.append(")");
-        result = new RegularExpression(pattern, TextCaseInsensitive);
-    }
-
-    // add regexp to the cache, making sure it is at the front for LRU ordering
-    if (cacheHit != 0) {
-        if (cacheHit != NSNotFound) {
-            // remove from old spot
-            [regExpLabels removeObjectAtIndex:cacheHit];
-            regExps.remove(cacheHit);
-        }
-        // add to start
-        [regExpLabels insertObject:labels atIndex:0];
-        regExps.insert(0, result);
-        // trim if too big
-        if ([regExpLabels count] > regExpCacheSize) {
-            [regExpLabels removeObjectAtIndex:regExpCacheSize];
-            RegularExpression* last = regExps.last();
-            regExps.removeLast();
-            delete last;
-        }
-    }
-    return result;
-}
-
-NSString* Frame::searchForLabelsBeforeElement(NSArray* labels, Element* element, size_t* resultDistance, bool* resultIsInCellAbove)
-{
-    RegularExpression* regExp = regExpForLabels(labels);
-    // We stop searching after we've seen this many chars
-    const unsigned int charsSearchedThreshold = 500;
-    // This is the absolute max we search.  We allow a little more slop than
-    // charsSearchedThreshold, to make it more likely that we'll search whole nodes.
-    const unsigned int maxCharsSearched = 600;
-    // If the starting element is within a table, the cell that contains it
-    HTMLTableCellElement* startingTableCell = 0;
-    bool searchedCellAbove = false;
-    
-    if (resultDistance)
-        *resultDistance = notFound;
-    if (resultIsInCellAbove)
-        *resultIsInCellAbove = false;
-
-    // walk backwards in the node tree, until another element, or form, or end of tree
-    unsigned lengthSearched = 0;
-    Node* n;
-    for (n = element->traversePreviousNode();
-         n && lengthSearched < charsSearchedThreshold;
-         n = n->traversePreviousNode())
-    {
-        if (n->hasTagName(formTag)
-            || (n->isHTMLElement() && static_cast<Element*>(n)->isFormControlElement()))
-        {
-            // We hit another form element or the start of the form - bail out
-            break;
-        } else if (n->hasTagName(tdTag) && !startingTableCell) {
-            startingTableCell = static_cast<HTMLTableCellElement*>(n);
-        } else if (n->hasTagName(trTag) && startingTableCell) {
-            NSString* result = searchForLabelsAboveCell(regExp, startingTableCell, resultDistance);
-            if (result && [result length] > 0) {
-                if (resultIsInCellAbove)
-                    *resultIsInCellAbove = true;
-                return result;
-            }
-            searchedCellAbove = true;
-        } else if (n->isTextNode() && n->renderer() && n->renderer()->style()->visibility() == VISIBLE) {
-            // For each text chunk, run the regexp
-            String nodeString = n->nodeValue();
-            // add 100 for slop, to make it more likely that we'll search whole nodes
-            if (lengthSearched + nodeString.length() > maxCharsSearched)
-                nodeString = nodeString.right(charsSearchedThreshold - lengthSearched);
-            int pos = regExp->searchRev(nodeString);
-            if (pos >= 0) {
-                if (resultDistance)
-                    *resultDistance = lengthSearched;
-                return nodeString.substring(pos, regExp->matchedLength());
-            }
-            lengthSearched += nodeString.length();
-        }
-    }
-
-    // If we started in a cell, but bailed because we found the start of the form or the
-    // previous element, we still might need to search the row above us for a label.
-    if (startingTableCell && !searchedCellAbove) {
-        NSString* result = searchForLabelsAboveCell(regExp, startingTableCell, resultDistance);
-        if (result && [result length] > 0) {
-            if (resultIsInCellAbove)
-                *resultIsInCellAbove = true;
-            return result;
-        }
-    }
-    
-    return nil;
-}
-
-static NSString *matchLabelsAgainstString(NSArray *labels, const String& stringToMatch)
-{
-    if (stringToMatch.isEmpty())
-        return nil;
-    
-    String mutableStringToMatch = stringToMatch;
-    
-    // Make numbers and _'s in field names behave like word boundaries, e.g., "address2"
-    replace(mutableStringToMatch, RegularExpression("\\d", TextCaseSensitive), " ");
-    mutableStringToMatch.replace('_', ' ');
-    
-    RegularExpression* regExp = regExpForLabels(labels);
-    // Use the largest match we can find in the whole string
-    int pos;
-    int length;
-    int bestPos = -1;
-    int bestLength = -1;
-    int start = 0;
-    do {
-        pos = regExp->match(mutableStringToMatch, start);
-        if (pos != -1) {
-            length = regExp->matchedLength();
-            if (length >= bestLength) {
-                bestPos = pos;
-                bestLength = length;
-            }
-            start = pos + 1;
-        }
-    } while (pos != -1);
-    
-    if (bestPos != -1)
-        return mutableStringToMatch.substring(bestPos, bestLength);
-    return nil;
-}
-
-NSString* Frame::matchLabelsAgainstElement(NSArray* labels, Element* element)
-{
-    // Match against the name element, then against the id element if no match is found for the name element.
-    // See 7538330 for one popular site that benefits from the id element check.
-    // FIXME: This code is mirrored in Frame.cpp. It would be nice to make the Mac code call the platform-agnostic
-    // code, which would require converting the NSArray of NSStrings to a Vector of Strings somewhere along the way.
-    String resultFromNameAttribute = matchLabelsAgainstString(labels, element->getAttribute(nameAttr));
-    if (!resultFromNameAttribute.isEmpty())
-        return resultFromNameAttribute;
-    
-    return matchLabelsAgainstString(labels, element->getAttribute(idAttr));
-}
-
 NSImage* Frame::imageFromRect(NSRect rect) const
 {
     PaintBehavior oldBehavior = m_view->paintBehavior();

Modified: trunk/Source/WebKit/mac/ChangeLog (96479 => 96480)


--- trunk/Source/WebKit/mac/ChangeLog	2011-10-03 00:22:04 UTC (rev 96479)
+++ trunk/Source/WebKit/mac/ChangeLog	2011-10-03 00:40:43 UTC (rev 96480)
@@ -1,3 +1,18 @@
+2011-10-02  Sam Weinig  <s...@webkit.org>
+
+        Strength reduce Mac only Frame::searchForLabelsBeforeElement and Frame::matchLabelsAgainstElement into callers
+        https://bugs.webkit.org/show_bug.cgi?id=69230
+
+        Reviewed by Anders Carlsson.
+
+        * WebView/WebHTMLRepresentation.mm:
+        (regExpForLabels):
+        (searchForLabelsBeforeElement):
+        (matchLabelsAgainstString):
+        (matchLabelsAgainstElement):
+        (-[WebHTMLRepresentation searchForLabels:beforeElement:resultDistance:resultIsInCellAbove:]):
+        (-[WebHTMLRepresentation matchLabels:againstElement:]):
+
 2011-10-02  Alexey Proskuryakov  <a...@apple.com>
 
         REGRESSION (r95474): Unable to type anywhere in nightly builds in WebKit1 mode

Modified: trunk/Source/WebKit/mac/WebView/WebHTMLRepresentation.mm (96479 => 96480)


--- trunk/Source/WebKit/mac/WebView/WebHTMLRepresentation.mm	2011-10-03 00:22:04 UTC (rev 96479)
+++ trunk/Source/WebKit/mac/WebView/WebHTMLRepresentation.mm	2011-10-03 00:40:43 UTC (rev 96480)
@@ -52,8 +52,11 @@
 #import <WebCore/HTMLFormElement.h>
 #import <WebCore/HTMLInputElement.h>
 #import <WebCore/HTMLNames.h>
+#import <WebCore/HTMLTableCellElement.h>
 #import <WebCore/MIMETypeRegistry.h>
 #import <WebCore/Range.h>
+#import <WebCore/RegularExpression.h>
+#import <WebCore/RenderObject.h>
 #import <WebCore/TextResourceDecoder.h>
 #import <WebKit/DOMHTMLInputElement.h>
 #import <wtf/Assertions.h>
@@ -352,6 +355,192 @@
     return results;
 }
 
+// Either get cached regexp or build one that matches any of the labels.
+// The regexp we build is of the form:  (STR1|STR2|STRN)
+static RegularExpression* regExpForLabels(NSArray *labels)
+{
+    // All the ObjC calls in this method are simple array and string
+    // calls which we can assume do not raise exceptions
+
+    // Parallel arrays that we use to cache regExps.  In practice the number of expressions
+    // that the app will use is equal to the number of locales is used in searching.
+    static const unsigned int regExpCacheSize = 4;
+    static NSMutableArray* regExpLabels = nil;
+    DEFINE_STATIC_LOCAL(Vector<RegularExpression*>, regExps, ());
+    DEFINE_STATIC_LOCAL(RegularExpression, wordRegExp, ("\\w", TextCaseSensitive));
+
+    RegularExpression* result;
+    if (!regExpLabels)
+        regExpLabels = [[NSMutableArray alloc] initWithCapacity:regExpCacheSize];
+    CFIndex cacheHit = [regExpLabels indexOfObject:labels];
+    if (cacheHit != NSNotFound)
+        result = regExps.at(cacheHit);
+    else {
+        String pattern("(");
+        unsigned int numLabels = [labels count];
+        unsigned int i;
+        for (i = 0; i < numLabels; i++) {
+            String label = [labels objectAtIndex:i];
+
+            bool startsWithWordChar = false;
+            bool endsWithWordChar = false;
+            if (label.length() != 0) {
+                startsWithWordChar = wordRegExp.match(label.substring(0, 1)) >= 0;
+                endsWithWordChar = wordRegExp.match(label.substring(label.length() - 1, 1)) >= 0;
+            }
+            
+            if (i != 0)
+                pattern.append("|");
+            // Search for word boundaries only if label starts/ends with "word characters".
+            // If we always searched for word boundaries, this wouldn't work for languages
+            // such as Japanese.
+            if (startsWithWordChar)
+                pattern.append("\\b");
+            pattern.append(label);
+            if (endsWithWordChar)
+                pattern.append("\\b");
+        }
+        pattern.append(")");
+        result = new RegularExpression(pattern, TextCaseInsensitive);
+    }
+
+    // add regexp to the cache, making sure it is at the front for LRU ordering
+    if (cacheHit != 0) {
+        if (cacheHit != NSNotFound) {
+            // remove from old spot
+            [regExpLabels removeObjectAtIndex:cacheHit];
+            regExps.remove(cacheHit);
+        }
+        // add to start
+        [regExpLabels insertObject:labels atIndex:0];
+        regExps.insert(0, result);
+        // trim if too big
+        if ([regExpLabels count] > regExpCacheSize) {
+            [regExpLabels removeObjectAtIndex:regExpCacheSize];
+            RegularExpression* last = regExps.last();
+            regExps.removeLast();
+            delete last;
+        }
+    }
+    return result;
+}
+
+static NSString* searchForLabelsBeforeElement(Frame* frame, NSArray* labels, Element* element, size_t* resultDistance, bool* resultIsInCellAbove)
+{
+    RegularExpression* regExp = regExpForLabels(labels);
+    // We stop searching after we've seen this many chars
+    const unsigned int charsSearchedThreshold = 500;
+    // This is the absolute max we search.  We allow a little more slop than
+    // charsSearchedThreshold, to make it more likely that we'll search whole nodes.
+    const unsigned int maxCharsSearched = 600;
+    // If the starting element is within a table, the cell that contains it
+    HTMLTableCellElement* startingTableCell = 0;
+    bool searchedCellAbove = false;
+    
+    if (resultDistance)
+        *resultDistance = notFound;
+    if (resultIsInCellAbove)
+        *resultIsInCellAbove = false;
+
+    // walk backwards in the node tree, until another element, or form, or end of tree
+    unsigned lengthSearched = 0;
+    Node* n;
+    for (n = element->traversePreviousNode();
+         n && lengthSearched < charsSearchedThreshold;
+         n = n->traversePreviousNode())
+    {
+        if (n->hasTagName(formTag)
+            || (n->isHTMLElement() && static_cast<Element*>(n)->isFormControlElement()))
+        {
+            // We hit another form element or the start of the form - bail out
+            break;
+        } else if (n->hasTagName(tdTag) && !startingTableCell) {
+            startingTableCell = static_cast<HTMLTableCellElement*>(n);
+        } else if (n->hasTagName(trTag) && startingTableCell) {
+            NSString* result = frame->searchForLabelsAboveCell(regExp, startingTableCell, resultDistance);
+            if (result && [result length] > 0) {
+                if (resultIsInCellAbove)
+                    *resultIsInCellAbove = true;
+                return result;
+            }
+            searchedCellAbove = true;
+        } else if (n->isTextNode() && n->renderer() && n->renderer()->style()->visibility() == VISIBLE) {
+            // For each text chunk, run the regexp
+            String nodeString = n->nodeValue();
+            // add 100 for slop, to make it more likely that we'll search whole nodes
+            if (lengthSearched + nodeString.length() > maxCharsSearched)
+                nodeString = nodeString.right(charsSearchedThreshold - lengthSearched);
+            int pos = regExp->searchRev(nodeString);
+            if (pos >= 0) {
+                if (resultDistance)
+                    *resultDistance = lengthSearched;
+                return nodeString.substring(pos, regExp->matchedLength());
+            }
+            lengthSearched += nodeString.length();
+        }
+    }
+
+    // If we started in a cell, but bailed because we found the start of the form or the
+    // previous element, we still might need to search the row above us for a label.
+    if (startingTableCell && !searchedCellAbove) {
+        NSString* result = frame->searchForLabelsAboveCell(regExp, startingTableCell, resultDistance);
+        if (result && [result length] > 0) {
+            if (resultIsInCellAbove)
+                *resultIsInCellAbove = true;
+            return result;
+        }
+    }
+    
+    return nil;
+}
+
+static NSString *matchLabelsAgainstString(NSArray *labels, const String& stringToMatch)
+{
+    if (stringToMatch.isEmpty())
+        return nil;
+    
+    String mutableStringToMatch = stringToMatch;
+    
+    // Make numbers and _'s in field names behave like word boundaries, e.g., "address2"
+    replace(mutableStringToMatch, RegularExpression("\\d", TextCaseSensitive), " ");
+    mutableStringToMatch.replace('_', ' ');
+    
+    RegularExpression* regExp = regExpForLabels(labels);
+    // Use the largest match we can find in the whole string
+    int pos;
+    int length;
+    int bestPos = -1;
+    int bestLength = -1;
+    int start = 0;
+    do {
+        pos = regExp->match(mutableStringToMatch, start);
+        if (pos != -1) {
+            length = regExp->matchedLength();
+            if (length >= bestLength) {
+                bestPos = pos;
+                bestLength = length;
+            }
+            start = pos + 1;
+        }
+    } while (pos != -1);
+    
+    if (bestPos != -1)
+        return mutableStringToMatch.substring(bestPos, bestLength);
+    return nil;
+}
+
+static NSString* matchLabelsAgainstElement(NSArray* labels, Element* element)
+{
+    // Match against the name element, then against the id element if no match is found for the name element.
+    // See 7538330 for one popular site that benefits from the id element check.
+    String resultFromNameAttribute = matchLabelsAgainstString(labels, element->getAttribute(nameAttr));
+    if (!resultFromNameAttribute.isEmpty())
+        return resultFromNameAttribute;
+    
+    return matchLabelsAgainstString(labels, element->getAttribute(idAttr));
+}
+
+
 - (NSString *)searchForLabels:(NSArray *)labels beforeElement:(DOMElement *)element
 {
     return [self searchForLabels:labels beforeElement:element resultDistance:0 resultIsInCellAbove:0];
@@ -362,7 +551,7 @@
     size_t distance;
     bool isInCellAbove;
     
-    NSString *result = core([_private->dataSource webFrame])->searchForLabelsBeforeElement(labels, core(element), &distance, &isInCellAbove);
+    NSString *result = searchForLabelsBeforeElement(core([_private->dataSource webFrame]), labels, core(element), &distance, &isInCellAbove);
     
     if (outDistance) {
         if (distance == notFound)
@@ -379,7 +568,7 @@
 
 - (NSString *)matchLabels:(NSArray *)labels againstElement:(DOMElement *)element
 {
-    return core([_private->dataSource webFrame])->matchLabelsAgainstElement(labels, core(element));
+    return matchLabelsAgainstElement(labels, core(element));
 }
 
 @end
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
http://lists.webkit.org/mailman/listinfo.cgi/webkit-changes

Reply via email to